|
17 | 17 | package compose
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "fmt" |
20 | 21 | "testing"
|
21 | 22 |
|
22 | 23 | "github.com/docker/compose/v2/pkg/api"
|
@@ -66,3 +67,56 @@ func TestStacksMixedStatus(t *testing.T) {
|
66 | 67 | assert.Equal(t, combinedStatus([]string{"running", "running", "running"}), "running(3)")
|
67 | 68 | assert.Equal(t, combinedStatus([]string{"running", "exited", "running"}), "exited(1), running(2)")
|
68 | 69 | }
|
| 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