func (s *LxcSuite) TestCreateContainerEvents(c *gc.C) { manager := s.makeManager(c, "test") instance := containertesting.CreateContainer(c, manager, "1") id := string(instance.Id()) s.AssertEvent(c, <-s.events, mock.Created, id) s.AssertEvent(c, <-s.events, mock.Started, id) }
func (s *LxcSuite) TestDestroyContainerRemovesAutostartLink(c *gc.C) { manager := s.makeManager(c, "test") instance := containertesting.CreateContainer(c, manager, "1/lxc/0") err := manager.DestroyContainer(instance.Id()) c.Assert(err, gc.IsNil) autostartLink := lxc.RestartSymlink(string(instance.Id())) c.Assert(autostartLink, jc.SymlinkDoesNotExist) }
func (s *LxcSuite) TestDestroyContainerNoRestartDir(c *gc.C) { err := os.Remove(s.RestartDir) c.Assert(err, gc.IsNil) manager := s.makeManager(c, "test") instance := containertesting.CreateContainer(c, manager, "1/lxc/0") err = manager.DestroyContainer(instance.Id()) c.Assert(err, gc.IsNil) }
func (s *LxcSuite) TestListContainers(c *gc.C) { foo := s.makeManager(c, "foo") bar := s.makeManager(c, "bar") foo1 := containertesting.CreateContainer(c, foo, "1/lxc/0") foo2 := containertesting.CreateContainer(c, foo, "1/lxc/1") foo3 := containertesting.CreateContainer(c, foo, "1/lxc/2") bar1 := containertesting.CreateContainer(c, bar, "1/lxc/0") bar2 := containertesting.CreateContainer(c, bar, "1/lxc/1") result, err := foo.ListContainers() c.Assert(err, gc.IsNil) instancetest.MatchInstances(c, result, foo1, foo2, foo3) result, err = bar.ListContainers() c.Assert(err, gc.IsNil) instancetest.MatchInstances(c, result, bar1, bar2) }
func (s *LxcSuite) TestCreateContainerEventsWithCloneExistingTemplate(c *gc.C) { s.createTemplate(c) s.PatchValue(&s.useClone, true) manager := s.makeManager(c, "test") instance := containertesting.CreateContainer(c, manager, "1") name := string(instance.Id()) cloned := <-s.events s.AssertEvent(c, cloned, mock.Cloned, "juju-quantal-lxc-template") c.Assert(cloned.Args, gc.IsNil) s.AssertEvent(c, <-s.events, mock.Started, name) }
func (s *LxcSuite) TestCreateContainerEventsWithCloneExistingTemplateBtrfs(c *gc.C) { s.HookCommandOutput(&lxc.FsCommandOutput, []byte("Type\nbtrfs\n"), nil) s.createTemplate(c) s.PatchValue(&s.useClone, true) manager := s.makeManager(c, "test") instance := containertesting.CreateContainer(c, manager, "1") name := string(instance.Id()) cloned := <-s.events s.AssertEvent(c, cloned, mock.Cloned, "juju-quantal-lxc-template") c.Assert(cloned.Args, gc.DeepEquals, []string{"--snapshot"}) s.AssertEvent(c, <-s.events, mock.Started, name) }
func (s *KVMSuite) TestDestroyContainer(c *gc.C) { instance := containertesting.CreateContainer(c, s.manager, "1/lxc/0") err := s.manager.DestroyContainer(instance.Id()) c.Assert(err, gc.IsNil) name := string(instance.Id()) // Check that the container dir is no longer in the container dir c.Assert(filepath.Join(s.ContainerDir, name), jc.DoesNotExist) // but instead, in the removed container dir c.Assert(filepath.Join(s.RemovedDir, name), jc.IsDirectory) }
func (s *LxcSuite) TestCreateContainerEventsWithCloneExistingTemplateAUFS(c *gc.C) { s.createTemplate(c) s.PatchValue(&s.useClone, true) s.PatchValue(&s.useAUFS, true) manager := s.makeManager(c, "test") instance := containertesting.CreateContainer(c, manager, "1") name := string(instance.Id()) cloned := <-s.events s.AssertEvent(c, cloned, mock.Cloned, "juju-quantal-lxc-template") c.Assert(cloned.Args, gc.DeepEquals, []string{"--snapshot", "--backingstore", "aufs"}) s.AssertEvent(c, <-s.events, mock.Started, name) }
func (s *LxcSuite) TestContainerState(c *gc.C) { manager := s.makeManager(c, "test") c.Logf("%#v", manager) instance := containertesting.CreateContainer(c, manager, "1/lxc/0") // The mock container will be immediately "running". c.Assert(instance.Status(), gc.Equals, string(golxc.StateRunning)) // DestroyContainer stops and then destroys the container, putting it // into "unknown" state. err := manager.DestroyContainer(instance.Id()) c.Assert(err, gc.IsNil) c.Assert(instance.Status(), gc.Equals, string(golxc.StateUnknown)) }
func (s *LxcSuite) TestCreateContainerWithCloneMountsAndAutostarts(c *gc.C) { s.createTemplate(c) s.PatchValue(&s.useClone, true) manager := s.makeManager(c, "test") instance := containertesting.CreateContainer(c, manager, "1") name := string(instance.Id()) autostartLink := lxc.RestartSymlink(name) config, err := ioutil.ReadFile(lxc.ContainerConfigFilename(name)) c.Assert(err, gc.IsNil) mountLine := "lxc.mount.entry=/var/log/juju var/log/juju none defaults,bind 0 0" c.Assert(string(config), jc.Contains, mountLine) c.Assert(autostartLink, jc.IsSymlink) }
func (s *LxcSuite) TestCreateContainerEventsWithClone(c *gc.C) { s.PatchValue(&s.useClone, true) // The template containers are created with an upstart job that // stops them once cloud init has finished. We emulate that here. template := "juju-series-template" s.ensureTemplateStopped(template) manager := s.makeManager(c, "test") instance := containertesting.CreateContainer(c, manager, "1") id := string(instance.Id()) s.AssertEvent(c, <-s.events, mock.Created, template) s.AssertEvent(c, <-s.events, mock.Started, template) s.AssertEvent(c, <-s.events, mock.Stopped, template) s.AssertEvent(c, <-s.events, mock.Cloned, template) s.AssertEvent(c, <-s.events, mock.Started, id) }
func (s *LxcSuite) TestContainerState(c *gc.C) { // TODO(perrito666) refactor state reporting to return a proper state. manager := s.makeManager(c, "test") c.Logf("%#v", manager) instance := containertesting.CreateContainer(c, manager, "1/lxc/0") // The mock container will be immediately "running". c.Assert(instance.Status().Message, gc.Equals, string(golxc.StateRunning)) // DestroyContainer stops and then destroys the container, putting it // into "unknown" state. err := manager.DestroyContainer(instance.Id()) c.Assert(err, jc.ErrorIsNil) c.Assert(instance.Status().Message, gc.Equals, string(golxc.StateUnknown)) }
func (s *LxcSuite) TestDestroyContainerNameClash(c *gc.C) { manager := s.makeManager(c, "test") instance := containertesting.CreateContainer(c, manager, "1/lxc/0") name := string(instance.Id()) targetDir := filepath.Join(s.RemovedDir, name) err := os.MkdirAll(targetDir, 0755) c.Assert(err, gc.IsNil) err = manager.DestroyContainer(instance.Id()) c.Assert(err, gc.IsNil) // Check that the container dir is no longer in the container dir c.Assert(filepath.Join(s.ContainerDir, name), jc.DoesNotExist) // but instead, in the removed container dir with a ".1" suffix as there was already a directory there. c.Assert(filepath.Join(s.RemovedDir, fmt.Sprintf("%s.1", name)), jc.IsDirectory) }
func (s *LxcSuite) TestDestroyContainer(c *gc.C) { manager := s.makeManager(c, "test") instance := containertesting.CreateContainer(c, manager, "1/lxc/0") err := manager.DestroyContainer(instance.Id()) c.Assert(err, jc.ErrorIsNil) name := string(instance.Id()) // Check that the container dir is no longer in the container dir c.Assert(filepath.Join(s.ContainerDir, name), jc.DoesNotExist) // but instead, in the removed container dir c.Assert(filepath.Join(s.RemovedDir, name), jc.IsDirectory) c.Assert(s.loopDeviceManager.detachLoopDevicesArgs, jc.DeepEquals, [][]string{ {filepath.Join(s.LxcDir, name, "rootfs"), "/"}, }) }
func (s *LxcSuite) TestCreateContainer(c *gc.C) { manager := s.makeManager(c, "test") instance := containertesting.CreateContainer(c, manager, "1/lxc/0") name := string(instance.Id()) // Check our container config files: initial lxc.conf, the // run-time effective config, and cloud-init userdata. lxcConfContents, err := ioutil.ReadFile(filepath.Join(s.ContainerDir, name, "lxc.conf")) c.Assert(err, jc.ErrorIsNil) c.Assert(string(lxcConfContents), jc.Contains, "lxc.network.link = nic42") lxcConfContents, err = ioutil.ReadFile(lxc.ContainerConfigFilename(name)) c.Assert(err, jc.ErrorIsNil) c.Assert(string(lxcConfContents), jc.Contains, "lxc.network.link = nic42") cloudInitFilename := filepath.Join(s.ContainerDir, name, "cloud-init") data := containertesting.AssertCloudInit(c, cloudInitFilename) x := make(map[interface{}]interface{}) err = goyaml.Unmarshal(data, &x) c.Assert(err, jc.ErrorIsNil) var scripts []string for _, s := range x["runcmd"].([]interface{}) { scripts = append(scripts, s.(string)) } c.Assert(scripts[len(scripts)-3:], gc.DeepEquals, []string{ "start jujud-machine-1-lxc-0", "rm $bin/tools.tar.gz && rm $bin/juju2.3.4-quantal-amd64.sha256", "ifconfig", }) // Check the mount point has been created inside the container. c.Assert(filepath.Join(s.LxcDir, name, "rootfs", agent.DefaultPaths.LogDir), jc.IsDirectory) // Check that the config file is linked in the restart dir. expectedLinkLocation := filepath.Join(s.RestartDir, name+".conf") expectedTarget := filepath.Join(s.LxcDir, name, "config") linkInfo, err := os.Lstat(expectedLinkLocation) c.Assert(err, jc.ErrorIsNil) c.Assert(linkInfo.Mode()&os.ModeSymlink, gc.Equals, os.ModeSymlink) location, err := symlink.Read(expectedLinkLocation) c.Assert(err, jc.ErrorIsNil) c.Assert(location, gc.Equals, expectedTarget) }
func (s *LxcSuite) TestCreateContainerNoRestartDir(c *gc.C) { err := os.Remove(s.RestartDir) c.Assert(err, gc.IsNil) manager := s.makeManager(c, "test") instance := containertesting.CreateContainer(c, manager, "1/lxc/0") name := string(instance.Id()) autostartLink := lxc.RestartSymlink(name) config, err := ioutil.ReadFile(lxc.ContainerConfigFilename(name)) c.Assert(err, gc.IsNil) expected := ` lxc.network.type = veth lxc.network.link = nic42 lxc.network.flags = up lxc.start.auto = 1 lxc.mount.entry=/var/log/juju var/log/juju none defaults,bind 0 0 ` c.Assert(string(config), gc.Equals, expected) c.Assert(autostartLink, jc.DoesNotExist) }
func (s *localJujuTestSuite) TestDestroyRemovesContainers(c *gc.C) { env := s.testBootstrap(c, minimalConfig(c)) s.makeAgentsDir(c, env) s.PatchValue(local.CheckIfRoot, func() bool { return true }) namespace := env.Config().AllAttrs()["namespace"].(string) manager, err := lxc.NewContainerManager(container.ManagerConfig{ container.ConfigName: namespace, container.ConfigLogDir: "logdir", "use-clone": "false", }, nil, nil) c.Assert(err, jc.ErrorIsNil) machine1 := containertesting.CreateContainer(c, manager, "1") err = env.Destroy() c.Assert(err, jc.ErrorIsNil) container := s.ContainerFactory.New(string(machine1.Id())) c.Assert(container.IsConstructed(), jc.IsFalse) }
func (s *LxcSuite) TestCreateContainerAutostarts(c *gc.C) { manager := s.makeManager(c, "test") instance := containertesting.CreateContainer(c, manager, "1/lxc/0") autostartLink := lxc.RestartSymlink(string(instance.Id())) c.Assert(autostartLink, jc.IsSymlink) }
func (s *LxcSuite) TestNamedManagerPrefix(c *gc.C) { manager := s.makeManager(c, "eric") instance := containertesting.CreateContainer(c, manager, "1/lxc/0") c.Assert(string(instance.Id()), gc.Equals, "eric-machine-1-lxc-0") }
func (s *KVMSuite) TestCreateContainer(c *gc.C) { instance := containertesting.CreateContainer(c, s.manager, "1/kvm/0") name := string(instance.Id()) cloudInitFilename := filepath.Join(s.ContainerDir, name, "cloud-init") containertesting.AssertCloudInit(c, cloudInitFilename) }