@@ -35,6 +35,22 @@ import (
35
35
36
36
const serverSideTimeoutSeconds = 10
37
37
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
+
38
54
var _ = Describe ("Client" , func () {
39
55
40
56
var scheme * runtime.Scheme
@@ -81,12 +97,8 @@ var _ = Describe("Client", func() {
81
97
GracePeriodSeconds : & zero ,
82
98
PropagationPolicy : & policy ,
83
99
}
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 {})
90
102
if err == nil {
91
103
err = clientset .CoreV1 ().Nodes ().Delete (node .Name , delOptions )
92
104
Expect (err ).NotTo (HaveOccurred ())
@@ -626,8 +638,7 @@ var _ = Describe("Client", func() {
626
638
})
627
639
628
640
Describe ("List" , func () {
629
- It ("should fetch collection of objects" , func () {
630
-
641
+ It ("should fetch collection of objects" , func (done Done ) {
631
642
By ("creating an initial object" )
632
643
dep , err := clientset .AppsV1 ().Deployments (ns ).Create (dep )
633
644
Expect (err ).NotTo (HaveOccurred ())
@@ -648,23 +659,193 @@ var _ = Describe("Client", func() {
648
659
}
649
660
}
650
661
Expect (hasDep ).To (BeTrue ())
651
- })
652
662
653
- It ("should return an empty list if there are no matching objects" , func () {
663
+ close (done )
664
+ }, serverSideTimeoutSeconds )
654
665
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 ())
656
669
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 ())
658
673
659
- })
674
+ By ("validating no Deployments are returned" )
675
+ Expect (deps .Items ).To (BeEmpty ())
660
676
661
- It ("should filter results by namespace selector" , func () {
677
+ close (done )
678
+ }, serverSideTimeoutSeconds )
662
679
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 ())
664
773
665
- It ("should filter results by field selector" , func () {
774
+ cl , err := client .New (cfg , client.Options {})
775
+ Expect (err ).NotTo (HaveOccurred ())
666
776
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)
668
849
669
850
It ("should fail if it cannot get a client" , func () {
670
851
@@ -686,25 +867,43 @@ var _ = Describe("Client", func() {
686
867
Describe ("ListOptions" , func () {
687
868
It ("should be able to set a LabelSelector" , func () {
688
869
lo := & client.ListOptions {}
689
- err := lo .SetLabelSelector ("x in ( foo, bar) " )
870
+ err := lo .SetLabelSelector ("foo= bar" )
690
871
Expect (err ).NotTo (HaveOccurred ())
872
+ Expect (lo .LabelSelector .String ()).To (Equal ("foo=bar" ))
691
873
})
692
874
693
875
It ("should be able to set a FieldSelector" , func () {
694
876
lo := & client.ListOptions {}
695
- err := lo .SetFieldSelector ("field1=foo " )
877
+ err := lo .SetFieldSelector ("field1=bar " )
696
878
Expect (err ).NotTo (HaveOccurred ())
879
+ Expect (lo .FieldSelector .String ()).To (Equal ("field1=bar" ))
697
880
})
698
881
699
882
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" ))
700
892
})
701
893
702
894
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" ))
704
900
})
705
901
706
902
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" ))
708
907
})
709
908
710
909
It ("should be able to set InNamespace" , func () {
@@ -714,15 +913,22 @@ var _ = Describe("Client", func() {
714
913
})
715
914
716
915
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" ))
718
920
})
719
921
720
922
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" ))
722
926
})
723
927
724
928
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" ))
726
932
})
727
933
})
728
934
})
0 commit comments