publish_test.go 3.0 KB

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