Skip to content

Commit a60d0b2

Browse files
authored
Merge pull request kubernetes-sigs#52 from seans3/client-tests
Added tests
2 parents 315ba15 + 21a7360 commit a60d0b2

File tree

2 files changed

+234
-25
lines changed

2 files changed

+234
-25
lines changed

pkg/client/client_test.go

Lines changed: 230 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ 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+
46+
func deleteNamespace(ns *corev1.Namespace) {
47+
_, err := clientset.CoreV1().Namespaces().Get(ns.Name, metav1.GetOptions{})
48+
if err == nil {
49+
err = clientset.CoreV1().Namespaces().Delete(ns.Name, &metav1.DeleteOptions{})
50+
Expect(err).NotTo(HaveOccurred())
51+
}
52+
}
53+
3854
var _ = Describe("Client", func() {
3955

4056
var scheme *runtime.Scheme
@@ -81,12 +97,8 @@ var _ = Describe("Client", func() {
8197
GracePeriodSeconds: &zero,
8298
PropagationPolicy: &policy,
8399
}
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{})
100+
deleteDeployment(dep, ns)
101+
_, err := clientset.CoreV1().Nodes().Get(node.Name, metav1.GetOptions{})
90102
if err == nil {
91103
err = clientset.CoreV1().Nodes().Delete(node.Name, delOptions)
92104
Expect(err).NotTo(HaveOccurred())
@@ -626,8 +638,7 @@ var _ = Describe("Client", func() {
626638
})
627639

628640
Describe("List", func() {
629-
It("should fetch collection of objects", func() {
630-
641+
It("should fetch collection of objects", func(done Done) {
631642
By("creating an initial object")
632643
dep, err := clientset.AppsV1().Deployments(ns).Create(dep)
633644
Expect(err).NotTo(HaveOccurred())
@@ -648,23 +659,193 @@ var _ = Describe("Client", func() {
648659
}
649660
}
650661
Expect(hasDep).To(BeTrue())
651-
})
652662

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

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

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

659-
})
674+
By("validating no Deployments are returned")
675+
Expect(deps.Items).To(BeEmpty())
660676

661-
It("should filter results by namespace selector", func() {
677+
close(done)
678+
}, serverSideTimeoutSeconds)
662679

663-
})
680+
// TODO(seans): get label selector test working
681+
// It("should filter results by label selector", func(done Done) {
682+
// By("creating a Deployment with the app=frontend label")
683+
// depFrontend := &appsv1.Deployment{
684+
// ObjectMeta: metav1.ObjectMeta{Name: "deployment-frontend", Namespace: ns},
685+
// Spec: appsv1.DeploymentSpec{
686+
// Selector: &metav1.LabelSelector{
687+
// MatchLabels: map[string]string{"app": "frontend"},
688+
// },
689+
// Template: corev1.PodTemplateSpec{
690+
// ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "frontend"}},
691+
// Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}},
692+
// },
693+
// },
694+
// }
695+
// depFrontend, err := clientset.AppsV1().Deployments(ns).Create(depFrontend)
696+
// Expect(err).NotTo(HaveOccurred())
697+
698+
// By("creating a Deployment with the app=backend label")
699+
// depBackend := &appsv1.Deployment{
700+
// ObjectMeta: metav1.ObjectMeta{Name: "deployment-backend", Namespace: ns},
701+
// Spec: appsv1.DeploymentSpec{
702+
// Selector: &metav1.LabelSelector{
703+
// MatchLabels: map[string]string{"app": "backend"},
704+
// },
705+
// Template: corev1.PodTemplateSpec{
706+
// ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "backend"}},
707+
// Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}},
708+
// },
709+
// },
710+
// }
711+
// depBackend, err = clientset.AppsV1().Deployments(ns).Create(depBackend)
712+
// Expect(err).NotTo(HaveOccurred())
713+
714+
// cl, err := client.New(cfg, client.Options{})
715+
// Expect(err).NotTo(HaveOccurred())
716+
717+
// By("listing all Deployments with label app=backend")
718+
// deps := &appsv1.DeploymentList{}
719+
// labels := map[string]string{"app": "backend"}
720+
// lo := client.InNamespace(ns).MatchingLabels(labels)
721+
// Expect(cl.List(context.Background(), lo, deps)).NotTo(HaveOccurred())
722+
723+
// By("only the Deployment with the backend label is returned")
724+
// Expect(deps.Items).NotTo(BeEmpty())
725+
// Expect(1).To(Equal(len(deps.Items)))
726+
// actual := deps.Items[0]
727+
// Expect(actual.Name).To(Equal("deployment-backend"))
728+
729+
// deleteDeployment(depFrontend, ns)
730+
// deleteDeployment(depBackend, ns)
731+
732+
// close(done)
733+
// }, serverSideTimeoutSeconds)
734+
735+
It("should filter results by namespace selector", func(done Done) {
736+
By("creating a Deployment in test-namespace-1")
737+
tns1 := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test-namespace-1"}}
738+
_, err := clientset.CoreV1().Namespaces().Create(tns1)
739+
Expect(err).NotTo(HaveOccurred())
740+
depFrontend := &appsv1.Deployment{
741+
ObjectMeta: metav1.ObjectMeta{Name: "deployment-frontend", Namespace: "test-namespace-1"},
742+
Spec: appsv1.DeploymentSpec{
743+
Selector: &metav1.LabelSelector{
744+
MatchLabels: map[string]string{"app": "frontend"},
745+
},
746+
Template: corev1.PodTemplateSpec{
747+
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "frontend"}},
748+
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}},
749+
},
750+
},
751+
}
752+
depFrontend, err = clientset.AppsV1().Deployments("test-namespace-1").Create(depFrontend)
753+
Expect(err).NotTo(HaveOccurred())
754+
755+
By("creating a Deployment in test-namespace-2")
756+
tns2 := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test-namespace-2"}}
757+
_, err = clientset.CoreV1().Namespaces().Create(tns2)
758+
Expect(err).NotTo(HaveOccurred())
759+
depBackend := &appsv1.Deployment{
760+
ObjectMeta: metav1.ObjectMeta{Name: "deployment-backend", Namespace: "test-namespace-2"},
761+
Spec: appsv1.DeploymentSpec{
762+
Selector: &metav1.LabelSelector{
763+
MatchLabels: map[string]string{"app": "backend"},
764+
},
765+
Template: corev1.PodTemplateSpec{
766+
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "backend"}},
767+
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}},
768+
},
769+
},
770+
}
771+
depBackend, err = clientset.AppsV1().Deployments("test-namespace-2").Create(depBackend)
772+
Expect(err).NotTo(HaveOccurred())
664773

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

