publish_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Copyright 2020 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. "errors"
  16. "os"
  17. "slices"
  18. "testing"
  19. "github.com/compose-spec/compose-go/v2/loader"
  20. "github.com/compose-spec/compose-go/v2/types"
  21. "github.com/google/go-cmp/cmp"
  22. v1 "github.com/opencontainers/image-spec/specs-go/v1"
  23. "gotest.tools/v3/assert"
  24. "github.com/docker/compose/v5/internal"
  25. "github.com/docker/compose/v5/pkg/api"
  26. )
  27. func Test_createLayers(t *testing.T) {
  28. project, err := loader.LoadWithContext(t.Context(), types.ConfigDetails{
  29. WorkingDir: "testdata/publish/",
  30. Environment: types.Mapping{},
  31. ConfigFiles: []types.ConfigFile{
  32. {
  33. Filename: "testdata/publish/compose.yaml",
  34. },
  35. },
  36. })
  37. assert.NilError(t, err)
  38. project.ComposeFiles = []string{"testdata/publish/compose.yaml"}
  39. service := &composeService{}
  40. layers, err := service.createLayers(t.Context(), project, api.PublishOptions{
  41. WithEnvironment: true,
  42. })
  43. assert.NilError(t, err)
  44. published := string(layers[0].Data)
  45. assert.Equal(t, published, `name: test
  46. services:
  47. test:
  48. extends:
  49. file: f8f9ede3d201ec37d5a5e3a77bbadab79af26035e53135e19571f50d541d390c.yaml
  50. service: foo
  51. string:
  52. image: test
  53. env_file: 5efca9cdbac9f5394c6c2e2094b1b42661f988f57fcab165a0bf72b205451af3.env
  54. list:
  55. image: test
  56. env_file:
  57. - 5efca9cdbac9f5394c6c2e2094b1b42661f988f57fcab165a0bf72b205451af3.env
  58. mapping:
  59. image: test
  60. env_file:
  61. - path: 5efca9cdbac9f5394c6c2e2094b1b42661f988f57fcab165a0bf72b205451af3.env
  62. `)
  63. expectedLayers := []v1.Descriptor{
  64. {
  65. MediaType: "application/vnd.docker.compose.file+yaml",
  66. Annotations: map[string]string{
  67. "com.docker.compose.file": "compose.yaml",
  68. "com.docker.compose.version": internal.Version,
  69. },
  70. },
  71. {
  72. MediaType: "application/vnd.docker.compose.file+yaml",
  73. Annotations: map[string]string{
  74. "com.docker.compose.extends": "true",
  75. "com.docker.compose.file": "f8f9ede3d201ec37d5a5e3a77bbadab79af26035e53135e19571f50d541d390c",
  76. "com.docker.compose.version": internal.Version,
  77. },
  78. },
  79. {
  80. MediaType: "application/vnd.docker.compose.envfile",
  81. Annotations: map[string]string{
  82. "com.docker.compose.envfile": "5efca9cdbac9f5394c6c2e2094b1b42661f988f57fcab165a0bf72b205451af3",
  83. "com.docker.compose.version": internal.Version,
  84. },
  85. },
  86. }
  87. assert.DeepEqual(t, expectedLayers, layers, cmp.FilterPath(func(path cmp.Path) bool {
  88. return !slices.Contains([]string{".Data", ".Digest", ".Size"}, path.String())
  89. }, cmp.Ignore()))
  90. }
  91. func Test_preChecks_sensitive_data_detected_decline(t *testing.T) {
  92. dir := t.TempDir()
  93. envPath := dir + "/secrets.env"
  94. secretData := `AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"`
  95. err := os.WriteFile(envPath, []byte(secretData), 0o600)
  96. assert.NilError(t, err)
  97. project := &types.Project{
  98. Services: types.Services{
  99. "web": {
  100. Name: "web",
  101. Image: "nginx",
  102. EnvFiles: []types.EnvFile{
  103. {Path: envPath, Required: true},
  104. },
  105. },
  106. },
  107. }
  108. declined := func(message string, defaultValue bool) (bool, error) {
  109. return false, nil
  110. }
  111. svc := &composeService{
  112. prompt: declined,
  113. }
  114. accept, err := svc.preChecks(t.Context(), project, api.PublishOptions{})
  115. assert.NilError(t, err)
  116. assert.Equal(t, accept, false)
  117. }
  118. func Test_publish_decline_returns_ErrCanceled(t *testing.T) {
  119. project := &types.Project{
  120. Services: types.Services{
  121. "web": {
  122. Name: "web",
  123. Image: "nginx",
  124. Volumes: []types.ServiceVolumeConfig{
  125. {
  126. Type: types.VolumeTypeBind,
  127. Source: "/host/path",
  128. Target: "/container/path",
  129. },
  130. },
  131. },
  132. },
  133. }
  134. declined := func(message string, defaultValue bool) (bool, error) {
  135. return false, nil
  136. }
  137. svc := &composeService{
  138. prompt: declined,
  139. events: &ignore{},
  140. }
  141. err := svc.publish(t.Context(), project, "docker.io/myorg/myapp:latest", api.PublishOptions{})
  142. assert.Assert(t, errors.Is(err, api.ErrCanceled),
  143. "expected api.ErrCanceled when user declines, got: %v", err)
  144. }