@@ -2,6 +2,8 @@ package watcher
2
2
3
3
import (
4
4
"errors"
5
+ "fmt"
6
+ "strconv"
5
7
"testing"
6
8
7
9
"github.com/civo/civogo"
@@ -22,6 +24,150 @@ var (
22
24
testNodeDesiredGPUCount = "8"
23
25
)
24
26
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
+
25
171
func TestRun (t * testing.T ) {
26
172
type args struct {
27
173
opts []Option
0 commit comments