publish_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "slices"
  16. "testing"
  17. "github.com/compose-spec/compose-go/v2/loader"
  18. "github.com/compose-spec/compose-go/v2/types"
  19. "github.com/google/go-cmp/cmp"
  20. v1 "github.com/opencontainers/image-spec/specs-go/v1"
  21. "gotest.tools/v3/assert"
  22. "github.com/docker/compose/v5/internal"
  23. "github.com/docker/compose/v5/pkg/api"
  24. )
  25. func Test_createLayers(t *testing.T) {
  26. project, err := loader.LoadWithContext(t.Context(), types.ConfigDetails{
  27. WorkingDir: "testdata/publish/",
  28. Environment: types.Mapping{},
  29. ConfigFiles: []types.ConfigFile{
  30. {
  31. Filename: "testdata/publish/compose.yaml",
  32. },
  33. },
  34. })
  35. assert.NilError(t, err)
  36. project.ComposeFiles = []string{"testdata/publish/compose.yaml"}
  37. service := &composeService{}
  38. layers, err := service.createLayers(t.Context(), project, api.PublishOptions{
  39. WithEnvironment: true,
  40. })
  41. assert.NilError(t, err)
  42. published := string(layers[0].Data)
  43. assert.Equal(t, published, `name: test
  44. services:
  45. test:
  46. extends:
  47. file: f8f9ede3d201ec37d5a5e3a77bbadab79af26035e53135e19571f50d541d390c.yaml
  48. service: foo
  49. string:
  50. image: test
  51. env_file: 5efca9cdbac9f5394c6c2e2094b1b42661f988f57fcab165a0bf72b205451af3.env
  52. list:
  53. image: test
  54. env_file:
  55. - 5efca9cdbac9f5394c6c2e2094b1b42661f988f57fcab165a0bf72b205451af3.env
  56. mapping:
  57. image: test
  58. env_file:
  59. - path: 5efca9cdbac9f5394c6c2e2094b1b42661f988f57fcab165a0bf72b205451af3.env
  60. `)
  61. expectedLayers := []v1.Descriptor{
  62. {
  63. MediaType: "application/vnd.docker.compose.file+yaml",
  64. Annotations: map[string]string{
  65. "com.docker.compose.file": "compose.yaml",
  66. "com.docker.compose.version": internal.Version,
  67. },
  68. },
  69. {
  70. MediaType: "application/vnd.docker.compose.file+yaml",
  71. Annotations: map[string]string{
  72. "com.docker.compose.extends": "true",
  73. "com.docker.compose.file": "f8f9ede3d201ec37d5a5e3a77bbadab79af26035e53135e19571f50d541d390c",
  74. "com.docker.compose.version": internal.Version,
  75. },
  76. },
  77. {
  78. MediaType: "application/vnd.docker.compose.envfile",
  79. Annotations: map[string]string{
  80. "com.docker.compose.envfile": "5efca9cdbac9f5394c6c2e2094b1b42661f988f57fcab165a0bf72b205451af3",
  81. "com.docker.compose.version": internal.Version,
  82. },
  83. },
  84. }
  85. assert.DeepEqual(t, expectedLayers, layers, cmp.FilterPath(func(path cmp.Path) bool {
  86. return !slices.Contains([]string{".Data", ".Digest", ".Size"}, path.String())
  87. }, cmp.Ignore()))
  88. }