options_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "testing"
  16. "github.com/compose-spec/compose-go/types"
  17. "github.com/stretchr/testify/require"
  18. )
  19. func TestApplyPlatforms_InferFromRuntime(t *testing.T) {
  20. makeProject := func() *types.Project {
  21. return &types.Project{
  22. Services: types.Services{
  23. {
  24. Name: "test",
  25. Image: "foo",
  26. Build: &types.BuildConfig{
  27. Context: ".",
  28. Platforms: []string{
  29. "linux/amd64",
  30. "linux/arm64",
  31. "alice/32",
  32. },
  33. },
  34. Platform: "alice/32",
  35. },
  36. },
  37. }
  38. }
  39. t.Run("SinglePlatform", func(t *testing.T) {
  40. project := makeProject()
  41. require.NoError(t, applyPlatforms(project, true))
  42. require.EqualValues(t, []string{"alice/32"}, project.Services[0].Build.Platforms)
  43. })
  44. t.Run("MultiPlatform", func(t *testing.T) {
  45. project := makeProject()
  46. require.NoError(t, applyPlatforms(project, false))
  47. require.EqualValues(t, []string{"linux/amd64", "linux/arm64", "alice/32"},
  48. project.Services[0].Build.Platforms)
  49. })
  50. }
  51. func TestApplyPlatforms_DockerDefaultPlatform(t *testing.T) {
  52. makeProject := func() *types.Project {
  53. return &types.Project{
  54. Environment: map[string]string{
  55. "DOCKER_DEFAULT_PLATFORM": "linux/amd64",
  56. },
  57. Services: types.Services{
  58. {
  59. Name: "test",
  60. Image: "foo",
  61. Build: &types.BuildConfig{
  62. Context: ".",
  63. Platforms: []string{
  64. "linux/amd64",
  65. "linux/arm64",
  66. },
  67. },
  68. },
  69. },
  70. }
  71. }
  72. t.Run("SinglePlatform", func(t *testing.T) {
  73. project := makeProject()
  74. require.NoError(t, applyPlatforms(project, true))
  75. require.EqualValues(t, []string{"linux/amd64"}, project.Services[0].Build.Platforms)
  76. })
  77. t.Run("MultiPlatform", func(t *testing.T) {
  78. project := makeProject()
  79. require.NoError(t, applyPlatforms(project, false))
  80. require.EqualValues(t, []string{"linux/amd64", "linux/arm64"},
  81. project.Services[0].Build.Platforms)
  82. })
  83. }
  84. func TestApplyPlatforms_UnsupportedPlatform(t *testing.T) {
  85. makeProject := func() *types.Project {
  86. return &types.Project{
  87. Environment: map[string]string{
  88. "DOCKER_DEFAULT_PLATFORM": "commodore/64",
  89. },
  90. Services: types.Services{
  91. {
  92. Name: "test",
  93. Image: "foo",
  94. Build: &types.BuildConfig{
  95. Context: ".",
  96. Platforms: []string{
  97. "linux/amd64",
  98. "linux/arm64",
  99. },
  100. },
  101. },
  102. },
  103. }
  104. }
  105. t.Run("SinglePlatform", func(t *testing.T) {
  106. project := makeProject()
  107. require.EqualError(t, applyPlatforms(project, true),
  108. `service "test" build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: commodore/64`)
  109. })
  110. t.Run("MultiPlatform", func(t *testing.T) {
  111. project := makeProject()
  112. require.EqualError(t, applyPlatforms(project, false),
  113. `service "test" build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: commodore/64`)
  114. })
  115. }