@@ -26,6 +26,10 @@ import (
2626 inv_utils "github.com/open-edge-platform/infra-managers/maintenance/pkg/utils"
2727)
2828
29+ const (
30+ semverVersion100 = "1.0.0"
31+ )
32+
2933func TestMain (m * testing.M ) {
3034 wd , err := os .Getwd ()
3135 if err != nil {
@@ -292,7 +296,7 @@ func TestInvClient_UpdateInstance(t *testing.T) {
292296 // Error - non-existent Instance
293297 t .Run ("ErrorNoInst" , func (t * testing.T ) {
294298 err := invclient .UpdateInstance (ctx , client , mm_testing .Tenant1 , "inst-12345678" ,
295- mm_status .UpdateStatusUpToDate , "" , newOSRes .GetResourceId ())
299+ mm_status .UpdateStatusUpToDate , "" , newOSRes .GetResourceId (), "" )
296300 require .Error (t , err )
297301 sts , _ := status .FromError (err )
298302 assert .Equal (t , codes .NotFound , sts .Code ())
@@ -303,7 +307,7 @@ func TestInvClient_UpdateInstance(t *testing.T) {
303307 t .Run ("UpdateInstStatusNotCurrentOS" , func (t * testing.T ) {
304308 timeBeforeUpdate := time .Now ().Unix ()
305309 err := invclient .UpdateInstance (ctx , client , mm_testing .Tenant1 , inst .ResourceId ,
306- mm_status .UpdateStatusInProgress , "" , "" )
310+ mm_status .UpdateStatusInProgress , "" , "" , "" )
307311
308312 require .NoError (t , err )
309313 updatedInst , err := client .Get (ctx , mm_testing .Tenant1 , inst .ResourceId )
@@ -323,7 +327,7 @@ func TestInvClient_UpdateInstance(t *testing.T) {
323327 require .NoError (t , err )
324328 assert .NotEqual (t , newOSRes .GetSha256 (), beforeUpdateInst .GetResource ().GetInstance ().GetCurrentOs ().GetSha256 ())
325329 err = invclient .UpdateInstance (ctx , client , mm_testing .Tenant1 , inst .ResourceId ,
326- mm_status .UpdateStatusDone , "some update status detail" , newOSRes .GetResourceId ())
330+ mm_status .UpdateStatusDone , "some update status detail" , newOSRes .GetResourceId (), "" )
327331 require .NoError (t , err )
328332 updatedInst , err := client .Get (ctx , mm_testing .Tenant1 , inst .ResourceId )
329333 require .NoError (t , err )
@@ -338,11 +342,11 @@ func TestInvClient_UpdateInstance(t *testing.T) {
338342 t .Run ("UpdateUpdateStatusToRunning" , func (t * testing.T ) {
339343 // initial setup of instance status to running and update status to unknown
340344 err := invclient .UpdateInstance (ctx , client , mm_testing .Tenant1 , inst .ResourceId ,
341- mm_status .UpdateStatusUnknown , "some update status detail" , newOSRes .GetResourceId ())
345+ mm_status .UpdateStatusUnknown , "some update status detail" , newOSRes .GetResourceId (), "" )
342346 require .NoError (t , err )
343347 // setup only the update status as instance status is already set to running
344348 err = invclient .UpdateInstance (ctx , client , mm_testing .Tenant1 , inst .ResourceId ,
345- mm_status .UpdateStatusDone , "some update status detail" , newOSRes .GetResourceId ())
349+ mm_status .UpdateStatusDone , "some update status detail" , newOSRes .GetResourceId (), "" )
346350 require .NoError (t , err )
347351 updatedInst , err := client .Get (ctx , mm_testing .Tenant1 , inst .ResourceId )
348352 require .NoError (t , err )
@@ -427,13 +431,12 @@ func TestInvClient_GetLatestImmutableOSByProfile(t *testing.T) {
427431 dao := inv_testing .NewInvResourceDAOOrFail (t )
428432 ctx := t .Context ()
429433 client := inv_testing .TestClients [inv_testing .RMClient ].GetTenantAwareInventoryClient ()
430- semver100 := "1.0.0"
431434
432435 osRes1 := dao .CreateOsWithOpts (t , mm_testing .Tenant1 , true , func (os * os_v1.OperatingSystemResource ) {
433436 os .Sha256 = inv_testing .GenerateRandomSha256 ()
434437 os .Name = "OS Resource 1"
435438 os .ProfileName = "profile name 1"
436- os .ProfileVersion = semver100
439+ os .ProfileVersion = semverVersion100
437440 os .SecurityFeature = os_v1 .SecurityFeature_SECURITY_FEATURE_NONE
438441 os .OsType = os_v1 .OsType_OS_TYPE_IMMUTABLE
439442 })
@@ -442,7 +445,7 @@ func TestInvClient_GetLatestImmutableOSByProfile(t *testing.T) {
442445 os .Sha256 = inv_testing .GenerateRandomSha256 ()
443446 os .Name = "OS Resource 2"
444447 os .ProfileName = "profile name 2"
445- os .ProfileVersion = semver100
448+ os .ProfileVersion = semverVersion100
446449 os .SecurityFeature = os_v1 .SecurityFeature_SECURITY_FEATURE_NONE
447450 os .OsType = os_v1 .OsType_OS_TYPE_IMMUTABLE
448451 })
@@ -460,7 +463,7 @@ func TestInvClient_GetLatestImmutableOSByProfile(t *testing.T) {
460463 os .Sha256 = inv_testing .GenerateRandomSha256 ()
461464 os .Name = "OS Resource 3"
462465 os .ProfileName = "profile name 3"
463- os .ProfileVersion = semver100
466+ os .ProfileVersion = semverVersion100
464467 os .SecurityFeature = os_v1 .SecurityFeature_SECURITY_FEATURE_NONE
465468 os .OsType = os_v1 .OsType_OS_TYPE_IMMUTABLE
466469 })
@@ -543,3 +546,139 @@ func TestInvClient_GetLatestImmutableOSByProfile(t *testing.T) {
543546 })
544547 }
545548}
549+
550+ // createOSResourceValidator creates a validation function for OS resource comparison.
551+ func createOSResourceValidator (expected * os_v1.OperatingSystemResource ) func (* testing.T , * os_v1.OperatingSystemResource ) {
552+ return func (t * testing.T , osRes * os_v1.OperatingSystemResource ) {
553+ t .Helper ()
554+ assert .Equal (t , expected .GetResourceId (), osRes .GetResourceId ())
555+ assert .Equal (t , expected .GetName (), osRes .GetName ())
556+ assert .Equal (t , expected .GetProfileName (), osRes .GetProfileName ())
557+ assert .Equal (t , expected .GetImageId (), osRes .GetImageId ())
558+ assert .Equal (t , expected .GetProfileVersion (), osRes .GetProfileVersion ())
559+ assert .Equal (t , expected .GetSha256 (), osRes .GetSha256 ())
560+ assert .Equal (t , expected .GetSecurityFeature (), osRes .GetSecurityFeature ())
561+ assert .Equal (t , expected .GetOsType (), osRes .GetOsType ())
562+ }
563+ }
564+
565+ func TestInvClient_GetOSResourceByID (t * testing.T ) {
566+ dao := inv_testing .NewInvResourceDAOOrFail (t )
567+ ctx := context .TODO ()
568+ client := inv_testing .TestClients [inv_testing .RMClient ].GetTenantAwareInventoryClient ()
569+
570+ // Create test OS resources
571+ osRes1 := dao .CreateOsWithOpts (t , mm_testing .Tenant1 , true , func (os * os_v1.OperatingSystemResource ) {
572+ os .Sha256 = inv_testing .GenerateRandomSha256 ()
573+ os .Name = "Test OS Resource 1"
574+ os .ProfileName = "test-profile-1"
575+ os .ImageId = "test-image-1.0.0"
576+ os .ProfileVersion = "1.0.0"
577+ os .SecurityFeature = os_v1 .SecurityFeature_SECURITY_FEATURE_SECURE_BOOT_AND_FULL_DISK_ENCRYPTION
578+ os .OsType = os_v1 .OsType_OS_TYPE_IMMUTABLE
579+ })
580+
581+ osRes2 := dao .CreateOsWithOpts (t , mm_testing .Tenant1 , true , func (os * os_v1.OperatingSystemResource ) {
582+ os .Sha256 = inv_testing .GenerateRandomSha256 ()
583+ os .Name = "Test OS Resource 2"
584+ os .ProfileName = "test-profile-2"
585+ os .ImageId = "test-image-2.0.0"
586+ os .ProfileVersion = "2.0.0"
587+ os .SecurityFeature = os_v1 .SecurityFeature_SECURITY_FEATURE_NONE
588+ os .OsType = os_v1 .OsType_OS_TYPE_MUTABLE
589+ })
590+
591+ tests := []struct {
592+ name string
593+ osResourceID string
594+ expectError bool
595+ expectedCode codes.Code
596+ validateFunc func (* testing.T , * os_v1.OperatingSystemResource )
597+ }{
598+ {
599+ name : "SuccessGetImmutableOS" ,
600+ osResourceID : osRes1 .GetResourceId (),
601+ expectError : false ,
602+ validateFunc : createOSResourceValidator (osRes1 ),
603+ },
604+ {
605+ name : "SuccessGetMutableOS" ,
606+ osResourceID : osRes2 .GetResourceId (),
607+ expectError : false ,
608+ validateFunc : createOSResourceValidator (osRes2 ),
609+ },
610+ {
611+ name : "ErrorNonExistentOSResource" ,
612+ osResourceID : "os-nonexistent-12345678" ,
613+ expectError : true ,
614+ expectedCode : codes .NotFound ,
615+ },
616+ {
617+ name : "ErrorEmptyOSResourceID" ,
618+ osResourceID : "" ,
619+ expectError : true ,
620+ expectedCode : codes .InvalidArgument ,
621+ },
622+ {
623+ name : "ErrorInvalidOSResourceID" ,
624+ osResourceID : "invalid-resource-id" ,
625+ expectError : true ,
626+ expectedCode : codes .InvalidArgument ,
627+ },
628+ }
629+
630+ for _ , tt := range tests {
631+ t .Run (tt .name , func (t * testing.T ) {
632+ osRes , err := invclient .GetOSResourceByID (ctx , client , mm_testing .Tenant1 , tt .osResourceID )
633+
634+ if tt .expectError {
635+ require .Error (t , err )
636+ assert .Equal (t , tt .expectedCode , status .Code (err ))
637+ assert .Nil (t , osRes )
638+ } else {
639+ require .NoError (t , err )
640+ require .NotNil (t , osRes )
641+ if tt .validateFunc != nil {
642+ tt .validateFunc (t , osRes )
643+ }
644+ }
645+ })
646+ }
647+ }
648+
649+ func TestInvClient_GetOSResourceByID_ErrorCases (t * testing.T ) {
650+ dao := inv_testing .NewInvResourceDAOOrFail (t )
651+ ctx := context .TODO ()
652+ client := inv_testing .TestClients [inv_testing .RMClient ].GetTenantAwareInventoryClient ()
653+
654+ // Create a test OS resource for some tests
655+ osRes1 := dao .CreateOsWithOpts (t , mm_testing .Tenant1 , true , func (os * os_v1.OperatingSystemResource ) {
656+ os .Sha256 = inv_testing .GenerateRandomSha256 ()
657+ os .Name = "Test OS Resource 1"
658+ os .ProfileName = "test-profile-1"
659+ os .ImageId = "test-image-1.0.0"
660+ os .ProfileVersion = "1.0.0"
661+ os .SecurityFeature = os_v1 .SecurityFeature_SECURITY_FEATURE_SECURE_BOOT_AND_FULL_DISK_ENCRYPTION
662+ os .OsType = os_v1 .OsType_OS_TYPE_IMMUTABLE
663+ })
664+
665+ // Test with different tenant
666+ t .Run ("ErrorWrongTenant" , func (t * testing.T ) {
667+ _ , err := invclient .GetOSResourceByID (ctx , client , "wrong-tenant-id" , osRes1 .GetResourceId ())
668+ require .Error (t , err )
669+ sts , _ := status .FromError (err )
670+ assert .Equal (t , codes .NotFound , sts .Code ())
671+ })
672+
673+ // Test context timeout
674+ t .Run ("ContextTimeout" , func (t * testing.T ) {
675+ timeoutCtx , cancel := context .WithTimeout (context .Background (), 1 * time .Nanosecond )
676+ defer cancel ()
677+ time .Sleep (1 * time .Millisecond ) // Ensure context is expired
678+
679+ _ , err := invclient .GetOSResourceByID (timeoutCtx , client , mm_testing .Tenant1 , osRes1 .GetResourceId ())
680+ require .Error (t , err )
681+ sts , _ := status .FromError (err )
682+ assert .Equal (t , codes .DeadlineExceeded , sts .Code ())
683+ })
684+ }
0 commit comments