@@ -2,6 +2,7 @@ package kubeutils
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"os"
6
7
"strings"
7
8
"testing"
@@ -12,7 +13,12 @@ import (
12
13
"github.com/go-logr/logr/testr"
13
14
ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
14
15
"github.com/replicatedhq/embedded-cluster/pkg/crds"
16
+ "github.com/replicatedhq/embedded-cluster/pkg/metrics"
17
+ "github.com/replicatedhq/embedded-cluster/pkg/runtimeconfig"
18
+ kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
19
+ "github.com/stretchr/testify/assert"
15
20
"github.com/stretchr/testify/require"
21
+ corev1 "k8s.io/api/core/v1"
16
22
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
17
23
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18
24
"k8s.io/apimachinery/pkg/runtime"
@@ -180,3 +186,178 @@ func TestEnsureInstallationCRD(t *testing.T) {
180
186
})
181
187
}
182
188
}
189
+
190
+ func TestRecordInstallation (t * testing.T ) {
191
+ // Setup the test scheme
192
+ s := runtime .NewScheme ()
193
+ require .NoError (t , apiextensionsv1 .AddToScheme (s ))
194
+ require .NoError (t , ecv1beta1 .AddToScheme (s ))
195
+ require .NoError (t , corev1 .AddToScheme (s ))
196
+
197
+ tests := []struct {
198
+ name string
199
+ opts RecordInstallationOptions
200
+ wantErr bool
201
+ validate func (t * testing.T , installation * ecv1beta1.Installation )
202
+ }{
203
+ {
204
+ name : "online installation without airgap" ,
205
+ opts : RecordInstallationOptions {
206
+ IsAirgap : false ,
207
+ License : & kotsv1beta1.License {
208
+ Spec : kotsv1beta1.LicenseSpec {
209
+ IsDisasterRecoverySupported : true ,
210
+ IsEmbeddedClusterMultiNodeEnabled : false ,
211
+ },
212
+ },
213
+ ConfigSpec : & ecv1beta1.ConfigSpec {
214
+ Version : "1.15.0+k8s-1.30" ,
215
+ },
216
+ MetricsBaseURL : "https://replicated.app" ,
217
+ RuntimeConfig : & ecv1beta1.RuntimeConfigSpec {
218
+ DataDir : "/var/lib/embedded-cluster" ,
219
+ },
220
+ EndUserConfig : & ecv1beta1.Config {
221
+ Spec : ecv1beta1.ConfigSpec {
222
+ UnsupportedOverrides : ecv1beta1.UnsupportedOverrides {
223
+ K0s : "apiVersion: k0s.k0sproject.io/v1beta1\n kind: Cluster" ,
224
+ },
225
+ },
226
+ },
227
+ },
228
+ wantErr : false ,
229
+ validate : func (t * testing.T , installation * ecv1beta1.Installation ) {
230
+ assert .False (t , installation .Spec .AirGap )
231
+ assert .Equal (t , int64 (0 ), installation .Spec .AirgapUncompressedSize )
232
+ assert .Equal (t , "1.15.0+k8s-1.30" , installation .Spec .Config .Version )
233
+ assert .Equal (t , "https://replicated.app" , installation .Spec .MetricsBaseURL )
234
+ assert .Equal (t , "/var/lib/embedded-cluster" , installation .Spec .RuntimeConfig .DataDir )
235
+ assert .Equal (t , "apiVersion: k0s.k0sproject.io/v1beta1\n kind: Cluster" , installation .Spec .EndUserK0sConfigOverrides )
236
+ assert .True (t , installation .Spec .LicenseInfo .IsDisasterRecoverySupported )
237
+ assert .False (t , installation .Spec .LicenseInfo .IsMultiNodeEnabled )
238
+ assert .Equal (t , ecv1beta1 .InstallationStateKubernetesInstalled , installation .Status .State )
239
+ assert .Equal (t , "Kubernetes installed" , installation .Status .Reason )
240
+ },
241
+ },
242
+ {
243
+ name : "airgap installation with uncompressed size" ,
244
+ opts : RecordInstallationOptions {
245
+ IsAirgap : true ,
246
+ License : & kotsv1beta1.License {
247
+ Spec : kotsv1beta1.LicenseSpec {
248
+ IsDisasterRecoverySupported : false ,
249
+ IsEmbeddedClusterMultiNodeEnabled : true ,
250
+ },
251
+ },
252
+ ConfigSpec : & ecv1beta1.ConfigSpec {
253
+ Version : "1.16.0+k8s-1.31" ,
254
+ },
255
+ MetricsBaseURL : "https://staging.replicated.app" ,
256
+ RuntimeConfig : & ecv1beta1.RuntimeConfigSpec {
257
+ DataDir : "/opt/embedded-cluster" ,
258
+ },
259
+ EndUserConfig : nil ,
260
+ AirgapUncompressedSize : 1234567890 ,
261
+ },
262
+ wantErr : false ,
263
+ validate : func (t * testing.T , installation * ecv1beta1.Installation ) {
264
+ assert .True (t , installation .Spec .AirGap )
265
+ assert .Equal (t , int64 (1234567890 ), installation .Spec .AirgapUncompressedSize )
266
+ assert .Equal (t , "1.16.0+k8s-1.31" , installation .Spec .Config .Version )
267
+ assert .Equal (t , "https://staging.replicated.app" , installation .Spec .MetricsBaseURL )
268
+ assert .Equal (t , "/opt/embedded-cluster" , installation .Spec .RuntimeConfig .DataDir )
269
+ assert .Empty (t , installation .Spec .EndUserK0sConfigOverrides )
270
+ assert .False (t , installation .Spec .LicenseInfo .IsDisasterRecoverySupported )
271
+ assert .True (t , installation .Spec .LicenseInfo .IsMultiNodeEnabled )
272
+ assert .Equal (t , ecv1beta1 .InstallationStateKubernetesInstalled , installation .Status .State )
273
+ assert .Equal (t , "Kubernetes installed" , installation .Status .Reason )
274
+ },
275
+ },
276
+ {
277
+ name : "airgap installation with large uncompressed size" ,
278
+ opts : RecordInstallationOptions {
279
+ IsAirgap : true ,
280
+ License : & kotsv1beta1.License {
281
+ Spec : kotsv1beta1.LicenseSpec {
282
+ IsDisasterRecoverySupported : false ,
283
+ IsEmbeddedClusterMultiNodeEnabled : false ,
284
+ },
285
+ },
286
+ ConfigSpec : & ecv1beta1.ConfigSpec {
287
+ Version : "1.18.0+k8s-1.33" ,
288
+ },
289
+ MetricsBaseURL : "https://custom.replicated.app" ,
290
+ RuntimeConfig : & ecv1beta1.RuntimeConfigSpec {
291
+ DataDir : "/custom/data/dir" ,
292
+ },
293
+ EndUserConfig : nil ,
294
+ AirgapUncompressedSize : 9876543210 ,
295
+ },
296
+ wantErr : false ,
297
+ validate : func (t * testing.T , installation * ecv1beta1.Installation ) {
298
+ assert .True (t , installation .Spec .AirGap )
299
+ assert .Equal (t , int64 (9876543210 ), installation .Spec .AirgapUncompressedSize )
300
+ assert .Equal (t , "1.18.0+k8s-1.33" , installation .Spec .Config .Version )
301
+ assert .Equal (t , "https://custom.replicated.app" , installation .Spec .MetricsBaseURL )
302
+ assert .Equal (t , "/custom/data/dir" , installation .Spec .RuntimeConfig .DataDir )
303
+ assert .Empty (t , installation .Spec .EndUserK0sConfigOverrides )
304
+ assert .False (t , installation .Spec .LicenseInfo .IsDisasterRecoverySupported )
305
+ assert .False (t , installation .Spec .LicenseInfo .IsMultiNodeEnabled )
306
+ assert .Equal (t , ecv1beta1 .InstallationStateKubernetesInstalled , installation .Status .State )
307
+ assert .Equal (t , "Kubernetes installed" , installation .Status .Reason )
308
+ },
309
+ },
310
+ }
311
+
312
+ for _ , tt := range tests {
313
+ t .Run (tt .name , func (t * testing.T ) {
314
+ // Setup the test environment
315
+ verbosity := 1
316
+ if os .Getenv ("DEBUG" ) != "" {
317
+ verbosity = 10
318
+ }
319
+ log := testr .NewWithOptions (t , testr.Options {Verbosity : verbosity })
320
+ ctx := logr .NewContext (context .Background (), log )
321
+
322
+ testEnv := & envtest.Environment {}
323
+ cfg , err := testEnv .Start ()
324
+ require .NoError (t , err )
325
+ t .Cleanup (func () { _ = testEnv .Stop () })
326
+
327
+ cli , err := client .New (cfg , client.Options {Scheme : s })
328
+ require .NoError (t , err )
329
+
330
+ // Call the function being tested
331
+ installation , err := RecordInstallation (ctx , cli , tt .opts )
332
+
333
+ if tt .wantErr {
334
+ require .Error (t , err )
335
+ return
336
+ }
337
+ require .NoError (t , err )
338
+ require .NotNil (t , installation )
339
+
340
+ // Verify the installation was created in the cluster
341
+ var resultInstallation ecv1beta1.Installation
342
+ err = cli .Get (ctx , client.ObjectKey {Name : installation .Name }, & resultInstallation )
343
+ require .NoError (t , err )
344
+
345
+ // Run custom validation
346
+ if tt .validate != nil {
347
+ tt .validate (t , & resultInstallation )
348
+ }
349
+
350
+ json , err := json .MarshalIndent (resultInstallation , "" , " " )
351
+ require .NoError (t , err )
352
+ t .Logf ("resultInstallation: %s" , string (json ))
353
+ // Verify common fields
354
+ assert .NotEmpty (t , resultInstallation .Name )
355
+ assert .Equal (t , "" , resultInstallation .APIVersion ) // I expected this to be "embeddedcluster.replicated.com/v1beta1"
356
+ assert .Equal (t , "" , resultInstallation .Kind ) // I expected this to be "Installation"
357
+ assert .Equal (t , metrics .ClusterID ().String (), resultInstallation .Spec .ClusterID )
358
+ assert .Equal (t , runtimeconfig .BinaryName (), resultInstallation .Spec .BinaryName )
359
+ assert .Equal (t , ecv1beta1 .InstallationSourceTypeCRD , resultInstallation .Spec .SourceType )
360
+ assert .Equal (t , "ec-install" , resultInstallation .Labels ["replicated.com/disaster-recovery" ])
361
+ })
362
+ }
363
+ }
0 commit comments