Skip to content

Commit cbb545a

Browse files
committed
Added tests
1 parent 365c04b commit cbb545a

File tree

2 files changed

+217
-25
lines changed

2 files changed

+217
-25
lines changed

pkg/client/client_test.go

Lines changed: 213 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ import (
3535

3636
const serverSideTimeoutSeconds = 10
3737

38+
func deleteDeployment(dep *appsv1.Deployment, ns string) {
39+
_, err := clientset.AppsV1().Deployments(ns).Get(dep.Name, metav1.GetOptions{})
40+
if err == nil {
41+
err = clientset.AppsV1().Deployments(ns).Delete(dep.Name, &metav1.DeleteOptions{})
42+
Expect(err).NotTo(HaveOccurred())
43+
}
44+
}
45+
3846
var _ = Describe("Client", func() {
3947

4048
var scheme *runtime.Scheme
@@ -81,12 +89,8 @@ var _ = Describe("Client", func() {
8189
GracePeriodSeconds: &zero,
8290
PropagationPolicy: &policy,
8391
}
84-
_, err := clientset.AppsV1().Deployments(ns).Get(dep.Name, metav1.GetOptions{})
85-
if err == nil {
86-
err = clientset.AppsV1().Deployments(ns).Delete(dep.Name, &metav1.DeleteOptions{})
87-
Expect(err).NotTo(HaveOccurred())
88-
}
89-
_, err = clientset.CoreV1().Nodes().Get(node.Name, metav1.GetOptions{})
92+
deleteDeployment(dep, ns)
93+
_, err := clientset.CoreV1().Nodes().Get(node.Name, metav1.GetOptions{})
9094
if err == nil {
9195
err = clientset.CoreV1().Nodes().Delete(node.Name, delOptions)
9296
Expect(err).NotTo(HaveOccurred())
@@ -626,8 +630,7 @@ var _ = Describe("Client", func() {
626630
})
627631

628632
Describe("List", func() {
629-
It("should fetch collection of objects", func() {
630-
633+
It("should fetch collection of objects", func(done Done) {
631634
By("creating an initial object")
632635
dep, err := clientset.AppsV1().Deployments(ns).Create(dep)
633636
Expect(err).NotTo(HaveOccurred())
@@ -648,23 +651,184 @@ var _ = Describe("Client", func() {
648651
}
649652
}
650653
Expect(hasDep).To(BeTrue())
651-
})
652654

653-
It("should return an empty list if there are no matching objects", func() {
655+
close(done)
656+
}, serverSideTimeoutSeconds)
654657

655-
})
658+
It("should return an empty list if there are no matching objects", func(done Done) {
659+
cl, err := client.New(cfg, client.Options{})
660+
Expect(err).NotTo(HaveOccurred())
656661

657-
It("should filter results by label selector", func() {
662+
By("listing all Deployments in the cluster")
663+
deps := &appsv1.DeploymentList{}
664+
Expect(cl.List(context.Background(), nil, deps)).NotTo(HaveOccurred())
658665

659-
})
666+
By("validating no Deployments are returned")
667+
Expect(deps.Items).To(BeEmpty())
660668

661-
It("should filter results by namespace selector", func() {
669+
close(done)
670+
}, serverSideTimeoutSeconds)
662671

663-
})
672+
It("should filter results by label selector", func(done Done) {
673+
By("creating a Deployment with the app=frontend label")
674+
depFrontend := &appsv1.Deployment{
675+
ObjectMeta: metav1.ObjectMeta{Name: "deployment-frontend", Namespace: ns},
676+
Spec: appsv1.DeploymentSpec{
677+
Selector: &metav1.LabelSelector{
678+
MatchLabels: map[string]string{"app": "frontend"},
679+
},
680+
Template: corev1.PodTemplateSpec{
681+
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "frontend"}},
682+
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}},
683+
},
684+
},
685+
}
686+
depFrontend, err := clientset.AppsV1().Deployments(ns).Create(depFrontend)
687+
Expect(err).NotTo(HaveOccurred())
688+
689+
By("creating a Deployment with the app=backend label")
690+
depBackend := &appsv1.Deployment{
691+
ObjectMeta: metav1.ObjectMeta{Name: "deployment-backend", Namespace: ns},
692+
Spec: appsv1.DeploymentSpec{
693+
Selector: &metav1.LabelSelector{
694+
MatchLabels: map[string]string{"app": "backend"},
695+
},
696+
Template: corev1.PodTemplateSpec{
697+
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "backend"}},
698+
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}},
699+
},
700+
},
701+
}
702+
depBackend, err = clientset.AppsV1().Deployments(ns).Create(depBackend)
703+
Expect(err).NotTo(HaveOccurred())
664704

665-
It("should filter results by field selector", func() {
705+
cl, err := client.New(cfg, client.Options{})
706+
Expect(err).NotTo(HaveOccurred())
666707

667-
})
708+
By("listing all Deployments with label app=backend")
709+
deps := &appsv1.DeploymentList{}
710+
labels := map[string]string{"app": "backend"}
711+
lo := client.MatchingLabels(labels)
712+
Expect(cl.List(context.Background(), lo, deps)).NotTo(HaveOccurred())
713+
714+
By("only the Deployment with the backend label is returned")
715+
Expect(deps.Items).NotTo(BeEmpty())
716+
Expect(1).To(Equal(len(deps.Items)))
717+
actual := deps.Items[0]
718+
Expect(actual.Name).To(Equal("deployment-backend"))
719+
720+
deleteDeployment(depFrontend, ns)
721+
deleteDeployment(depBackend, ns)
722+
723+
close(done)
724+
}, serverSideTimeoutSeconds)
725+
726+
It("should filter results by namespace selector", func(done Done) {
727+
By("creating a Deployment in test-namespace-1")
728+
depFrontend := &appsv1.Deployment{
729+
ObjectMeta: metav1.ObjectMeta{Name: "deployment-frontend", Namespace: "test-namespace-1"},
730+
Spec: appsv1.DeploymentSpec{
731+
Selector: &metav1.LabelSelector{
732+
MatchLabels: map[string]string{"app": "frontend"},
733+
},
734+
Template: corev1.PodTemplateSpec{
735+
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "frontend"}},
736+
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}},
737+
},
738+
},
739+
}
740+
depFrontend, err := clientset.AppsV1().Deployments("test-namespace-1").Create(depFrontend)
741+
Expect(err).NotTo(HaveOccurred())
742+
743+
By("creating a Deployment in test-namespace-2")
744+
depBackend := &appsv1.Deployment{
745+
ObjectMeta: metav1.ObjectMeta{Name: "deployment-backend", Namespace: "test-namespace-2"},
746+
Spec: appsv1.DeploymentSpec{
747+
Selector: &metav1.LabelSelector{
748+
MatchLabels: map[string]string{"app": "backend"},
749+
},
750+
Template: corev1.PodTemplateSpec{
751+
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "backend"}},
752+
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}},
753+
},
754+
},
755+
}
756+
depBackend, err = clientset.AppsV1().Deployments("test-namespace-2").Create(depBackend)
757+
Expect(err).NotTo(HaveOccurred())
758+
759+
cl, err := client.New(cfg, client.Options{})
760+
Expect(err).NotTo(HaveOccurred())
761+
762+
By("listing all Deployments in test-namespace-1")
763+
deps := &appsv1.DeploymentList{}
764+
lo := client.InNamespace("test-namespace-1")
765+
Expect(cl.List(context.Background(), lo, deps)).NotTo(HaveOccurred())
766+
767+
By("only the Deployment in test-namespace-1 is returned")
768+
Expect(deps.Items).NotTo(BeEmpty())
769+
Expect(1).To(Equal(len(deps.Items)))
770+
actual := deps.Items[0]
771+
Expect(actual.Name).To(Equal("deployment-frontend"))
772+
773+
deleteDeployment(depFrontend, "test-namespace-1")
774+
deleteDeployment(depBackend, "test-namespace-2")
775+
776+
close(done)
777+
}, serverSideTimeoutSeconds)
778+
779+
// TODO(seans): get field selector test working
780+
// It("should filter results by field selector", func(done Done) {
781+
// By("creating a Deployment with the app=frontend label")
782+
// depFrontend := &appsv1.Deployment{
783+
// ObjectMeta: metav1.ObjectMeta{Name: "deployment-frontend", Namespace: ns},
784+
// Spec: appsv1.DeploymentSpec{
785+
// Selector: &metav1.LabelSelector{
786+
// MatchLabels: map[string]string{"app": "frontend"},
787+
// },
788+
// Template: corev1.PodTemplateSpec{
789+
// ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "frontend"}},
790+
// Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}},
791+
// },
792+
// },
793+
// }
794+
// depFrontend, err := clientset.AppsV1().Deployments(ns).Create(depFrontend)
795+
// Expect(err).NotTo(HaveOccurred())
796+
797+
// By("creating a Deployment with the app=backend label")
798+
// depBackend := &appsv1.Deployment{
799+
// ObjectMeta: metav1.ObjectMeta{Name: "deployment-backend", Namespace: ns},
800+
// Spec: appsv1.DeploymentSpec{
801+
// Selector: &metav1.LabelSelector{
802+
// MatchLabels: map[string]string{"app": "backend"},
803+
// },
804+
// Template: corev1.PodTemplateSpec{
805+
// ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "backend"}},
806+
// Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}},
807+
// },
808+
// },
809+
// }
810+
// depBackend, err = clientset.AppsV1().Deployments(ns).Create(depBackend)
811+
// Expect(err).NotTo(HaveOccurred())
812+
813+
// cl, err := client.New(cfg, client.Options{})
814+
// Expect(err).NotTo(HaveOccurred())
815+
816+
// By("listing all Deployments with field metadata.name=deployment-backend")
817+
// deps := &appsv1.DeploymentList{}
818+
// lo := client.MatchingField("metadata.name", "deployment-backend")
819+
// Expect(cl.List(context.Background(), lo, deps)).NotTo(HaveOccurred())
820+
821+
// By("only the Deployment with the backend field is returned")
822+
// Expect(deps.Items).NotTo(BeEmpty())
823+
// Expect(1).To(Equal(len(deps.Items)))
824+
// actual := deps.Items[0]
825+
// Expect(actual.Name).To(Equal("deployment-backend"))
826+
827+
// deleteDeployment(depFrontend, ns)
828+
// deleteDeployment(depBackend, ns)
829+
830+
// close(done)
831+
// }, serverSideTimeoutSeconds)
668832

