Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions workspaces/backend/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,20 @@ func (a *App) Routes() http.Handler {
router.NotFound = http.HandlerFunc(a.notFoundResponse)
router.MethodNotAllowed = http.HandlerFunc(a.methodNotAllowedResponse)

router.GET(HealthCheckPath, a.HealthcheckHandler)
// healthcheck
router.GET(HealthCheckPath, a.GetHealthcheckHandler)

// namespaces
router.GET(AllNamespacesPath, a.GetNamespacesHandler)

// workspaces
router.GET(AllWorkspacesPath, a.GetWorkspacesHandler)
router.GET(WorkspacesByNamespacePath, a.GetWorkspacesHandler)

router.GET(WorkspacesByNamePath, a.GetWorkspaceHandler)
router.POST(WorkspacesByNamespacePath, a.CreateWorkspaceHandler)
router.DELETE(WorkspacesByNamePath, a.DeleteWorkspaceHandler)

// workspacekinds
router.GET(AllWorkspaceKindsPath, a.GetWorkspaceKindsHandler)
router.GET(WorkspaceKindsByNamePath, a.GetWorkspaceKindHandler)

Expand Down
70 changes: 0 additions & 70 deletions workspaces/backend/api/healthcheck__handler_test.go

This file was deleted.

3 changes: 1 addition & 2 deletions workspaces/backend/api/healthcheck_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/julienschmidt/httprouter"
)