667-
})
777+
By("listing all Deployments in test-namespace-1")
778+
deps := &appsv1.DeploymentList{}
779+
lo := client.InNamespace("test-namespace-1")
780+
Expect(cl.List(context.Background(), lo, deps)).NotTo(HaveOccurred())
781+
782+
By("only the Deployment in test-namespace-1 is returned")
783+
Expect(deps.Items).NotTo(BeEmpty())
784+
Expect(1).To(Equal(len(deps.Items)))
785+
actual := deps.Items[0]
786+
Expect(actual.Name).To(Equal("deployment-frontend"))
787+
788+
deleteDeployment(depFrontend, "test-namespace-1")
789+
deleteDeployment(depBackend, "test-namespace-2")
790+
deleteNamespace(tns1)
791+
deleteNamespace(tns2)
792+
793+
close(done)
794+
}, serverSideTimeoutSeconds)
795+
796+
// TODO(seans): get field selector test working
797+
// It("should filter results by field selector", func(done Done) {
798+
// By("creating a Deployment with name deployment-frontend")
799+
// depFrontend := &appsv1.Deployment{
800+
// ObjectMeta: metav1.ObjectMeta{Name: "deployment-frontend", Namespace: ns},
801+
// Spec: appsv1.DeploymentSpec{
802+
// Selector: &metav1.LabelSelector{
803+
// MatchLabels: map[string]string{"app": "frontend"},
804+
// },
805+
// Template: corev1.PodTemplateSpec{
806+
// ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "frontend"}},
807+
// Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}},
808+
// },
809+
// },
810+
// }
811+
// depFrontend, err := clientset.AppsV1().Deployments(ns).Create(depFrontend)
812+
// Expect(err).NotTo(HaveOccurred())
813+
814+
// By("creating a Deployment with name deployment-backend")
815+
// depBackend := &appsv1.Deployment{
816+
// ObjectMeta: metav1.ObjectMeta{Name: "deployment-backend", Namespace: ns},
817+
// Spec: appsv1.DeploymentSpec{
818+
// Selector: &metav1.LabelSelector{
819+
// MatchLabels: map[string]string{"app": "backend"},
820+
// },
821+
// Template: corev1.PodTemplateSpec{
822+
// ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "backend"}},
823+
// Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}},
824+
// },
825+
// },
826+
// }
827+
// depBackend, err = clientset.AppsV1().Deployments(ns).Create(depBackend)
828+
// Expect(err).NotTo(HaveOccurred())
829+
830+
// cl, err := client.New(cfg, client.Options{})
831+
// Expect(err).NotTo(HaveOccurred())
832+
833+
// By("listing all Deployments with field metadata.name=deployment-backend")
834+
// deps := &appsv1.DeploymentList{}
835+
// lo := client.MatchingField("metadata.name", "deployment-backend")
836+
// Expect(cl.List(context.Background(), lo, deps)).NotTo(HaveOccurred())
837+
838+
// By("only the Deployment with the backend field is returned")
839+
// Expect(deps.Items).NotTo(BeEmpty())
840+
// Expect(1).To(Equal(len(deps.Items)))
841+
// actual := deps.Items[0]
842+
// Expect(actual.Name).To(Equal("deployment-backend"))
843+
844+
// deleteDeployment(depFrontend, ns)
845+
// deleteDeployment(depBackend, ns)
846+
847+
// close(done)
848+
// }, serverSideTimeoutSeconds)
668849

