-
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.
Port tests for the List workflow to test list.List
- Loading branch information
1 parent
63a7141
commit e10c2f5
Showing
4 changed files
with
478 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
package list_test | ||
|
||
import ( | ||
"cmp" | ||
"context" | ||
"regexp" | ||
"slices" | ||
"testing" | ||
|
||
"github.com/go-logr/logr" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/env" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/list" | ||
"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/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, "List Suite") | ||
} | ||
|
||
var _ = Describe("List", func() { | ||
var ( | ||
envOpts []env.Option | ||
) | ||
|
||
JustBeforeEach(func() { | ||
addr, shutdown := testhelpers.NewServer() | ||
DeferCleanup(shutdown) | ||
|
||
envOpts = append( | ||
envOpts, | ||
env.WithClient(&remote.Client{ | ||
Log: testLog.WithName("test-remote-client"), | ||
Bucket: "kubebuilder-tools-test", | ||
Server: addr, | ||
Insecure: true, | ||
}), | ||
env.WithStore(testhelpers.NewMockStore()), | ||
) | ||
}) | ||
|
||
Context("when downloads are disabled", func() { | ||
JustBeforeEach(func() { | ||
envOpts = append(envOpts, env.WithClient(nil)) // ensure tests fail if we try to contact remote | ||
}) | ||
|
||
It("should include local contents sorted by version", func() { | ||
result, err := list.List( | ||
ctx, | ||
versions.AnyVersion, | ||
list.NoDownload(true), | ||
list.WithPlatform("*", "*"), | ||
list.WithEnvOptions(envOpts...), | ||
) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// build this list based on test data, to avoid having to change | ||
// in two places if we add some more test cases | ||
expected := make([]list.Result, 0) | ||
for _, v := range testhelpers.LocalVersions { | ||
for _, p := range v.Platforms { | ||
expected = append(expected, list.Result{ | ||
Version: v.Version, | ||
Platform: p.Platform, | ||
Status: list.Installed, | ||
}) | ||
} | ||
} | ||
// this sorting ensures the List method fulfils the contract of | ||
// returning the most relevant items first | ||
slices.SortFunc(expected, func(a, b list.Result) int { | ||
return cmp.Or( | ||
// we want the results sorted in descending order by version | ||
cmp.Compare(b.Version.Major, a.Version.Major), | ||
cmp.Compare(b.Version.Minor, a.Version.Minor), | ||
cmp.Compare(b.Version.Patch, a.Version.Patch), | ||
// ..and then in ascending order by platform | ||
cmp.Compare(a.Platform.OS, b.Platform.OS), | ||
cmp.Compare(a.Platform.Arch, b.Platform.Arch), | ||
) | ||
}) | ||
|
||
Expect(result).To(HaveExactElements(expected)) | ||
}) | ||
}) | ||
|
||
Context("when downloads are enabled", func() { | ||
It("should sort local & remote by version", func() { | ||
result, err := list.List( | ||
ctx, | ||
versions.AnyVersion, | ||
list.WithPlatform("*", "*"), | ||
list.WithEnvOptions(envOpts...), | ||
) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// build this list based on test data, to avoid having to change | ||
// in two places if we add some more test cases | ||
expected := make([]list.Result, 0) | ||
for _, v := range testhelpers.LocalVersions { | ||
for _, p := range v.Platforms { | ||
expected = append(expected, list.Result{ | ||
Version: v.Version, | ||
Platform: p.Platform, | ||
Status: list.Installed, | ||
}) | ||
} | ||
} | ||
rx := regexp.MustCompile(`^kubebuilder-tools-(\d+\.\d+\.\d+)-(\w+)-(\w+).tar.gz$`) | ||
for _, v := range testhelpers.RemoteNames { | ||
if m := rx.FindStringSubmatch(v); m != nil { | ||
s, err := versions.FromExpr(m[1]) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
expected = append(expected, list.Result{ | ||
Version: *s.AsConcrete(), | ||
Platform: versions.Platform{ | ||
OS: m[2], | ||
Arch: m[3], | ||
}, | ||
Status: list.Available, | ||
}) | ||
} | ||
} | ||
// this sorting ensures the List method fulfils the contract of | ||
// returning the most relevant items first | ||
slices.SortFunc(expected, func(a, b list.Result) int { | ||
return cmp.Or( | ||
// we want installed versions first, available after; | ||
// compare in reverse order since "installed > "available" | ||
cmp.Compare(b.Status, a.Status), | ||
// then, sort in descending order by version | ||
cmp.Compare(b.Version.Major, a.Version.Major), | ||
cmp.Compare(b.Version.Minor, a.Version.Minor), | ||
cmp.Compare(b.Version.Patch, a.Version.Patch), | ||
// ..and then in ascending order by platform | ||
cmp.Compare(a.Platform.OS, b.Platform.OS), | ||
cmp.Compare(a.Platform.Arch, b.Platform.Arch), | ||
) | ||
}) | ||
|
||
Expect(result).To(HaveExactElements(expected)) | ||
}) | ||
|
||
It("should skip non-matching remote contents", func() { | ||
result, err := list.List( | ||
ctx, | ||
versions.Spec{ | ||
Selector: versions.PatchSelector{Major: 1, Minor: 16, Patch: versions.AnyPoint}, | ||
}, | ||
list.WithPlatform("*", "*"), | ||
list.WithEnvOptions(envOpts...), | ||
) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// build this list based on test data, to avoid having to change | ||
// in two places if we add some more test cases | ||
expected := make([]list.Result, 0) | ||
for _, v := range testhelpers.LocalVersions { | ||
// ignore versions not matching the filter in the options | ||
if v.Version.Major != 1 || v.Version.Minor != 16 { | ||
continue | ||
} | ||
for _, p := range v.Platforms { | ||
expected = append(expected, list.Result{ | ||
Version: v.Version, | ||
Platform: p.Platform, | ||
Status: list.Installed, | ||
}) | ||
} | ||
} | ||
rx := regexp.MustCompile(`^kubebuilder-tools-(\d+\.\d+\.\d+)-(\w+)-(\w+).tar.gz$`) | ||
for _, v := range testhelpers.RemoteNames { | ||
if m := rx.FindStringSubmatch(v); m != nil { | ||
s, err := versions.FromExpr(m[1]) | ||
Expect(err).NotTo(HaveOccurred()) | ||
v := *s.AsConcrete() | ||
// ignore versions not matching the filter in the options | ||
if v.Major != 1 || v.Minor != 16 { | ||
continue | ||
} | ||
expected = append(expected, list.Result{ | ||
Version: v, | ||
Platform: versions.Platform{ | ||
OS: m[2], | ||
Arch: m[3], | ||
}, | ||
Status: list.Available, | ||
}) | ||
} | ||
} | ||
// this sorting ensures the List method fulfils the contract of | ||
// returning the most relevant items first | ||
slices.SortFunc(expected, func(a, b list.Result) int { | ||
return cmp.Or( | ||
// we want installed versions first, available after; | ||
// compare in reverse order since "installed > "available" | ||
cmp.Compare(b.Status, a.Status), | ||
// then, sort in descending order by version | ||
cmp.Compare(b.Version.Major, a.Version.Major), | ||
cmp.Compare(b.Version.Minor, a.Version.Minor), | ||
cmp.Compare(b.Version.Patch, a.Version.Patch), | ||
// ..and then in ascending order by platform | ||
cmp.Compare(a.Platform.OS, b.Platform.OS), | ||
cmp.Compare(a.Platform.Arch, b.Platform.Arch), | ||
) | ||
}) | ||
|
||
Expect(result).To(HaveExactElements(expected)) | ||
}) | ||
}) | ||
}) |
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,21 @@ | ||
package testhelpers | ||
|
||
import ( | ||
"github.com/go-logr/logr" | ||
"github.com/go-logr/zapr" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"github.com/onsi/ginkgo/v2" | ||
) | ||
|
||
// GetLogger configures a logger that's suitable for testing | ||
func GetLogger() logr.Logger { | ||
testOut := zapcore.AddSync(ginkgo.GinkgoWriter) | ||
enc := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()) | ||
|
||
zapLog := zap.New(zapcore.NewCore(enc, testOut, zap.DebugLevel), | ||
zap.ErrorOutput(testOut), zap.Development(), zap.AddStacktrace(zap.WarnLevel)) | ||
|
||
return zapr.NewLogger(zapLog) | ||
} |
Oops, something went wrong.