Skip to content

Commit 3e8fb25

Browse files
committed
feat: bugfix of initialize function
Signed-off-by: hlts2 <[email protected]>
1 parent aa3682d commit 3e8fb25

File tree

2 files changed

+152
-2
lines changed

2 files changed

+152
-2
lines changed

pkg/watcher/watcher.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/civo/civogo"
1111
corev1 "k8s.io/api/core/v1"
1212
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13-
1413
"k8s.io/client-go/kubernetes"
1514
"k8s.io/client-go/rest"
1615
"k8s.io/client-go/tools/clientcmd"
@@ -43,7 +42,12 @@ type watcher struct {
4342
}
4443

4544
func NewWatcher(ctx context.Context, apiURL, apiKey, region, clusterID, nodePoolID, nodeDesiredGPUCount string, opts ...Option) (Watcher, error) {
46-
w := new(watcher)
45+
w := &watcher{
46+
clusterID: clusterID,
47+
apiKey: apiKey,
48+
apiURL: apiURL,
49+
region: region,
50+
}
4751
for _, opt := range append(defaultOptions, opts...) {
4852
opt(w)
4953
}

pkg/watcher/watcher_test.go

+146
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package watcher
22

33
import (
44
"errors"
5+
"fmt"
6+
"strconv"
57
"testing"
68

79
"github.com/civo/civogo"
@@ -22,6 +24,150 @@ var (
2224
testNodeDesiredGPUCount = "8"
2325
)
2426

27+
func TestNew(t *testing.T) {
28+
type args struct {
29+
clusterID string
30+
region string
31+
apiKey string
32+
apiURL string
33+
nodePoolID string
34+
nodeDesiredGPUCount string
35+
opts []Option
36+
}
37+
type test struct {
38+
name string
39+
args args
40+
checkFunc func(*watcher) error
41+
wantErr bool
42+
}
43+
44+
tests := []test{
45+
{
46+
name: "Returns no error when given valid input",
47+
args: args{
48+
clusterID: testClusterID,
49+
region: testRegion,
50+
apiKey: testApiKey,
51+
apiURL: testApiURL,
52+
nodePoolID: testNodePoolID,
53+
nodeDesiredGPUCount: testNodeDesiredGPUCount,
54+
opts: []Option{
55+
WithKubernetesClient(fake.NewSimpleClientset()),
56+
WithCivoClient(&FakeClient{}),
57+
},
58+
},
59+
checkFunc: func(w *watcher) error {
60+
if w.clusterID != testClusterID {
61+
return fmt.Errorf("clusterID mismatch: got %s, want %s", w.clusterID, testClusterID)
62+
}
63+
if w.region != testRegion {
64+
return fmt.Errorf("region mismatch: got %s, want %s", w.region, testRegion)
65+
}
66+
if w.apiKey != testApiKey {
67+
return fmt.Errorf("apiKey mismatch: got %s, want %s", w.apiKey, testApiKey)
68+
}
69+
if w.apiURL != testApiURL {
70+
return fmt.Errorf("apiURL mismatch: got %s, want %s", w.apiURL, testApiURL)
71+
}
72+
73+
cnt, err := strconv.Atoi(testNodeDesiredGPUCount)
74+
if err != nil {
75+
return err
76+
}
77+
if w.nodeDesiredGPUCount != cnt {
78+
return fmt.Errorf("nodeDesiredGPUCount mismatch: got %d, want %s", w.nodeDesiredGPUCount, testNodeDesiredGPUCount)
79+
}
80+
if w.nodeSelector == nil || w.nodeSelector.MatchLabels[nodePoolLabelKey] != testNodePoolID {
81+
return fmt.Errorf("nodeSelector mismatch: got %v, want %s", w.nodeSelector, testNodePoolID)
82+
}
83+
if w.client == nil {
84+
return fmt.Errorf("client is nil")
85+
}
86+
if w.civoClient == nil {
87+
return fmt.Errorf("civoClient is nil")
88+
}
89+
return nil
90+
},
91+
},
92+
{
93+
name: "Returns an error when clusterID is missing",
94+
args: args{
95+
region: testRegion,
96+
apiKey: testApiKey,
97+
apiURL: testApiURL,
98+
nodePoolID: testNodePoolID,
99+
nodeDesiredGPUCount: testNodeDesiredGPUCount,
100+
opts: []Option{
101+
WithKubernetesClient(fake.NewSimpleClientset()),
102+
WithCivoClient(&FakeClient{}),
103+
},
104+
},
105+
wantErr: true,
106+
},
107+
{
108+
name: "Returns an error when nodeDesiredGPUCount is invalid",
109+
args: args{
110+
clusterID: testClusterID,
111+
region: testRegion,
112+
apiKey: testApiKey,
113+
apiURL: testApiURL,
114+
nodePoolID: testNodePoolID,
115+
nodeDesiredGPUCount: "invalid_number",
116+
opts: []Option{
117+
WithKubernetesClient(fake.NewSimpleClientset()),
118+
WithCivoClient(&FakeClient{}),
119+
},
120+
},
121+
wantErr: true,
122+
},
123+
{
124+
name: "Returns an error when nodeDesiredGPUCount is 0",
125+
args: args{
126+
clusterID: testClusterID,
127+
region: testRegion,
128+
apiKey: testApiKey,
129+
apiURL: testApiURL,
130+
nodePoolID: testNodePoolID,
131+
nodeDesiredGPUCount: "0",
132+
opts: []Option{
133+
WithKubernetesClient(fake.NewSimpleClientset()),
134+
WithCivoClient(&FakeClient{}),
135+
},
136+
},
137+
wantErr: true,
138+
},
139+
}
140+
141+
for _, test := range tests {
142+
t.Run(test.name, func(t *testing.T) {
143+
w, err := NewWatcher(t.Context(),
144+
test.args.apiURL,
145+
test.args.apiKey,
146+
test.args.region,
147+
test.args.clusterID,
148+
test.args.nodePoolID,
149+
test.args.nodeDesiredGPUCount,
150+
test.args.opts...)
151+
if (err != nil) != test.wantErr {
152+
t.Errorf("error = %v, wantErr %v", err, test.wantErr)
153+
}
154+
155+
if !test.wantErr {
156+
if w == nil {
157+
t.Errorf("expected non-nil object, but got nil")
158+
return
159+
}
160+
obj := w.(*watcher)
161+
if test.checkFunc != nil {
162+
if err := test.checkFunc(obj); err != nil {
163+
t.Errorf("checkFunc error: %v", err)
164+
}
165+
}
166+
}
167+
})
168+
}
169+
}
170+
25171
func TestRun(t *testing.T) {
26172
type args struct {
27173
opts []Option

0 commit comments

Comments
 (0)