Skip to content

Commit

Permalink
test: add unit test for implicit restart
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel committed Oct 24, 2021
1 parent eb831df commit f921a70
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
6 changes: 3 additions & 3 deletions internal/actions/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
49 changes: 49 additions & 0 deletions internal/actions/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions scripts/dependency-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f921a70

Please sign in to comment.