Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
add internal/updater unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: akihikokuroda <[email protected]>
  • Loading branch information
akihikokuroda authored and exdx committed Mar 15, 2022
1 parent c968f76 commit 08e42d4
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 2 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/go-logr/logr v1.2.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.17.0
github.com/onsi/gomega v1.18.1
github.com/operator-framework/api v0.13.0
github.com/operator-framework/helm-operator-plugins v0.0.9
github.com/spf13/cobra v1.3.0
Expand Down Expand Up @@ -99,6 +99,7 @@ require (
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/onsi/ginkgo/v2 v2.1.3 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/operator-framework/operator-lib v0.3.0 // indirect
Expand Down
7 changes: 6 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210122040257-d980be63207e/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210601050228-01bbb1931b22/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
Expand Down Expand Up @@ -1049,6 +1050,9 @@ github.com/onsi/ginkgo v1.14.1/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9k
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
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.1.3 h1:e/3Cwtogj0HA+25nMP1jCMDIf8RtRYbGwGGuBIFztkc=
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/gomega v0.0.0-20151007035656-2152b45fa28a/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
Expand All @@ -1062,8 +1066,9 @@ github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc=
github.com/onsi/gomega v1.14.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+tEHG0=
github.com/onsi/gomega v1.15.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+tEHG0=
github.com/onsi/gomega v1.17.0 h1:9Luw4uT5HTjHTN8+aNcSThgH1vdXnmdJ8xIfZ4wyTRE=
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/opencontainers/go-digest v0.0.0-20170106003457-a6d0ee40d420/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/opencontainers/go-digest v0.0.0-20180430190053-c9281466c8b2/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
Expand Down
13 changes: 13 additions & 0 deletions internal/updater/updater_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package updater_test

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

"testing"
)

func TestUpdater(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Updater Suite")
}
256 changes: 256 additions & 0 deletions internal/updater/updater_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
package updater_test

import (
"context"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/operator-framework/rukpak/internal/updater"
)

var _ = Describe("Updater", func() {
var (
client client.Client
u updater.Updater
obj *rukpakv1alpha1.Bundle
status = &rukpakv1alpha1.BundleStatus{
Info: &rukpakv1alpha1.BundleInfo{},
Phase: rukpakv1alpha1.PhaseFailing,
Digest: "digest",
ObservedGeneration: 1,
Conditions: []metav1.Condition{
{
Type: "Working",
Status: metav1.ConditionTrue,
ObservedGeneration: 3,
LastTransitionTime: metav1.Time{},
Reason: "requested",
Message: "Working correctly",
},
{
Type: "starting",
Status: metav1.ConditionTrue,
ObservedGeneration: 1,
LastTransitionTime: metav1.Time{},
Reason: "started",
Message: "starting up",
},
},
}
)

BeforeEach(func() {
schemeBuilder := runtime.NewSchemeBuilder(
kscheme.AddToScheme,
rukpakv1alpha1.AddToScheme,
)
scheme := runtime.NewScheme()
Expect(schemeBuilder.AddToScheme(scheme)).ShouldNot(HaveOccurred())

client = fake.NewClientBuilder().WithScheme(scheme).Build()
u = updater.New(client)
obj = &rukpakv1alpha1.Bundle{
ObjectMeta: metav1.ObjectMeta{
Name: "testBundle",
Namespace: "testNamespace",
},
Spec: rukpakv1alpha1.BundleSpec{
ProvisionerClassName: "core.rukpak.io/plain",
Image: "quay.io/tflannag/olm-plain-bundle:olm-crds-v0.20.0",
},
Status: rukpakv1alpha1.BundleStatus{
Info: &rukpakv1alpha1.BundleInfo{},
Phase: rukpakv1alpha1.PhaseFailing,
Digest: "digest",
ObservedGeneration: 1,
Conditions: []metav1.Condition{
{
Type: "Working",
Status: metav1.ConditionTrue,
ObservedGeneration: 3,
LastTransitionTime: metav1.Time{},
Reason: "requested",
Message: "Working correctly",
},
{
Type: "starting",
Status: metav1.ConditionTrue,
ObservedGeneration: 1,
LastTransitionTime: metav1.Time{},
Reason: "started",
Message: "starting up",
},
},
},
}
Expect(client.Create(context.TODO(), obj)).To(Succeed())
})

When("the object does not exist", func() {
It("should fail", func() {
Expect(client.Delete(context.TODO(), obj)).To(Succeed())
u.UpdateStatus(updater.EnsureCondition(status.Conditions[0]), updater.EnsureObservedGeneration(status.ObservedGeneration), updater.EnsureBundleDigest(status.Digest), updater.SetBundleInfo(status.Info), updater.SetPhase(status.Phase))
err := u.Apply(context.TODO(), obj)
Expect(err).NotTo(BeNil())
Expect(apierrors.IsNotFound(err)).To(BeTrue())
})
})

When("an update is a change", func() {
It("should apply an update status function", func() {
u.UpdateStatus(updater.EnsureCondition(metav1.Condition{

Type: "Working",
Status: metav1.ConditionTrue,
ObservedGeneration: 4,
LastTransitionTime: metav1.Time{},
Reason: "requested",
Message: "Working correctly",
}))
resourceVersion := obj.GetResourceVersion()

Expect(u.Apply(context.TODO(), obj)).To(Succeed())
Expect(client.Get(context.TODO(), types.NamespacedName{Namespace: "testNamespace", Name: "testBundle"}, obj)).To(Succeed())
Expect(obj.Status.Conditions).To(HaveLen(2))
Expect(obj.GetResourceVersion()).NotTo(Equal(resourceVersion))
})
})
})

