Ejemplo n.º 1
0
func (s *networkerSuite) TestNetworker(c *gc.C) {
	// Create a sample interfaces file (MAAS configuration)
	interfacesFileContents := fmt.Sprintf(sampleInterfacesFile, networker.ConfigDirName)
	err := utils.AtomicWriteFile(networker.ConfigFileName, []byte(interfacesFileContents), 0644)
	c.Assert(err, gc.IsNil)
	err = utils.AtomicWriteFile(filepath.Join(networker.ConfigDirName, "eth0.config"), []byte(sampleEth0DotConfigFile), 0644)
	c.Assert(err, gc.IsNil)

	// Patch the network interface functions
	s.PatchValue(&networker.InterfaceIsUp,
		func(name string) bool {
			return readyInterfaces.Contains(name)
		})
	s.PatchValue(&networker.InterfaceHasAddress,
		func(name string) bool {
			return interfacesWithAddress.Contains(name)
		})

	// Patch the command executor function
	s.configStates = []*configState{}
	s.PatchValue(&networker.ExecuteCommands,
		func(commands []string) error {
			return executeCommandsHook(c, s, commands)
		},
	)

	// Create and setup networker.
	s.executed = make(chan bool)
	nw, err := networker.NewNetworker(s.networkerState, agentConfig(s.machine.Tag()))
	c.Assert(err, gc.IsNil)
	defer func() { c.Assert(worker.Stop(nw), gc.IsNil) }()

	executeCount := 0
loop:
	for {
		select {
		case <-s.executed:
			executeCount++
			if executeCount == 3 {
				break loop
			}
		case <-time.After(coretesting.ShortWait):
			fmt.Printf("%#v\n", s.configStates)
			c.Fatalf("command not executed")
		}
	}

	// Verify the executed commands from SetUp()
	expectedConfigFiles := networker.ConfigFiles{
		networker.ConfigFileName: {
			Data: fmt.Sprintf(expectedInterfacesFile, networker.ConfigSubDirName,
				networker.ConfigSubDirName, networker.ConfigSubDirName, networker.ConfigSubDirName),
		},
		networker.IfaceConfigFileName("br0"): {
			Data: "auto br0\niface br0 inet dhcp\n  bridge_ports eth0\n",
		},
		networker.IfaceConfigFileName("eth0"): {
			Data: "auto eth0\niface eth0 inet manual\n",
		},
		networker.IfaceConfigFileName("wlan0"): {
			Data: "auto wlan0\niface wlan0 inet dhcp\n",
		},
	}
	c.Assert(s.configStates[0].files, gc.DeepEquals, expectedConfigFiles)
	expectedCommands := []string(nil)
	c.Assert(s.configStates[0].commands, gc.DeepEquals, expectedCommands)
	c.Assert(s.configStates[0].readyInterfaces, gc.DeepEquals, []string{"br0", "eth0", "wlan0"})
	c.Assert(s.configStates[0].interfacesWithAddress, gc.DeepEquals, []string{"br0", "wlan0"})

	// Verify the executed commands from Handle()
	c.Assert(s.configStates[1].files, gc.DeepEquals, expectedConfigFiles)
	expectedCommands = []string(nil)
	c.Assert(s.configStates[1].commands, gc.DeepEquals, expectedCommands)
	c.Assert(s.configStates[1].readyInterfaces, gc.DeepEquals, []string{"br0", "eth0", "wlan0"})
	c.Assert(s.configStates[1].interfacesWithAddress, gc.DeepEquals, []string{"br0", "wlan0"})

	// Verify the executed commands from Handle()
	expectedConfigFiles[networker.IfaceConfigFileName("eth0.69")] = &networker.ConfigFile{
		Data: "# Managed by Juju, don't change.\nauto eth0.69\niface eth0.69 inet dhcp\n\tvlan-raw-device eth0\n",
	}
	expectedConfigFiles[networker.IfaceConfigFileName("eth1")] = &networker.ConfigFile{
		Data: "# Managed by Juju, don't change.\nauto eth1\niface eth1 inet dhcp\n",
	}
	expectedConfigFiles[networker.IfaceConfigFileName("eth1.42")] = &networker.ConfigFile{
		Data: "# Managed by Juju, don't change.\nauto eth1.42\niface eth1.42 inet dhcp\n\tvlan-raw-device eth1\n",
	}
	expectedConfigFiles[networker.IfaceConfigFileName("eth2")] = &networker.ConfigFile{
		Data: "# Managed by Juju, don't change.\nauto eth2\niface eth2 inet dhcp\n",
	}
	for k, _ := range s.configStates[2].files {
		c.Check(s.configStates[2].files[k], gc.DeepEquals, expectedConfigFiles[k])
	}
	c.Assert(s.configStates[2].files, gc.DeepEquals, expectedConfigFiles)
	expectedCommands = []string{
		"dpkg-query -s vlan || apt-get --option Dpkg::Options::=--force-confold --assume-yes install vlan",
		"lsmod | grep -q 8021q || modprobe 8021q",
		"grep -q 8021q /etc/modules || echo 8021q >> /etc/modules",
		"vconfig set_name_type DEV_PLUS_VID_NO_PAD",
		"ifup eth0.69",
		"ifup eth1",
		"ifup eth1.42",
		"ifup eth2",
	}
	c.Assert(s.configStates[2].commands, gc.DeepEquals, expectedCommands)
	c.Assert(s.configStates[2].readyInterfaces, gc.DeepEquals,
		[]string{"br0", "eth0", "eth0.69", "eth1", "eth1.42", "eth2", "wlan0"})
	c.Assert(s.configStates[2].interfacesWithAddress, gc.DeepEquals,
		[]string{"br0", "eth0.69", "eth1", "eth1.42", "eth2", "wlan0"})
}
Ejemplo n.º 2
0
func (s *configSuite) TestConfigFileOperations(c *gc.C) {
	// Create sample config files
	interfacesContents := fmt.Sprintf(interfacesTemplate, networker.ConfigDirName, networker.ConfigDirName)
	err := utils.AtomicWriteFile(networker.ConfigFileName, []byte(interfacesContents), 0644)
	c.Assert(err, gc.IsNil)
	err = utils.AtomicWriteFile(filepath.Join(networker.ConfigDirName, "eth0.config"), []byte(eth0DotConfigContents), 0644)
	c.Assert(err, gc.IsNil)
	err = utils.AtomicWriteFile(filepath.Join(networker.ConfigDirName, "eth1.config"), []byte(eth1DotConfigContents), 0644)
	c.Assert(err, gc.IsNil)
	err = os.Mkdir(networker.ConfigSubDirName, 0755)
	c.Assert(err, gc.IsNil)
	err = utils.AtomicWriteFile(filepath.Join(networker.ConfigSubDirName, "eth0.cfg"),
		[]byte(interfacesDSlashEth0DotCfgContents), 0644)
	c.Assert(err, gc.IsNil)
	err = utils.AtomicWriteFile(filepath.Join(networker.ConfigSubDirName, "eth4.cfg"),
		[]byte(interfacesDSlashEth4DotCfgContents), 0644)
	c.Assert(err, gc.IsNil)

	cf := networker.ConfigFiles{}
	err = networker.ReadAll(&cf)
	c.Assert(err, gc.IsNil)
	expect := networker.ConfigFiles{
		networker.ConfigFileName: {
			Data: interfacesContents,
		},
		networker.IfaceConfigFileName("eth0"): {
			Data: interfacesDSlashEth0DotCfgContents,
		},
		networker.IfaceConfigFileName("eth4"): {
			Data: interfacesDSlashEth4DotCfgContents,
		},
	}
	c.Assert(cf, gc.DeepEquals, expect)
	err = networker.FixMAAS(cf)
	c.Assert(err, gc.IsNil)
	expect = networker.ConfigFiles{
		networker.ConfigFileName: {
			Data: interfacesExpectedPrefix +
				fmt.Sprintf(networker.SourceCommentAndCommand,
					networker.ConfigSubDirName, networker.ConfigSubDirName,
					networker.ConfigSubDirName, networker.ConfigSubDirName),
			Op: networker.DoWrite,
		},
		networker.IfaceConfigFileName("eth0"): {
			Data: "auto eth0\niface eth0 inet manual\n",
			Op:   networker.DoWrite,
		},
		networker.IfaceConfigFileName("br0"): {
			Data: "auto br0\niface br0 inet dhcp\n  bridge_ports eth0\n",
			Op:   networker.DoWrite,
		},
		networker.IfaceConfigFileName("eth1"): {
			Data: "auto eth1\niface eth1 inet manual\n",
			Op:   networker.DoWrite,
		},
		networker.IfaceConfigFileName("br2"): {
			Data: "auto br2\niface br2 inet dhcp\n  bridge_ports eth1\n",
			Op:   networker.DoWrite,
		},
		networker.IfaceConfigFileName("eth1.2"): {
			Data: "auto eth1.2\niface eth1.2 inet dhcp\n",
			Op:   networker.DoWrite,
		},
		networker.IfaceConfigFileName("eth2"): {
			Data: "auto eth2\niface eth2 inet dhcp\n",
			Op:   networker.DoWrite,
		},
		networker.IfaceConfigFileName("eth4"): {
			Data: "",
			Op:   networker.DoRemove,
		},
		filepath.Join(networker.ConfigDirName, "eth0.config"): {
			Data: "",
			Op:   networker.DoRemove,
		},
		filepath.Join(networker.ConfigDirName, "eth1.config"): {
			Data: "",
			Op:   networker.DoRemove,
		},
	}
	c.Assert(cf, gc.DeepEquals, expect)
	err = networker.WriteOrRemove(cf)
	c.Assert(err, gc.IsNil)

	// Do another ineration, some interfaces should disappear
	cf = networker.ConfigFiles{}
	err = networker.ReadAll(&cf)
	c.Assert(err, gc.IsNil)
	delete(expect, networker.IfaceConfigFileName("eth4"))
	delete(expect, filepath.Join(networker.ConfigDirName, "eth0.config"))
	delete(expect, filepath.Join(networker.ConfigDirName, "eth1.config"))
	for k, _ := range expect {
		expect[k].Op = networker.DoNone
	}
	c.Assert(cf, gc.DeepEquals, expect)

	// fixMAAS should not change anything
	err = networker.FixMAAS(cf)
	c.Assert(err, gc.IsNil)
	c.Assert(cf, gc.DeepEquals, expect)
}