Skip to content

Commit

Permalink
Update to the latest toolchain-common and rewrite the tests using the…
Browse files Browse the repository at this point in the history
… new

funcs.
  • Loading branch information
metlos committed Jan 25, 2024
1 parent 7145a32 commit 7d8d5d0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 32 deletions.
18 changes: 9 additions & 9 deletions controllers/spaceprovisionerconfig/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
"github.com/codeready-toolchain/toolchain-common/pkg/test"
testSpc "github.com/codeready-toolchain/toolchain-common/pkg/test/spaceprovisionerconfig"
. "github.com/codeready-toolchain/toolchain-common/pkg/test/spaceprovisionerconfig"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -26,9 +26,9 @@ func TestFindingReferencingSpaceProvisionerConfigs(t *testing.T) {
ExpectedRequests []reconcile.Request
}

spc0 := testSpc.NewSpaceProvisionerConfig("spc0", test.HostOperatorNs, testSpc.ReferencingToolchainCluster("cluster1"))
spc1 := testSpc.NewSpaceProvisionerConfig("spc1", test.HostOperatorNs, testSpc.ReferencingToolchainCluster("cluster2"))
spc2 := testSpc.NewSpaceProvisionerConfig("spc2", test.HostOperatorNs, testSpc.ReferencingToolchainCluster("cluster1"))
spc0 := NewSpaceProvisionerConfig("spc0", test.HostOperatorNs, ReferencingToolchainCluster("cluster1"))
spc1 := NewSpaceProvisionerConfig("spc1", test.HostOperatorNs, ReferencingToolchainCluster("cluster2"))
spc2 := NewSpaceProvisionerConfig("spc2", test.HostOperatorNs, ReferencingToolchainCluster("cluster1"))