var _ = Describe("EnsureBundleDigest", func() {
var status *rukpakv1alpha1.BundleStatus

BeforeEach(func() {
status = &rukpakv1alpha1.BundleStatus{}
})

It("should add BundleDigest if not present", func() {
Expect(updater.EnsureBundleDigest("digest")(status)).To(BeTrue())
Expect(status.Digest).To(Equal("digest"))
})

It("should return false for no update", func() {
status.Digest = "digest"
Expect(updater.EnsureBundleDigest("digest")(status)).To(BeFalse())
Expect(status.Digest).To(Equal("digest"))
})
})

var _ = Describe("EnsureCondition", func() {
var status *rukpakv1alpha1.BundleStatus
var condition, anotherCondition metav1.Condition

BeforeEach(func() {
status = &rukpakv1alpha1.BundleStatus{}
condition = metav1.Condition{Type: "Working"}
anotherCondition = metav1.Condition{Type: "Completed"}
})

It("should add Condition if not present", func() {
Expect(updater.EnsureCondition(condition)(status)).To(BeTrue())
status.Conditions[0].LastTransitionTime = metav1.Time{}
Expect(status.Conditions[0]).To(Equal(condition))
})

It("should return false for no update", func() {
status = &rukpakv1alpha1.BundleStatus{Conditions: []metav1.Condition{condition}}
Expect(updater.EnsureCondition(condition)(status)).To(BeFalse())
Expect(status.Conditions[0]).To(Equal(condition))
})

It("should add Condition if same type not present", func() {
status = &rukpakv1alpha1.BundleStatus{Conditions: []metav1.Condition{condition}}
Expect(updater.EnsureCondition(anotherCondition)(status)).To(BeTrue())
status.Conditions[1].LastTransitionTime = metav1.Time{}
Expect(status.Conditions[1]).To(Equal(anotherCondition))
})
})

var _ = Describe("EnsureObservedGeneration", func() {
var status *rukpakv1alpha1.BundleStatus

BeforeEach(func() {
status = &rukpakv1alpha1.BundleStatus{}
})

It("should add ObservedGeneration if not present", func() {
Expect(updater.EnsureObservedGeneration(3)(status)).To(BeTrue())
Expect(status.ObservedGeneration).To(Equal(int64(3)))
})

It("should return false for no update", func() {
status.ObservedGeneration = 5
Expect(updater.EnsureObservedGeneration(5)(status)).To(BeFalse())
Expect(status.ObservedGeneration).To(Equal(int64(5)))
})
})

var _ = Describe("SetPhase", func() {
var status *rukpakv1alpha1.BundleStatus

BeforeEach(func() {
status = &rukpakv1alpha1.BundleStatus{}
})

It("should set phase if not present", func() {
Expect(updater.SetPhase("phase")(status)).To(BeTrue())
Expect(status.Phase).To(Equal("phase"))
})

It("should return false for no update", func() {
status.Phase = "phase"
Expect(updater.SetPhase("phase")(status)).To(BeFalse())
Expect(status.Phase).To(Equal("phase"))
})
})

var _ = Describe("SetBundleInfo", func() {
var status *rukpakv1alpha1.BundleStatus
var info *rukpakv1alpha1.BundleInfo

BeforeEach(func() {
status = &rukpakv1alpha1.BundleStatus{}
info = &rukpakv1alpha1.BundleInfo{}
})

It("should set phase if not present", func() {
Expect(updater.SetBundleInfo(info)(status)).To(BeTrue())
Expect(status.Info).To(Equal(info))
})

It("should return false for no update", func() {
status.Info = info
Expect(updater.SetBundleInfo(info)(status)).To(BeFalse())
Expect(status.Info).To(Equal(info))
})
})

var _ = Describe("UnsetBundleInfo", func() {
var status *rukpakv1alpha1.BundleStatus
var info *rukpakv1alpha1.BundleInfo

BeforeEach(func() {
status = &rukpakv1alpha1.BundleStatus{}
info = &rukpakv1alpha1.BundleInfo{}
})

It("should set phase if not present", func() {
Expect(updater.UnsetBundleInfo()(status)).To(BeFalse())
Expect(status.Info).To(Equal((*rukpakv1alpha1.BundleInfo)(nil)))
})

It("should return false for no update", func() {
status.Info = info
Expect(updater.UnsetBundleInfo()(status)).To(BeTrue())
Expect(status.Info).To(Equal((*rukpakv1alpha1.BundleInfo)(nil)))
})
})

0 comments on commit 08e42d4

Please sign in to comment.