Skip to content

Commit 2a9f95a

Browse files
author
rafriat
committed
feat(ws): Notebooks 2.0 // Backend // Add tests to repositories/namespaces
Signed-off-by: rafriat <[email protected]>
1 parent 09f8f37 commit 2a9f95a

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

workspaces/backend/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ require (
9393
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
9494
google.golang.org/grpc v1.65.0 // indirect
9595
google.golang.org/protobuf v1.34.2 // indirect
96+
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
9697
gopkg.in/inf.v0 v0.9.1 // indirect
9798
gopkg.in/yaml.v2 v2.4.0 // indirect
9899
gopkg.in/yaml.v3 v3.0.1 // indirect
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Copyright 2024.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package namespaces
18+
19+
import (
20+
"context"
21+
"errors"
22+
"testing"
23+
24+
. "github.com/onsi/ginkgo/v2"
25+
. "github.com/onsi/gomega"
26+
corev1 "k8s.io/api/core/v1"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/apimachinery/pkg/runtime"
29+
"sigs.k8s.io/controller-runtime/pkg/client"
30+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
31+
)
32+
33+
func TestNamespaceRepository(t *testing.T) {
34+
RegisterFailHandler(Fail)
35+
RunSpecs(t, "NamespaceRepository Suite")
36+
}
37+
38+
var _ = Describe("Namespaces", Ordered, func() {
39+
var (
40+
scheme *runtime.Scheme
41+
fakeClient client.Client
42+
namespaceRepo *NamespaceRepository
43+
ctx context.Context
44+
)
45+
BeforeAll(func() {
46+
ctx = context.Background()
47+
scheme = runtime.NewScheme()
48+
Expect(corev1.AddToScheme(scheme)).To(Succeed())
49+
})
50+
Context("with no existing Namespaces", func() {
51+
BeforeEach(func() {
52+
fakeClient = fake.NewClientBuilder().WithScheme(scheme).Build()
53+
namespaceRepo = NewNamespaceRepository(fakeClient)
54+
55+
})
56+
It("should return an empty list of Namespaces", func() {
57+
namespaces, err := namespaceRepo.GetNamespaces(ctx)
58+
Expect(err).NotTo(HaveOccurred())
59+
Expect(namespaces).To(BeEmpty())
60+
})
61+
})
62+
Context("with existing Namespaces", func() {
63+
BeforeEach(func() {
64+
ns1 := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test-ns-1"}}
65+
ns2 := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test-ns-2"}}
66+
67+
fakeClient = fake.NewClientBuilder().WithScheme(scheme).WithObjects(ns1, ns2).Build()
68+
namespaceRepo = NewNamespaceRepository(fakeClient)
69+
70+
})
71+
It("should return all namespaces", func() {
72+
namespaces, err := namespaceRepo.GetNamespaces(ctx)
73+
Expect(err).NotTo(HaveOccurred())
74+
Expect(namespaces).To(HaveLen(2))
75+
Expect(namespaces[0].Name).To(Equal("test-ns-1"))
76+
Expect(namespaces[1].Name).To(Equal("test-ns-2"))
77+
})
78+
})
79+
Context("when client.List returns an error", func() {
80+
BeforeEach(func() {
81+
fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build()
82+
errorClient := &erroringClient{Client: fakeClient}
83+
namespaceRepo = NewNamespaceRepository(errorClient)
84+
})
85+
86+
It("should return an error", func() {
87+
namespaces, err := namespaceRepo.GetNamespaces(ctx)
88+
Expect(err).To(HaveOccurred())
89+
var mockErr *MockError
90+
Expect(errors.As(err, &mockErr)).To(BeTrue())
91+
Expect(namespaces).To(BeNil())
92+
})
93+
})
94+
})
95+
96+
// Define a wrapper client that forces an error on List() method of client
97+
type erroringClient struct {
98+
client.Client
99+
}
100+
101+
type MockError struct {
102+
message string
103+
}
104+
105+
func (e *MockError) Error() string {
106+
return e.message
107+
}
108+
109+
func (e *erroringClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
110+
return &MockError{message: "mocked list error"}
111+
}

0 commit comments

Comments
 (0)