|
| 1 | +/* |
| 2 | + Copyright 2023 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 | + "context" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/compose-spec/compose-go/types" |
| 25 | + "github.com/davecgh/go-spew/spew" |
| 26 | + "github.com/docker/compose/v2/pkg/api" |
| 27 | + "github.com/docker/compose/v2/pkg/mocks" |
| 28 | + "github.com/golang/mock/gomock" |
| 29 | + "github.com/google/go-cmp/cmp" |
| 30 | + "github.com/stretchr/testify/require" |
| 31 | +) |
| 32 | + |
| 33 | +func TestRunCreate(t *testing.T) { |
| 34 | + ctrl, ctx := gomock.WithContext(context.Background(), t) |
| 35 | + backend := mocks.NewMockService(ctrl) |
| 36 | + backend.EXPECT().Create( |
| 37 | + gomock.Eq(ctx), |
| 38 | + pullPolicy(""), |
| 39 | + deepEqual(defaultCreateOptions(true)), |
| 40 | + ) |
| 41 | + |
| 42 | + createOpts := createOptions{} |
| 43 | + buildOpts := buildOptions{} |
| 44 | + project := sampleProject() |
| 45 | + err := runCreate(ctx, nil, backend, createOpts, buildOpts, project, nil) |
| 46 | + require.NoError(t, err) |
| 47 | +} |
| 48 | + |
| 49 | +func TestRunCreate_Build(t *testing.T) { |
| 50 | + ctrl, ctx := gomock.WithContext(context.Background(), t) |
| 51 | + backend := mocks.NewMockService(ctrl) |
| 52 | + backend.EXPECT().Create( |
| 53 | + gomock.Eq(ctx), |
| 54 | + pullPolicy("build"), |
| 55 | + deepEqual(defaultCreateOptions(true)), |
| 56 | + ) |
| 57 | + |
| 58 | + createOpts := createOptions{ |
| 59 | + Build: true, |
| 60 | + } |
| 61 | + buildOpts := buildOptions{} |
| 62 | + project := sampleProject() |
| 63 | + err := runCreate(ctx, nil, backend, createOpts, buildOpts, project, nil) |
| 64 | + require.NoError(t, err) |
| 65 | +} |
| 66 | + |
| 67 | +func TestRunCreate_NoBuild(t *testing.T) { |
| 68 | + ctrl, ctx := gomock.WithContext(context.Background(), t) |
| 69 | + backend := mocks.NewMockService(ctrl) |
| 70 | + backend.EXPECT().Create( |
| 71 | + gomock.Eq(ctx), |
| 72 | + pullPolicy(""), |
| 73 | + deepEqual(defaultCreateOptions(false)), |
| 74 | + ) |
| 75 | + |
| 76 | + createOpts := createOptions{ |
| 77 | + noBuild: true, |
| 78 | + } |
| 79 | + buildOpts := buildOptions{} |
| 80 | + project := sampleProject() |
| 81 | + err := runCreate(ctx, nil, backend, createOpts, buildOpts, project, nil) |
| 82 | + require.NoError(t, err) |
| 83 | +} |
| 84 | + |
| 85 | +func sampleProject() *types.Project { |
| 86 | + return &types.Project{ |
| 87 | + Name: "test", |
| 88 | + Services: types.Services{ |
| 89 | + { |
| 90 | + Name: "svc", |
| 91 | + Build: &types.BuildConfig{ |
| 92 | + Context: ".", |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func defaultCreateOptions(includeBuild bool) api.CreateOptions { |
| 100 | + var build *api.BuildOptions |
| 101 | + if includeBuild { |
| 102 | + bo := defaultBuildOptions() |
| 103 | + build = &bo |
| 104 | + } |
| 105 | + return api.CreateOptions{ |
| 106 | + Build: build, |
| 107 | + Services: nil, |
| 108 | + RemoveOrphans: false, |
| 109 | + IgnoreOrphans: false, |
| 110 | + Recreate: "diverged", |
| 111 | + RecreateDependencies: "diverged", |
| 112 | + Inherit: true, |
| 113 | + Timeout: nil, |
| 114 | + QuietPull: false, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func defaultBuildOptions() api.BuildOptions { |
| 119 | + return api.BuildOptions{ |
| 120 | + Args: make(types.MappingWithEquals), |
| 121 | + Progress: "auto", |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// deepEqual returns a nice diff on failure vs gomock.Eq when used |
| 126 | +// on structs. |
| 127 | +func deepEqual(x interface{}) gomock.Matcher { |
| 128 | + return gomock.GotFormatterAdapter( |
| 129 | + gomock.GotFormatterFunc(func(got interface{}) string { |
| 130 | + return cmp.Diff(x, got) |
| 131 | + }), |
| 132 | + gomock.Eq(x), |
| 133 | + ) |
| 134 | +} |
| 135 | + |
| 136 | +func spewAdapter(m gomock.Matcher) gomock.Matcher { |
| 137 | + return gomock.GotFormatterAdapter( |
| 138 | + gomock.GotFormatterFunc(func(got interface{}) string { |
| 139 | + return spew.Sdump(got) |
| 140 | + }), |
| 141 | + m, |
| 142 | + ) |
| 143 | +} |
| 144 | + |
| 145 | +type withPullPolicy struct { |
| 146 | + policy string |
| 147 | +} |
| 148 | + |
| 149 | +func pullPolicy(policy string) gomock.Matcher { |
| 150 | + return spewAdapter(withPullPolicy{policy: policy}) |
| 151 | +} |
| 152 | + |
| 153 | +func (w withPullPolicy) Matches(x interface{}) bool { |
| 154 | + proj, ok := x.(*types.Project) |
| 155 | + if !ok || proj == nil || len(proj.Services) == 0 { |
| 156 | + return false |
| 157 | + } |
| 158 | + |
| 159 | + for _, svc := range proj.Services { |
| 160 | + if svc.PullPolicy != w.policy { |
| 161 | + return false |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return true |
| 166 | +} |
| 167 | + |
| 168 | +func (w withPullPolicy) String() string { |
| 169 | + return fmt.Sprintf("has pull policy %q for all services", w.policy) |
| 170 | +} |
0 commit comments