Skip to content

Commit

Permalink
controller-manager: add tests and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
CapacitorSet authored and palexster committed Oct 21, 2021
1 parent 67a8bf1 commit 9336232
Showing 1 changed file with 66 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"

discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
sharingv1alpha1 "github.com/liqotech/liqo/apis/sharing/v1alpha1"
"github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/discovery"
identitymanager "github.com/liqotech/liqo/pkg/identityManager"
Expand Down Expand Up @@ -122,7 +123,6 @@ var _ = Describe("ForeignClusterOperator", func() {
})

// peer namespaced

Context("Peer Namespaced", func() {

type peerTestcase struct {
Expand Down Expand Up @@ -405,9 +405,7 @@ var _ = Describe("ForeignClusterOperator", func() {
)

})

Context("Test Reconciler functions", func() {

It("Create Tenant Namespace", func() {
foreignCluster := &discoveryv1alpha1.ForeignCluster{
TypeMeta: metav1.TypeMeta{
Expand Down Expand Up @@ -456,6 +454,7 @@ var _ = Describe("ForeignClusterOperator", func() {
type checkPeeringStatusTestcase struct {
foreignClusterStatus discoveryv1alpha1.ForeignClusterStatus
resourceRequests []discoveryv1alpha1.ResourceRequest
resourceOffers []sharingv1alpha1.ResourceOffer
expectedIncomingPhase discoveryv1alpha1.PeeringConditionStatusType
}

Expand All @@ -482,6 +481,24 @@ var _ = Describe("ForeignClusterOperator", func() {
}
}

getIncomingResourceOffer = func() sharingv1alpha1.ResourceOffer {
return sharingv1alpha1.ResourceOffer{
ObjectMeta: metav1.ObjectMeta{
Name: "resource-offer-incoming",
Namespace: "default",
Labels: map[string]string{
consts.ReplicationStatusLabel: "true",
consts.ReplicationOriginLabel: "foreign-cluster-abcd",
},
},
Spec: sharingv1alpha1.ResourceOfferSpec{},
Status: sharingv1alpha1.ResourceOfferStatus{
Phase: sharingv1alpha1.ResourceOfferAccepted,
VirtualKubeletStatus: "Created",
},
}
}

getOutgoingResourceRequest = func(accepted bool) discoveryv1alpha1.ResourceRequest {
rr := discoveryv1alpha1.ResourceRequest{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -553,12 +570,23 @@ var _ = Describe("ForeignClusterOperator", func() {
Expect(err).To(Succeed())
}

for i := range c.resourceOffers {
ro := c.resourceOffers[i].DeepCopy()
err = client.Create(ctx, ro)
Expect(err).To(Succeed())

ro.Status = *c.resourceOffers[i].Status.DeepCopy()
err = client.Status().Update(ctx, ro)
Expect(err).To(Succeed())
}

err = controller.checkIncomingPeeringStatus(ctx, foreignCluster)
Expect(err).To(BeNil())

Expect(peeringconditionsutils.GetStatus(foreignCluster, discoveryv1alpha1.IncomingPeeringCondition)).To(Equal(c.expectedIncomingPhase))
},

// Test that the condition is None if there are no ResourceRequests.
Entry("none", checkPeeringStatusTestcase{
foreignClusterStatus: discoveryv1alpha1.ForeignClusterStatus{
TenantNamespace: defaultTenantNamespace,
Expand All @@ -579,6 +607,7 @@ var _ = Describe("ForeignClusterOperator", func() {
expectedIncomingPhase: discoveryv1alpha1.PeeringConditionStatusNone,
}),

// Test that the condition is None if the foreign cluster has no peering.
Entry("none and no update", checkPeeringStatusTestcase{
foreignClusterStatus: discoveryv1alpha1.ForeignClusterStatus{
TenantNamespace: defaultTenantNamespace,
Expand All @@ -599,6 +628,7 @@ var _ = Describe("ForeignClusterOperator", func() {
expectedIncomingPhase: discoveryv1alpha1.PeeringConditionStatusNone,
}),

// Test that the condition is None if there are no incoming ResourceRequest, only outgoing
Entry("outgoing", checkPeeringStatusTestcase{
foreignClusterStatus: discoveryv1alpha1.ForeignClusterStatus{
TenantNamespace: defaultTenantNamespace,
Expand All @@ -621,6 +651,7 @@ var _ = Describe("ForeignClusterOperator", func() {
expectedIncomingPhase: discoveryv1alpha1.PeeringConditionStatusNone,
}),

// Test that the condition is None if there are no incoming ResourceRequest, only outgoing
Entry("outgoing not accepted", checkPeeringStatusTestcase{
foreignClusterStatus: discoveryv1alpha1.ForeignClusterStatus{
TenantNamespace: defaultTenantNamespace,
Expand All @@ -643,7 +674,8 @@ var _ = Describe("ForeignClusterOperator", func() {
expectedIncomingPhase: discoveryv1alpha1.PeeringConditionStatusNone,
}),

Entry("incoming", checkPeeringStatusTestcase{
// Test that the condition is Pending if the incoming ResourceRequest does not have a matching ResourceOffer
Entry("incoming without offer", checkPeeringStatusTestcase{
foreignClusterStatus: discoveryv1alpha1.ForeignClusterStatus{
TenantNamespace: defaultTenantNamespace,
PeeringConditions: []discoveryv1alpha1.PeeringCondition{
Expand All @@ -662,6 +694,32 @@ var _ = Describe("ForeignClusterOperator", func() {
resourceRequests: []discoveryv1alpha1.ResourceRequest{
getIncomingResourceRequest(),
},
expectedIncomingPhase: discoveryv1alpha1.PeeringConditionStatusPending,
}),

// Test that the condition is Established if the incoming ResourceRequest has a matching ResourceOffer
Entry("incoming with offer", checkPeeringStatusTestcase{
foreignClusterStatus: discoveryv1alpha1.ForeignClusterStatus{
TenantNamespace: defaultTenantNamespace,
PeeringConditions: []discoveryv1alpha1.PeeringCondition{
{
Type: discoveryv1alpha1.IncomingPeeringCondition,
Status: discoveryv1alpha1.PeeringConditionStatusPending,
LastTransitionTime: metav1.Now(),
},
{
Type: discoveryv1alpha1.OutgoingPeeringCondition,
Status: discoveryv1alpha1.PeeringConditionStatusNone,
LastTransitionTime: metav1.Now(),
},
},
},
resourceRequests: []discoveryv1alpha1.ResourceRequest{
getIncomingResourceRequest(),
},
resourceOffers: []sharingv1alpha1.ResourceOffer{
getIncomingResourceOffer(),
},
expectedIncomingPhase: discoveryv1alpha1.PeeringConditionStatusEstablished,
}),

Expand All @@ -685,6 +743,10 @@ var _ = Describe("ForeignClusterOperator", func() {
getIncomingResourceRequest(),
getOutgoingResourceRequest(true),
},
resourceOffers: []sharingv1alpha1.ResourceOffer{
getIncomingResourceOffer(),
// getOutgoingResourceOffer(),
},
expectedIncomingPhase: discoveryv1alpha1.PeeringConditionStatusEstablished,
}),
)
Expand Down

0 comments on commit 9336232

Please sign in to comment.