Skip to content

Commit 75d632e

Browse files
authored
Test: unit tests for utils (#101)
* test: unit tests for utils * go mod tidy
1 parent 70e0d37 commit 75d632e

File tree

4 files changed

+271
-135
lines changed

4 files changed

+271
-135
lines changed

controllers/instascale_test.go

Lines changed: 0 additions & 49 deletions
This file was deleted.

controllers/utils_test.go

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
package controllers
2+
3+
import (
4+
"testing"
5+
6+
"fmt"
7+
"github.com/onsi/gomega"
8+
machinev1 "github.com/openshift/api/machine/v1beta1"
9+
10+
arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
11+
clusterstateapi "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/controller/clusterstate/api"
12+
v1 "k8s.io/api/core/v1"
13+
"k8s.io/apimachinery/pkg/api/resource"
14+
"k8s.io/apimachinery/pkg/runtime"
15+
)
16+
17+
func TestGetPodResourcesWithReplicas(t *testing.T) {
18+
g := gomega.NewGomegaWithT(t)
19+
20+
tests := []struct {
21+
name string
22+
pod arbv1.CustomPodResourceTemplate
23+
wantRes *clusterstateapi.Resource
24+
wantRep int
25+
}{
26+
{
27+
name: "Empty requests and limits",
28+
pod: arbv1.CustomPodResourceTemplate{
29+
Replicas: 0,
30+
Requests: v1.ResourceList{},
31+
Limits: v1.ResourceList{},
32+
},
33+
wantRes: &clusterstateapi.Resource{
34+
MilliCPU: 0,
35+
Memory: 0,
36+
GPU: 0,
37+
},
38+
wantRep: 0,
39+
},
40+
{
41+
name: "Requests and limits",
42+
pod: arbv1.CustomPodResourceTemplate{
43+
Replicas: 3,
44+
Requests: v1.ResourceList{
45+
v1.ResourceCPU: resource.MustParse("1"),
46+
v1.ResourceMemory: resource.MustParse("2Gi"),
47+
v1.ResourceName(clusterstateapi.GPUResourceName): resource.MustParse("1"),
48+
},
49+
Limits: v1.ResourceList{
50+
v1.ResourceCPU: resource.MustParse("2"),
51+
v1.ResourceMemory: resource.MustParse("2Gi"),
52+
v1.ResourceName(clusterstateapi.GPUResourceName): resource.MustParse("2"),
53+
},
54+
},
55+
wantRes: &clusterstateapi.Resource{
56+
MilliCPU: 1000,
57+
Memory: 2 * 1024 * 1024 * 1024,
58+
GPU: 1,
59+
},
60+
wantRep: 3,
61+
},
62+
}
63+
64+
for _, tt := range tests {
65+
t.Run(tt.name, func(t *testing.T) {
66+
res, rep := getPodResourcesWithReplicas(tt.pod)
67+
g.Expect(res).To(gomega.Equal(tt.wantRes))
68+
g.Expect(rep).To(gomega.Equal(tt.wantRep))
69+
})
70+
}
71+
}
72+
73+
func TestGetListOfPodResourcesFromOneGenericItem(t *testing.T) {
74+
g := gomega.NewGomegaWithT(t)
75+
76+
tests := []struct {
77+
name string
78+
awr *arbv1.AppWrapperGenericResource
79+
want []*clusterstateapi.Resource
80+
}{
81+
{
82+
name: "Empty requests and limits",
83+
awr: &arbv1.AppWrapperGenericResource{
84+
CustomPodResources: []arbv1.CustomPodResourceTemplate{
85+
{
86+
Replicas: 0,
87+
Requests: v1.ResourceList{},
88+
Limits: v1.ResourceList{},
89+
},
90+
},
91+
},
92+
want: []*clusterstateapi.Resource{},
93+
},
94+
{
95+
name: "Request and Limits",
96+
awr: &arbv1.AppWrapperGenericResource{
97+
GenericTemplate: runtime.RawExtension{
98+
Raw: []byte(`{"customPodResources": [{"replicas": 1, "requests": {"gpu": "0", "memory": "1Gi"}, "limits": {"gpu": "0.5", "memory": "2Gi"}}]}`),
99+
},
100+
CustomPodResources: []arbv1.CustomPodResourceTemplate{
101+
{
102+
Replicas: 1,
103+
Requests: v1.ResourceList{
104+
v1.ResourceMemory: resource.MustParse("2Gi"),
105+
v1.ResourceName(clusterstateapi.GPUResourceName): resource.MustParse("1"),
106+
},
107+
Limits: v1.ResourceList{
108+
v1.ResourceMemory: resource.MustParse("2Gi"),
109+
v1.ResourceName(clusterstateapi.GPUResourceName): resource.MustParse("2"),
110+
},
111+
},
112+
},
113+
},
114+
want: []*clusterstateapi.Resource{
115+
{
116+
GPU: 1,
117+
Memory: 2 * 1024 * 1024 * 1024,
118+
},
119+
},
120+
},
121+
}
122+
123+
for _, tt := range tests {
124+
t.Run(tt.name, func(t *testing.T) {
125+
result, _ := GetListOfPodResourcesFromOneGenericItem(tt.awr)
126+
g.Expect(len(result)).To(gomega.Equal(len(tt.want)))
127+
for i := range result {
128+
g.Expect(result[i]).To(gomega.Equal(tt.want[i]))
129+
}
130+
})
131+
}
132+
}
133+
134+
func TestProviderSpecFromRawExtension(t *testing.T) {
135+
g := gomega.NewGomegaWithT(t)
136+
137+
tests := []struct {
138+
name string
139+
rawExtension *runtime.RawExtension
140+
want *machinev1.AWSMachineProviderConfig
141+
wantErr error
142+
}{
143+
{
144+
name: "Empty raw extension",
145+
rawExtension: nil,
146+
want: &machinev1.AWSMachineProviderConfig{},
147+
wantErr: nil,
148+
},
149+
{
150+
name: "valid raw extension",
151+
rawExtension: &runtime.RawExtension{
152+
Raw: []byte(`{"instanceType": "m4.xlarge"}`),
153+
},
154+
want: &machinev1.AWSMachineProviderConfig{
155+
InstanceType: "m4.xlarge",
156+
},
157+
wantErr: nil,
158+
},
159+
{
160+
name: "invalid raw extension",
161+
rawExtension: &runtime.RawExtension{
162+
Raw: []byte(`{"ami"}, "instanceType": "m4.xlarge"}`),
163+
},
164+
want: nil,
165+
wantErr: fmt.Errorf("error unmarshalling providerSpec: invalid character '}' after object key"),
166+
},
167+
}
168+
169+
for _, tt := range tests {
170+
t.Run(tt.name, func(t *testing.T) {
171+
result, err := ProviderSpecFromRawExtension(tt.rawExtension)
172+
if err != nil {
173+
g.Expect(err).To(gomega.Equal(tt.wantErr))
174+
} else {
175+
g.Expect(err).ToNot(gomega.HaveOccurred())
176+
g.Expect(result).To(gomega.Equal(tt.want))
177+
}
178+
})
179+
}
180+
}
181+
182+
func TestNewClientBuilder(t *testing.T) {
183+
g := gomega.NewGomegaWithT(t)
184+
185+
tests := []struct {
186+
name string
187+
kubeconfig string
188+
wantErr bool
189+
}{
190+
{
191+
name: "empty kubeconfig",
192+
kubeconfig: "",
193+
wantErr: true,
194+
},
195+
}
196+
197+
for _, tt := range tests {
198+
t.Run(tt.name, func(t *testing.T) {
199+
result, err := NewClientBuilder(tt.kubeconfig)
200+
g.Expect(err).To(gomega.HaveOccurred())
201+
g.Expect(result).To(gomega.BeNil())
202+
})
203+
}
204+
}
205+
206+
func TestGetRestConfig(t *testing.T) {
207+
g := gomega.NewGomegaWithT(t)
208+
209+
tests := []struct {
210+
name string
211+
kubeconfig string
212+
wantErr bool
213+
}{
214+
{
215+
name: "invalid kubeconfig",
216+
kubeconfig: "/path-to-invalid-kube/config",
217+
wantErr: true,
218+
},
219+
{
220+
name: "empty kubeconfig",
221+
kubeconfig: "",
222+
wantErr: true,
223+
},
224+
}
225+
226+
for _, tt := range tests {
227+
t.Run(tt.name, func(t *testing.T) {
228+
result, err := getRestConfig(tt.kubeconfig)
229+
g.Expect(err).To(gomega.HaveOccurred())
230+
g.Expect(result).To(gomega.BeNil())
231+
})
232+
}
233+
}
234+
235+
func TestContains(t *testing.T) {
236+
g := gomega.NewGomegaWithT(t)
237+
238+
tests := []struct {
239+
name string
240+
s []string
241+
str string
242+
want bool
243+
}{
244+
{
245+
name: "value is present",
246+
s: []string{"test1", "test2", "test3"},
247+
str: "test2",
248+
want: true,
249+
},
250+
{
251+
name: "value is not present",
252+
s: []string{"test1", "test2", "test3"},
253+
str: "test4",
254+
want: false,
255+
},
256+
{
257+
name: "empty slice",
258+
s: []string{},
259+
str: "test1",
260+
want: false,
261+
},
262+
}
263+
264+
for _, tt := range tests {
265+
t.Run(tt.name, func(t *testing.T) {
266+
result := contains(tt.s, tt.str)
267+
g.Expect(result).To(gomega.Equal(tt.want))
268+
})
269+
}
270+
}

go.mod

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/project-codeflare/instascale
33
go 1.18
44

55
require (
6+
github.com/onsi/gomega v1.19.0
67
github.com/openshift-online/ocm-sdk-go v0.1.327
78
github.com/openshift/api v0.0.0-20220411210816-c3bb724c282a
89
github.com/openshift/client-go v0.0.0-20211209144617-7385dd6338e3
@@ -33,33 +34,21 @@ require (
3334
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
3435
github.com/beorn7/perks v1.0.1 // indirect
3536
github.com/cespare/xxhash/v2 v2.1.2 // indirect
36-
github.com/cilium/ebpf v0.10.0 // indirect
37-
github.com/cosiner/argv v0.1.0 // indirect
38-
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
3937
github.com/davecgh/go-spew v1.1.1 // indirect
40-
github.com/derekparker/trie v0.0.0-20221221181808-1424fce0c981 // indirect
4138
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
4239
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
4340
github.com/fsnotify/fsnotify v1.5.1 // indirect
44-
github.com/go-delve/delve v1.20.2 // indirect
45-
github.com/go-delve/liner v1.2.3-0.20220127212407-d32d89dd2a5d // indirect
4641
github.com/go-logr/logr v1.2.0 // indirect
4742
github.com/go-logr/zapr v1.2.0 // indirect
4843
github.com/gogo/protobuf v1.3.2 // indirect
4944
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
5045
github.com/golang/protobuf v1.5.2 // indirect
5146
github.com/google/go-cmp v0.5.9 // indirect
52-
github.com/google/go-dap v0.9.1 // indirect
5347
github.com/google/gofuzz v1.1.0 // indirect
5448
github.com/google/uuid v1.3.0 // indirect
5549
github.com/googleapis/gnostic v0.5.5 // indirect
56-
github.com/hashicorp/golang-lru v0.5.4 // indirect
5750
github.com/imdario/mergo v0.3.12 // indirect
58-
github.com/inconshreveable/mousetrap v1.1.0 // indirect
5951
github.com/json-iterator/go v1.1.12 // indirect
60-
github.com/mattn/go-colorable v0.1.13 // indirect
61-
github.com/mattn/go-isatty v0.0.19 // indirect
62-
github.com/mattn/go-runewidth v0.0.14 // indirect
6352
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
6453
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
6554
github.com/modern-go/reflect2 v1.0.2 // indirect
@@ -68,17 +57,10 @@ require (
6857
github.com/prometheus/client_model v0.3.0 // indirect
6958
github.com/prometheus/common v0.37.0 // indirect
7059
github.com/prometheus/procfs v0.8.0 // indirect
71-
github.com/rivo/uniseg v0.4.4 // indirect
72-
github.com/russross/blackfriday/v2 v2.1.0 // indirect
73-
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
74-
github.com/sirupsen/logrus v1.9.3 // indirect
75-
github.com/spf13/cobra v1.7.0 // indirect
7660
github.com/spf13/pflag v1.0.5 // indirect
77-
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
7861
go.uber.org/atomic v1.7.0 // indirect
7962
go.uber.org/multierr v1.6.0 // indirect
8063
go.uber.org/zap v1.19.1 // indirect
81-
golang.org/x/arch v0.3.0 // indirect
8264
golang.org/x/crypto v0.6.0 // indirect
8365
golang.org/x/net v0.7.0 // indirect
8466
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect

0 commit comments

Comments
 (0)