@@ -22,70 +22,66 @@ import (
2222 "net/http"
2323 "net/http/httptest"
2424
25- corev1 "k8s.io/api/core/v1"
26- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27-
2825 "github.com/julienschmidt/httprouter"
2926 . "github.com/onsi/ginkgo/v2"
3027 . "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"
3131
3232 "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 "
3434 "github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
3535)
3636
3737var _ = Describe ("Namespaces Handler" , func () {
3838 var (
39- a App
40- testRouter * httprouter.Router
39+ a App
4140 )
4241
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 () {
5545
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"
5948
6049 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" )
6259 namespace1 := & corev1.Namespace {
6360 ObjectMeta : metav1.ObjectMeta {
6461 Name : namespaceName1 ,
6562 },
6663 }
6764 Expect (k8sClient .Create (ctx , namespace1 )).To (Succeed ())
6865
66+ By ("creating Namespace 2" )
6967 namespace2 := & corev1.Namespace {
7068 ObjectMeta : metav1.ObjectMeta {
7169 Name : namespaceName2 ,
7270 },
7371 }
7472 Expect (k8sClient .Create (ctx , namespace2 )).To (Succeed ())
75-
7673 })
7774
7875 AfterEach (func () {
79- By ("deleting namespaces" )
80- By ("deleting the namespace1" )
76+ By ("deleting Namespace 1" )
8177 namespace1 := & corev1.Namespace {
8278 ObjectMeta : metav1.ObjectMeta {
8379 Name : namespaceName1 ,
8480 },
8581 }
8682 Expect (k8sClient .Delete (ctx , namespace1 )).To (Succeed ())
8783
88- By ("deleting the namespace2 " )
84+ By ("deleting Namespace 2 " )
8985 namespace2 := & corev1.Namespace {
9086 ObjectMeta : metav1.ObjectMeta {
9187 Name : namespaceName2 ,
@@ -96,32 +92,40 @@ var _ = Describe("Namespaces Handler", func() {
9692
9793 It ("should retrieve all namespaces successfully" , func () {
9894 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 ())
10197
10298 By ("executing GetNamespacesHandler" )
99+ ps := httprouter.Params {}
103100 rr := httptest .NewRecorder ()
104- testRouter . ServeHTTP (rr , req )
101+ a . GetNamespacesHandler (rr , req , ps )
105102 rs := rr .Result ()
106103 defer rs .Body .Close ()
107104
108105 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 ))
110107
111108 By ("reading the HTTP response body" )
112109 body , err := io .ReadAll (rs .Body )
113- Expect (err ).NotTo (HaveOccurred (), "Failed to read HTTP response body" )
110+ Expect (err ).NotTo (HaveOccurred ())
114111
115- By ("unmarshalling the response JSON" )
112+ By ("unmarshalling the response JSON to NamespacesEnvelope " )
116113 var response NamespacesEnvelope
117114 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 ())
119122
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
121125 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+ ))
125129 })
126130 })
127131})
0 commit comments