@@ -727,3 +727,71 @@ func TestListPVCsForTenant(t *testing.T) {
727
727
assert .Equal (strings .Contains (listObjs .Pvcs [i ].Name , pvcArray [i ]), true )
728
728
}
729
729
}
730
+
731
+ func CreateNamespace (nameSpace string ) (* http.Response , error ) {
732
+ /*
733
+ Description: Creates a new Namespace with given information
734
+ URL: /namespace
735
+ HTTP Verb: POST
736
+ */
737
+ requestDataAdd := map [string ]interface {}{
738
+ "name" : nameSpace ,
739
+ }
740
+ requestDataJSON , _ := json .Marshal (requestDataAdd )
741
+ requestDataBody := bytes .NewReader (requestDataJSON )
742
+ request , err := http .NewRequest (
743
+ "POST" ,
744
+ "http://localhost:9090/api/v1/namespace/" ,
745
+ requestDataBody ,
746
+ )
747
+ if err != nil {
748
+ log .Println (err )
749
+ }
750
+ request .Header .Add ("Cookie" , fmt .Sprintf ("token=%s" , token ))
751
+ request .Header .Add ("Content-Type" , "application/json" )
752
+ client := & http.Client {
753
+ Timeout : 2 * time .Second ,
754
+ }
755
+ response , err := client .Do (request )
756
+ return response , err
757
+ }
758
+
759
+ func TestCreateNamespace (t * testing.T ) {
760
+ /*
761
+ Function to Create a Namespace only once.
762
+ */
763
+ assert := assert .New (t )
764
+ namespace := "new-namespace-thujun2208pm"
765
+ tests := []struct {
766
+ name string
767
+ nameSpace string
768
+ expectedStatus int
769
+ }{
770
+ {
771
+ name : "Create Namespace for the first time" ,
772
+ expectedStatus : 201 ,
773
+ nameSpace : namespace ,
774
+ },
775
+ {
776
+ name : "Create repeated namespace for second time" ,
777
+ expectedStatus : 500 ,
778
+ nameSpace : namespace ,
779
+ },
780
+ }
781
+ for _ , tt := range tests {
782
+ t .Run (tt .name , func (t * testing.T ) {
783
+ resp , err := CreateNamespace (tt .nameSpace )
784
+ assert .Nil (err )
785
+ if err != nil {
786
+ log .Println (err )
787
+ return
788
+ }
789
+ if resp != nil {
790
+ assert .Equal (
791
+ tt .expectedStatus , resp .StatusCode , "failed" )
792
+ } else {
793
+ assert .Fail ("resp cannot be nil" )
794
+ }
795
+ })
796
+ }
797
+ }
0 commit comments