Merge pull request #10 from drewwalters96/addl-tests
Increase test coverage
This commit is contained in:
		
							
								
								
									
										3
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								go.sum
									
									
									
									
									
								
							| @@ -233,6 +233,7 @@ github.com/googleapis/gnostic v0.3.1/go.mod h1:on+2t9HRStVgn95RSsFWFz+6Q0Snyqv1a | ||||
| github.com/googleapis/gnostic v0.4.1 h1:DLJCy1n/vrD4HPjOvYcT8aYQXpPIzoRZONaYwyycI+I= | ||||
| github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3ir6b65WBswg= | ||||
| github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8= | ||||
| github.com/gophercloud/gophercloud v0.12.0 h1:mZrie07npp6ODiwHZolTicr5jV8Ogn43AvAsSMm6Ork= | ||||
| github.com/gophercloud/gophercloud v0.12.0/go.mod h1:gmC5oQqMDOMO1t1gq5DquX/yAU808e/4mzjjDA76+Ss= | ||||
| github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= | ||||
| github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= | ||||
| @@ -400,6 +401,7 @@ github.com/vektah/gqlparser v1.1.2/go.mod h1:1ycwN7Ij5njmMkPPAOaRFY4rET2Enx7IkVv | ||||
| github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= | ||||
| github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= | ||||
| go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= | ||||
| go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738 h1:VcrIfasaLFkyjk6KNlXQSzO+B0fZcnECiDrKJsfxka0= | ||||
| go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg= | ||||
| go.mongodb.org/mongo-driver v1.0.3/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM= | ||||
| go.mongodb.org/mongo-driver v1.1.1/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM= | ||||
| @@ -669,6 +671,7 @@ k8s.io/apimachinery v0.18.6/go.mod h1:OaXp26zu/5J7p0f92ASynJa1pZo06YlV9fG7BoWbCk | ||||
| k8s.io/apimachinery v0.18.9/go.mod h1:PF5taHbXgTEJLU+xMypMmYTXTWPJ5LaW8bfsisxnEXk= | ||||
| k8s.io/apimachinery v0.19.0 h1:gjKnAda/HZp5k4xQYjL0K/Yb66IvNqjthCb03QlKpaQ= | ||||
| k8s.io/apimachinery v0.19.0/go.mod h1:DnPGDnARWFvYa3pMHgSxtbZb7gpzzAZ1pTfaUNDVlmA= | ||||
| k8s.io/apimachinery v0.19.4 h1:+ZoddM7nbzrDCp0T3SWnyxqf8cbWPT2fkZImoyvHUG0= | ||||
| k8s.io/apiserver v0.17.2/go.mod h1:lBmw/TtQdtxvrTk0e2cgtOxHizXI+d0mmGQURIHQZlo= | ||||
| k8s.io/apiserver v0.18.6/go.mod h1:Zt2XvTHuaZjBz6EFYzpp+X4hTmgWGy8AthNVnTdm3Wg= | ||||
| k8s.io/apiserver v0.18.9/go.mod h1:vXQzMtUCLsGg1Bh+7Jo2mZKHpHZFCZn8eTNSepcIA1M= | ||||
|   | ||||
| @@ -27,14 +27,15 @@ import ( | ||||
| 	corev1 "k8s.io/api/core/v1" | ||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||
| 	"k8s.io/apimachinery/pkg/types" | ||||
| 	"sigs.k8s.io/controller-runtime/pkg/client" | ||||
|  | ||||
| 	airshipv1 "sipcluster/pkg/api/v1" | ||||
| 	"sipcluster/pkg/vbmh" | ||||
| ) | ||||
|  | ||||
| var _ = Describe("SIPCluster controller", func() { | ||||
| 	Context("When it detects a SIPCluster", func() { | ||||
| 		It("Should schedule BMHs accordingly", func() { | ||||
| 	Context("When it detects a new SIPCluster", func() { | ||||
| 		It("Should schedule available nodes", func() { | ||||
| 			By("Labelling nodes") | ||||
|  | ||||
| 			// Create vBMH test objects | ||||
| @@ -67,7 +68,83 @@ var _ = Describe("SIPCluster controller", func() { | ||||
| 				} | ||||
|  | ||||
| 				return compareLabels(expectedLabels, bmh.GetLabels()) | ||||
| 			}, 60, 5).Should(Succeed()) | ||||
| 			}, 30, 5).Should(Succeed()) | ||||
|  | ||||
| 			cleanTestResources() | ||||
| 		}) | ||||
|  | ||||
| 		It("Should not schedule nodes when there is an insufficient number of available master nodes", func() { | ||||
| 			By("Not labelling any nodes") | ||||
|  | ||||
| 			// Create vBMH test objects | ||||
| 			nodes := []string{"master", "master", "worker", "worker", "worker", "worker"} | ||||
| 			namespace := "default" | ||||
| 			for node, role := range nodes { | ||||
| 				vBMH, networkData := createBMH(node, namespace, role, 6) | ||||
| 				Expect(k8sClient.Create(context.Background(), vBMH)).Should(Succeed()) | ||||
| 				Expect(k8sClient.Create(context.Background(), networkData)).Should(Succeed()) | ||||
| 			} | ||||
|  | ||||
| 			// Create SIP cluster | ||||
| 			clusterName := "subcluster-test2" | ||||
| 			sipCluster := createSIPCluster(clusterName, namespace, 3, 4) | ||||
| 			Expect(k8sClient.Create(context.Background(), sipCluster)).Should(Succeed()) | ||||
|  | ||||
| 			// Poll BMHs and validate they are not scheduled | ||||
| 			Consistently(func() error { | ||||
| 				expectedLabels := map[string]string{ | ||||
| 					vbmh.SipScheduleLabel: "false", | ||||
| 				} | ||||
|  | ||||
| 				var bmh metal3.BareMetalHost | ||||
| 				for node := range nodes { | ||||
| 					Expect(k8sClient.Get(context.Background(), types.NamespacedName{ | ||||
| 						Name:      fmt.Sprintf("node%d", node), | ||||
| 						Namespace: namespace, | ||||
| 					}, &bmh)).Should(Succeed()) | ||||
| 				} | ||||
|  | ||||
| 				return compareLabels(expectedLabels, bmh.GetLabels()) | ||||
| 			}, 30, 5).Should(Succeed()) | ||||
|  | ||||
| 			cleanTestResources() | ||||
| 		}) | ||||
|  | ||||
| 		It("Should not schedule nodes when there is an insufficient number of available worker nodes", func() { | ||||
| 			By("Not labelling any nodes") | ||||
|  | ||||
| 			// Create vBMH test objects | ||||
| 			nodes := []string{"master", "master", "master", "worker", "worker"} | ||||
| 			namespace := "default" | ||||
| 			for node, role := range nodes { | ||||
| 				vBMH, networkData := createBMH(node, namespace, role, 6) | ||||
| 				Expect(k8sClient.Create(context.Background(), vBMH)).Should(Succeed()) | ||||
| 				Expect(k8sClient.Create(context.Background(), networkData)).Should(Succeed()) | ||||
| 			} | ||||
|  | ||||
| 			// Create SIP cluster | ||||
| 			clusterName := "subcluster-test3" | ||||
| 			sipCluster := createSIPCluster(clusterName, namespace, 3, 4) | ||||
| 			Expect(k8sClient.Create(context.Background(), sipCluster)).Should(Succeed()) | ||||
|  | ||||
| 			// Poll BMHs and validate they are not scheduled | ||||
| 			Consistently(func() error { | ||||
| 				expectedLabels := map[string]string{ | ||||
| 					vbmh.SipScheduleLabel: "false", | ||||
| 				} | ||||
|  | ||||
| 				var bmh metal3.BareMetalHost | ||||
| 				for node := range nodes { | ||||
| 					Expect(k8sClient.Get(context.Background(), types.NamespacedName{ | ||||
| 						Name:      fmt.Sprintf("node%d", node), | ||||
| 						Namespace: namespace, | ||||
| 					}, &bmh)).Should(Succeed()) | ||||
| 				} | ||||
|  | ||||
| 				return compareLabels(expectedLabels, bmh.GetLabels()) | ||||
| 			}, 30, 5).Should(Succeed()) | ||||
|  | ||||
| 			cleanTestResources() | ||||
| 		}) | ||||
| 	}) | ||||
| }) | ||||
| @@ -88,6 +165,13 @@ func compareLabels(expected map[string]string, actual map[string]string) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func cleanTestResources() { | ||||
| 	opts := []client.DeleteAllOfOption{client.InNamespace("default")} | ||||
| 	Expect(k8sClient.DeleteAllOf(context.Background(), &metal3.BareMetalHost{}, opts...)).Should(Succeed()) | ||||
| 	Expect(k8sClient.DeleteAllOf(context.Background(), &airshipv1.SIPCluster{}, opts...)).Should(Succeed()) | ||||
| 	Expect(k8sClient.DeleteAllOf(context.Background(), &corev1.Secret{}, opts...)).Should(Succeed()) | ||||
| } | ||||
|  | ||||
| func createBMH(node int, namespace string, role string, rack int) (*metal3.BareMetalHost, *corev1.Secret) { | ||||
| 	rackLabel := fmt.Sprintf("r%d", rack) | ||||
| 	networkDataName := fmt.Sprintf("node%d-network-data", node) | ||||
|   | ||||
							
								
								
									
										103
									
								
								pkg/vbmh/vbmh_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								pkg/vbmh/vbmh_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,103 @@ | ||||
| package vbmh | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
|  | ||||
| 	. "github.com/onsi/ginkgo" | ||||
| 	. "github.com/onsi/gomega" | ||||
| 	metal3 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1" | ||||
| 	corev1 "k8s.io/api/core/v1" | ||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||
| 	"k8s.io/apimachinery/pkg/types" | ||||
| 	mockClient "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||||
| 	"k8s.io/apimachinery/pkg/runtime" | ||||
| 	"k8s.io/client-go/kubernetes/scheme" | ||||
| 	ctrl "sigs.k8s.io/controller-runtime" | ||||
|  | ||||
| 	airshipv1 "sipcluster/pkg/api/v1" | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| 	// numNodes is the number of test vBMH objects (nodes) created for each test | ||||
| 	numNodes = 7 | ||||
| ) | ||||
|  | ||||
| var _ = Describe("MachineList", func() { | ||||
| 	var machineList *MachineList | ||||
| 	BeforeEach(func() { | ||||
| 		nodes := map[string]*Machine{} | ||||
| 		for n := 0; n < numNodes; n++ { | ||||
| 			bmh := metal3.BareMetalHost{ | ||||
| 				ObjectMeta: metav1.ObjectMeta{ | ||||
| 					Name:      fmt.Sprintf("node0%d", n), | ||||
| 					Namespace: "default", | ||||
| 					Labels: map[string]string{ | ||||
| 						"airshipit.org/vino-flavor": "master", | ||||
| 						SipScheduleLabel:       "false", | ||||
| 						RackLabel:              "r002", | ||||
| 						ServerLabel:            fmt.Sprintf("node0%dr002", n), | ||||
| 					}, | ||||
| 				}, | ||||
| 				Spec: metal3.BareMetalHostSpec{ | ||||
| 					NetworkData: &corev1.SecretReference{ | ||||
| 						Namespace: "default", | ||||
| 						Name:      "fake-network-data", | ||||
| 					}, | ||||
| 				}, | ||||
| 			} | ||||
|  | ||||
| 			nodes[bmh.Name] = NewMachine(bmh, airshipv1.VmMaster, NotScheduled) | ||||
| 		} | ||||
|  | ||||
| 		machineList = &MachineList{ | ||||
| 			NamespacedName: types.NamespacedName{ | ||||
| 				Name: "vbmh", | ||||
| 				Namespace: "default", | ||||
| 			}, | ||||
| 			Machines: nodes, | ||||
| 			Log: ctrl.Log.WithName("controllers").WithName("SIPCluster"), | ||||
| 		} | ||||
|  | ||||
| 		err := metal3.AddToScheme(scheme.Scheme) | ||||
| 		Expect(err).NotTo(HaveOccurred()) | ||||
| 	}) | ||||
|  | ||||
| 	It("Should report if it has a machine registered for a BMH object", func() { | ||||
| 		for _, bmh := range machineList.Machines { | ||||
| 			Expect(machineList.hasMachine(bmh.BMH)).Should(BeTrue()) | ||||
| 		} | ||||
|  | ||||
| 		unregisteredMachine := machineList.Machines["node01"] | ||||
| 		unregisteredMachine.BMH.Name = "foo" | ||||
| 		Expect(machineList.hasMachine(unregisteredMachine.BMH)).Should(BeFalse()) | ||||
| 	}) | ||||
|  | ||||
| 	It("Should produce a list of unscheduled BMH objects", func() { | ||||
| 		// "Schedule" two nodes | ||||
| 		machineList.Machines["node00"].BMH.Labels[SipScheduleLabel] = "true" | ||||
| 		machineList.Machines["node01"].BMH.Labels[SipScheduleLabel] = "true" | ||||
| 		scheduledNodes := []metal3.BareMetalHost{ | ||||
| 			machineList.Machines["node00"].BMH, | ||||
| 			machineList.Machines["node01"].BMH, | ||||
| 		} | ||||
|  | ||||
| 		var objs []runtime.Object | ||||
| 		for _, machine := range machineList.Machines { | ||||
| 			objs = append(objs, &machine.BMH) | ||||
| 		} | ||||
|  | ||||
| 		k8sClient := mockClient.NewFakeClient(objs...) | ||||
| 		bmhList, err := machineList.getBMHs(k8sClient) | ||||
| 		Expect(err).To(BeNil()) | ||||
|  | ||||
| 		// Validate that the BMH list does not contain scheduled nodes | ||||
| 		for _, bmh := range bmhList.Items { | ||||
| 			for _, scheduled := range scheduledNodes { | ||||
| 				Expect(bmh).ToNot(Equal(scheduled)) | ||||
| 				Expect(bmh.Labels[SipScheduleLabel]).To(Equal("false")) | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	}) | ||||
|  | ||||
| }) | ||||
		Reference in New Issue
	
	Block a user
	 Kostiantyn Kalynovskyi
					Kostiantyn Kalynovskyi