func (a *App) HealthcheckHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
func (a *App) GetHealthcheckHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {

healthCheck, err := a.repositories.HealthCheck.HealthCheck(Version)
if err != nil {
Expand All @@ -34,5 +34,4 @@ func (a *App) HealthcheckHandler(w http.ResponseWriter, r *http.Request, ps http
if err != nil {
a.serverErrorResponse(w, r, err)
}

}
85 changes: 85 additions & 0 deletions workspaces/backend/api/healthcheck_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
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"

"github.com/kubeflow/notebooks/workspaces/backend/internal/config"
models "github.com/kubeflow/notebooks/workspaces/backend/internal/models/health_check"
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
)

var _ = Describe("HealthCheck Handler", func() {
var (
a App
)

Context("when backend is healthy", func() {

BeforeEach(func() {
repos := repositories.NewRepositories(k8sClient)
a = App{
Config: config.EnvConfig{
Port: 4000,
},
repositories: repos,
}
})

It("should return a health check response", func() {
By("creating the HTTP request")
req, err := http.NewRequest(http.MethodGet, HealthCheckPath, http.NoBody)
Expect(err).NotTo(HaveOccurred())

By("executing GetHealthCheckHandler")
ps := httprouter.Params{}
rr := httptest.NewRecorder()
a.GetHealthcheckHandler(rr, req, ps)
rs := rr.Result()
defer rs.Body.Close()

By("verifying the HTTP response status code")
Expect(rs.StatusCode).To(Equal(http.StatusOK))

By("reading the HTTP response body")
body, err := io.ReadAll(rs.Body)
Expect(err).NotTo(HaveOccurred())

By("unmarshalling the response JSON to HealthCheck")
var response models.HealthCheck
err = json.Unmarshal(body, &response)
Expect(err).NotTo(HaveOccurred())

By("ensuring that the health check is as expected")
expected := models.HealthCheck{
Status: models.ServiceStatusHealthy,
SystemInfo: models.SystemInfo{
Version: Version,
},
}
Expect(response).To(BeComparableTo(expected))
})
})
})
4 changes: 2 additions & 2 deletions workspaces/backend/api/namespaces_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (

"github.com/julienschmidt/httprouter"

"github.com/kubeflow/notebooks/workspaces/backend/internal/models"
models "github.com/kubeflow/notebooks/workspaces/backend/internal/models/namespaces"
)

type NamespacesEnvelope Envelope[[]models.NamespaceModel]
type NamespacesEnvelope Envelope[[]models.Namespace]

func (a *App) GetNamespacesHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {

Expand Down
78 changes: 41 additions & 37 deletions workspaces/backend/api/namespaces_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,70 +22,66 @@ import (
"net/http"
"net/http/httptest"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"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"

"github.com/kubeflow/notebooks/workspaces/backend/internal/config"
"github.com/kubeflow/notebooks/workspaces/backend/internal/models"
models "github.com/kubeflow/notebooks/workspaces/backend/internal/models/namespaces"
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
)

var _ = Describe("Namespaces Handler", func() {
var (
a App
testRouter *httprouter.Router
a App
)

BeforeEach(func() {
repos := repositories.NewRepositories(k8sClient)
a = App{
Config: config.EnvConfig{
Port: 4000,
},
repositories: repos,
}

testRouter = httprouter.New()
testRouter.GET("/api/namespaces", a.GetNamespacesHandler)
})
// 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() {

Context("when namespaces exist", func() {
const namespaceName1 = "namespaceone"
const namespaceName2 = "namespacetwo"
const namespaceName1 = "get-ns-test-ns1"
const namespaceName2 = "get-ns-test-ns2"

BeforeEach(func() {
By("creating namespaces")
repos := repositories.NewRepositories(k8sClient)
a = App{
Config: config.EnvConfig{
Port: 4000,
},
repositories: repos,
}

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 namespaces")
By("deleting the namespace1")
By("deleting Namespace 1")
namespace1 := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName1,
},
}
Expect(k8sClient.Delete(ctx, namespace1)).To(Succeed())

By("deleting the namespace2")
By("deleting Namespace 2")
namespace2 := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName2,
Expand All @@ -96,32 +92,40 @@ var _ = Describe("Namespaces Handler", func() {

It("should retrieve all namespaces successfully", func() {
By("creating the HTTP request")
req, err := http.NewRequest(http.MethodGet, "/api/namespaces", http.NoBody)
Expect(err).NotTo(HaveOccurred(), "Failed to create HTTP request")
req, err := http.NewRequest(http.MethodGet, AllNamespacesPath, http.NoBody)
Expect(err).NotTo(HaveOccurred())

By("executing GetNamespacesHandler")
ps := httprouter.Params{}
rr := httptest.NewRecorder()
testRouter.ServeHTTP(rr, req)
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), "Expected HTTP status 200 OK")
Expect(rs.StatusCode).To(Equal(http.StatusOK))

By("reading the HTTP response body")
body, err := io.ReadAll(rs.Body)
Expect(err).NotTo(HaveOccurred(), "Failed to read HTTP response body")
Expect(err).NotTo(HaveOccurred())

By("unmarshalling the response JSON")
By("unmarshalling the response JSON to NamespacesEnvelope")
var response NamespacesEnvelope
err = json.Unmarshal(body, &response)
Expect(err).NotTo(HaveOccurred(), "Error unmarshalling response JSON")
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("asserting that the created namespaces are in the response")
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.NamespaceModel{Name: namespaceName1},
models.NamespaceModel{Name: namespaceName2},
), "Expected created namespaces to be in the response")
models.NewNamespaceModelFromNamespace(namespace1),
models.NewNamespaceModelFromNamespace(namespace2),
))
})
})
})
11 changes: 6 additions & 5 deletions workspaces/backend/api/workspacekinds_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import (

"github.com/julienschmidt/httprouter"

"github.com/kubeflow/notebooks/workspaces/backend/internal/models"
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
models "github.com/kubeflow/notebooks/workspaces/backend/internal/models/workspacekinds"
repository "github.com/kubeflow/notebooks/workspaces/backend/internal/repositories/workspacekinds"
)

type WorkspaceKindsEnvelope Envelope[[]models.WorkspaceKindModel]
type WorkspaceKindEnvelope Envelope[models.WorkspaceKindModel]
type WorkspaceKindsEnvelope Envelope[[]models.WorkspaceKind]

type WorkspaceKindEnvelope Envelope[models.WorkspaceKind]

func (a *App) GetWorkspaceKindHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
name := ps.ByName("name")
Expand All @@ -40,7 +41,7 @@ func (a *App) GetWorkspaceKindHandler(w http.ResponseWriter, r *http.Request, ps

workspaceKind, err := a.repositories.WorkspaceKind.GetWorkspaceKind(r.Context(), name)
if err != nil {
if errors.Is(err, repositories.ErrWorkspaceKindNotFound) {
if errors.Is(err, repository.ErrWorkspaceKindNotFound) {
a.notFoundResponse(w, r)
return
}
Expand Down
Loading