|
| 1 | +package container |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/containrrr/watchtower/pkg/container/mocks" |
| 5 | + "github.com/containrrr/watchtower/pkg/filters" |
| 6 | + cli "github.com/docker/docker/client" |
| 7 | + . "github.com/onsi/ginkgo" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + "github.com/onsi/gomega/gbytes" |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | +) |
| 12 | + |
| 13 | +var _ = Describe("the client", func() { |
| 14 | + var docker *cli.Client |
| 15 | + var client Client |
| 16 | + BeforeSuite(func() { |
| 17 | + server := mocks.NewMockAPIServer() |
| 18 | + docker, _ = cli.NewClientWithOpts( |
| 19 | + cli.WithHost(server.URL), |
| 20 | + cli.WithHTTPClient(server.Client())) |
| 21 | + client = dockerClient{ |
| 22 | + api: docker, |
| 23 | + pullImages: false, |
| 24 | + } |
| 25 | + }) |
| 26 | + It("should return a client for the api", func() { |
| 27 | + Expect(client).NotTo(BeNil()) |
| 28 | + }) |
| 29 | + Describe("WarnOnHeadPullFailed", func() { |
| 30 | + containerUnknown := *mockContainerWithImageName("unknown.repo/prefix/imagename:latest") |
| 31 | + containerKnown := *mockContainerWithImageName("docker.io/prefix/imagename:latest") |
| 32 | + |
| 33 | + When("warn on head failure is set to \"always\"", func() { |
| 34 | + c := newClientNoAPI(false, false, false, false, false, "always") |
| 35 | + It("should always return true", func() { |
| 36 | + Expect(c.WarnOnHeadPullFailed(containerUnknown)).To(BeTrue()) |
| 37 | + Expect(c.WarnOnHeadPullFailed(containerKnown)).To(BeTrue()) |
| 38 | + }) |
| 39 | + }) |
| 40 | + When("warn on head failure is set to \"auto\"", func() { |
| 41 | + c := newClientNoAPI(false, false, false, false, false, "auto") |
| 42 | + It("should always return true", func() { |
| 43 | + Expect(c.WarnOnHeadPullFailed(containerUnknown)).To(BeFalse()) |
| 44 | + }) |
| 45 | + It("should", func() { |
| 46 | + Expect(c.WarnOnHeadPullFailed(containerKnown)).To(BeTrue()) |
| 47 | + }) |
| 48 | + }) |
| 49 | + When("warn on head failure is set to \"never\"", func() { |
| 50 | + c := newClientNoAPI(false, false, false, false, false, "never") |
| 51 | + It("should never return true", func() { |
| 52 | + Expect(c.WarnOnHeadPullFailed(containerUnknown)).To(BeFalse()) |
| 53 | + Expect(c.WarnOnHeadPullFailed(containerKnown)).To(BeFalse()) |
| 54 | + }) |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + When("listing containers without any filter", func() { |
| 59 | + It("should return all available containers", func() { |
| 60 | + containers, err := client.ListContainers(filters.NoFilter) |
| 61 | + Expect(err).NotTo(HaveOccurred()) |
| 62 | + Expect(len(containers) == 2).To(BeTrue()) |
| 63 | + }) |
| 64 | + }) |
| 65 | + When("listing containers with a filter matching nothing", func() { |
| 66 | + It("should return an empty array", func() { |
| 67 | + filter := filters.FilterByNames([]string{"lollercoaster"}, filters.NoFilter) |
| 68 | + containers, err := client.ListContainers(filter) |
| 69 | + Expect(err).NotTo(HaveOccurred()) |
| 70 | + Expect(len(containers) == 0).To(BeTrue()) |
| 71 | + }) |
| 72 | + }) |
| 73 | + When("listing containers with a watchtower filter", func() { |
| 74 | + It("should return only the watchtower container", func() { |
| 75 | + containers, err := client.ListContainers(filters.WatchtowerContainersFilter) |
| 76 | + Expect(err).NotTo(HaveOccurred()) |
| 77 | + Expect(len(containers) == 1).To(BeTrue()) |
| 78 | + Expect(containers[0].ImageName()).To(Equal("containrrr/watchtower:latest")) |
| 79 | + }) |
| 80 | + }) |
| 81 | + When(`listing containers with the "include stopped" option`, func() { |
| 82 | + It("should return both stopped and running containers", func() { |
| 83 | + client = dockerClient{ |
| 84 | + api: docker, |
| 85 | + pullImages: false, |
| 86 | + includeStopped: true, |
| 87 | + } |
| 88 | + containers, err := client.ListContainers(filters.NoFilter) |
| 89 | + Expect(err).NotTo(HaveOccurred()) |
| 90 | + Expect(len(containers) > 0).To(BeTrue()) |
| 91 | + }) |
| 92 | + }) |
| 93 | + When(`listing containers with the "include restart" option`, func() { |
| 94 | + It("should return both stopped, restarting and running containers", func() { |
| 95 | + client = dockerClient{ |
| 96 | + api: docker, |
| 97 | + pullImages: false, |
| 98 | + includeRestarting: true, |
| 99 | + } |
| 100 | + containers, err := client.ListContainers(filters.NoFilter) |
| 101 | + Expect(err).NotTo(HaveOccurred()) |
| 102 | + RestartingContainerFound := false |
| 103 | + for _, ContainerRunning := range containers { |
| 104 | + if ContainerRunning.containerInfo.State.Restarting { |
| 105 | + RestartingContainerFound = true |
| 106 | + } |
| 107 | + } |
| 108 | + Expect(RestartingContainerFound).To(BeTrue()) |
| 109 | + Expect(RestartingContainerFound).NotTo(BeFalse()) |
| 110 | + }) |
| 111 | + }) |
| 112 | + When(`listing containers without restarting ones`, func() { |
| 113 | + It("should not return restarting containers", func() { |
| 114 | + client = dockerClient{ |
| 115 | + api: docker, |
| 116 | + pullImages: false, |
| 117 | + includeRestarting: false, |
| 118 | + } |
| 119 | + containers, err := client.ListContainers(filters.NoFilter) |
| 120 | + Expect(err).NotTo(HaveOccurred()) |
| 121 | + RestartingContainerFound := false |
| 122 | + for _, ContainerRunning := range containers { |
| 123 | + if ContainerRunning.containerInfo.State.Restarting { |
| 124 | + RestartingContainerFound = true |
| 125 | + } |
| 126 | + } |
| 127 | + Expect(RestartingContainerFound).To(BeFalse()) |
| 128 | + Expect(RestartingContainerFound).NotTo(BeTrue()) |
| 129 | + }) |
| 130 | + }) |
| 131 | + Describe(`ExecuteCommand`, func() { |
| 132 | + When(`logging`, func() { |
| 133 | + It("should include container id field", func() { |
| 134 | + // Capture logrus output in buffer |
| 135 | + logbuf := gbytes.NewBuffer() |
| 136 | + origOut := logrus.StandardLogger().Out |
| 137 | + defer logrus.SetOutput(origOut) |
| 138 | + logrus.SetOutput(logbuf) |
| 139 | + |
| 140 | + _, err := client.ExecuteCommand("ex-cont-id", "exec-cmd", 1) |
| 141 | + Expect(err).NotTo(HaveOccurred()) |
| 142 | + // Note: Since Execute requires opening up a raw TCP stream to the daemon for the output, this will fail |
| 143 | + // when using the mock API server. Regardless of the outcome, the log should include the container ID |
| 144 | + Eventually(logbuf).Should(gbytes.Say(`containerID="?ex-cont-id"?`)) |
| 145 | + }) |
| 146 | + }) |
| 147 | + }) |
| 148 | +}) |
0 commit comments