Skip to content

Commit

Permalink
NO-ISSUE: Allow use of 'envtest' (#7100)
Browse files Browse the repository at this point in the history
This patch prepares the project so that tests will be able to use the
'envtest' package.

Related: https://issues.redhat.com/browse/MGMT-19120

Signed-off-by: Juan Hernandez <juan.hernandez@redhat.com>
  • Loading branch information
jhernand authored Dec 19, 2024
1 parent 96a1434 commit 8607a87
Show file tree
Hide file tree
Showing 25 changed files with 3,881 additions and 15 deletions.
19 changes: 4 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,8 @@ github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/go-toolsmith/astcast v1.0.0/go.mod h1:mt2OdQTeAQcY4DQgPSArJjHCcOwlX+Wl/kwN+LbLGQ4=
Expand Down Expand Up @@ -622,9 +623,8 @@ github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hf
github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20200507031123-427632fa3b1c/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 h1:k7nVchz72niMH6YLQNvHSdIE7iqsQxK1P41mySCvssg=
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/renameio v1.0.1 h1:Lh/jXZmvZxb0BBeSY5VKEfidcbcbenKjZFzM/q0fSeU=
github.com/google/renameio v1.0.1/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk=
Expand Down Expand Up @@ -966,9 +966,8 @@ github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vv
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
github.com/onsi/ginkgo/v2 v2.19.0 h1:9Cnnf7UHo57Hy3k6/m5k3dRfGTMXGvxhHFvkDTCTpvA=
github.com/onsi/ginkgo/v2 v2.19.0/go.mod h1:rlwLi9PilAFJ8jCg9UE1QP6VBpd6/xj3SRC0d6TU0To=
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
Expand All @@ -978,8 +977,6 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
Expand Down Expand Up @@ -1419,8 +1416,6 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b h1:kLiC65FbiHWFAOu+lxwNPujcsl8VYyTYYEZnsOO1WK4=
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
Expand All @@ -1447,8 +1442,6 @@ golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
Expand Down Expand Up @@ -1671,8 +1664,6 @@ golang.org/x/tools v0.1.6-0.20210820212750-d4cc65f0b2ff/go.mod h1:YD9qOF0M9xpSpd
golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg=
golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down Expand Up @@ -1773,8 +1764,6 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
Expand Down
17 changes: 17 additions & 0 deletions hack/setup_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,30 @@ function podman_remote() {
rm -f podman-remote4.tar.gz
}

function envtest() {
# Branch 'release-0.17' is the newest version that can be installed with Go 1.21. This should be updated when we
# update the version of Go.
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@release-0.17

# The unit tests will try to use the 'setup-envtest' tool to download and locate the required assets. But that doesn't
# work in the CI environment because that tool saves the assets to a directory in the home of the user, which may not
# be writeable. To avoid that we download them here, and we move them to the default directory where the unit tests
# expect them.
src=$(setup-envtest use --print path 1.30.0)
dst="/usr/local/kubebuilder/bin"
mkdir -p "${dst}"
mv "${src}"/* "${dst}"/.
setup-envtest cleanup
}

function test_tools() {
go install github.com/onsi/ginkgo/ginkgo@v1.16.4
go install github.com/golang/mock/mockgen@v1.6.0
go install github.com/vektra/mockery/v2@v2.12.3
go install gotest.tools/gotestsum@v1.6.3
go install github.com/axw/gocov/gocov@v1.1.0
go install github.com/AlekSi/gocov-xml@v1.1.0
envtest
}

function assisted_service() {
Expand Down
107 changes: 107 additions & 0 deletions internal/testing/envtest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package testing

import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"sigs.k8s.io/controller-runtime/pkg/envtest"
)

const (
// This is the name of the tool used to prepare 'envtest'.
envtestSetupToolName = "setup-envtest"

// Name of the Go package that contains the tool:
envtestSetupToolsPkg = "sigs.k8s.io/controller-runtime/tools"

// Branch `release-0.17` is the newest version that can be installed with Go 1.21. This should be updated when
// we update the version of Go.
envtestSetupToolVersion = "release-0.17"

// This is the version of the Kubernetes binaries that will be installed by the 'setup-envtest' tool.
envtestAssetsVersion = "1.30.0"

// Default location where the library looks for binaries.
envtestAssetsDir = "/usr/local/kubebuilder/bin"

// Environment variable that overrides the default location of binaries.
envtestAssetsEnv = "KUBEBUILDER_ASSETS"
)

var (
// Names of asset files:
envtestAssetFiles = []string{
"etcd",
"kube-apiserver",
"kubectl",
}
)

// SetupEnvtest prepares the machine for use of the 'envtest` package. It installs the 'setup-envtest' tool if needed,
// uses it to download the assets and prepares an envtest.Environment to use them. If the passed environment is nil a
// new one will be created. The returned environment is the passed one, or a new one if that is nil. The rest of the
// preparation, like adding CRDs, starting and stopping the environment, are responsibility of the caller.
func SetupEnvtest(env *envtest.Environment) *envtest.Environment {
var err error

// Create a new empty environment if needed:
if env == nil {
env = &envtest.Environment{}
}

// If the binaries are already available then we don't need to do anything else, the library will pick and
// use them automatically.
assetsDir, ok := os.LookupEnv(envtestAssetsEnv)
if !ok || assetsDir == "" {
assetsDir = envtestAssetsDir
}
assetsMissing := 0
for _, assetFile := range envtestAssetFiles {
assetPath := filepath.Join(assetsDir, assetFile)
_, err = os.Stat(assetPath)
if err != nil {
fmt.Fprintf(GinkgoWriter, "Asset file '%s' doesn't exist: %v\n", assetPath, err)
assetsMissing++
}
}
if assetsMissing == 0 {
return env
}

// Install the setup tool if needed:
setupToolPath, err := exec.LookPath(envtestSetupToolName)
if errors.Is(err, exec.ErrNotFound) {
fmt.Fprintf(GinkgoWriter, "Tool '%s' isn't available, will try to install it\n", envtestSetupToolName)
// #nosec:G204
goInstallCmd := exec.Command(
"go", "install",
fmt.Sprintf("%s/%s@%s", envtestSetupToolsPkg, envtestSetupToolName, envtestSetupToolVersion),
)
goInstallCmd.Stdout = GinkgoWriter
goInstallCmd.Stderr = GinkgoWriter
err = goInstallCmd.Run()
Expect(err).ToNot(HaveOccurred())
setupToolPath, err = exec.LookPath(envtestSetupToolName)
}
Expect(err).ToNot(HaveOccurred())

// Run the setup tool to ensure install the assets and get their location:
setupToolOut := &bytes.Buffer{}
setupToolCmd := exec.Command(setupToolPath, "use", "--print", "path", envtestAssetsVersion)
setupToolCmd.Stdout = setupToolOut
setupToolCmd.Stderr = GinkgoWriter
err = setupToolCmd.Run()
Expect(err).ToNot(HaveOccurred())
assetsDir = strings.TrimSpace(setupToolOut.String())

// Prepare the environment:
env.BinaryAssetsDirectory = assetsDir
return env
}
19 changes: 19 additions & 0 deletions internal/testing/envtest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package testing

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Envtest support", func() {
It("Creates a working environment", func() {
env := SetupEnvtest(nil)
config, err := env.Start()
Expect(err).ToNot(HaveOccurred())
defer func() {
err := env.Stop()
Expect(err).ToNot(HaveOccurred())
}()
Expect(config).ToNot(BeNil())
})
})
13 changes: 13 additions & 0 deletions internal/testing/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package testing

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestTesting(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Testing")
}
6 changes: 6 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2108,17 +2108,23 @@ sigs.k8s.io/controller-runtime/pkg/config/v1alpha1
sigs.k8s.io/controller-runtime/pkg/controller
sigs.k8s.io/controller-runtime/pkg/controller/controllerutil
sigs.k8s.io/controller-runtime/pkg/conversion
sigs.k8s.io/controller-runtime/pkg/envtest
sigs.k8s.io/controller-runtime/pkg/event
sigs.k8s.io/controller-runtime/pkg/handler
sigs.k8s.io/controller-runtime/pkg/healthz
sigs.k8s.io/controller-runtime/pkg/internal/controller
sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics
sigs.k8s.io/controller-runtime/pkg/internal/field/selector
sigs.k8s.io/controller-runtime/pkg/internal/flock
sigs.k8s.io/controller-runtime/pkg/internal/httpserver
sigs.k8s.io/controller-runtime/pkg/internal/log
sigs.k8s.io/controller-runtime/pkg/internal/objectutil
sigs.k8s.io/controller-runtime/pkg/internal/recorder
sigs.k8s.io/controller-runtime/pkg/internal/source
sigs.k8s.io/controller-runtime/pkg/internal/testing/addr
sigs.k8s.io/controller-runtime/pkg/internal/testing/certs
sigs.k8s.io/controller-runtime/pkg/internal/testing/controlplane
sigs.k8s.io/controller-runtime/pkg/internal/testing/process
sigs.k8s.io/controller-runtime/pkg/leaderelection
sigs.k8s.io/controller-runtime/pkg/log
sigs.k8s.io/controller-runtime/pkg/log/zap
Expand Down
Loading

0 comments on commit 8607a87

Please sign in to comment.