tests := map[string]testCase{
"find 1 referencing SpaceProvisionerConfig in many": {
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestFindingReferencingSpaceProvisionerConfigs(t *testing.T) {

t.Run("empty references when listing space provisioner configs fails", func(t *testing.T) {
// given
cl := test.NewFakeClient(t, testSpc.NewSpaceProvisionerConfig("spc0", test.HostOperatorNs, testSpc.ReferencingToolchainCluster("cluster1")))
cl := test.NewFakeClient(t, NewSpaceProvisionerConfig("spc0", test.HostOperatorNs, ReferencingToolchainCluster("cluster1")))
expectedErr := errors.New("expected list error")
cl.MockList = func(ctx context.Context, list runtimeclient.ObjectList, opts ...runtimeclient.ListOption) error {
if _, ok := list.(*toolchainv1alpha1.SpaceProvisionerConfigList); ok {
Expand All @@ -91,9 +91,9 @@ func TestMapToolchainClusterToSpaceProvisionerConfigs(t *testing.T) {
//

// given
spc0 := testSpc.NewSpaceProvisionerConfig("spc0", test.HostOperatorNs, testSpc.ReferencingToolchainCluster("cluster1"))
spc1 := testSpc.NewSpaceProvisionerConfig("spc1", test.HostOperatorNs, testSpc.ReferencingToolchainCluster("cluster2"))
spc2 := testSpc.NewSpaceProvisionerConfig("spc2", test.HostOperatorNs, testSpc.ReferencingToolchainCluster("cluster1"))
spc0 := NewSpaceProvisionerConfig("spc0", test.HostOperatorNs, ReferencingToolchainCluster("cluster1"))
spc1 := NewSpaceProvisionerConfig("spc1", test.HostOperatorNs, ReferencingToolchainCluster("cluster2"))
spc2 := NewSpaceProvisionerConfig("spc2", test.HostOperatorNs, ReferencingToolchainCluster("cluster1"))
cl := test.NewFakeClient(t, spc0, spc1, spc2)

// when
Expand All @@ -113,7 +113,7 @@ func TestMapToolchainClusterToSpaceProvisionerConfigs(t *testing.T) {

t.Run("interprets errors as empty result", func(t *testing.T) {
// given
cl := test.NewFakeClient(t, testSpc.NewSpaceProvisionerConfig("spc0", test.HostOperatorNs, testSpc.ReferencingToolchainCluster("cluster1")))
cl := test.NewFakeClient(t, NewSpaceProvisionerConfig("spc0", test.HostOperatorNs, ReferencingToolchainCluster("cluster1")))
expectedErr := errors.New("expected list error")
cl.MockList = func(ctx context.Context, list runtimeclient.ObjectList, opts ...runtimeclient.ListOption) error {
if _, ok := list.(*toolchainv1alpha1.SpaceProvisionerConfigList); ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
"github.com/codeready-toolchain/toolchain-common/pkg/apis"
"github.com/codeready-toolchain/toolchain-common/pkg/test"
testSpc "github.com/codeready-toolchain/toolchain-common/pkg/test/spaceprovisionerconfig"
. "github.com/codeready-toolchain/toolchain-common/pkg/test/assertions"
. "github.com/codeready-toolchain/toolchain-common/pkg/test/spaceprovisionerconfig"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -24,7 +24,7 @@ import (
func TestSpaceProvisionerConfigValidation(t *testing.T) {
t.Run("is not valid when non-existing ToolchainCluster is referenced", func(t *testing.T) {
// given
spc := testSpc.NewSpaceProvisionerConfig("spc", test.HostOperatorNs, testSpc.ReferencingToolchainCluster("non-existent"))
spc := NewSpaceProvisionerConfig("spc", test.HostOperatorNs, ReferencingToolchainCluster("non-existent"))
r, req, cl := prepareReconcile(t, spc)

// when
Expand All @@ -33,16 +33,12 @@ func TestSpaceProvisionerConfigValidation(t *testing.T) {

// then
assert.NoError(t, reconcileErr)
assert.Len(t, spc.Status.Conditions, 1)
assert.Equal(t, toolchainv1alpha1.ConditionReady, spc.Status.Conditions[0].Type)
assert.Equal(t, corev1.ConditionFalse, spc.Status.Conditions[0].Status)
assert.Equal(t, toolchainv1alpha1.SpaceProvisionerConfigToolchainClusterNotFoundReason, spc.Status.Conditions[0].Reason)
assert.Empty(t, spc.Status.Conditions[0].Message)
AssertThat(t, spc, Is(NotReadyWithReason(toolchainv1alpha1.SpaceProvisionerConfigToolchainClusterNotFoundReason)))
})

t.Run("is valid when existing ToolchainCluster is referenced", func(t *testing.T) {
// given
spc := testSpc.NewSpaceProvisionerConfig("spc", test.HostOperatorNs, testSpc.ReferencingToolchainCluster("cluster1"))
spc := NewSpaceProvisionerConfig("spc", test.HostOperatorNs, ReferencingToolchainCluster("cluster1"))
r, req, cl := prepareReconcile(t, spc, &toolchainv1alpha1.ToolchainCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster1",
Expand All @@ -56,16 +52,60 @@ func TestSpaceProvisionerConfigValidation(t *testing.T) {

// then
assert.NoError(t, reconcileErr)
assert.Len(t, spc.Status.Conditions, 1)
assert.Equal(t, toolchainv1alpha1.ConditionReady, spc.Status.Conditions[0].Type)
assert.Equal(t, corev1.ConditionTrue, spc.Status.Conditions[0].Status)
assert.Equal(t, toolchainv1alpha1.SpaceProvisionerConfigValidReason, spc.Status.Conditions[0].Reason)
assert.Empty(t, spc.Status.Conditions[0].Message)
AssertThat(t, spc, Is(Ready()))
})
t.Run("is invalid when no ToolchainCluster is referenced", func(t *testing.T) {
// given
spc := NewSpaceProvisionerConfig("spc", test.HostOperatorNs)
r, req, cl := prepareReconcile(t, spc)

// when
_, reconcileErr := r.Reconcile(context.TODO(), req)
require.NoError(t, cl.Get(context.TODO(), runtimeclient.ObjectKeyFromObject(spc), spc))

// then
assert.NoError(t, reconcileErr)
AssertThat(t, spc, Is(NotReadyWithReason(toolchainv1alpha1.SpaceProvisionerConfigToolchainClusterNotFoundReason)))
})
t.Run("becomes valid when the referenced ToolchainCluster appears", func(t *testing.T) {
// given
spc := NewSpaceProvisionerConfig("spc", test.HostOperatorNs,
ReferencingToolchainCluster("cluster1"),
WithReadyConditionInvalid(toolchainv1alpha1.SpaceProvisionerConfigToolchainClusterNotFoundReason))
r, req, cl := prepareReconcile(t, spc, &toolchainv1alpha1.ToolchainCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster1",
Namespace: test.HostOperatorNs,
},
})

// when
_, reconcileErr := r.Reconcile(context.TODO(), req)
require.NoError(t, cl.Get(context.TODO(), runtimeclient.ObjectKeyFromObject(spc), spc))

// then
assert.NoError(t, reconcileErr)
AssertThat(t, spc, Is(Ready()))
})
t.Run("becomes invalid when the referenced ToolchainCluster disappears", func(t *testing.T) {
// given
spc := NewSpaceProvisionerConfig("spc", test.HostOperatorNs,
ReferencingToolchainCluster("cluster1"),
WithReadyConditionValid())
r, req, cl := prepareReconcile(t, spc)

// when
_, reconcileErr := r.Reconcile(context.TODO(), req)
require.NoError(t, cl.Get(context.TODO(), runtimeclient.ObjectKeyFromObject(spc), spc))

// then
assert.NoError(t, reconcileErr)
AssertThat(t, spc, Is(NotReadyWithReason(toolchainv1alpha1.SpaceProvisionerConfigToolchainClusterNotFoundReason)))
})
}

func TestSpaceProvisionerConfigReEnqueing(t *testing.T) {
spc := testSpc.NewSpaceProvisionerConfig("spc", test.HostOperatorNs, testSpc.ReferencingToolchainCluster("cluster1"))
spc := NewSpaceProvisionerConfig("spc", test.HostOperatorNs, ReferencingToolchainCluster("cluster1"))

t.Run("re-enqueues on failure to GET", func(t *testing.T) {
// given
Expand Down Expand Up @@ -101,10 +141,8 @@ func TestSpaceProvisionerConfigReEnqueing(t *testing.T) {
// then
assert.NoError(t, reconcileErr)
assert.True(t, res.Requeue)
AssertThat(t, spcInCluster, Is(NotReadyWithReason(toolchainv1alpha1.SpaceProvisionerConfigToolchainClusterNotFoundReason)))
assert.Len(t, spcInCluster.Status.Conditions, 1)
assert.Equal(t, toolchainv1alpha1.ConditionReady, spcInCluster.Status.Conditions[0].Type)
assert.Equal(t, corev1.ConditionFalse, spcInCluster.Status.Conditions[0].Status)
assert.Equal(t, toolchainv1alpha1.SpaceProvisionerConfigToolchainClusterNotFoundReason, spcInCluster.Status.Conditions[0].Reason)
assert.Equal(t, "failed to get the referenced ToolchainCluster: "+getErr.Error(), spcInCluster.Status.Conditions[0].Message)
})
t.Run("re-enqueues on failure to update the status", func(t *testing.T) {
Expand All @@ -131,22 +169,26 @@ func TestSpaceProvisionerConfigReEnqueing(t *testing.T) {
}

// when
_, reconcileErr := r.Reconcile(context.TODO(), req)
res, reconcileErr := r.Reconcile(context.TODO(), req)

// then
assert.NoError(t, reconcileErr)
assert.False(t, res.Requeue)
assert.Empty(t, spc.Status.Conditions)
})
t.Run("doesn't re-enqueue when object being deleted", func(t *testing.T) {
// given
spc := spc.DeepCopy()
spc.SetDeletionTimestamp(&metav1.Time{Time: time.Now()})
r, req, _ := prepareReconcile(t, spc)
r, req, cl := prepareReconcile(t, spc)

// when
_, reconcileErr := r.Reconcile(context.TODO(), req)
res, reconcileErr := r.Reconcile(context.TODO(), req)
require.NoError(t, cl.Get(context.TODO(), runtimeclient.ObjectKeyFromObject(spc), spc))

// then
assert.NoError(t, reconcileErr)
assert.False(t, res.Requeue)
assert.Empty(t, spc.Status.Conditions)
})
t.Run("doesn't re-enqueue when ToolchainCluster not found", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/codeready-toolchain/host-operator

replace github.com/codeready-toolchain/toolchain-common => github.com/metlos/toolchain-common v0.0.0-20240125165010-df87a40fe9c9

require (
github.com/codeready-toolchain/api v0.0.0-20240116164228-8d18c9262420
github.com/codeready-toolchain/toolchain-common v0.0.0-20231218221155-9d1179b6a349
Expand Down Expand Up @@ -31,8 +33,6 @@ require (
sigs.k8s.io/controller-runtime v0.13.0
)

replace github.com/codeready-toolchain/toolchain-common => github.com/metlos/toolchain-common v0.0.0-20240125091627-98a8e74d0fcb

require github.com/google/uuid v1.3.0 // indirect

require (
Expand Down

0 comments on commit 7d8d5d0

Please sign in to comment.