@@ -42,6 +42,7 @@ import (
4242 "github.com/google/subcommands"
4343 "github.com/GoogleCloudPlatform/sapagent/internal/configuration"
4444 "github.com/GoogleCloudPlatform/sapagent/internal/databaseconnector"
45+ "github.com/GoogleCloudPlatform/sapagent/internal/heartbeat"
4546 "github.com/GoogleCloudPlatform/sapagent/internal/iam"
4647 "github.com/GoogleCloudPlatform/sapagent/shared/iam"
4748 "github.com/GoogleCloudPlatform/workloadagentplatform/sharedlibraries/commandlineexecutor"
@@ -310,9 +311,10 @@ func TestStartStatusCollection(t *testing.T) {
310311
311312func TestCollectAndSendStatus (t * testing.T ) {
312313 tests := []struct {
313- name string
314- s Status
315- want error
314+ name string
315+ s Status
316+ want error
317+ wantDiskSnapshotInFeatureFlag bool
316318 }{
317319 {
318320 name : "SuccessEmptyConfig" ,
@@ -339,6 +341,7 @@ func TestCollectAndSendStatus(t *testing.T) {
339341 Zone : "us-central1-a" ,
340342 Region : "test-region" ,
341343 },
344+ HeartbeatSpec : & heartbeat.Spec {BeatFunc : func () {}},
342345 httpGet : httpGetSuccess ,
343346 createDBHandle : dbConnectorSuccess ,
344347 iamService : & iam.IAM {},
@@ -353,7 +356,8 @@ func TestCollectAndSendStatus(t *testing.T) {
353356 WriteInsightErrs : []error {nil },
354357 },
355358 },
356- want : nil ,
359+ want : nil ,
360+ wantDiskSnapshotInFeatureFlag : true , // Default behavior
357361 },
358362 {
359363 name : "SuccessAllEnabled" ,
@@ -405,6 +409,7 @@ func TestCollectAndSendStatus(t *testing.T) {
405409 Zone : "us-central1-a" ,
406410 Region : "test-region" ,
407411 },
412+ HeartbeatSpec : & heartbeat.Spec {BeatFunc : func () {}},
408413 stat : func (name string ) (os.FileInfo , error ) {
409414 return & mockFileInfo {perm : 0400 }, nil
410415 },
@@ -429,11 +434,12 @@ func TestCollectAndSendStatus(t *testing.T) {
429434 WriteInsightErrs : []error {nil },
430435 },
431436 },
432- want : nil ,
437+ want : nil ,
438+ wantDiskSnapshotInFeatureFlag : true , // Default behavior
433439 },
434440 {
435441 name : "StatusStructNotInitialized" ,
436- s : Status {},
442+ s : Status {HeartbeatSpec : & heartbeat. Spec { BeatFunc : func () {}} },
437443 want : cmpopts .AnyError ,
438444 },
439445 {
@@ -461,6 +467,7 @@ func TestCollectAndSendStatus(t *testing.T) {
461467 Zone : "us-central1-a" ,
462468 Region : "test-region" ,
463469 },
470+ HeartbeatSpec : & heartbeat.Spec {BeatFunc : func () {}},
464471 httpGet : httpGetSuccess ,
465472 createDBHandle : dbConnectorSuccess ,
466473 iamService : & iam.IAM {},
@@ -475,15 +482,54 @@ func TestCollectAndSendStatus(t *testing.T) {
475482 WriteInsightErrs : []error {cmpopts .AnyError },
476483 },
477484 },
478- want : cmpopts .AnyError ,
485+ want : cmpopts .AnyError ,
486+ wantDiskSnapshotInFeatureFlag : true ,
487+ },
488+ {
489+ name : "DiskSnapshotCheckDisabledByFlag" ,
490+ s : Status {
491+ NoDiskSnapshotStatusCheck : true , // Disable disk snapshot status check
492+ readFile : func (string ) ([]byte , error ) { return nil , nil },
493+ exec : func (context.Context , commandlineexecutor.Params ) commandlineexecutor.Result {
494+ return commandlineexecutor.Result {StdOut : "enabled" , ExitCode : 0 }
495+ },
496+ CloudProps : & iipb.CloudProperties {Scopes : []string {requiredScope }, Zone : "us-central1-a" , Region : "test-region" },
497+ HeartbeatSpec : & heartbeat.Spec {BeatFunc : func () {}},
498+ iamService : & iam.IAM {},
499+ arClient : fakeArtifactRegistryClient ([]string {"" }),
500+ permissionsStatus : func (ctx context.Context , iamService permissions.IAMService , serviceName string , r * permissions.ResourceDetails ) (map [string ]bool , error ) {
501+ return map [string ]bool {"monitoring.timeSeries.create" : true }, nil
502+ },
503+ WLMService : & fake.TestWLM {WriteInsightErrs : []error {nil }},
504+ // Add other necessary mocks if statusHandler call depends on them
505+ httpGet : httpGetSuccess ,
506+ createDBHandle : dbConnectorSuccess ,
507+ stat : func (name string ) (os.FileInfo , error ) { return & mockFileInfo {perm : 0777 }, nil },
508+ readDir : func (dirname string ) ([]fs.FileInfo , error ) { return []fs.FileInfo {& mockFileInfo {perm : 0777 }}, nil },
509+ },
510+ want : nil ,
511+ wantDiskSnapshotInFeatureFlag : false , // Expect disk_snapshot to be removed
479512 },
480513 }
481514 for _ , test := range tests {
482515 t .Run (test .name , func (t * testing.T ) {
483516 t .Parallel ()
517+
518+ if test .s .WLMService != nil {
519+ test .s .WLMService .(* fake.TestWLM ).WriteInsightArgs = []fake.WriteInsightArgs {} // Reset mock
520+ }
521+
484522 got := test .s .collectAndSendStatus (context .Background ())
485523 if ! cmp .Equal (got , test .want , cmpopts .EquateErrors ()) {
486- t .Errorf ("statusHandler()=%v want %v" , got , test .want )
524+ t .Errorf ("collectAndSendStatus()=%v want %v" , got , test .want )
525+ }
526+
527+ if test .want == nil {
528+ var featuresSeenByHandler string = test .s .Feature
529+ hasDiskSnapshot := strings .Contains (featuresSeenByHandler , diskSnapshot )
530+ if hasDiskSnapshot != test .wantDiskSnapshotInFeatureFlag {
531+ t .Errorf ("collectAndSendStatus() features string: %q, contains disk_snapshot: %v, want: %v" , featuresSeenByHandler , hasDiskSnapshot , test .wantDiskSnapshotInFeatureFlag )
532+ }
487533 }
488534 })
489535 }
0 commit comments