create_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Copyright 2023 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "testing"
  18. "github.com/compose-spec/compose-go/v2/types"
  19. "github.com/davecgh/go-spew/spew"
  20. "github.com/docker/compose/v2/pkg/api"
  21. "github.com/docker/compose/v2/pkg/mocks"
  22. "github.com/google/go-cmp/cmp"
  23. "github.com/stretchr/testify/require"
  24. "go.uber.org/mock/gomock"
  25. )
  26. func TestRunCreate(t *testing.T) {
  27. ctrl, ctx := gomock.WithContext(context.Background(), t)
  28. backend := mocks.NewMockService(ctrl)
  29. backend.EXPECT().Create(
  30. gomock.Eq(ctx),
  31. pullPolicy(""),
  32. deepEqual(defaultCreateOptions(true)),
  33. )
  34. createOpts := createOptions{}
  35. buildOpts := buildOptions{}
  36. project := sampleProject()
  37. err := runCreate(ctx, nil, backend, createOpts, buildOpts, project, nil)
  38. require.NoError(t, err)
  39. }
  40. func TestRunCreate_Build(t *testing.T) {
  41. ctrl, ctx := gomock.WithContext(context.Background(), t)
  42. backend := mocks.NewMockService(ctrl)
  43. backend.EXPECT().Create(
  44. gomock.Eq(ctx),
  45. pullPolicy("build"),
  46. deepEqual(defaultCreateOptions(true)),
  47. )
  48. createOpts := createOptions{
  49. Build: true,
  50. }
  51. buildOpts := buildOptions{}
  52. project := sampleProject()
  53. err := runCreate(ctx, nil, backend, createOpts, buildOpts, project, nil)
  54. require.NoError(t, err)
  55. }
  56. func TestRunCreate_NoBuild(t *testing.T) {
  57. ctrl, ctx := gomock.WithContext(context.Background(), t)
  58. backend := mocks.NewMockService(ctrl)
  59. backend.EXPECT().Create(
  60. gomock.Eq(ctx),
  61. pullPolicy(""),
  62. deepEqual(defaultCreateOptions(false)),
  63. )
  64. createOpts := createOptions{
  65. noBuild: true,
  66. }
  67. buildOpts := buildOptions{}
  68. project := sampleProject()
  69. err := runCreate(ctx, nil, backend, createOpts, buildOpts, project, nil)
  70. require.NoError(t, err)
  71. }
  72. func sampleProject() *types.Project {
  73. return &types.Project{
  74. Name: "test",
  75. Services: types.Services{
  76. "svc": {
  77. Name: "svc",
  78. Build: &types.BuildConfig{
  79. Context: ".",
  80. },
  81. },
  82. },
  83. }
  84. }
  85. func defaultCreateOptions(includeBuild bool) api.CreateOptions {
  86. var build *api.BuildOptions
  87. if includeBuild {
  88. bo := defaultBuildOptions()
  89. build = &bo
  90. }
  91. return api.CreateOptions{
  92. Build: build,
  93. Services: nil,
  94. RemoveOrphans: false,
  95. IgnoreOrphans: false,
  96. Recreate: "diverged",
  97. RecreateDependencies: "diverged",
  98. Inherit: true,
  99. Timeout: nil,
  100. QuietPull: false,
  101. }
  102. }
  103. func defaultBuildOptions() api.BuildOptions {
  104. return api.BuildOptions{
  105. Args: make(types.MappingWithEquals),
  106. Progress: "auto",
  107. }
  108. }
  109. // deepEqual returns a nice diff on failure vs gomock.Eq when used
  110. // on structs.
  111. func deepEqual(x interface{}) gomock.Matcher {
  112. return gomock.GotFormatterAdapter(
  113. gomock.GotFormatterFunc(func(got interface{}) string {
  114. return cmp.Diff(x, got)
  115. }),
  116. gomock.Eq(x),
  117. )
  118. }
  119. func spewAdapter(m gomock.Matcher) gomock.Matcher {
  120. return gomock.GotFormatterAdapter(
  121. gomock.GotFormatterFunc(func(got interface{}) string {
  122. return spew.Sdump(got)
  123. }),
  124. m,
  125. )
  126. }
  127. type withPullPolicy struct {
  128. policy string
  129. }
  130. func pullPolicy(policy string) gomock.Matcher {
  131. return spewAdapter(withPullPolicy{policy: policy})
  132. }
  133. func (w withPullPolicy) Matches(x interface{}) bool {
  134. proj, ok := x.(*types.Project)
  135. if !ok || proj == nil || len(proj.Services) == 0 {
  136. return false
  137. }
  138. for _, svc := range proj.Services {
  139. if svc.PullPolicy != w.policy {
  140. return false
  141. }
  142. }
  143. return true
  144. }
  145. func (w withPullPolicy) String() string {
  146. return fmt.Sprintf("has pull policy %q for all services", w.policy)
  147. }