@@ -11,7 +11,11 @@ import (
11
11
"time"
12
12
13
13
gomock "github.com/golang/mock/gomock"
14
+ "github.com/google/uuid"
14
15
embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
16
+ "github.com/replicatedhq/embedded-cluster/kinds/types/join"
17
+ versionTypes "github.com/replicatedhq/kots/pkg/api/version/types"
18
+ appTypes "github.com/replicatedhq/kots/pkg/app/types"
15
19
"github.com/replicatedhq/kots/pkg/handlers/kubeclient"
16
20
"github.com/replicatedhq/kots/pkg/store"
17
21
mockstore "github.com/replicatedhq/kots/pkg/store/mock"
@@ -33,13 +37,15 @@ type testNodeJoinCommandHarness struct {
33
37
token string
34
38
getRoles func (t * testing.T , token string ) ([]string , error )
35
39
embeddedClusterID string
36
- validateBody func (t * testing.T , h * testNodeJoinCommandHarness , r * GetEmbeddedClusterNodeJoinCommandResponse )
40
+ appVersionLabel string
41
+ validateBody func (t * testing.T , h * testNodeJoinCommandHarness , r * join.JoinCommandResponse )
37
42
}
38
43
39
44
func TestGetEmbeddedClusterNodeJoinCommand (t * testing.T ) {
40
45
scheme := runtime .NewScheme ()
41
46
corev1 .AddToScheme (scheme )
42
47
embeddedclusterv1beta1 .AddToScheme (scheme )
48
+ ecUUID := uuid .New ().String ()
43
49
44
50
tests := []testNodeJoinCommandHarness {
45
51
{
@@ -50,15 +56,15 @@ func TestGetEmbeddedClusterNodeJoinCommand(t *testing.T) {
50
56
{
51
57
name : "store returns error" ,
52
58
httpStatus : http .StatusInternalServerError ,
53
- embeddedClusterID : "cluster-id" ,
59
+ embeddedClusterID : ecUUID ,
54
60
getRoles : func (* testing.T , string ) ([]string , error ) {
55
61
return nil , fmt .Errorf ("some error" )
56
62
},
57
63
},
58
64
{
59
65
name : "store gets passed the provided token" ,
60
66
httpStatus : http .StatusInternalServerError ,
61
- embeddedClusterID : "cluster-id" ,
67
+ embeddedClusterID : ecUUID ,
62
68
token : "some-token" ,
63
69
getRoles : func (t * testing.T , token string ) ([]string , error ) {
64
70
require .Equal (t , "some-token" , token )
@@ -68,8 +74,9 @@ func TestGetEmbeddedClusterNodeJoinCommand(t *testing.T) {
68
74
{
69
75
name : "bootstrap token secret creation succeeds and it matches returned K0SToken" ,
70
76
httpStatus : http .StatusOK ,
71
- embeddedClusterID : "cluster-id" ,
72
- validateBody : func (t * testing.T , h * testNodeJoinCommandHarness , r * GetEmbeddedClusterNodeJoinCommandResponse ) {
77
+ embeddedClusterID : ecUUID ,
78
+ appVersionLabel : "test-app-version" ,
79
+ validateBody : func (t * testing.T , h * testNodeJoinCommandHarness , r * join.JoinCommandResponse ) {
73
80
req := require .New (t )
74
81
// Check that a secret was created with the cluster bootstrap token
75
82
var secrets corev1.SecretList
@@ -91,7 +98,11 @@ func TestGetEmbeddedClusterNodeJoinCommand(t *testing.T) {
91
98
decompressed , err := util .GunzipData (decodedK0SToken )
92
99
req .NoError (err )
93
100
94
- require .Containsf (t , string (decompressed ), fmt .Sprintf ("token: %s" , expectedToken ), "expected K0sToken:\n %s\n to contain the generated bootstrap token: %s" , string (decompressed ), expectedToken )
101
+ req .Containsf (string (decompressed ), fmt .Sprintf ("token: %s" , expectedToken ), "expected K0sToken:\n %s\n to contain the generated bootstrap token: %s" , string (decompressed ), expectedToken )
102
+
103
+ // returned embedded cluster and app version should match the expected values
104
+ req .Equal (r .AppVersionLabel , "test-app-version" )
105
+ req .Equal (r .ClusterID .String (), ecUUID )
95
106
},
96
107
kbClient : fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (
97
108
& embeddedclusterv1beta1.Installation {
@@ -100,6 +111,7 @@ func TestGetEmbeddedClusterNodeJoinCommand(t *testing.T) {
100
111
},
101
112
Spec : embeddedclusterv1beta1.InstallationSpec {
102
113
BinaryName : "my-app" ,
114
+ ClusterID : ecUUID ,
103
115
Config : & embeddedclusterv1beta1.ConfigSpec {
104
116
Version : "v1.100.0" ,
105
117
Roles : embeddedclusterv1beta1.Roles {
@@ -144,8 +156,9 @@ func TestGetEmbeddedClusterNodeJoinCommand(t *testing.T) {
144
156
{
145
157
name : "tcp connections required are returned based on the controller role provided" ,
146
158
httpStatus : http .StatusOK ,
147
- embeddedClusterID : "cluster-id" ,
148
- validateBody : func (t * testing.T , h * testNodeJoinCommandHarness , r * GetEmbeddedClusterNodeJoinCommandResponse ) {
159
+ embeddedClusterID : ecUUID ,
160
+ appVersionLabel : "test-app-version" ,
161
+ validateBody : func (t * testing.T , h * testNodeJoinCommandHarness , r * join.JoinCommandResponse ) {
149
162
req := require .New (t )
150
163
151
164
req .Equal ([]string {
@@ -166,6 +179,7 @@ func TestGetEmbeddedClusterNodeJoinCommand(t *testing.T) {
166
179
},
167
180
Spec : embeddedclusterv1beta1.InstallationSpec {
168
181
BinaryName : "my-app" ,
182
+ ClusterID : ecUUID ,
169
183
Config : & embeddedclusterv1beta1.ConfigSpec {
170
184
Version : "v1.100.0" ,
171
185
Roles : embeddedclusterv1beta1.Roles {
@@ -227,6 +241,65 @@ func TestGetEmbeddedClusterNodeJoinCommand(t *testing.T) {
227
241
},
228
242
).Build (),
229
243
},
244
+ {
245
+ name : "If there is no installed app, the app version label should be empty" ,
246
+ httpStatus : http .StatusOK ,
247
+ embeddedClusterID : ecUUID ,
248
+ validateBody : func (t * testing.T , h * testNodeJoinCommandHarness , r * join.JoinCommandResponse ) {
249
+ req := require .New (t )
250
+ // returned embedded cluster and app version should match the expected values
251
+ req .Equal (r .AppVersionLabel , "" )
252
+ req .Equal (r .ClusterID .String (), ecUUID )
253
+ },
254
+ kbClient : fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (
255
+ & embeddedclusterv1beta1.Installation {
256
+ ObjectMeta : metav1.ObjectMeta {
257
+ Name : time .Now ().Format ("20060102150405" ),
258
+ },
259
+ Spec : embeddedclusterv1beta1.InstallationSpec {
260
+ BinaryName : "my-app" ,
261
+ ClusterID : ecUUID ,
262
+ Config : & embeddedclusterv1beta1.ConfigSpec {
263
+ Version : "v1.100.0" ,
264
+ Roles : embeddedclusterv1beta1.Roles {
265
+ Controller : embeddedclusterv1beta1.NodeRole {
266
+ Name : "controller-role" ,
267
+ },
268
+ },
269
+ },
270
+ },
271
+ },
272
+ & corev1.ConfigMap {
273
+ ObjectMeta : metav1.ObjectMeta {
274
+ Name : "kube-root-ca.crt" ,
275
+ Namespace : "kube-system" ,
276
+ },
277
+ Data : map [string ]string {"ca.crt" : "some-ca-cert" },
278
+ },
279
+ & corev1.Node {
280
+ ObjectMeta : metav1.ObjectMeta {
281
+ Name : "controller 1" ,
282
+ Labels : map [string ]string {
283
+ "node-role.kubernetes.io/control-plane" : "true" ,
284
+ },
285
+ },
286
+ Status : corev1.NodeStatus {
287
+ Conditions : []corev1.NodeCondition {
288
+ {
289
+ Type : corev1 .NodeReady ,
290
+ Status : corev1 .ConditionTrue ,
291
+ },
292
+ },
293
+ Addresses : []corev1.NodeAddress {
294
+ {
295
+ Type : corev1 .NodeInternalIP ,
296
+ Address : "192.168.0.100" ,
297
+ },
298
+ },
299
+ },
300
+ },
301
+ ).Build (),
302
+ },
230
303
}
231
304
for _ , test := range tests {
232
305
t .Run (test .name , func (t * testing.T ) {
@@ -251,6 +324,24 @@ func TestGetEmbeddedClusterNodeJoinCommand(t *testing.T) {
251
324
return []string {"controller-role" , "worker-role" }, nil
252
325
})
253
326
327
+ if test .appVersionLabel != "" {
328
+ mockStore .EXPECT ().ListInstalledApps ().AnyTimes ().DoAndReturn (func () ([]* appTypes.App , error ) {
329
+ return []* appTypes.App {
330
+ {
331
+ ID : "test-app-id" ,
332
+ CurrentSequence : 1 ,
333
+ },
334
+ }, nil
335
+ })
336
+ mockStore .EXPECT ().GetAppVersion ("test-app-id" , int64 (1 )).AnyTimes ().DoAndReturn (func (appID string , sequence int64 ) (* versionTypes.AppVersion , error ) {
337
+ return & versionTypes.AppVersion {
338
+ VersionLabel : test .appVersionLabel ,
339
+ }, nil
340
+ })
341
+ } else if test .httpStatus == http .StatusOK {
342
+ mockStore .EXPECT ().ListInstalledApps ().AnyTimes ().Return ([]* appTypes.App {}, nil )
343
+ }
344
+
254
345
// There's an early check in the handler for the presence of `EMBEDDED_CLUSTER_ID` env var
255
346
// so we need to set it here whenever the test requires it
256
347
if test .embeddedClusterID != "" {
@@ -274,7 +365,7 @@ func TestGetEmbeddedClusterNodeJoinCommand(t *testing.T) {
274
365
}
275
366
276
367
// Run the body validation function if provided
277
- var body GetEmbeddedClusterNodeJoinCommandResponse
368
+ var body join. JoinCommandResponse
278
369
req .NoError (json .NewDecoder (response .Body ).Decode (& body ))
279
370
if test .validateBody != nil {
280
371
test .validateBody (t , & test , & body )
0 commit comments