From 8e919d70a6f888a9e409103c5d78741387f3d8c7 Mon Sep 17 00:00:00 2001 From: Joe Lanford Date: Fri, 11 Sep 2020 23:41:44 -0400 Subject: [PATCH] e2e: use regex replace to forcibly replace scaffolded Dockerfile base image strings --- .golangci.yml | 6 +++++ .../bundle/internal/result_test.go | 4 ++-- test/e2e-ansible/e2e_ansible_suite_test.go | 4 +--- test/e2e-helm/e2e_helm_cluster_test.go | 4 ++-- test/e2e-helm/e2e_helm_local_test.go | 4 ++-- test/e2e-helm/e2e_helm_suite_test.go | 4 +--- test/internal/utils.go | 22 ++++++++++++++++--- 7 files changed, 33 insertions(+), 15 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index bf79e66163..db55ab86a6 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -22,6 +22,12 @@ linters: - gosec issues: exclude-rules: + # Allow dot imports for ginkgo and gomega + - source: ginkgo|gomega + linters: + - golint + text: "should not use dot imports" + - linters: - gosec # these exclusion rules are for current failures in the code base for gosec which are diff --git a/internal/cmd/operator-sdk/bundle/internal/result_test.go b/internal/cmd/operator-sdk/bundle/internal/result_test.go index f47861aa40..aac1c593f7 100644 --- a/internal/cmd/operator-sdk/bundle/internal/result_test.go +++ b/internal/cmd/operator-sdk/bundle/internal/result_test.go @@ -21,8 +21,8 @@ import ( "os" "testing" - . "github.com/onsi/ginkgo" //nolint:golint - . "github.com/onsi/gomega" //nolint:golint + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" log "github.com/sirupsen/logrus" ) diff --git a/test/e2e-ansible/e2e_ansible_suite_test.go b/test/e2e-ansible/e2e_ansible_suite_test.go index d61aedf3a0..2e63eecae0 100644 --- a/test/e2e-ansible/e2e_ansible_suite_test.go +++ b/test/e2e-ansible/e2e_ansible_suite_test.go @@ -24,7 +24,6 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "github.com/operator-framework/operator-sdk/internal/version" testutils "github.com/operator-framework/operator-sdk/test/internal" ) @@ -114,8 +113,7 @@ var _ = BeforeSuite(func(done Done) { Expect(err).Should(Succeed()) By("replacing project Dockerfile to use ansible base image with the dev tag") - version := strings.TrimSuffix(version.Version, "+git") - testutils.ReplaceInFile(filepath.Join(tc.Dir, "Dockerfile"), version, "dev") + testutils.ReplaceRegexInFile(filepath.Join(tc.Dir, "Dockerfile"), "quay.io/operator-framework/ansible-operator:.*", "quay.io/operator-framework/ansible-operator:dev") By("adding Memcached mock task to the role") testutils.ReplaceInFile(filepath.Join(tc.Dir, "roles", strings.ToLower(tc.Kind), "tasks", "main.yml"), diff --git a/test/e2e-helm/e2e_helm_cluster_test.go b/test/e2e-helm/e2e_helm_cluster_test.go index 7de87a3669..5fdedb51fc 100644 --- a/test/e2e-helm/e2e_helm_cluster_test.go +++ b/test/e2e-helm/e2e_helm_cluster_test.go @@ -21,8 +21,8 @@ import ( "strings" "time" - . "github.com/onsi/ginkgo" //nolint:golint - . "github.com/onsi/gomega" //nolint:golint + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" kbtestutils "sigs.k8s.io/kubebuilder/test/e2e/utils" testutils "github.com/operator-framework/operator-sdk/test/internal" diff --git a/test/e2e-helm/e2e_helm_local_test.go b/test/e2e-helm/e2e_helm_local_test.go index 2a89f2c00a..ff3ff6e896 100644 --- a/test/e2e-helm/e2e_helm_local_test.go +++ b/test/e2e-helm/e2e_helm_local_test.go @@ -17,8 +17,8 @@ package e2e_helm_test import ( "os/exec" - . "github.com/onsi/ginkgo" //nolint:golint - . "github.com/onsi/gomega" //nolint:golint + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" ) var _ = Describe("Running Helm projects", func() { diff --git a/test/e2e-helm/e2e_helm_suite_test.go b/test/e2e-helm/e2e_helm_suite_test.go index f4b315f14f..94d3d76b14 100644 --- a/test/e2e-helm/e2e_helm_suite_test.go +++ b/test/e2e-helm/e2e_helm_suite_test.go @@ -23,7 +23,6 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "github.com/operator-framework/operator-sdk/internal/version" testutils "github.com/operator-framework/operator-sdk/test/internal" ) @@ -105,8 +104,7 @@ var _ = BeforeSuite(func(done Done) { Expect(err).Should(Succeed()) By("replacing project Dockerfile to use Helm base image with the dev tag") - version := strings.TrimSuffix(version.Version, "+git") - testutils.ReplaceInFile(filepath.Join(tc.Dir, "Dockerfile"), version, "dev") + testutils.ReplaceRegexInFile(filepath.Join(tc.Dir, "Dockerfile"), "quay.io/operator-framework/helm-operator:.*", "quay.io/operator-framework/helm-operator:dev") By("checking the kustomize setup") err = tc.Make("kustomize") diff --git a/test/internal/utils.go b/test/internal/utils.go index b6fc9cd0af..b1d10bcbb1 100644 --- a/test/internal/utils.go +++ b/test/internal/utils.go @@ -20,11 +20,11 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "strings" - . "github.com/onsi/ginkgo" //nolint:golint - . "github.com/onsi/gomega" //nolint:golint - + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" kbtestutils "sigs.k8s.io/kubebuilder/test/e2e/utils" ) @@ -101,6 +101,22 @@ func ReplaceInFile(path, old, new string) { b, err := ioutil.ReadFile(path) ExpectWithOffset(1, err).NotTo(HaveOccurred()) s := strings.Replace(string(b), old, new, -1) + ExpectWithOffset(1, s).NotTo(Equal(string(b)), "No replacement occurred") + err = ioutil.WriteFile(path, []byte(s), info.Mode()) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) +} + +// ReplaceRegexInFile finds all strings that match `match` and replaces them +// with `replace` in the file at path. +func ReplaceRegexInFile(path, match, replace string) { + matcher, err := regexp.Compile(match) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + info, err := os.Stat(path) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + b, err := ioutil.ReadFile(path) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + s := matcher.ReplaceAllString(string(b), replace) + ExpectWithOffset(1, s).NotTo(Equal(string(b)), "No replacement occurred") err = ioutil.WriteFile(path, []byte(s), info.Mode()) ExpectWithOffset(1, err).NotTo(HaveOccurred()) }