669850
It("should fail if it cannot get a client", func() {
670851

@@ -686,25 +867,43 @@ var _ = Describe("Client", func() {
686867
Describe("ListOptions", func() {
687868
It("should be able to set a LabelSelector", func() {
688869
lo := &client.ListOptions{}
689-
err := lo.SetLabelSelector("x in (foo,bar)")
870+
err := lo.SetLabelSelector("foo=bar")
690871
Expect(err).NotTo(HaveOccurred())
872+
Expect(lo.LabelSelector.String()).To(Equal("foo=bar"))
691873
})
692874

693875
It("should be able to set a FieldSelector", func() {
694876
lo := &client.ListOptions{}
695-
err := lo.SetFieldSelector("field1=foo")
877+
err := lo.SetFieldSelector("field1=bar")
696878
Expect(err).NotTo(HaveOccurred())
879+
Expect(lo.FieldSelector.String()).To(Equal("field1=bar"))
697880
})
698881

699882
It("should be converted to metav1.ListOptions", func() {
883+
lo := &client.ListOptions{}
884+
labels := map[string]string{"foo": "bar"}
885+
mlo := lo.MatchingLabels(labels).
886+
MatchingField("field1", "bar").
887+
InNamespace("test-namespace").
888+
AsListOptions()
889+
Expect(mlo).NotTo(BeNil())
890+
Expect(mlo.LabelSelector).To(Equal("foo=bar"))
891+
Expect(mlo.FieldSelector).To(Equal("field1=bar"))
700892
})
701893

702894
It("should be able to set MatchingLabels", func() {
703-
895+
lo := &client.ListOptions{}
896+
Expect(lo.LabelSelector).To(BeNil())
897+
labels := map[string]string{"foo": "bar"}
898+
lo = lo.MatchingLabels(labels)
899+
Expect(lo.LabelSelector.String()).To(Equal("foo=bar"))
704900
})
705901

706902
It("should be able to set MatchingField", func() {
707-
903+
lo := &client.ListOptions{}
904+
Expect(lo.FieldSelector).To(BeNil())
905+
lo = lo.MatchingField("field1", "bar")
906+
Expect(lo.FieldSelector.String()).To(Equal("field1=bar"))
708907
})
709908

710909
It("should be able to set InNamespace", func() {
@@ -714,15 +913,22 @@ var _ = Describe("Client", func() {
714913
})
715914

716915
It("should be created from MatchingLabels", func() {
717-
916+
labels := map[string]string{"foo": "bar"}
917+
lo := client.MatchingLabels(labels)
918+
Expect(lo).NotTo(BeNil())
919+
Expect(lo.LabelSelector.String()).To(Equal("foo=bar"))
718920
})
719921

720922
It("should be created from MatchingField", func() {
721-
923+
lo := client.MatchingField("field1", "bar")
924+
Expect(lo).NotTo(BeNil())
925+
Expect(lo.FieldSelector.String()).To(Equal("field1=bar"))
722926
})
723927

724928
It("should be created from InNamespace", func() {
725-
929+
lo := client.InNamespace("test")
930+
Expect(lo).NotTo(BeNil())
931+
Expect(lo.Namespace).To(Equal("test"))
726932
})
727933
})
728934
})

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)