-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a99e5c3
commit dc226b6
Showing
5 changed files
with
182 additions
and
29 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
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
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,141 @@ | ||
package use_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/spf13/afero" | ||
|
||
"github.com/go-logr/logr" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/env" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/remote" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/testhelpers" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/use" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/versions" | ||
) | ||
|
||
var ( | ||
testLog logr.Logger | ||
ctx context.Context | ||
) | ||
|
||
func TestEnv(t *testing.T) { | ||
testLog = testhelpers.GetLogger() | ||
ctx = logr.NewContext(context.Background(), testLog) | ||
|
||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Use Suite") | ||
} | ||
|
||
var _ = Describe("Use", func() { | ||
var ( | ||
envOpts []env.Option | ||
version = versions.Spec{ | ||
Selector: versions.Concrete{Major: 1, Minor: 16, Patch: 0}, | ||
} | ||
) | ||
JustBeforeEach(func() { | ||
addr, shutdown, err := testhelpers.NewServer() | ||
Expect(err).NotTo(HaveOccurred()) | ||
DeferCleanup(shutdown) | ||
|
||
s := testhelpers.NewMockStore() | ||
|
||
envOpts = append( | ||
envOpts, | ||
env.WithClient(&remote.Client{ | ||
Log: testLog.WithName("test-remote-client"), | ||
Bucket: "kubebuilder-tools-test", | ||
Server: addr, | ||
Insecure: true, | ||
}), | ||
env.WithStore(s), | ||
env.WithFS(afero.NewIOFS(s.Root)), | ||
) | ||
}) | ||
|
||
Context("when useEnv is set", func() { | ||
It("should fall back to normal behavior when the env is not set", func() { | ||
result, err := use.Use( | ||
ctx, | ||
version, | ||
use.WithAssetsFromEnv(true), | ||
use.WithPlatform("*", "*"), | ||
use.WithEnvOptions(envOpts...), | ||
) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(result.Version).To(Equal(version)) | ||
Expect(result.Path).To(HaveSuffix("/1.16.0-linux-amd64"), "should fall back to a local version") | ||
}) | ||
|
||
It("should fall back to normal behavior if binaries are missing", func() { | ||
result, err := use.Use( | ||
ctx, | ||
version, | ||
use.WithAssetsFromEnv(true), | ||
use.WithAssetsAt(".test-binaries/missing-binaries"), | ||
use.WithPlatform("*", "*"), | ||
use.WithEnvOptions(envOpts...), | ||
) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(result.Version).To(Equal(version), "should fall back to a local version") | ||
Expect(result.Path).To(HaveSuffix("/1.16.0-linux-amd64")) | ||
}) | ||
|
||
It("should use the value of the env if it contains the right binaries", func() { | ||
result, err := use.Use( | ||
ctx, | ||
version, | ||
use.WithAssetsFromEnv(true), | ||
use.WithAssetsAt("a/good/version"), | ||
use.WithPlatform("*", "*"), | ||
use.WithEnvOptions(envOpts...), | ||
) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(result.Version).To(Equal(versions.AnyVersion)) | ||
Expect(result.Path).To(HaveSuffix("/good/version")) | ||
}) | ||
|
||
It("should not try to check the version of the binaries", func() { | ||
result, err := use.Use( | ||
ctx, | ||
version, | ||
use.WithAssetsFromEnv(true), | ||
use.WithAssetsAt("wrong/version"), | ||
use.WithPlatform("*", "*"), | ||
use.WithEnvOptions(envOpts...), | ||
) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(result.Version).To(Equal(versions.AnyVersion)) | ||
Expect(result.Path).To(Equal("wrong/version")) | ||
}) | ||
|
||
It("should not need to contact the network", func() { | ||
envOpts = append(envOpts, env.WithClient(nil)) // ensure tests fail if we try to contact remote | ||
|
||
result, err := use.Use( | ||
ctx, | ||
version, | ||
use.WithAssetsFromEnv(true), | ||
use.WithAssetsAt("a/good/version"), | ||
use.WithPlatform("*", "*"), | ||
use.WithEnvOptions(envOpts...), | ||
) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(result.Version).To(Equal(versions.AnyVersion)) | ||
Expect(result.Path).To(HaveSuffix("/good/version")) | ||
}) | ||
}) | ||
}) |