Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ginkgo from util tests #401

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/blang/semver/v4 v4.0.0
github.com/go-logr/logr v1.2.4
github.com/google/go-cmp v0.5.9
github.com/onsi/ginkgo/v2 v2.12.0
github.com/onsi/gomega v1.27.10
github.com/operator-framework/api v0.17.4-0.20230223191600-0131a6301e42
Expand Down Expand Up @@ -72,7 +73,6 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/cel-go v0.12.6 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand Down
323 changes: 199 additions & 124 deletions internal/resolution/util/predicates/predicates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,143 +5,218 @@ import (

mmsemver "github.com/Masterminds/semver/v3"
bsemver "github.com/blang/semver/v4"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/operator-framework/deppy/pkg/deppy/input"
"github.com/operator-framework/operator-registry/alpha/property"

olmentity "github.com/operator-framework/operator-controller/internal/resolution/entities"
"github.com/operator-framework/operator-controller/internal/resolution/util/predicates"
)

func TestPredicates(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Predicates Suite")
type testData struct {
entity map[string]string
value string
result bool
}

var _ = Describe("Predicates", func() {
Describe("WithPackageName", func() {
It("should return true when the entity has the same package name", func() {
entity := input.NewEntity("test", map[string]string{
property.TypePackage: `{"packageName": "mypackage", "version": "1.0.0"}`,
})
Expect(predicates.WithPackageName("mypackage")(entity)).To(BeTrue())
Expect(predicates.WithPackageName("notmypackage")(entity)).To(BeFalse())
})
It("should return false when the entity does not have a package name", func() {
entity := input.NewEntity("test", map[string]string{})
Expect(predicates.WithPackageName("mypackage")(entity)).To(BeFalse())
})
})

Describe("InMastermindsSemverRange", func() {
It("should return true when the entity has the has version in the right range", func() {
entity := input.NewEntity("test", map[string]string{
property.TypePackage: `{"packageName": "mypackage", "version": "1.0.0"}`,
})
inRange, err := mmsemver.NewConstraint(">=1.0.0")
Expect(err).NotTo(HaveOccurred())
notInRange, err := mmsemver.NewConstraint(">=2.0.0")
Expect(err).NotTo(HaveOccurred())
Expect(predicates.InMastermindsSemverRange(inRange)(entity)).To(BeTrue())
Expect(predicates.InMastermindsSemverRange(notInRange)(entity)).To(BeFalse())
})
It("should return false when the entity does not have a version", func() {
entity := input.NewEntity("test", map[string]string{})
inRange, err := mmsemver.NewConstraint(">=1.0.0")
Expect(err).NotTo(HaveOccurred())
Expect(predicates.InMastermindsSemverRange(inRange)(entity)).To(BeFalse())
})
})

Describe("InBlangSemverRange", func() {
It("should return true when the entity has the has version in the right range", func() {
entity := input.NewEntity("test", map[string]string{
property.TypePackage: `{"packageName": "mypackage", "version": "1.0.0"}`,
})
inRange := bsemver.MustParseRange(">=1.0.0")
notInRange := bsemver.MustParseRange(">=2.0.0")
Expect(predicates.InBlangSemverRange(inRange)(entity)).To(BeTrue())
Expect(predicates.InBlangSemverRange(notInRange)(entity)).To(BeFalse())
})
It("should return false when the entity does not have a version", func() {
entity := input.NewEntity("test", map[string]string{})
inRange := bsemver.MustParseRange(">=1.0.0")
Expect(predicates.InBlangSemverRange(inRange)(entity)).To(BeFalse())
})
})

Describe("InChannel", func() {
It("should return true when the entity comes from the specified channel", func() {
entity := input.NewEntity("test", map[string]string{
property.TypeChannel: `{"channelName":"stable","priority":0}`,
})
Expect(predicates.InChannel("stable")(entity)).To(BeTrue())
Expect(predicates.InChannel("unstable")(entity)).To(BeFalse())
})
It("should return false when the entity does not have a channel", func() {
entity := input.NewEntity("test", map[string]string{})
Expect(predicates.InChannel("stable")(entity)).To(BeFalse())
})
})

Describe("ProvidesGVK", func() {
It("should return true when the entity provides the specified gvk", func() {
entity := input.NewEntity("test", map[string]string{
property.TypeGVK: `[{"group":"foo.io","kind":"Foo","version":"v1"},{"group":"bar.io","kind":"Bar","version":"v1"}]`,
})
Expect(predicates.ProvidesGVK(&olmentity.GVK{
Group: "foo.io",
Version: "v1",
Kind: "Foo",
})(entity)).To(BeTrue())
Expect(predicates.ProvidesGVK(&olmentity.GVK{
Group: "baz.io",
Version: "v1alpha1",
Kind: "Baz",
})(entity)).To(BeFalse())
func TestPredicatesWithPackageName(t *testing.T) {
testData := []testData{
{map[string]string{property.TypePackage: `{"packageName": "mypackage", "version": "1.0.0"}`},
"mypackage",
true},
{map[string]string{property.TypePackage: `{"packageName": "mypackage", "version": "1.0.0"}`},
"notmypackage",
false},
{map[string]string{},
"mypackage",
false},
}

for _, d := range testData {
t.Run("InMastermindsSemverRange", func(t *testing.T) {
entity := input.NewEntity("test", d.entity)
if predicates.WithPackageName(d.value)(entity) != d.result {
if d.result {
t.Errorf("package %v should be in entity %v", d.value, entity)
} else {
t.Errorf("package %v should not be in entity %v", d.value, entity)
}
}
})
It("should return false when the entity does not provide a gvk", func() {
entity := input.NewEntity("test", map[string]string{})
Expect(predicates.ProvidesGVK(&olmentity.GVK{
Group: "foo.io",
Version: "v1",
Kind: "Foo",
})(entity)).To(BeFalse())
}
}

func TestPredicatesInMastermindsSemverRange(t *testing.T) {
testData := []testData{
{map[string]string{property.TypePackage: `{"packageName": "mypackage", "version": "1.0.0"}`},
">=1.0.0",
true},
{map[string]string{property.TypePackage: `{"packageName": "mypackage", "version": "1.0.0"}`},
">=2.0.0",
false},
{map[string]string{},
">=1.0.0",
false},
}

for _, d := range testData {
t.Run("InMastermindsSemverRange", func(t *testing.T) {
entity := input.NewEntity("test", d.entity)
c, err := mmsemver.NewConstraint(d.value)
if err != nil {
t.Fatalf("unable to parse constraint '%v': %v", d.value, err)
}
if predicates.InMastermindsSemverRange(c)(entity) != d.result {
if d.result {
t.Errorf("version %v should be in entity %v", d.value, entity)
} else {
t.Errorf("version %v should not be in entity %v", d.value, entity)
}
}
})
})

Describe("WithBundleImage", func() {
It("should return true when the entity provides the same bundle image", func() {
entity := input.NewEntity("test", map[string]string{
olmentity.PropertyBundlePath: `"registry.io/repo/image@sha256:1234567890"`,
})
Expect(predicates.WithBundleImage("registry.io/repo/image@sha256:1234567890")(entity)).To(BeTrue())
Expect(predicates.WithBundleImage("registry.io/repo/image@sha256:0987654321")(entity)).To(BeFalse())
}
}

func TestPredicatesInBlangSemverRange(t *testing.T) {
testData := []testData{
{map[string]string{property.TypePackage: `{"packageName": "mypackage", "version": "1.0.0"}`},
">=1.0.0",
true},
{map[string]string{property.TypePackage: `{"packageName": "mypackage", "version": "1.0.0"}`},
">=2.0.0",
false},
{map[string]string{},
">=1.0.0",
false},
}

for _, d := range testData {
t.Run("InBlangSemverRange", func(t *testing.T) {
entity := input.NewEntity("test", d.entity)
r := bsemver.MustParseRange(d.value)
if predicates.InBlangSemverRange(r)(entity) != d.result {
if d.result {
t.Errorf("version %v should be in entity %v", d.value, entity)
} else {
t.Errorf("version %v should not be in entity %v", d.value, entity)
}
}
})
It("should return false when the entity does not provide a bundle image", func() {
entity := input.NewEntity("test", map[string]string{})
Expect(predicates.WithBundleImage("registry.io/repo/image@sha256:1234567890")(entity)).To(BeFalse())
}
}

func TestPredicatesInChannel(t *testing.T) {
testData := []testData{
{map[string]string{property.TypeChannel: `{"channelName":"stable","priority":0}`},
"stable",
true},
{map[string]string{property.TypeChannel: `{"channelName":"stable","priority":0}`},
"unstable",
false},
{map[string]string{},
"stable",
false},
}

for _, d := range testData {
t.Run("InChannel", func(t *testing.T) {
entity := input.NewEntity("test", d.entity)
if predicates.InChannel(d.value)(entity) != d.result {
if d.result {
t.Errorf("channel %v should be in entity %v", d.value, entity)
} else {
t.Errorf("channel %v should not be in entity %v", d.value, entity)
}
}
})
})

Describe("Replaces", func() {
It("should return true when the entity provides the replaces property", func() {
entity := input.NewEntity("test", map[string]string{
"olm.bundle.channelEntry": `{"replaces": "test.v0.2.0"}`,
})
Expect(predicates.Replaces("test.v0.2.0")(entity)).To(BeTrue())
Expect(predicates.Replaces("test.v0.1.0")(entity)).To(BeFalse())
}
}

func TestPredicatesWithBundleImage(t *testing.T) {
testData := []testData{
{map[string]string{olmentity.PropertyBundlePath: `"registry.io/repo/image@sha256:1234567890"`},
"registry.io/repo/image@sha256:1234567890",
true},
{map[string]string{olmentity.PropertyBundlePath: `"registry.io/repo/image@sha256:1234567890"`},
"registry.io/repo/image@sha256:0987654321",
false},
{map[string]string{},
"registry.io/repo/image@sha256:1234567890",
false},
}

for _, d := range testData {
t.Run("WithBundleImage", func(t *testing.T) {
entity := input.NewEntity("test", d.entity)
if predicates.WithBundleImage(d.value)(entity) != d.result {
if d.result {
t.Errorf("bundle %v should be in entity %v", d.value, entity)
} else {
t.Errorf("bundle %v should not be in entity %v", d.value, entity)
}
}
})
It("should return false when the entity does not provide a replaces property", func() {
entity := input.NewEntity("test", map[string]string{})
Expect(predicates.Replaces("test.v0.2.0")(entity)).To(BeFalse())
}
}

type testGVK struct {
entity map[string]string
value *olmentity.GVK
result bool
}

func TestProvidesGVK(t *testing.T) {
testData := []testGVK{
{map[string]string{property.TypeGVK: `[{"group":"foo.io","kind":"Foo","version":"v1"},{"group":"bar.io","kind":"Bar","version":"v1"}]`},
&olmentity.GVK{Group: "foo.io", Version: "v1", Kind: "Foo"},
true},
{map[string]string{property.TypeGVK: `[{"group":"foo.io","kind":"Foo","version":"v1"},{"group":"bar.io","kind":"Bar","version":"v1"}]`},
&olmentity.GVK{Group: "baz.io", Version: "v1alpha1", Kind: "Baz"},
false},
{map[string]string{},
&olmentity.GVK{Group: "foo.io", Version: "v1", Kind: "Foo"},
false},
}

for _, d := range testData {
t.Run("WithBundleImage", func(t *testing.T) {
entity := input.NewEntity("test", d.entity)
if predicates.ProvidesGVK(d.value)(entity) != d.result {
if d.result {
t.Errorf("replaces %v should be in entity %v", d.value, entity)
} else {
t.Errorf("replaces %v should not be in entity %v", d.value, entity)
}
}
})
It("should return false when the replace value is not a valid json", func() {
entity := input.NewEntity("test", map[string]string{
"olm.bundle.channelEntry": `{"replaces"}`,
})
Expect(predicates.Replaces("test.v0.2.0")(entity)).To(BeFalse())
}
}

func TestPredicatesReplaces(t *testing.T) {
testData := []testData{
{map[string]string{"olm.bundle.channelEntry": `{"replaces": "test.v0.2.0"}`},
"test.v0.2.0",
true},
{map[string]string{"olm.bundle.channelEntry": `{"replaces": "test.v0.2.0"}`},
"test.v0.1.0",
false},
{map[string]string{},
"test.v0.2.0",
false},
{map[string]string{"olm.bundle.channelEntry": `{"replaces"}`},
"test.v0.2.0",
false},
}

for _, d := range testData {
t.Run("WithBundleImage", func(t *testing.T) {
entity := input.NewEntity("test", d.entity)
if predicates.Replaces(d.value)(entity) != d.result {
if d.result {
t.Errorf("replaces %v should be in entity %v", d.value, entity)
} else {
t.Errorf("replaces %v should not be in entity %v", d.value, entity)
}
}
})
})
})
}
}
Loading