func (s *MetadataTestSuite) TestReadsHostsCorrectly(c *check.C) {
	tc := &testCattleClient{}

	stack := metadata.Stack{
		Name: "test_stack1",
		Services: []metadata.Service{
			service1,
			{
				Name: "service2",
			},
		},
	}
	setSelfStack(stack)

	go func() {
		spAgent := storagepool.NewStoragepoolAgent(100, ".root", "1234567890", tc)
		err := spAgent.Run("http://localhost" + metadataUrl + "/mock-12-19-2015")
		if err != nil {
			c.Fatalf("Error starting storagepool agent [%v]", err)
		}
	}()
	time.Sleep(200 * time.Millisecond)

	uuids := tc.getLastSync()
	actual := map[string]bool{}
	for _, uuid := range uuids {
		actual[uuid] = true
	}
	c.Assert(actual, check.DeepEquals, map[string]bool{
		"hostUuid1": true,
		"hostUuid2": true,
	})
}
Esempio n. 2
0
func TestSyncLostHostDetectedInStoragePool(t *testing.T) {
	controlChan1 := make(chan bool, 1)
	controlChan2 := make(chan bool, 1)
	go writeTestHostHealthcheckFile("hostUuid1", controlChan1)
	go writeTestHostHealthcheckFile("hostUuid2", controlChan2)

	tc := &testCattleClient{}

	go func() {
		spAgent := storagepool.NewStoragepoolAgent(5, ".root", "1234567890", sphc, hcFileType, tc)
		err := spAgent.Run("")
		if err != nil {
			t.Fatalf("Error starting storagepool agent [%v]", err)
		}
	}()

	<-time.After(10 * time.Second)

	uuids := tc.getLastSync()
	if len(uuids) != 2 {
		t.Fatalf("expected 2 hosts in storagepool sync event, but received %d", len(uuids))
	}

	uuid1found := false
	uuid2found := false

	for _, uuid := range uuids {
		if uuid == "hostUuid1" {
			uuid1found = true
		}
		if uuid == "hostUuid2" {
			uuid2found = true
		}
	}

	if !uuid1found || !uuid2found {
		t.Fatalf("sync event not as expected, received %v", uuids)
	}

	// pause first host
	controlChan1 <- true

	<-time.After(20 * time.Second)

	uuids = tc.getLastSync()
	if len(uuids) != 1 {
		t.Fatalf("expected 1 host in storagepool sync event, but received %s", len(uuids))
	}

	if uuids[0] != "hostUuid2" {
		t.Fatalf("Expected hostUuid2 to be present,but found %s", uuids[0])
	}

	// resume
	controlChan1 <- true

	controlChan1 <- false
	controlChan2 <- false
}
Esempio n. 3
0
func TestSyncInitialHostsInStoragePool(t *testing.T) {
	controlChan1 := make(chan bool, 1)
	controlChan2 := make(chan bool, 1)
	go writeTestHostHealthcheckFile("hostUuid1", controlChan1)
	go writeTestHostHealthcheckFile("hostUuid2", controlChan2)

	tc := &testCattleClient{}

	go func() {
		spAgent := storagepool.NewStoragepoolAgent(5, ".root", "1234567890", sphc, hcFileType, tc)
		err := spAgent.Run("")
		if err != nil {
			t.Fatalf("Error starting storagepool agent [%v]", err)
		}
	}()

	<-time.After(10 * time.Second)

	uuids := tc.getLastSync()
	if len(uuids) != 2 {
		t.Fatalf("expected 2 storagepool sync events, but received [%d] %v", len(uuids), uuids)
	}

	uuid1found := false
	uuid2found := false

	for _, uuid := range uuids {
		if uuid == "hostUuid1" {
			uuid1found = true
		}
		if uuid == "hostUuid2" {
			uuid2found = true
		}
	}

	if !uuid1found || !uuid2found {
		t.Fatalf("sync event not as expected, received %v", uuids)
	}

	controlChan1 <- false
	controlChan2 <- false
}
func TestVersionChangeAndDeletion(t *testing.T) {
	tc := &testCattleClient{}
	stack := metadata.Stack{
		Name: "test_stack1",
		Services: []string{
			"service1",
			"service2",
		},
	}
	setSelfStack(stack)

	services := []metadata.Service{
		{
			Name: "service1",
			Containers: []string{
				"container1",
				"container2",
			},
			StackName: "test_stack1",
		},
		{
			Name: "service2",
			Containers: []string{
				"container3",
				"container4",
			},
			StackName: "test_stack2",
		},
		{
			Name: "service3",
			Containers: []string{
				"container5",
				"container6",
			},
			StackName: "test_stack1",
		},
	}
	setServices(services)

	containers := []metadata.Container{
		{
			Name:        "container1",
			ServiceName: "service1",
			StackName:   "test_stack1",
			HostUUID:    "hostUuid1",
		},
		{
			Name:        "container2",
			ServiceName: "service1",
			StackName:   "test_stack1",
			HostUUID:    "hostUuid2",
		},
		{
			Name:        "container3",
			ServiceName: "service2",
			StackName:   "test_stack2",
			HostUUID:    "hostUuid3",
		},
		{
			Name:        "container4",
			ServiceName: "service2",
			StackName:   "test_stack2",
			HostUUID:    "hostUuid4",
		},
		{
			Name:        "container5",
			ServiceName: "service3",
			StackName:   "test_stack1",
			HostUUID:    "hostUuid5",
		},
		{
			Name:        "container6",
			ServiceName: "service3",
			StackName:   "test_stack1",
			HostUUID:    "hostUuid6",
		},
	}
	setContainers(containers)
	go func() {
		spAgent := storagepool.NewStoragepoolAgent(5, ".root", "1234567890", sphc, hcMetadataType, tc)
		err := spAgent.Run("http://localhost" + metadataUrl + "/07-25-2015")
		if err != nil {
			t.Fatalf("Error starting storagepool agent [%v]", err)
		}
	}()
	time.Sleep(10 * time.Second)

	uuids := tc.getLastSync()
	if len(uuids) != 2 {
		t.Fatalf("expected 2 storagepool sync events, but received [%d] %v", len(uuids), uuids)
	}

	uuid1found := false
	uuid2found := false

	for _, uuid := range uuids {
		if uuid == "hostUuid1" {
			uuid1found = true
		}
		if uuid == "hostUuid2" {
			uuid2found = true
		}
	}

	if !uuid1found || !uuid2found {
		t.Fatalf("sync event not as expected, received %v", uuids)
	}

	stack = metadata.Stack{
		Name: "test_stack1",
		Services: []string{
			"service1",
			"service3",
		},
	}
	setSelfStack(stack)

	newServices := []metadata.Service{
		{
			Name: "service1",
			Containers: []string{
				"container1",
				"container2",
			},
			StackName: "test_stack1",
		},
		{
			Name: "service2",
			Containers: []string{
				"container3",
				"container4",
			},
			StackName: "test_stack2",
		},
		{
			Name: "service3",
			Containers: []string{
				"container5",
				"container6",
			},
			StackName: "test_stack1",
		},
	}
	setServices(newServices)

	newContainers := []metadata.Container{
		{
			Name:        "container1",
			ServiceName: "service1",
			StackName:   "test_stack1",
			HostUUID:    "hostUuid1",
		},
		{
			Name:        "container3",
			ServiceName: "service2",
			StackName:   "test_stack2",
			HostUUID:    "hostUuid3",
		},
		{
			Name:        "container4",
			ServiceName: "service2",
			StackName:   "test_stack2",
			HostUUID:    "hostUuid4",
		},
	}
	setContainers(newContainers)
	time.Sleep(10 * time.Second)

	uuids = tc.getLastSync()
	if len(uuids) != 1 {
		t.Fatalf("expected 0 storagepool sync events, but received [%d] %v", len(uuids), uuids)
	}

	if uuids[0] != "hostUuid1" {
		t.Fatalf("sync event on delete does not work")
	}

}