func (s *ContainersSuite) TestTeardown(c *gocheck.C) {
	os.Setenv("SUPERVISOR_PRETEND", "true")
	saveDir := "save_test"
	os.RemoveAll(saveDir)
	c.Assert(Init("localhost", saveDir, uint16(2), uint16(2), uint16(61000), 100, 1024, false), gocheck.IsNil)
	// First reserve should work
	_, err := Reserve("first", &types.Manifest{CPUShares: 1, MemoryLimit: 1})
	c.Assert(err, gocheck.IsNil)
	// Second should fail because id is taken
	_, err = Reserve("first", &types.Manifest{CPUShares: 1, MemoryLimit: 1})
	c.Assert(err, gocheck.ErrorMatches, "The ID \\(first\\) is in use\\.")
	// Teardown
	c.Assert(Teardown("first"), gocheck.Equals, true)
	// Third should work
	_, err = Reserve("first", &types.Manifest{CPUShares: 1, MemoryLimit: 1})
	c.Assert(err, gocheck.IsNil)
	os.RemoveAll(saveDir)
	dieChan <- true
}
func (s *SerializeSuite) TestSerialize(c *gocheck.C) {
	SaveDir = "save_test"
	os.RemoveAll(SaveDir)
	c.Assert(os.MkdirAll(SaveDir, 0755), gocheck.IsNil)
	// test save/retrieve slice
	savedSlice := []uint16{5, 4, 3, 2, 1}
	var retrievedSlice []uint16
	SaveObject("slice", savedSlice)
	c.Assert(RetrieveObject("slice", &retrievedSlice), gocheck.Equals, nil)
	c.Assert(retrievedSlice, gocheck.DeepEquals, savedSlice)
	// test save/retrieve map
	savedMap := map[string]*TestSerializeStruct{}
	var retrievedMap map[string]*TestSerializeStruct
	savedMap["one"] = &TestSerializeStruct{1, true, "one", []string{"one", "alsoOne"}, map[string]string{
		"one":     "yes",
		"alsoOne": "alsoYes",
	}}
	savedMap["two"] = &TestSerializeStruct{2, false, "two", nil, nil}
	SaveObject("map", savedMap)
	c.Assert(RetrieveObject("map", &retrievedMap), gocheck.Equals, nil)
	c.Assert(retrievedMap, gocheck.DeepEquals, savedMap)
	os.RemoveAll(SaveDir)
}
func (s *ContainersSuite) TestInit(c *gocheck.C) {
	// Positive test
	saveDir := "save_test"
	os.RemoveAll(saveDir)
	c.Assert(Init("localhost", saveDir, uint16(100), uint16(5), uint16(61000), 100, 1024, false), gocheck.IsNil)
	if _, err := os.Stat(saveDir); err != nil {
		c.Fatal("Init did not make the save directory")
	}
	os.RemoveAll(saveDir)
	dieChan <- true

	// Negative test for invalid config
	c.Assert(Init("localhost", saveDir, uint16(1000), uint16(5), uint16(61000), 100, 1024, false), gocheck.ErrorMatches, "Invalid Config.+")

	// Negative test for invalid config
	c.Assert(Init("localhost", saveDir, uint16(65535), uint16(65535), uint16(65535), 100, 1024, false), gocheck.ErrorMatches, "Invalid Config.+")
	os.RemoveAll(saveDir)
}
示例#4
0
func (s *RpcSuite) TestTeardown(c *gocheck.C) {
	os.Setenv("SUPERVISOR_PRETEND", "true")
	saveDir := "save_test"
	os.RemoveAll(saveDir)
	containers.Init("localhost", saveDir, 10, 2, 61000, 100, 1024, false)
	ih := new(Supervisor)
	// deploy a container
	var dreply SupervisorDeployReply
	darg := SupervisorDeployArg{App: "theApp", Sha: "theSha", ContainerID: "theContainerID", Manifest: &Manifest{CPUShares: 1, MemoryLimit: 1}}
	c.Assert(ih.Deploy(darg, &dreply), gocheck.IsNil)
	// teardown invalid args
	arg := SupervisorTeardownArg{}
	var reply SupervisorTeardownReply
	c.Assert(ih.Teardown(arg, &reply), gocheck.ErrorMatches, "Please specify container ids or all\\.")
	// teardown no container ids
	arg = SupervisorTeardownArg{ContainerIDs: []string{}}
	reply = SupervisorTeardownReply{}
	c.Assert(ih.Teardown(arg, &reply), gocheck.IsNil)
	c.Assert(reply.Status, gocheck.Equals, StatusOk)
	c.Assert(reply.ContainerIDs, gocheck.DeepEquals, arg.ContainerIDs)
	// teardown nonexistant container id
	arg = SupervisorTeardownArg{ContainerIDs: []string{"doesntExist"}}
	reply = SupervisorTeardownReply{}
	c.Assert(ih.Teardown(arg, &reply), gocheck.IsNil)
	c.Assert(reply.Status != StatusOk, gocheck.Equals, true) // gocheck doesn't have a NotEquals?!
	c.Assert(reply.ContainerIDs, gocheck.DeepEquals, []string{})
	// deploy more
	darg = SupervisorDeployArg{App: "theApp2", Sha: "theSha2", ContainerID: "theContainerID2", Manifest: &Manifest{CPUShares: 1, MemoryLimit: 1}}
	dreply = SupervisorDeployReply{}
	c.Assert(ih.Deploy(darg, &dreply), gocheck.IsNil)
	darg = SupervisorDeployArg{App: "theApp3", Sha: "theSha3", ContainerID: "theContainerID3", Manifest: &Manifest{CPUShares: 1, MemoryLimit: 1}}
	dreply = SupervisorDeployReply{}
	c.Assert(ih.Deploy(darg, &dreply), gocheck.IsNil)
	darg = SupervisorDeployArg{App: "theApp4", Sha: "theSha4", ContainerID: "theContainerID4", Manifest: &Manifest{CPUShares: 1, MemoryLimit: 1}}
	dreply = SupervisorDeployReply{}
	c.Assert(ih.Deploy(darg, &dreply), gocheck.IsNil)
	darg = SupervisorDeployArg{App: "theApp5", Sha: "theSha5", ContainerID: "theContainerID5", Manifest: &Manifest{CPUShares: 1, MemoryLimit: 1}}
	dreply = SupervisorDeployReply{}
	c.Assert(ih.Deploy(darg, &dreply), gocheck.IsNil)
	// teardown multiple container ids with one failure
	arg = SupervisorTeardownArg{ContainerIDs: []string{"doesntExist", "theContainerID", "theContainerID2"}}
	reply = SupervisorTeardownReply{}
	c.Assert(ih.Teardown(arg, &reply), gocheck.IsNil)
	c.Assert(reply.Status != StatusOk, gocheck.Equals, true) // gocheck doesn't have a NotEquals?!
	c.Assert(reply.ContainerIDs, gocheck.DeepEquals, []string{"theContainerID", "theContainerID2"})
	// deploy more
	darg = SupervisorDeployArg{App: "theApp", Sha: "theSha", ContainerID: "theContainerID", Manifest: &Manifest{CPUShares: 1, MemoryLimit: 1}}
	dreply = SupervisorDeployReply{}
	c.Assert(ih.Deploy(darg, &dreply), gocheck.IsNil)
	darg = SupervisorDeployArg{App: "theApp2", Sha: "theSha2", ContainerID: "theContainerID2", Manifest: &Manifest{CPUShares: 1, MemoryLimit: 1}}
	dreply = SupervisorDeployReply{}
	c.Assert(ih.Deploy(darg, &dreply), gocheck.IsNil)
	// teardown multiple container ids with no failure
	arg = SupervisorTeardownArg{ContainerIDs: []string{"theContainerID", "theContainerID2"}}
	reply = SupervisorTeardownReply{}
	c.Assert(ih.Teardown(arg, &reply), gocheck.IsNil)
	c.Assert(reply.Status, gocheck.Equals, StatusOk) // gocheck doesn't have a NotEquals?!
	c.Assert(reply.ContainerIDs, gocheck.DeepEquals, []string{"theContainerID", "theContainerID2"})
	// teardown all
	arg = SupervisorTeardownArg{All: true}
	reply = SupervisorTeardownReply{}
	c.Assert(ih.Teardown(arg, &reply), gocheck.IsNil)
	c.Assert(reply.Status, gocheck.Equals, StatusOk) // gocheck doesn't have a NotEquals?!
	expectedContainerIDs := []string{"theContainerID3", "theContainerID4", "theContainerID5"}
	// sort container ids before Assert
	sort.Strings(reply.ContainerIDs)
	c.Assert(reply.ContainerIDs, gocheck.DeepEquals, expectedContainerIDs)
	os.RemoveAll(saveDir)
}
示例#5
0
func (s *RpcSuite) TestDeploy(c *gocheck.C) {
	os.Setenv("SUPERVISOR_PRETEND", "true")
	saveDir := "save_test"
	os.RemoveAll(saveDir)
	containers.Init("localhost", saveDir, 2, 2, 61000, 100, 1024, false)
	ih := new(Supervisor)
	arg := SupervisorDeployArg{}
	var reply SupervisorDeployReply
	c.Assert(ih.Deploy(arg, &reply), gocheck.ErrorMatches, "Please specify an app\\.")
	arg = SupervisorDeployArg{App: "theApp"}
	reply = SupervisorDeployReply{}
	c.Assert(ih.Deploy(arg, &reply), gocheck.ErrorMatches, "Please specify a sha\\.")
	arg = SupervisorDeployArg{App: "theApp", Sha: "theSha"}
	reply = SupervisorDeployReply{}
	c.Assert(ih.Deploy(arg, &reply), gocheck.ErrorMatches, "Please specify a container id\\.")
	arg = SupervisorDeployArg{App: "theApp", Sha: "theSha", ContainerID: "theContainerID"}
	reply = SupervisorDeployReply{}
	c.Assert(ih.Deploy(arg, &reply), gocheck.ErrorMatches, "Please specify a manifest\\.")
	arg = SupervisorDeployArg{App: "theApp", Sha: "theSha", ContainerID: "theContainerID", Manifest: &Manifest{}}
	reply = SupervisorDeployReply{}
	c.Assert(ih.Deploy(arg, &reply), gocheck.ErrorMatches, "Please specify a number of CPU shares\\.")
	arg = SupervisorDeployArg{App: "theApp", Sha: "theSha", ContainerID: "theContainerID", Manifest: &Manifest{CPUShares: 1}}
	reply = SupervisorDeployReply{}
	c.Assert(ih.Deploy(arg, &reply), gocheck.ErrorMatches, "Please specify a memory limit\\.")
	arg = SupervisorDeployArg{App: "theApp", Sha: "theSha", ContainerID: "theContainerID", Manifest: &Manifest{CPUShares: 1, MemoryLimit: 1}}
	reply = SupervisorDeployReply{}
	c.Assert(ih.Deploy(arg, &reply), gocheck.IsNil)
	c.Assert(reply.Container.ID, gocheck.Equals, arg.ContainerID)
	c.Assert(reply.Container.PrimaryPort, gocheck.Equals, uint16(61000))
	c.Assert(reply.Container.SecondaryPorts, gocheck.DeepEquals, []uint16{61004, 61006})
	c.Assert(reply.Container.SSHPort, gocheck.Equals, uint16(61002))
	c.Assert(reply.Container.ID, gocheck.Equals, "theContainerID")
	c.Assert(reply.Container.App, gocheck.Equals, "theApp")
	c.Assert(reply.Container.Sha, gocheck.Equals, "theSha")
	c.Assert(reply.Container.DockerID, gocheck.Equals, "pretend-docker-id-theContainerID")
	os.RemoveAll(saveDir)
}
示例#6
0
func (s *RpcSuite) TestInit(c *gocheck.C) {
	c.Assert(Listen, gocheck.PanicMatches, "Not Initialized.")
	c.Assert(Init(":1337"), gocheck.IsNil)
}
示例#7
0
func (s *RpcSuite) TestList(c *gocheck.C) {
	os.Setenv("SUPERVISOR_PRETEND", "true")
	saveDir := "save_test"
	os.RemoveAll(saveDir)
	containers.Init("localhost", saveDir, 2, 2, 61000, 100, 1024, false)
	ih := new(Supervisor)
	// list
	arg := SupervisorListArg{}
	var reply SupervisorListReply
	c.Assert(ih.List(arg, &reply), gocheck.IsNil)
	c.Assert(reply.Containers, gocheck.DeepEquals, map[string]*Container{})
	c.Assert(reply.UnusedPorts, gocheck.DeepEquals, []uint16{61000, 61001})
	// deploy one
	var dreply SupervisorDeployReply
	darg := SupervisorDeployArg{App: "theApp1", Sha: "theSha1", ContainerID: "theContainerID1", Manifest: &Manifest{CPUShares: 1, MemoryLimit: 1}}
	c.Assert(ih.Deploy(darg, &dreply), gocheck.IsNil)
	container1 := *dreply.Container
	// list
	arg = SupervisorListArg{}
	reply = SupervisorListReply{}
	c.Assert(ih.List(arg, &reply), gocheck.IsNil)
	c.Assert(reply.Containers, gocheck.DeepEquals, map[string]*Container{container1.ID: &container1})
	c.Assert(reply.UnusedPorts, gocheck.DeepEquals, []uint16{61001})
	// deploy another
	dreply = SupervisorDeployReply{}
	darg = SupervisorDeployArg{App: "theApp2", Sha: "theSha2", ContainerID: "theContainerID2", Manifest: &Manifest{CPUShares: 1, MemoryLimit: 1}}
	c.Assert(ih.Deploy(darg, &dreply), gocheck.IsNil)
	container2 := *dreply.Container
	// list
	arg = SupervisorListArg{}
	reply = SupervisorListReply{}
	c.Assert(ih.List(arg, &reply), gocheck.IsNil)
	c.Assert(reply.Containers, gocheck.DeepEquals, map[string]*Container{container1.ID: &container1,
		container2.ID: &container2})
	c.Assert(reply.UnusedPorts, gocheck.DeepEquals, []uint16{})
	// teardown all
	targ := SupervisorTeardownArg{All: true}
	var treply SupervisorTeardownReply
	c.Assert(ih.Teardown(targ, &treply), gocheck.IsNil)
	// list
	arg = SupervisorListArg{}
	reply = SupervisorListReply{}
	c.Assert(ih.List(arg, &reply), gocheck.IsNil)
	c.Assert(reply.Containers, gocheck.DeepEquals, map[string]*Container{})
	c.Assert(reply.UnusedPorts, gocheck.DeepEquals, []uint16{61000, 61001})
	os.RemoveAll(saveDir)
}
示例#8
0
func (s *RpcSuite) TestHealthCheck(c *gocheck.C) {
	os.Setenv("SUPERVISOR_PRETEND", "true")
	saveDir := "save_test"
	os.RemoveAll(saveDir)
	containers.Init("localhost", saveDir, 2, 2, 61000, 100, 1024, false)
	ih := new(Supervisor)
	// check health
	arg := SupervisorHealthCheckArg{}
	var reply SupervisorHealthCheckReply
	c.Assert(ih.HealthCheck(arg, &reply), gocheck.IsNil)
	c.Assert(reply.Status, gocheck.Equals, StatusOk)
	c.Assert(reply.Containers.Total, gocheck.Equals, uint(2))
	c.Assert(reply.Containers.Free, gocheck.Equals, uint(2))
	c.Assert(reply.Containers.Used, gocheck.Equals, uint(0))
	// deploy one
	var dreply SupervisorDeployReply
	darg := SupervisorDeployArg{App: "theApp1", Sha: "theSha1", ContainerID: "theContainerID1", Manifest: &Manifest{CPUShares: 1, MemoryLimit: 1}}
	c.Assert(ih.Deploy(darg, &dreply), gocheck.IsNil)
	// check health
	arg = SupervisorHealthCheckArg{}
	reply = SupervisorHealthCheckReply{}
	c.Assert(ih.HealthCheck(arg, &reply), gocheck.IsNil)
	c.Assert(reply.Status, gocheck.Equals, StatusOk)
	c.Assert(reply.Containers.Total, gocheck.Equals, uint(2))
	c.Assert(reply.Containers.Free, gocheck.Equals, uint(1))
	c.Assert(reply.Containers.Used, gocheck.Equals, uint(1))
	// deploy another
	dreply = SupervisorDeployReply{}
	darg = SupervisorDeployArg{App: "theApp2", Sha: "theSha2", ContainerID: "theContainerID2", Manifest: &Manifest{CPUShares: 1, MemoryLimit: 1}}
	c.Assert(ih.Deploy(darg, &dreply), gocheck.IsNil)
	// check health
	arg = SupervisorHealthCheckArg{}
	reply = SupervisorHealthCheckReply{}
	c.Assert(ih.HealthCheck(arg, &reply), gocheck.IsNil)
	c.Assert(reply.Status, gocheck.Equals, StatusFull)
	c.Assert(reply.Containers.Total, gocheck.Equals, uint(2))
	c.Assert(reply.Containers.Free, gocheck.Equals, uint(0))
	c.Assert(reply.Containers.Used, gocheck.Equals, uint(2))
	// teardown all
	targ := SupervisorTeardownArg{All: true}
	var treply SupervisorTeardownReply
	c.Assert(ih.Teardown(targ, &treply), gocheck.IsNil)
	// check health
	arg = SupervisorHealthCheckArg{}
	reply = SupervisorHealthCheckReply{}
	c.Assert(ih.HealthCheck(arg, &reply), gocheck.IsNil)
	c.Assert(reply.Status, gocheck.Equals, StatusOk)
	c.Assert(reply.Containers.Total, gocheck.Equals, uint(2))
	c.Assert(reply.Containers.Free, gocheck.Equals, uint(2))
	c.Assert(reply.Containers.Used, gocheck.Equals, uint(0))
	os.RemoveAll(saveDir)
}
func (s *ContainersSuite) TestReserve(c *gocheck.C) {
	saveDir := "save_test"
	os.RemoveAll(saveDir)
	c.Assert(Init("localhost", saveDir, uint16(2), uint16(2), uint16(61000), 100, 1024, false), gocheck.IsNil)
	// First reserve should work
	container, err := Reserve("first", &types.Manifest{CPUShares: 50, MemoryLimit: 512})
	c.Assert(err, gocheck.IsNil)
	c.Assert(container.PrimaryPort, gocheck.Equals, uint16(61000))
	c.Assert(container.SecondaryPorts, gocheck.DeepEquals, []uint16{61004, 61006})
	c.Assert(container.SSHPort, gocheck.Equals, uint16(61002))
	c.Assert(container.ID, gocheck.Equals, "first")
	c.Assert(container.App, gocheck.Equals, "")
	c.Assert(container.Sha, gocheck.Equals, "")
	c.Assert(container.DockerID, gocheck.Equals, "")
	// Second should fail because id is taken
	container, err = Reserve("first", &types.Manifest{CPUShares: 1, MemoryLimit: 1})
	c.Assert(err, gocheck.ErrorMatches, "The ID \\(first\\) is in use\\.")
	// Third should fail because not enough CPU shares
	container, err = Reserve("third", &types.Manifest{CPUShares: 51, MemoryLimit: 1})
	c.Assert(err, gocheck.ErrorMatches, "Not enough CPU Shares to reserve. \\(51 requested, 50 available\\)")
	// Fourth should fail because not enough CPU shares
	container, err = Reserve("fourth", &types.Manifest{CPUShares: 1, MemoryLimit: 513})
	c.Assert(err, gocheck.ErrorMatches, "Not enough Memory to reserve. \\(513 requested, 512 available\\)")
	// Fifth should work
	container, err = Reserve("fifth", &types.Manifest{CPUShares: 1, MemoryLimit: 1})
	c.Assert(err, gocheck.IsNil)
	c.Assert(container.PrimaryPort, gocheck.Equals, uint16(61001))
	c.Assert(container.SecondaryPorts, gocheck.DeepEquals, []uint16{61005, 61007})
	c.Assert(container.SSHPort, gocheck.Equals, uint16(61003))
	c.Assert(container.ID, gocheck.Equals, "fifth")
	c.Assert(container.App, gocheck.Equals, "")
	c.Assert(container.Sha, gocheck.Equals, "")
	c.Assert(container.DockerID, gocheck.Equals, "")
	// Sixth should fail because there are no containers
	container, err = Reserve("sixth", &types.Manifest{CPUShares: 1, MemoryLimit: 1})
	c.Assert(err, gocheck.ErrorMatches, "No free containers to reserve\\.")
	os.RemoveAll(saveDir)
	dieChan <- true
}
func (s *ContainersSuite) TestNums(c *gocheck.C) {
	os.Setenv("SUPERVISOR_PRETEND", "true")
	saveDir := "save_test"
	os.RemoveAll(saveDir)
	c.Assert(Init("localhost", saveDir, uint16(2), uint16(2), uint16(61000), 100, 1024, false), gocheck.IsNil)
	// reserve first and list
	_, err := Reserve("first", &types.Manifest{CPUShares: 1, MemoryLimit: 100})
	c.Assert(err, gocheck.IsNil)
	cont, cpu, mem := Nums()
	c.Assert(cont.Total, gocheck.Equals, uint(2))
	c.Assert(cont.Used, gocheck.Equals, uint(1))
	c.Assert(cont.Free, gocheck.Equals, uint(1))
	c.Assert(cpu.Total, gocheck.Equals, uint(100))
	c.Assert(cpu.Used, gocheck.Equals, uint(1))
	c.Assert(cpu.Free, gocheck.Equals, uint(99))
	c.Assert(mem.Total, gocheck.Equals, uint(1024))
	c.Assert(mem.Used, gocheck.Equals, uint(100))
	c.Assert(mem.Free, gocheck.Equals, uint(924))
	// reserve second and list
	_, err = Reserve("second", &types.Manifest{CPUShares: 2, MemoryLimit: 200})
	c.Assert(err, gocheck.IsNil)
	cont, cpu, mem = Nums()
	c.Assert(cont.Total, gocheck.Equals, uint(2))
	c.Assert(cont.Used, gocheck.Equals, uint(2))
	c.Assert(cont.Free, gocheck.Equals, uint(0))
	c.Assert(cpu.Total, gocheck.Equals, uint(100))
	c.Assert(cpu.Used, gocheck.Equals, uint(3))
	c.Assert(cpu.Free, gocheck.Equals, uint(97))
	c.Assert(mem.Total, gocheck.Equals, uint(1024))
	c.Assert(mem.Used, gocheck.Equals, uint(300))
	c.Assert(mem.Free, gocheck.Equals, uint(724))
	// teardown first and list
	c.Assert(Teardown("first"), gocheck.Equals, true)
	cont, cpu, mem = Nums()
	c.Assert(cont.Total, gocheck.Equals, uint(2))
	c.Assert(cont.Used, gocheck.Equals, uint(1))
	c.Assert(cont.Free, gocheck.Equals, uint(1))
	c.Assert(cpu.Total, gocheck.Equals, uint(100))
	c.Assert(cpu.Used, gocheck.Equals, uint(2))
	c.Assert(cpu.Free, gocheck.Equals, uint(98))
	c.Assert(mem.Total, gocheck.Equals, uint(1024))
	c.Assert(mem.Used, gocheck.Equals, uint(200))
	c.Assert(mem.Free, gocheck.Equals, uint(824))
	os.RemoveAll(saveDir)
	dieChan <- true
}
func (s *ContainersSuite) TestList(c *gocheck.C) {
	os.Setenv("SUPERVISOR_PRETEND", "true")
	saveDir := "save_test"
	os.RemoveAll(saveDir)
	c.Assert(Init("localhost", saveDir, uint16(2), uint16(2), uint16(61000), 100, 1024, false), gocheck.IsNil)
	// reserve first and list
	first, err := Reserve("first", &types.Manifest{CPUShares: 1, MemoryLimit: 1})
	c.Assert(err, gocheck.IsNil)
	conts, ports := List()
	c.Assert(*conts["first"], gocheck.DeepEquals, first.Container)
	c.Assert(ports, gocheck.DeepEquals, []uint16{61001})
	// reserve second and list
	second, err := Reserve("second", &types.Manifest{CPUShares: 2, MemoryLimit: 2})
	c.Assert(err, gocheck.IsNil)
	conts, ports = List()
	c.Assert(*conts["second"], gocheck.DeepEquals, second.Container)
	c.Assert(ports, gocheck.DeepEquals, []uint16{})
	// teardown first and list
	c.Assert(Teardown("first"), gocheck.Equals, true)
	conts, ports = List()
	_, present := conts["first"]
	c.Assert(present, gocheck.Equals, false)
	c.Assert(ports, gocheck.DeepEquals, []uint16{61000})
	os.RemoveAll(saveDir)
	dieChan <- true
}