notebooks/workspaces/backend/api/namespaces_handler_test.go

122 lines
3.7 KiB
Go

/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"github.com/julienschmidt/httprouter"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
models "github.com/kubeflow/notebooks/workspaces/backend/internal/models/namespaces"
)
var _ = Describe("Namespaces Handler", func() {
// NOTE: these tests assume a specific state of the cluster, so cannot be run in parallel with other tests.
// therefore, we run them using the `Serial` Ginkgo decorators.
Context("when namespaces exist", Serial, func() {
const namespaceName1 = "get-ns-test-ns1"
const namespaceName2 = "get-ns-test-ns2"
BeforeEach(func() {
By("creating Namespace 1")
namespace1 := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName1,
},
}
Expect(k8sClient.Create(ctx, namespace1)).To(Succeed())
By("creating Namespace 2")
namespace2 := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName2,
},
}
Expect(k8sClient.Create(ctx, namespace2)).To(Succeed())
})
AfterEach(func() {
By("deleting Namespace 1")
namespace1 := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName1,
},
}
Expect(k8sClient.Delete(ctx, namespace1)).To(Succeed())
By("deleting Namespace 2")
namespace2 := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName2,
},
}
Expect(k8sClient.Delete(ctx, namespace2)).To(Succeed())
})
It("should retrieve all namespaces successfully", func() {
By("creating the HTTP request")
req, err := http.NewRequest(http.MethodGet, AllNamespacesPath, http.NoBody)
Expect(err).NotTo(HaveOccurred())
By("setting the auth headers")
req.Header.Set(userIdHeader, adminUser)
By("executing GetNamespacesHandler")
ps := httprouter.Params{}
rr := httptest.NewRecorder()
a.GetNamespacesHandler(rr, req, ps)
rs := rr.Result()
defer rs.Body.Close()
By("verifying the HTTP response status code")
Expect(rs.StatusCode).To(Equal(http.StatusOK), descUnexpectedHTTPStatus, rr.Body.String())
By("reading the HTTP response body")
body, err := io.ReadAll(rs.Body)
Expect(err).NotTo(HaveOccurred())
By("unmarshalling the response JSON to NamespaceListEnvelope")
var response NamespaceListEnvelope
err = json.Unmarshal(body, &response)
Expect(err).NotTo(HaveOccurred())
By("getting the Namespaces from the Kubernetes API")
namespace1 := &corev1.Namespace{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: namespaceName1}, namespace1)).To(Succeed())
namespace2 := &corev1.Namespace{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: namespaceName2}, namespace2)).To(Succeed())
By("ensuring the response contains the expected Namespaces")
// NOTE: we use `ContainElements` instead of `ConsistOf` because envtest creates some namespaces by default
Expect(response.Data).To(ContainElements(
models.NewNamespaceModelFromNamespace(namespace1),
models.NewNamespaceModelFromNamespace(namespace2),
))
})
})
})