669833
It("should fail if it cannot get a client", func() {
670834

@@ -686,25 +850,43 @@ var _ = Describe("Client", func() {
686850
Describe("ListOptions", func() {
687851
It("should be able to set a LabelSelector", func() {
688852
lo := &client.ListOptions{}
689-
err := lo.SetLabelSelector("x in (foo,bar)")
853+
err := lo.SetLabelSelector("foo=bar")
690854
Expect(err).NotTo(HaveOccurred())
855+
Expect(lo.LabelSelector.String()).To(Equal("foo=bar"))
691856
})
692857

693858
It("should be able to set a FieldSelector", func() {
694859
lo := &client.ListOptions{}
695-
err := lo.SetFieldSelector("field1=foo")
860+
err := lo.SetFieldSelector("field1=bar")
696861
Expect(err).NotTo(HaveOccurred())
862+
Expect(lo.FieldSelector.String()).To(Equal("field1=bar"))
697863
})
698864

699865
It("should be converted to metav1.ListOptions", func() {
866+
lo := &client.ListOptions{}
867+
labels := map[string]string{"foo": "bar"}
868+
mlo := lo.MatchingLabels(labels).
869+
MatchingField("field1", "bar").
870+
InNamespace("test-namespace").
871+
AsListOptions()
872+
Expect(mlo).NotTo(BeNil())
873+
Expect(mlo.LabelSelector).To(Equal("foo=bar"))
874+
Expect(mlo.FieldSelector).To(Equal("field1=bar"))
700875
})
701876

702877
It("should be able to set MatchingLabels", func() {
703-
878+
lo := &client.ListOptions{}
879+
Expect(lo.LabelSelector).To(BeNil())
880+
labels := map[string]string{"foo": "bar"}
881+
lo = lo.MatchingLabels(labels)
882+
Expect(lo.LabelSelector.String()).To(Equal("foo=bar"))
704883
})
705884

706885
It("should be able to set MatchingField", func() {
707-
886+
lo := &client.ListOptions{}
887+
Expect(lo.FieldSelector).To(BeNil())
888+
lo = lo.MatchingField("field1", "bar")
889+
Expect(lo.FieldSelector.String()).To(Equal("field1=bar"))
708890
})
709891

710892
It("should be able to set InNamespace", func() {
@@ -714,15 +896,22 @@ var _ = Describe("Client", func() {
714896
})
715897

716898
It("should be created from MatchingLabels", func() {
717-
899+
labels := map[string]string{"foo": "bar"}
900+
lo := client.MatchingLabels(labels)
901+
Expect(lo).NotTo(BeNil())
902+
Expect(lo.LabelSelector.String()).To(Equal("foo=bar"))
718903
})
719904

720905
It("should be created from MatchingField", func() {
721-
906+
lo := client.MatchingField("field1", "bar")
907+
Expect(lo).NotTo(BeNil())
908+
Expect(lo.FieldSelector.String()).To(Equal("field1=bar"))
722909
})
723910

724911
It("should be created from InNamespace", func() {
725-
912+
lo := client.InNamespace("test")
913+
Expect(lo).NotTo(BeNil())
914+
Expect(lo.Namespace).To(Equal("test"))
726915
})
727916
})
728917
})

pkg/client/interfaces.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ type ListOptions struct {
9696
Namespace string
9797

9898
// Raw represents raw ListOptions, as passed to the API server. Note
99-
// that these may not be respsected by all implementations of interface,
99+
// that these may not be respected by all implementations of interface,
100100
// and the LabelSelector and FieldSelector fields are ignored.
101101
Raw *metav1.ListOptions
102102
}
@@ -129,6 +129,9 @@ func (o *ListOptions) AsListOptions() *metav1.ListOptions {
129129
if o == nil {
130130
return &metav1.ListOptions{}
131131
}
132+
if o.Raw == nil {
133+
o.Raw = &metav1.ListOptions{}
134+
}
132135
if o.LabelSelector != nil {
133136
o.Raw.LabelSelector = o.LabelSelector.String()
134137
}

0 commit comments

Comments
 (0)