From ec7d417a3731e379c04a8a526b77bb5975932665 Mon Sep 17 00:00:00 2001 From: Jana Radhakrishnan Date: Thu, 16 Apr 2015 04:58:15 +0000 Subject: [PATCH] Added a test binary to test README.md code Signed-off-by: Jana Radhakrishnan --- libnetwork/cmd/readme_test/readme.go | 53 ++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 libnetwork/cmd/readme_test/readme.go diff --git a/libnetwork/cmd/readme_test/readme.go b/libnetwork/cmd/readme_test/readme.go new file mode 100644 index 0000000000..90e2409c8f --- /dev/null +++ b/libnetwork/cmd/readme_test/readme.go @@ -0,0 +1,53 @@ +package main + +import ( + "github.com/docker/libnetwork" + "github.com/docker/libnetwork/pkg/options" + "github.com/docker/libnetwork/sandbox" +) + +func main() { + // Create a new controller instance + controller := libnetwork.New() + + option := options.Generic{} + driver, err := controller.NewNetworkDriver("simplebridge", option) + if err != nil { + return + } + + netOptions := options.Generic{} + // Create a network for containers to join. + network, err := controller.NewNetwork(driver, "network1", netOptions) + if err != nil { + return + } + + // For a new container: create a sandbox instance (providing a unique key). + // For linux it is a filesystem path + networkPath := "/var/lib/docker/.../4d23e" + networkNamespace, err := sandbox.NewSandbox(networkPath) + if err != nil { + return + } + + // For each new container: allocate IP and interfaces. The returned network + // settings will be used for container infos (inspect and such), as well as + // iptables rules for port publishing. + _, sinfo, err := network.CreateEndpoint("Endpoint1", networkNamespace.Key(), "") + if err != nil { + return + } + + // Add interfaces to the namespace. + for _, iface := range sinfo.Interfaces { + if err := networkNamespace.AddInterface(iface); err != nil { + return + } + } + + // Set the gateway IP + if err := networkNamespace.SetGateway(sinfo.Gateway); err != nil { + return + } +}