Introduce test cases for proposed dependency sorting

This commit is contained in:
DarkAEther 2020-09-12 02:26:42 +05:30
parent 348f121a61
commit d65e6cd96c
4 changed files with 175 additions and 9 deletions

View file

@ -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,

View file

@ -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
}

View file

@ -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{

View file

@ -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))
})
})
})
})