func New() System { id, _ := sys.UUID() return System{ Hosts: etcconf.NewHosts(""), // default hosts files, e.g. /etc/hosts on linux ResolvConf: etcconf.NewResolvConf(""), // default resolv.conf file, e.g. /etc/resolv.conf on linux Syscall: &syscallImpl{}, // the syscall interface Root: "/", // the system root path UUID: id, } }
func TestSetIpAddress(t *testing.T) { _, mocker := testSetup(t) defer testTeardown(t, mocker) hFile, err := ioutil.TempFile("", "vic_set_ip_test_hosts") if err != nil { t.Errorf("Failed to create tmp hosts file: %s", err) } rFile, err := ioutil.TempFile("", "vic_set_ip_test_resolv") if err != nil { t.Errorf("Failed to create tmp resolv file: %s", err) } // give us a hosts file we can modify defer func(hosts etcconf.Hosts, resolv etcconf.ResolvConf) { Sys.Hosts = hosts Sys.ResolvConf = resolv }(Sys.Hosts, Sys.ResolvConf) Sys.Hosts = etcconf.NewHosts(hFile.Name()) Sys.ResolvConf = etcconf.NewResolvConf(rFile.Name()) bridge := AddInterface("eth1", mocker) external := AddInterface("eth2", mocker) secondIP, _ := netlink.ParseIPNet("172.16.0.10/24") gwIP, _ := netlink.ParseIPNet("172.16.0.1/24") cfg := executor.ExecutorConfig{ Common: executor.Common{ ID: "ipconfig", Name: "tether_test_executor", }, Networks: map[string]*executor.NetworkEndpoint{ "bridge": { Common: executor.Common{ ID: bridge, // interface rename Name: "bridge", }, Network: executor.ContainerNetwork{ Common: executor.Common{ Name: "bridge", }, Default: true, Gateway: *gwIP, }, Static: true, IP: &net.IPNet{ IP: localhost, Mask: lmask.Mask, }, }, "cnet": { Common: executor.Common{ ID: bridge, // no interface rename }, Network: executor.ContainerNetwork{ Common: executor.Common{ Name: "cnet", }, }, Static: true, IP: secondIP, }, "external": { Common: executor.Common{ ID: external, // interface rename Name: "external", }, Network: executor.ContainerNetwork{ Common: executor.Common{ Name: "external", }, }, Static: true, IP: &net.IPNet{ IP: gateway, Mask: gmask.Mask, }, }, }, } tthr, _ := StartTether(t, &cfg, mocker) defer func() { // prevent indefinite wait in tether - normally session exit would trigger this tthr.Stop() // wait for tether to exit <-mocker.Cleaned }() <-mocker.Started assert.NotNil(t, mocker.Interfaces["bridge"], "Expected bridge network if endpoints applied correctly") // check addresses bIface, _ := mocker.Interfaces["bridge"].(*Interface) assert.NotNil(t, bIface) assert.Equal(t, 2, len(bIface.Addrs), "Expected two addresses on bridge interface") eIface, _ := mocker.Interfaces["external"].(*Interface) assert.NotNil(t, eIface) assert.Equal(t, 1, len(eIface.Addrs), "Expected one address on external interface") }
func (t *BaseOperations) Setup(config Config) error { c, err := client.NewClient() if err != nil { return err } h := etcconf.NewHosts(hostsFile) if err = h.Load(); err != nil { return err } // make sure localhost entries are present entries := []struct { hostname string addr net.IP }{ {"localhost", net.ParseIP("127.0.0.1")}, {"ip6-localhost", net.ParseIP("::1")}, {"ip6-loopback", net.ParseIP("::1")}, {"ip6-localnet", net.ParseIP("fe00::0")}, {"ip6-mcastprefix", net.ParseIP("ff00::0")}, {"ip6-allnodes", net.ParseIP("ff02::1")}, {"ip6-allrouters", net.ParseIP("ff02::2")}, } for _, e := range entries { h.SetHost(e.hostname, e.addr) } if err = h.Save(); err != nil { return err } // start with empty resolv.conf os.Remove(resolvFile) rc := etcconf.NewResolvConf(resolvFile) t.dynEndpoints = make(map[string][]*NetworkEndpoint) t.dhcpLoops = make(map[string]chan bool) t.dhcpClient = c t.hosts = h t.resolvConf = rc t.config = config // support the df command (#1642) if err = os.Symlink("/proc/mounts", "/etc/mtab"); err != nil { return err } mounted, err := mount.Mounted(runMountPoint) if err != nil { return err } if mounted { // unmount /run - https://github.com/vmware/vic/issues/1643 if err := syscall.Unmount(runMountPoint, syscall.MNT_DETACH); err != nil { return fmt.Errorf("unmount %s failed with %q", runMountPoint, err) } } return nil }