From f921a70fa002df8350ca5a5b1a455af7df074483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Sun, 24 Oct 2021 15:16:21 +0200 Subject: [PATCH] test: add unit test for implicit restart --- internal/actions/update.go | 6 ++-- internal/actions/update_test.go | 49 +++++++++++++++++++++++++++++++++ scripts/dependency-test.sh | 4 +-- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/internal/actions/update.go b/internal/actions/update.go index f807acd9d..e0f70653c 100644 --- a/internal/actions/update.go +++ b/internal/actions/update.go @@ -69,7 +69,7 @@ func Update(client container.Client, params types.UpdateParams) (types.Report, e return nil, err } - updateImplicitRestart(containers) + UpdateImplicitRestart(containers) var containersToUpdate []container.Container if !params.MonitorOnly { @@ -217,9 +217,9 @@ func restartStaleContainer(container container.Container, client container.Clien return nil } -// updateImplicitRestart iterates through the passed containers, setting the +// UpdateImplicitRestart iterates through the passed containers, setting the // `LinkedToRestarting` flag if any of it's linked containers are marked for restart -func updateImplicitRestart(containers []container.Container) { +func UpdateImplicitRestart(containers []container.Container) { for ci, c := range containers { if c.ToRestart() { diff --git a/internal/actions/update_test.go b/internal/actions/update_test.go index 875025366..0e283ceb1 100644 --- a/internal/actions/update_test.go +++ b/internal/actions/update_test.go @@ -30,6 +30,7 @@ var _ = Describe("the update action", func() { BeforeEach(func() { pullImages := false removeVolumes := false + //goland:noinspection GoBoolExpressions client = CreateMockClient( &TestData{ NameOfContainerToKeep: "test-container-02", @@ -271,6 +272,54 @@ var _ = Describe("the update action", func() { }) }) + When("container is linked to restarting containers", func() { + It("should be marked for restart", func() { + + provider := CreateMockContainerWithConfig( + "test-container-provider", + "/test-container-provider", + "fake-image2:latest", + true, + false, + time.Now(), + &dockerContainer.Config{ + Labels: map[string]string{}, + ExposedPorts: map[nat.Port]struct{}{}, + }) + + provider.Stale = true + + consumer := CreateMockContainerWithConfig( + "test-container-consumer", + "/test-container-consumer", + "fake-image3:latest", + true, + false, + time.Now(), + &dockerContainer.Config{ + Labels: map[string]string{ + "com.centurylinklabs.watchtower.depends-on": "test-container-provider", + }, + ExposedPorts: map[nat.Port]struct{}{}, + }) + + containers := []container.Container{ + provider, + consumer, + } + + Expect(provider.ToRestart()).To(BeTrue()) + Expect(consumer.ToRestart()).To(BeFalse()) + + actions.UpdateImplicitRestart(containers) + + Expect(containers[0].ToRestart()).To(BeTrue()) + Expect(containers[1].ToRestart()).To(BeTrue()) + + }) + + }) + When("container is not running", func() { BeforeEach(func() { client = CreateMockClient( diff --git a/scripts/dependency-test.sh b/scripts/dependency-test.sh index 2a040adc9..49c672bcd 100755 --- a/scripts/dependency-test.sh +++ b/scripts/dependency-test.sh @@ -4,8 +4,8 @@ # Note that this test does not verify the results in any way set -e -SCRIPT_ROOT=$(dirname $(readlink -m $( type -p ${0} ))) -source $SCRIPT_ROOT/docker-util.sh +SCRIPT_ROOT=$(dirname "$(readlink -m "$(type -p "$0")")") +source "$SCRIPT_ROOT/docker-util.sh" DepArgs="" if [ -z "$1" ] || [ "$1" == "depends-on" ]; then