Skip to content

Commit

Permalink
Increase controllers/submariner test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis authored and dfarrell07 committed Jun 29, 2023
1 parent 3df2779 commit 7c3803a
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 1 deletion.
98 changes: 98 additions & 0 deletions controllers/submariner/broker_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package submariner_test

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/submariner-io/submariner-operator/api/v1alpha1"
submarinerController "github.com/submariner-io/submariner-operator/controllers/submariner"
"github.com/submariner-io/submariner-operator/controllers/test"
"github.com/submariner-io/submariner-operator/pkg/discovery/globalnet"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const brokerName = "test-broker"

var _ = Describe("Broker controller tests", func() {
t := test.Driver{
Namespace: submarinerNamespace,
ResourceName: brokerName,
}

var broker *v1alpha1.Broker

BeforeEach(func() {
t.BeforeEach()
broker = &v1alpha1.Broker{
ObjectMeta: metav1.ObjectMeta{
Name: brokerName,
Namespace: submarinerNamespace,
},
Spec: v1alpha1.BrokerSpec{
GlobalnetCIDRRange: "168.254.0.0/16",
DefaultGlobalnetClusterSize: 8192,
GlobalnetEnabled: true,
},
}

t.InitScopedClientObjs = []client.Object{broker}
})

JustBeforeEach(func() {
t.JustBeforeEach()

t.Controller = &submarinerController.BrokerReconciler{
Client: t.ScopedClient,
}
})

It("should create the globalnet ConfigMap", func() {
t.AssertReconcileSuccess()

globalnetInfo, _, err := globalnet.GetGlobalNetworks(context.Background(), t.ScopedClient, submarinerNamespace)
Expect(err).To(Succeed())
Expect(globalnetInfo.CidrRange).To(Equal(broker.Spec.GlobalnetCIDRRange))
Expect(globalnetInfo.ClusterSize).To(Equal(broker.Spec.DefaultGlobalnetClusterSize))
})

It("should create the CRDs", func() {
t.AssertReconcileSuccess()

crd := &apiextensions.CustomResourceDefinition{}
Expect(t.ScopedClient.Get(context.Background(), client.ObjectKey{Name: "clusters.submariner.io"}, crd)).To(Succeed())
Expect(t.ScopedClient.Get(context.Background(), client.ObjectKey{Name: "endpoints.submariner.io"}, crd)).To(Succeed())
Expect(t.ScopedClient.Get(context.Background(), client.ObjectKey{Name: "gateways.submariner.io"}, crd)).To(Succeed())
Expect(t.ScopedClient.Get(context.Background(), client.ObjectKey{Name: "serviceimports.multicluster.x-k8s.io"}, crd)).To(Succeed())
})

When("the Broker resource doesn't exist", func() {
BeforeEach(func() {
t.InitScopedClientObjs = nil
})

It("should return success", func() {
t.AssertReconcileSuccess()
})
})
})
41 changes: 41 additions & 0 deletions controllers/submariner/submariner_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,47 @@ func testReconciliation() {
})
})

When("ServiceDiscovery is enabled", func() {
BeforeEach(func() {
t.submariner.Spec.ServiceDiscoveryEnabled = true
})

It("should create the ServiceDiscovery resource", func() {
t.AssertReconcileSuccess()

serviceDiscovery := &v1alpha1.ServiceDiscovery{}
err := t.ScopedClient.Get(context.TODO(), types.NamespacedName{Name: names.ServiceDiscoveryCrName, Namespace: submarinerNamespace},
serviceDiscovery)
Expect(err).To(Succeed())

Expect(serviceDiscovery.Spec.Version).To(Equal(t.submariner.Spec.Version))
Expect(serviceDiscovery.Spec.Repository).To(Equal(t.submariner.Spec.Repository))
Expect(serviceDiscovery.Spec.BrokerK8sCA).To(Equal(t.submariner.Spec.BrokerK8sCA))
Expect(serviceDiscovery.Spec.BrokerK8sRemoteNamespace).To(Equal(t.submariner.Spec.BrokerK8sRemoteNamespace))
Expect(serviceDiscovery.Spec.BrokerK8sApiServerToken).To(Equal(t.submariner.Spec.BrokerK8sApiServerToken))
Expect(serviceDiscovery.Spec.BrokerK8sApiServer).To(Equal(t.submariner.Spec.BrokerK8sApiServer))
Expect(serviceDiscovery.Spec.ClusterID).To(Equal(t.submariner.Spec.ClusterID))
Expect(serviceDiscovery.Spec.Namespace).To(Equal(t.submariner.Spec.Namespace))
Expect(serviceDiscovery.Spec.GlobalnetEnabled).To(BeTrue())
})
})

When("load balancer is enabled", func() {
BeforeEach(func() {
t.submariner.Spec.LoadBalancerEnabled = true
})

It("should create the load balancer service", func() {
t.AssertReconcileSuccess()

service := &corev1.Service{}
err := t.ScopedClient.Get(context.TODO(), types.NamespacedName{Name: "submariner-gateway", Namespace: submarinerNamespace},
service)
Expect(err).To(Succeed())
Expect(service.Spec.Type).To(Equal(corev1.ServiceTypeLoadBalancer))
})
})

When("the Submariner resource doesn't exist", func() {
BeforeEach(func() {
t.InitScopedClientObjs = nil
Expand Down
2 changes: 1 addition & 1 deletion controllers/submariner/submariner_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func newSubmariner() *v1alpha1.Submariner {
ClusterCIDR: "",
GlobalCIDR: "169.254.0.0/16",
ColorCodes: "red",
Namespace: "submariner_ns",
Namespace: submarinerNamespace,
Debug: true,
},
}
Expand Down

0 comments on commit 7c3803a

Please sign in to comment.