makefile_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. Copyright © The ESO 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. https://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 e2e
  14. import (
  15. "os"
  16. "os/exec"
  17. "strings"
  18. "testing"
  19. )
  20. const (
  21. testVersionArg = "VERSION=test-version"
  22. kubernetesBuildTarget = "docker.build.provider.kubernetes"
  23. kubernetesProviderImage = "ghcr.io/external-secrets/provider-kubernetes:test-version"
  24. helmDependencyBuildCmd = "helm dependency build ../deploy/charts/external-secrets"
  25. controllerImageLoadCount = `kind load docker-image --name="external-secrets" ghcr.io/external-secrets/external-secrets:test-version`
  26. controllerImageBuildCmd = "docker.build.controller.e2e"
  27. dockerCleanupCmd = "docker system prune --all --force --volumes"
  28. )
  29. func TestClassicMakeTargetBuildsOnlyControllerImageOnce(t *testing.T) {
  30. t.Parallel()
  31. dryRun := runMakeDryRun(t, "test", testVersionArg, `TEST_SUITES=provider`, `GINKGO_LABELS=kubernetes && !v2`)
  32. if !strings.Contains(dryRun, controllerImageBuildCmd) {
  33. t.Fatalf("expected classic test dry-run to build the controller image via docker.build.controller.e2e, output:\n%s", dryRun)
  34. }
  35. if strings.Contains(dryRun, kubernetesBuildTarget) {
  36. t.Fatalf("expected classic test dry-run to omit kubernetes provider image builds, output:\n%s", dryRun)
  37. }
  38. if strings.Contains(dryRun, "docker.build.provider.aws") {
  39. t.Fatalf("expected classic test dry-run to omit aws provider image builds, output:\n%s", dryRun)
  40. }
  41. if strings.Contains(dryRun, "docker.build.provider.fake") {
  42. t.Fatalf("expected classic test dry-run to omit fake provider image builds, output:\n%s", dryRun)
  43. }
  44. if count := strings.Count(dryRun, controllerImageLoadCount); count != 1 {
  45. t.Fatalf("expected classic test dry-run to load the controller image once, got %d occurrences, output:\n%s", count, dryRun)
  46. }
  47. if strings.Contains(dryRun, kubernetesProviderImage) {
  48. t.Fatalf("expected classic test dry-run to avoid loading the kubernetes provider image, output:\n%s", dryRun)
  49. }
  50. if !strings.Contains(dryRun, helmDependencyBuildCmd) {
  51. t.Fatalf("expected classic test dry-run to ensure helm dependencies before copying the chart, output:\n%s", dryRun)
  52. }
  53. }
  54. func TestV2MakeTargetCanSkipKubernetesProviderBuild(t *testing.T) {
  55. t.Parallel()
  56. defaultDryRun := runMakeDryRun(t, "test.v2", testVersionArg)
  57. if !strings.Contains(defaultDryRun, kubernetesBuildTarget) {
  58. t.Fatalf("expected default test.v2 dry-run to build the kubernetes provider image, output:\n%s", defaultDryRun)
  59. }
  60. if count := strings.Count(defaultDryRun, controllerImageBuildCmd); count != 1 {
  61. t.Fatalf("expected default test.v2 dry-run to build the controller image once, got %d occurrences, output:\n%s", count, defaultDryRun)
  62. }
  63. if !strings.Contains(defaultDryRun, "docker.build.provider.aws") {
  64. t.Fatalf("expected default test.v2 dry-run to build the aws provider image, output:\n%s", defaultDryRun)
  65. }
  66. if !strings.Contains(defaultDryRun, "docker.build.provider.fake") {
  67. t.Fatalf("expected default test.v2 dry-run to build the fake provider image, output:\n%s", defaultDryRun)
  68. }
  69. if strings.Contains(defaultDryRun, "docker.build.provider.gcp") {
  70. t.Fatalf("expected default test.v2 dry-run to omit nonexistent gcp provider builds, output:\n%s", defaultDryRun)
  71. }
  72. if count := strings.Count(defaultDryRun, controllerImageLoadCount); count != 1 {
  73. t.Fatalf("expected default test.v2 dry-run to load the controller image once, got %d occurrences, output:\n%s", count, defaultDryRun)
  74. }
  75. if !strings.Contains(defaultDryRun, kubernetesProviderImage) {
  76. t.Fatalf("expected default test.v2 dry-run to still load the kubernetes provider image, output:\n%s", defaultDryRun)
  77. }
  78. if !strings.Contains(defaultDryRun, "ghcr.io/external-secrets/provider-aws:test-version") {
  79. t.Fatalf("expected default test.v2 dry-run to load the aws provider image, output:\n%s", defaultDryRun)
  80. }
  81. if !strings.Contains(defaultDryRun, "ghcr.io/external-secrets/provider-fake:test-version") {
  82. t.Fatalf("expected default test.v2 dry-run to load the fake provider image, output:\n%s", defaultDryRun)
  83. }
  84. if strings.Contains(defaultDryRun, "ghcr.io/external-secrets/provider-gcp:test-version") {
  85. t.Fatalf("expected default test.v2 dry-run to omit nonexistent gcp provider image loads, output:\n%s", defaultDryRun)
  86. }
  87. if !strings.Contains(defaultDryRun, helmDependencyBuildCmd) {
  88. t.Fatalf("expected default test.v2 dry-run to ensure helm dependencies before copying the chart, output:\n%s", defaultDryRun)
  89. }
  90. if !strings.Contains(defaultDryRun, `TEST_SUITES="provider"`) {
  91. t.Fatalf("expected default test.v2 dry-run to run the provider suite, output:\n%s", defaultDryRun)
  92. }
  93. if strings.Contains(defaultDryRun, dockerCleanupCmd) {
  94. t.Fatalf("expected default test.v2 dry-run to avoid CI-only docker cleanup, output:\n%s", defaultDryRun)
  95. }
  96. skippedDryRun := runMakeDryRun(t, "test.v2", testVersionArg, "SKIP_PROVIDER_KUBERNETES_BUILD=true")
  97. if strings.Contains(skippedDryRun, kubernetesBuildTarget) {
  98. t.Fatalf("expected skipped test.v2 dry-run to omit the kubernetes provider build, output:\n%s", skippedDryRun)
  99. }
  100. if count := strings.Count(skippedDryRun, controllerImageBuildCmd); count != 1 {
  101. t.Fatalf("expected skipped test.v2 dry-run to build the controller image once, got %d occurrences, output:\n%s", count, skippedDryRun)
  102. }
  103. if !strings.Contains(skippedDryRun, "docker.build.provider.fake") {
  104. t.Fatalf("expected skipped test.v2 dry-run to still build the fake provider image, output:\n%s", skippedDryRun)
  105. }
  106. if strings.Contains(skippedDryRun, "docker.build.provider.gcp") {
  107. t.Fatalf("expected skipped test.v2 dry-run to omit nonexistent gcp provider builds, output:\n%s", skippedDryRun)
  108. }
  109. if count := strings.Count(skippedDryRun, controllerImageLoadCount); count != 1 {
  110. t.Fatalf("expected skipped test.v2 dry-run to load the controller image once, got %d occurrences, output:\n%s", count, skippedDryRun)
  111. }
  112. if !strings.Contains(skippedDryRun, kubernetesProviderImage) {
  113. t.Fatalf("expected skipped test.v2 dry-run to still load the kubernetes provider image, output:\n%s", skippedDryRun)
  114. }
  115. if !strings.Contains(skippedDryRun, "ghcr.io/external-secrets/provider-aws:test-version") {
  116. t.Fatalf("expected skipped test.v2 dry-run to still load the aws provider image, output:\n%s", skippedDryRun)
  117. }
  118. if !strings.Contains(skippedDryRun, "ghcr.io/external-secrets/provider-fake:test-version") {
  119. t.Fatalf("expected skipped test.v2 dry-run to still load the fake provider image, output:\n%s", skippedDryRun)
  120. }
  121. if strings.Contains(skippedDryRun, "ghcr.io/external-secrets/provider-gcp:test-version") {
  122. t.Fatalf("expected skipped test.v2 dry-run to omit nonexistent gcp provider image loads, output:\n%s", skippedDryRun)
  123. }
  124. if !strings.Contains(skippedDryRun, helmDependencyBuildCmd) {
  125. t.Fatalf("expected skipped test.v2 dry-run to ensure helm dependencies before copying the chart, output:\n%s", skippedDryRun)
  126. }
  127. if !strings.Contains(skippedDryRun, `TEST_SUITES="provider"`) {
  128. t.Fatalf("expected skipped test.v2 dry-run to still run the provider suite, output:\n%s", skippedDryRun)
  129. }
  130. }
  131. func TestV2MakeTargetPrunesDockerImagesInCI(t *testing.T) {
  132. t.Parallel()
  133. dryRun := runMakeDryRunWithEnv(t, []string{"CI=true"}, "test.v2", testVersionArg)
  134. if count := strings.Count(dryRun, dockerCleanupCmd); count != 1 {
  135. t.Fatalf("expected CI test.v2 dry-run to prune docker state once, got %d occurrences, output:\n%s", count, dryRun)
  136. }
  137. }
  138. func TestV2OperationalMakeTarget(t *testing.T) {
  139. t.Parallel()
  140. dryRun := runMakeDryRun(t, "test.v2.operational", testVersionArg)
  141. if !strings.Contains(dryRun, `V2_GINKGO_LABELS='v2 && operational && fake'`) {
  142. t.Fatalf("expected operational labels in target, got:\n%s", dryRun)
  143. }
  144. if !strings.Contains(dryRun, `V2_TEST_SUITES='provider'`) {
  145. t.Fatalf("expected provider suite in target, got:\n%s", dryRun)
  146. }
  147. if !strings.Contains(dryRun, `TEST_SUITES="provider"`) {
  148. t.Fatalf("expected operational target to render provider-only v2 suites, got:\n%s", dryRun)
  149. }
  150. }
  151. func runMakeDryRun(t *testing.T, target string, extraArgs ...string) string {
  152. t.Helper()
  153. return runMakeDryRunWithEnv(t, nil, target, extraArgs...)
  154. }
  155. func runMakeDryRunWithEnv(t *testing.T, extraEnv []string, target string, extraArgs ...string) string {
  156. t.Helper()
  157. args := append([]string{"-n", target}, extraArgs...)
  158. cmd := exec.Command("make", args...)
  159. cmd.Dir = "."
  160. cmd.Env = append(os.Environ(), extraEnv...)
  161. output, err := cmd.CombinedOutput()
  162. if err != nil {
  163. t.Fatalf("make dry-run failed: %v\n%s", err, string(output))
  164. }
  165. return string(output)
  166. }