@@ -22,70 +22,66 @@ import (
22
22
"net/http"
23
23
"net/http/httptest"
24
24
25
- corev1 "k8s.io/api/core/v1"
26
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
-
28
25
"github.com/julienschmidt/httprouter"
29
26
. "github.com/onsi/ginkgo/v2"
30
27
. "github.com/onsi/gomega"
28
+ corev1 "k8s.io/api/core/v1"
29
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30
+ "k8s.io/apimachinery/pkg/types"
31
31
32
32
"github.com/kubeflow/notebooks/workspaces/backend/internal/config"
33
- "github.com/kubeflow/notebooks/workspaces/backend/internal/models"
33
+ models "github.com/kubeflow/notebooks/workspaces/backend/internal/models/namespaces "
34
34
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
35
35
)
36
36
37
37
var _ = Describe ("Namespaces Handler" , func () {
38
38
var (
39
- a App
40
- testRouter * httprouter.Router
39
+ a App
41
40
)
42
41
43
- BeforeEach (func () {
44
- repos := repositories .NewRepositories (k8sClient )
45
- a = App {
46
- Config : config.EnvConfig {
47
- Port : 4000 ,
48
- },
49
- repositories : repos ,
50
- }
51
-
52
- testRouter = httprouter .New ()
53
- testRouter .GET ("/api/namespaces" , a .GetNamespacesHandler )
54
- })
42
+ // NOTE: these tests assume a specific state of the cluster, so cannot be run in parallel with other tests.
43
+ // therefore, we run them using the `Serial` Ginkgo decorators.
44
+ Context ("when namespaces exist" , Serial , func () {
55
45
56
- Context ("when namespaces exist" , func () {
57
- const namespaceName1 = "namespaceone"
58
- const namespaceName2 = "namespacetwo"
46
+ const namespaceName1 = "get-ns-test-ns1"
47
+ const namespaceName2 = "get-ns-test-ns2"
59
48
60
49
BeforeEach (func () {
61
- By ("creating namespaces" )
50
+ repos := repositories .NewRepositories (k8sClient )
51
+ a = App {
52
+ Config : config.EnvConfig {
53
+ Port : 4000 ,
54
+ },
55
+ repositories : repos ,
56
+ }
57
+
58
+ By ("creating Namespace 1" )
62
59
namespace1 := & corev1.Namespace {
63
60
ObjectMeta : metav1.ObjectMeta {
64
61
Name : namespaceName1 ,
65
62
},
66
63
}
67
64
Expect (k8sClient .Create (ctx , namespace1 )).To (Succeed ())
68
65
66
+ By ("creating Namespace 2" )
69
67
namespace2 := & corev1.Namespace {
70
68
ObjectMeta : metav1.ObjectMeta {
71
69
Name : namespaceName2 ,
72
70
},
73
71
}
74
72
Expect (k8sClient .Create (ctx , namespace2 )).To (Succeed ())
75
-
76
73
})
77
74
78
75
AfterEach (func () {
79
- By ("deleting namespaces" )
80
- By ("deleting the namespace1" )
76
+ By ("deleting Namespace 1" )
81
77
namespace1 := & corev1.Namespace {
82
78
ObjectMeta : metav1.ObjectMeta {
83
79
Name : namespaceName1 ,
84
80
},
85
81
}
86
82
Expect (k8sClient .Delete (ctx , namespace1 )).To (Succeed ())
87
83
88
- By ("deleting the namespace2 " )
84
+ By ("deleting Namespace 2 " )
89
85
namespace2 := & corev1.Namespace {
90
86
ObjectMeta : metav1.ObjectMeta {
91
87
Name : namespaceName2 ,
@@ -96,32 +92,40 @@ var _ = Describe("Namespaces Handler", func() {
96
92
97
93
It ("should retrieve all namespaces successfully" , func () {
98
94
By ("creating the HTTP request" )
99
- req , err := http .NewRequest (http .MethodGet , "/api/namespaces" , http .NoBody )
100
- Expect (err ).NotTo (HaveOccurred (), "Failed to create HTTP request" )
95
+ req , err := http .NewRequest (http .MethodGet , AllNamespacesPath , http .NoBody )
96
+ Expect (err ).NotTo (HaveOccurred ())
101
97
102
98
By ("executing GetNamespacesHandler" )
99
+ ps := httprouter.Params {}
103
100
rr := httptest .NewRecorder ()
104
- testRouter . ServeHTTP (rr , req )
101
+ a . GetNamespacesHandler (rr , req , ps )
105
102
rs := rr .Result ()
106
103
defer rs .Body .Close ()
107
104
108
105
By ("verifying the HTTP response status code" )
109
- Expect (rs .StatusCode ).To (Equal (http .StatusOK ), "Expected HTTP status 200 OK" )
106
+ Expect (rs .StatusCode ).To (Equal (http .StatusOK ))
110
107
111
108
By ("reading the HTTP response body" )
112
109
body , err := io .ReadAll (rs .Body )
113
- Expect (err ).NotTo (HaveOccurred (), "Failed to read HTTP response body" )
110
+ Expect (err ).NotTo (HaveOccurred ())
114
111
115
- By ("unmarshalling the response JSON" )
112
+ By ("unmarshalling the response JSON to NamespacesEnvelope " )
116
113
var response NamespacesEnvelope
117
114
err = json .Unmarshal (body , & response )
118
- Expect (err ).NotTo (HaveOccurred (), "Error unmarshalling response JSON" )
115
+ Expect (err ).NotTo (HaveOccurred ())
116
+
117
+ By ("getting the Namespaces from the Kubernetes API" )
118
+ namespace1 := & corev1.Namespace {}
119
+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : namespaceName1 }, namespace1 )).To (Succeed ())
120
+ namespace2 := & corev1.Namespace {}
121
+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : namespaceName2 }, namespace2 )).To (Succeed ())
119
122
120
- By ("asserting that the created namespaces are in the response" )
123
+ By ("ensuring the response contains the expected Namespaces" )
124
+ // NOTE: we use `ContainElements` instead of `ConsistOf` because envtest creates some namespaces by default
121
125
Expect (response .Data ).To (ContainElements (
122
- models.NamespaceModel { Name : namespaceName1 } ,
123
- models.NamespaceModel { Name : namespaceName2 } ,
124
- ), "Expected created namespaces to be in the response" )
126
+ models .NewNamespaceModelFromNamespace ( namespace1 ) ,
127
+ models .NewNamespaceModelFromNamespace ( namespace2 ) ,
128
+ ))
125
129
})
126
130
})
127
131
})
0 commit comments