Skip to content

Commit 6deb669

Browse files
megelatimcopybara-github
authored andcommitted
Added no_disk_snapshot_status_check flag to disable checks for disk snapshot feature (hanadiskbackup and hanadiskrestore). Default value is false i.e. checks are enabled.
PiperOrigin-RevId: 853304388
1 parent 92daaf9 commit 6deb669

3 files changed

Lines changed: 92 additions & 22 deletions

File tree

internal/onetime/status/status.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ type (
116116

117117
// Status stores the status subcommand parameters.
118118
type Status struct {
119-
ConfigFilePath string
120-
BackintParametersPath string
121-
Feature string
122-
CloudProps *iipb.CloudProperties
123-
HeartbeatSpec *heartbeat.Spec
124-
WLMService WLMInterface
119+
ConfigFilePath string
120+
BackintParametersPath string
121+
Feature string
122+
CloudProps *iipb.CloudProperties
123+
HeartbeatSpec *heartbeat.Spec
124+
WLMService WLMInterface
125+
NoDiskSnapshotStatusCheck bool
125126

126127
compact bool
127128
help bool
@@ -295,8 +296,28 @@ func start(ctx context.Context, a any) {
295296
}
296297
}
297298

299+
// Creates a new string from given comma separated string without the specified element.
300+
func removeStringElement(str string, elementToRemove string) string {
301+
slice := strings.Split(str, ",")
302+
var result []string
303+
for _, item := range slice {
304+
if item != elementToRemove {
305+
result = append(result, item)
306+
}
307+
}
308+
return strings.Join(result, ",")
309+
}
310+
298311
func (s *Status) collectAndSendStatus(ctx context.Context) error {
299312
s.HeartbeatSpec.Beat()
313+
314+
// If the disk snapshot status check is disabled, remove the disk snapshot feature from the list
315+
// of features to check.
316+
if s.NoDiskSnapshotStatusCheck {
317+
s.Feature = removeStringElement(allFeatures, diskSnapshot)
318+
log.CtxLogger(ctx).Debug("Disk snapshot status check DISABLED for daemon")
319+
}
320+
300321
agentStatus, err := s.statusHandler(ctx)
301322
if err != nil {
302323
log.CtxLogger(ctx).Errorw("Could not get agent status", "error", err)

internal/onetime/status/status_test.go

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

311312
func 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
}

internal/startdaemon/startdaemon.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,11 @@ var (
109109

110110
// Daemon has args for startdaemon subcommand.
111111
type Daemon struct {
112-
configFilePath string
113-
lp log.Parameters
114-
config *cpb.Configuration
115-
cloudProps *iipb.CloudProperties
112+
configFilePath string
113+
lp log.Parameters
114+
config *cpb.Configuration
115+
cloudProps *iipb.CloudProperties
116+
noDiskSnapshotStatusCheck bool
116117
}
117118

118119
// Name implements the subcommand interface for startdaemon.
@@ -123,13 +124,14 @@ func (*Daemon) Synopsis() string { return "start daemon mode of the agent" }
123124

124125
// Usage implements the subcommand interface for startdaemon.
125126
func (*Daemon) Usage() string {
126-
return "Usage: startdaemon [-config <path-to-config-file>] \n"
127+
return "Usage: startdaemon [-config <path-to-config-file>] [-no_disk_snapshot_status_check]\n"
127128
}
128129

129130
// SetFlags implements the subcommand interface for startdaemon.
130131
func (d *Daemon) SetFlags(fs *flag.FlagSet) {
131132
fs.StringVar(&d.configFilePath, "config", "", "configuration path for startdaemon mode")
132133
fs.StringVar(&d.configFilePath, "c", "", "configuration path for startdaemon mode")
134+
fs.BoolVar(&d.noDiskSnapshotStatusCheck, "no_disk_snapshot_status_check", false, "suppress disk_snapshot status checks in daemon mode")
133135
}
134136

135137
// Execute implements the subcommand interface for startdaemon.
@@ -483,9 +485,10 @@ func (d *Daemon) startServices(ctx context.Context, cancel context.CancelFunc, g
483485
// Start Status Collection
484486
statusCtx := log.SetCtx(ctx, "context", "Status")
485487
sp := StatusParams{&status.Status{
486-
ConfigFilePath: d.configFilePath,
487-
CloudProps: d.cloudProps,
488-
WLMService: wlmService}, healthMonitor}
488+
ConfigFilePath: d.configFilePath,
489+
CloudProps: d.cloudProps,
490+
WLMService: wlmService,
491+
NoDiskSnapshotStatusCheck: d.noDiskSnapshotStatusCheck}, healthMonitor}
489492
sp.startCollection(statusCtx)
490493

491494
waitForShutdown(ctx, shutdownch, cancel, restarting)

0 commit comments

Comments
 (0)