-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
25 changed files
with
3,877 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,13 +43,29 @@ 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/[email protected] | ||
|
||
# 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}" | ||
} | ||
|
||
function test_tools() { | ||
go install github.com/onsi/ginkgo/[email protected] | ||
go install github.com/golang/mock/[email protected] | ||
go install github.com/vektra/mockery/[email protected] | ||
go install gotest.tools/[email protected] | ||
go install github.com/axw/gocov/[email protected] | ||
go install github.com/AlekSi/[email protected] | ||
envtest | ||
} | ||
|
||
function assisted_service() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
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" | ||
) | ||
|
||
// SetupEnvtest prepares the machine for use of the 'envtest` package. It installs the 'setup-envtest' tool if needed, | ||
// uses it to download the Kubernetes binaries and creates a envtest. The passed environment will be modified to use | ||
// thos binaries. If the passed environment is nil a new one will be created. The returned environment is the passed | ||
// one, or a new one if the passed one was 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 | ||
} | ||
assetFiles := []string{ | ||
"etcd", | ||
"kube-apiserver", | ||
"kubectl", | ||
} | ||
assetsMissing := 0 | ||
for _, assetFile := range assetFiles { | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.