|
| 1 | +/* |
| 2 | + Copyright 2020 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package compose |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "gotest.tools/v3/assert" |
| 23 | +) |
| 24 | + |
| 25 | +func TestOperationTypeString(t *testing.T) { |
| 26 | + tests := []struct { |
| 27 | + op OperationType |
| 28 | + want string |
| 29 | + }{ |
| 30 | + {OpCreateNetwork, "CreateNetwork"}, |
| 31 | + {OpRemoveNetwork, "RemoveNetwork"}, |
| 32 | + {OpDisconnectNetwork, "DisconnectNetwork"}, |
| 33 | + {OpConnectNetwork, "ConnectNetwork"}, |
| 34 | + {OpCreateVolume, "CreateVolume"}, |
| 35 | + {OpRemoveVolume, "RemoveVolume"}, |
| 36 | + {OpCreateContainer, "CreateContainer"}, |
| 37 | + {OpStartContainer, "StartContainer"}, |
| 38 | + {OpStopContainer, "StopContainer"}, |
| 39 | + {OpRemoveContainer, "RemoveContainer"}, |
| 40 | + {OpRenameContainer, "RenameContainer"}, |
| 41 | + {OperationType(999), "Unknown(999)"}, |
| 42 | + } |
| 43 | + for _, tt := range tests { |
| 44 | + assert.Equal(t, tt.op.String(), tt.want) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestPlanStringEmpty(t *testing.T) { |
| 49 | + p := &Plan{} |
| 50 | + assert.Equal(t, p.String(), "") |
| 51 | + assert.Assert(t, p.IsEmpty()) |
| 52 | +} |
| 53 | + |
| 54 | +func TestPlanStringNoDeps(t *testing.T) { |
| 55 | + p := &Plan{} |
| 56 | + p.addNode(Operation{ |
| 57 | + Type: OpCreateNetwork, |
| 58 | + ResourceID: "network:default", |
| 59 | + Cause: "not found", |
| 60 | + }, "") |
| 61 | + p.addNode(Operation{ |
| 62 | + Type: OpCreateVolume, |
| 63 | + ResourceID: "volume:data", |
| 64 | + Cause: "not found", |
| 65 | + }, "") |
| 66 | + |
| 67 | + expected := "[] -> #1 network:default, CreateNetwork, not found\n" + |
| 68 | + "[] -> #2 volume:data, CreateVolume, not found\n" |
| 69 | + assert.Equal(t, p.String(), expected) |
| 70 | + assert.Assert(t, !p.IsEmpty()) |
| 71 | +} |
| 72 | + |
| 73 | +func TestPlanStringWithDeps(t *testing.T) { |
| 74 | + p := &Plan{} |
| 75 | + nw := p.addNode(Operation{ |
| 76 | + Type: OpCreateNetwork, |
| 77 | + ResourceID: "network:default", |
| 78 | + Cause: "not found", |
| 79 | + }, "") |
| 80 | + vol := p.addNode(Operation{ |
| 81 | + Type: OpCreateVolume, |
| 82 | + ResourceID: "volume:data", |
| 83 | + Cause: "not found", |
| 84 | + }, "") |
| 85 | + p.addNode(Operation{ |
| 86 | + Type: OpCreateContainer, |
| 87 | + ResourceID: "service:db:1", |
| 88 | + Cause: "no existing container", |
| 89 | + }, "", nw, vol) |
| 90 | + |
| 91 | + expected := "[] -> #1 network:default, CreateNetwork, not found\n" + |
| 92 | + "[] -> #2 volume:data, CreateVolume, not found\n" + |
| 93 | + "[1,2] -> #3 service:db:1, CreateContainer, no existing container\n" |
| 94 | + assert.Equal(t, p.String(), expected) |
| 95 | +} |
| 96 | + |
| 97 | +func TestPlanStringWithGroup(t *testing.T) { |
| 98 | + p := &Plan{} |
| 99 | + create := p.addNode(Operation{ |
| 100 | + Type: OpCreateContainer, |
| 101 | + ResourceID: "service:web:1", |
| 102 | + Cause: "config hash changed (tmpName)", |
| 103 | + }, "recreate:web:1") |
| 104 | + stop := p.addNode(Operation{ |
| 105 | + Type: OpStopContainer, |
| 106 | + ResourceID: "service:web:1", |
| 107 | + Cause: "replaced by #1", |
| 108 | + }, "recreate:web:1", create) |
| 109 | + remove := p.addNode(Operation{ |
| 110 | + Type: OpRemoveContainer, |
| 111 | + ResourceID: "service:web:1", |
| 112 | + Cause: "replaced by #1", |
| 113 | + }, "recreate:web:1", stop) |
| 114 | + p.addNode(Operation{ |
| 115 | + Type: OpRenameContainer, |
| 116 | + ResourceID: "service:web:1", |
| 117 | + Cause: "finalize recreate", |
| 118 | + }, "recreate:web:1", remove) |
| 119 | + |
| 120 | + expected := "[] -> #1 service:web:1, CreateContainer, config hash changed (tmpName) [recreate:web:1]\n" + |
| 121 | + "[1] -> #2 service:web:1, StopContainer, replaced by #1 [recreate:web:1]\n" + |
| 122 | + "[2] -> #3 service:web:1, RemoveContainer, replaced by #1 [recreate:web:1]\n" + |
| 123 | + "[3] -> #4 service:web:1, RenameContainer, finalize recreate [recreate:web:1]\n" |
| 124 | + assert.Equal(t, p.String(), expected) |
| 125 | +} |
| 126 | + |
| 127 | +func TestPlanAddNodeAutoIncrements(t *testing.T) { |
| 128 | + p := &Plan{} |
| 129 | + n1 := p.addNode(Operation{Type: OpCreateNetwork, ResourceID: "a", Cause: "x"}, "") |
| 130 | + n2 := p.addNode(Operation{Type: OpCreateVolume, ResourceID: "b", Cause: "y"}, "") |
| 131 | + n3 := p.addNode(Operation{Type: OpCreateContainer, ResourceID: "c", Cause: "z"}, "", n1, n2) |
| 132 | + |
| 133 | + assert.Equal(t, n1.ID, 1) |
| 134 | + assert.Equal(t, n2.ID, 2) |
| 135 | + assert.Equal(t, n3.ID, 3) |
| 136 | + assert.Equal(t, len(n3.DependsOn), 2) |
| 137 | + assert.Equal(t, n3.DependsOn[0].ID, 1) |
| 138 | + assert.Equal(t, n3.DependsOn[1].ID, 2) |
| 139 | +} |
0 commit comments