Exemplo n.º 1
0
func executeCommandsHook(c *gc.C, s *networkerSuite, commands []string) error {
	cs := &configState{}
	err := networker.ReadAll(&cs.files)
	c.Assert(err, gc.IsNil)
	cs.commands = append(cs.commands, commands...)
	// modify state of interfaces
	for _, cmd := range commands {
		args := strings.Split(cmd, " ")
		if len(args) == 2 && args[0] == "ifup" {
			readyInterfaces.Add(args[1])
			interfacesWithAddress.Add(args[1])
		} else if len(args) == 2 && args[0] == "ifdown" {
			readyInterfaces.Remove(args[1])
			interfacesWithAddress.Remove(args[1])
		}
	}
	cs.readyInterfaces = readyInterfaces.SortedValues()
	cs.interfacesWithAddress = interfacesWithAddress.SortedValues()
	s.configStates = append(s.configStates, cs)
	s.executed <- true
	return nil
}
Exemplo 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)
}