From d65e6cd96c122d22a8f4bcce1fc583f7109ba384 Mon Sep 17 00:00:00 2001 From: DarkAEther <30438425+DarkAEther@users.noreply.github.com> Date: Sat, 12 Sep 2020 02:26:42 +0530 Subject: [PATCH] Introduce test cases for proposed dependency sorting --- internal/actions/actions_suite_test.go | 15 ++- internal/actions/mocks/client.go | 4 + internal/actions/mocks/container.go | 11 +- internal/actions/update_test.go | 154 ++++++++++++++++++++++++- 4 files changed, 175 insertions(+), 9 deletions(-) diff --git a/internal/actions/actions_suite_test.go b/internal/actions/actions_suite_test.go index 3a51bfa..e966fd8 100644 --- a/internal/actions/actions_suite_test.go +++ b/internal/actions/actions_suite_test.go @@ -57,7 +57,8 @@ var _ = Describe("the actions package", func() { "test-container", "test-container", "watchtower", - time.Now()), + time.Now(), + make([]string,0)), } err := actions.CheckForMultipleWatchtowerInstances(client, false) Expect(err).NotTo(HaveOccurred()) @@ -75,12 +76,14 @@ var _ = Describe("the actions package", func() { "test-container-01", "test-container-01", "watchtower", - time.Now().AddDate(0, 0, -1)), + time.Now().AddDate(0, 0, -1), + make([]string,0),), CreateMockContainer( "test-container-02", "test-container-02", "watchtower", - time.Now()), + time.Now(), + make([]string,0)), }, }, dockerClient, @@ -106,12 +109,14 @@ var _ = Describe("the actions package", func() { "test-container-01", "test-container-01", "watchtower", - time.Now().AddDate(0, 0, -1)), + time.Now().AddDate(0, 0, -1), + make([]string,0),), CreateMockContainer( "test-container-02", "test-container-02", "watchtower", - time.Now()), + time.Now(), + make([]string,0),), }, }, dockerClient, diff --git a/internal/actions/mocks/client.go b/internal/actions/mocks/client.go index 33c196d..fb49249 100644 --- a/internal/actions/mocks/client.go +++ b/internal/actions/mocks/client.go @@ -22,6 +22,8 @@ type TestData struct { TriedToRemoveImageCount int NameOfContainerToKeep string Containers []container.Container + StopOrder []string + RestartOrder []string } // TriedToRemoveImage is a test helper function to check whether RemoveImageByID has been called @@ -49,11 +51,13 @@ func (client MockClient) StopContainer(c container.Container, d time.Duration) e if c.Name() == client.TestData.NameOfContainerToKeep { return errors.New("tried to stop the instance we want to keep") } + client.TestData.StopOrder = append(client.TestData.StopOrder, c.Name()) return nil } // StartContainer is a mock method func (client MockClient) StartContainer(c container.Container) (string, error) { + client.TestData.RestartOrder = append(client.TestData.RestartOrder, c.Name()) return "", nil } diff --git a/internal/actions/mocks/container.go b/internal/actions/mocks/container.go index 060340e..12ac3c8 100644 --- a/internal/actions/mocks/container.go +++ b/internal/actions/mocks/container.go @@ -8,7 +8,7 @@ import ( ) // CreateMockContainer creates a container substitute valid for testing -func CreateMockContainer(id string, name string, image string, created time.Time) container.Container { +func CreateMockContainer(id string, name string, image string, created time.Time, depends []string) container.Container { content := types.ContainerJSON{ ContainerJSONBase: &types.ContainerJSONBase{ ID: id, @@ -20,6 +20,15 @@ func CreateMockContainer(id string, name string, image string, created time.Time Labels: make(map[string]string), }, } + dependency_string := "" + for ind, i := range depends { + if ind == 0 { + dependency_string += i; + }else{ + dependency_string += "," + i; + } + } + content.Config.Labels["com.centurylinklabs.watchtower.depends-on"] = dependency_string return *container.NewContainer( &content, &types.ImageInspect{ diff --git a/internal/actions/update_test.go b/internal/actions/update_test.go index 62945dc..315c1c7 100644 --- a/internal/actions/update_test.go +++ b/internal/actions/update_test.go @@ -36,17 +36,20 @@ var _ = Describe("the update action", func() { "test-container-01", "test-container-01", "fake-image:latest", - time.Now().AddDate(0, 0, -1)), + time.Now().AddDate(0, 0, -1), + make([]string, 0)), CreateMockContainer( "test-container-02", "test-container-02", "fake-image:latest", - time.Now()), + time.Now(), + make([]string, 0)), CreateMockContainer( "test-container-02", "test-container-02", "fake-image:latest", - time.Now()), + time.Now(), + make([]string, 0)), }, }, dockerClient, @@ -63,6 +66,7 @@ var _ = Describe("the update action", func() { Expect(client.TestData.TriedToRemoveImageCount).To(Equal(1)) }) }) + When("there are multiple containers using different images", func() { It("should try to remove each of them", func() { client.TestData.Containers = append( @@ -72,12 +76,156 @@ var _ = Describe("the update action", func() { "unique-test-container", "unique-fake-image:latest", time.Now(), + make([]string, 0), ), ) err := actions.Update(client, types.UpdateParams{Cleanup: true}) Expect(err).NotTo(HaveOccurred()) Expect(client.TestData.TriedToRemoveImageCount).To(Equal(2)) }) + }) + }) + + When("there are containers with and without links", func() { + links := [7][]string{ + {}, + {"k-container-01"}, + {"k-container-02"}, + {}, + {"t-container-01"}, + {"t-container-02"}, + {}, + } + BeforeEach(func() { + pullImages := false + removeVolumes := false + client = CreateMockClient( + &TestData{ + NameOfContainerToKeep: "", + Containers: []container.Container{ + CreateMockContainer( + "k-container-03", + "k-container-03", + "fake-image:latest", + time.Now().Add(time.Second * 4), + links[2],), + CreateMockContainer( + "k-container-02", + "k-container-02", + "fake-image:latest", + time.Now().Add(time.Second * 2), + links[1],), + CreateMockContainer( + "k-container-01", + "k-container-01", + "fake-image:latest", + time.Now(), + links[0],), + + CreateMockContainer( + "t-container-03", + "t-container-03", + "fake-image-2:latest", + time.Now().Add(time.Second * 4), + links[5],), + CreateMockContainer( + "t-container-02", + "t-container-02", + "fake-image-2:latest", + time.Now().Add(time.Second * 2), + links[4],), + CreateMockContainer( + "t-container-01", + "t-container-01", + "fake-image-2:latest", + time.Now(), + links[3],), + + CreateMockContainer( + "x-container-01", + "x-container-01", + "fake-image-1:latest", + time.Now(), + links[6],), + CreateMockContainer( + "x-container-02", + "x-container-02", + "fake-image-1:latest", + time.Now().Add(time.Second * 2), + links[6],), + CreateMockContainer( + "x-container-03", + "x-container-03", + "fake-image-1:latest", + time.Now().Add(time.Second * 4), + links[6],), + }, + }, + dockerClient, + pullImages, + removeVolumes, + ) + }) + + When("there are multiple containers with links", func() { + It("should create appropriate dependency sorted lists", func() { + dependencySortedGraphs, err := actions.PrepareContainerList(client, types.UpdateParams{Cleanup: true}) + Expect(err).NotTo(HaveOccurred()) + + var output [][]string + + for _, i := range dependencySortedGraphs { + var inner []string + for _, j := range i { + inner = append(inner, j.Name()) + } + output = append(output,inner) + } + + ExpectedOutput := [][]string{ + {"k-container-01", "k-container-02", "k-container-03",}, + {"t-container-01", "t-container-02", "t-container-03",}, + {"x-container-01",}, + {"x-container-02",}, + {"x-container-03",}, + } + + Expect(output).To(Equal(ExpectedOutput)) + }) + }) + + When("there are multiple containers using the same image", func() { + It("should stop and restart containers in a correct order", func() { + err := actions.Update(client, types.UpdateParams{Cleanup: true}) + Expect(err).NotTo(HaveOccurred()) + + ExpectedStopOutput := []string{ + "k-container-03", + "k-container-02", + "k-container-01", + "t-container-03", + "t-container-02", + "t-container-01", + "x-container-01", + "x-container-02", + "x-container-03", + } + + ExpectedRestartOutput := []string{ + "k-container-01", + "k-container-02", + "k-container-03", + "t-container-01", + "t-container-02", + "t-container-03", + "x-container-01", + "x-container-02", + "x-container-03", + } + + Expect(client.TestData.StopOrder).To(Equal(ExpectedStopOutput)) + Expect(client.TestData.RestartOrder).To(Equal(ExpectedRestartOutput)) + }) }) }) })