Skip to content

Commit cb45c6f

Browse files
KoditkarVedantndeloof
authored andcommitted
Add unit tests for combinedConfigFiles logic ✅
Signed-off-by: Vedant Koditkar <[email protected]>
1 parent 90ca373 commit cb45c6f

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

pkg/compose/ls_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package compose
1818

1919
import (
20+
"fmt"
2021
"testing"
2122

2223
"github.com/docker/compose/v2/pkg/api"
@@ -66,3 +67,56 @@ func TestStacksMixedStatus(t *testing.T) {
6667
assert.Equal(t, combinedStatus([]string{"running", "running", "running"}), "running(3)")
6768
assert.Equal(t, combinedStatus([]string{"running", "exited", "running"}), "exited(1), running(2)")
6869
}
70+
71+
func TestCombinedConfigFiles(t *testing.T) {
72+
containersByLabel := map[string][]moby.Container{
73+
"project1": {
74+
{
75+
ID: "service1",
76+
State: "running",
77+
Labels: map[string]string{api.ProjectLabel: "project1", api.ConfigFilesLabel: "/home/docker-compose.yaml"},
78+
},
79+
{
80+
ID: "service2",
81+
State: "running",
82+
Labels: map[string]string{api.ProjectLabel: "project1", api.ConfigFilesLabel: "/home/docker-compose.yaml"},
83+
},
84+
},
85+
"project2": {
86+
{
87+
ID: "service3",
88+
State: "running",
89+
Labels: map[string]string{api.ProjectLabel: "project2", api.ConfigFilesLabel: "/home/project2-docker-compose.yaml"},
90+
},
91+
},
92+
"project3": {
93+
{
94+
ID: "service4",
95+
State: "running",
96+
Labels: map[string]string{api.ProjectLabel: "project3"},
97+
},
98+
},
99+
}
100+
101+
testData := map[string]struct {
102+
ConfigFiles string
103+
Error error
104+
}{
105+
"project1": {ConfigFiles: "/home/docker-compose.yaml", Error: nil},
106+
"project2": {ConfigFiles: "/home/project2-docker-compose.yaml", Error: nil},
107+
"project3": {ConfigFiles: "", Error: fmt.Errorf("No label %q set on container %q of compose project", api.ConfigFilesLabel, "service4")},
108+
}
109+
110+
for project, containers := range containersByLabel {
111+
configFiles, err := combinedConfigFiles(containers)
112+
113+
expected := testData[project]
114+
115+
if expected.Error != nil {
116+
assert.Equal(t, err.Error(), expected.Error.Error())
117+
} else {
118+
assert.Equal(t, err, expected.Error)
119+
}
120+
assert.Equal(t, configFiles, expected.ConfigFiles)
121+
}
122+
}

0 commit comments

Comments
 (0)