|
| 1 | +// SPDX-FileCopyrightText: The RamenDR authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package validate |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/ramendr/ramen/e2e/types" |
| 12 | + |
| 13 | + "github.com/ramendr/ramenctl/pkg/command" |
| 14 | + "github.com/ramendr/ramenctl/pkg/config" |
| 15 | + "github.com/ramendr/ramenctl/pkg/report" |
| 16 | + "github.com/ramendr/ramenctl/pkg/validation" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + validateClusters = "validate-clusters" |
| 21 | +) |
| 22 | + |
| 23 | +var ( |
| 24 | + testConfig = &config.Config{} |
| 25 | + |
| 26 | + testEnv = &types.Env{ |
| 27 | + Hub: &types.Cluster{Name: "hub"}, |
| 28 | + C1: &types.Cluster{Name: "c1"}, |
| 29 | + C2: &types.Cluster{Name: "c2"}, |
| 30 | + } |
| 31 | + |
| 32 | + validateConfigFailed = &validation.Mock{ |
| 33 | + ValidateFunc: func(ctx validation.Context) error { |
| 34 | + return errors.New("No validate for you!") |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + validateConfigCanceled = &validation.Mock{ |
| 39 | + ValidateFunc: func(ctx validation.Context) error { |
| 40 | + return context.Canceled |
| 41 | + }, |
| 42 | + } |
| 43 | +) |
| 44 | + |
| 45 | +func TestValidateClustersPassed(t *testing.T) { |
| 46 | + validate := testCommand(t, validateClusters, &validation.Mock{}) |
| 47 | + if err := validate.Clusters(); err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | + checkReport(t, validate.report, report.Passed) |
| 51 | + if len(validate.report.Steps) != 2 { |
| 52 | + t.Fatalf("unexpected steps %+v", validate.report.Steps) |
| 53 | + } |
| 54 | + checkStep(t, validate.report.Steps[0], "validate config", report.Passed) |
| 55 | + checkStep(t, validate.report.Steps[1], "validate clusters", report.Passed) |
| 56 | +} |
| 57 | + |
| 58 | +func TestValidateClustersFailed(t *testing.T) { |
| 59 | + validate := testCommand(t, validateClusters, validateConfigFailed) |
| 60 | + if err := validate.Clusters(); err == nil { |
| 61 | + t.Fatal("command did not fail") |
| 62 | + } |
| 63 | + checkReport(t, validate.report, report.Failed) |
| 64 | + if len(validate.report.Steps) != 1 { |
| 65 | + t.Fatalf("unexpected steps %+v", validate.report.Steps) |
| 66 | + } |
| 67 | + checkStep(t, validate.report.Steps[0], "validate config", report.Failed) |
| 68 | +} |
| 69 | + |
| 70 | +func TestValidateClustersCanceled(t *testing.T) { |
| 71 | + validate := testCommand(t, validateClusters, validateConfigCanceled) |
| 72 | + if err := validate.Clusters(); err == nil { |
| 73 | + t.Fatal("command did not fail") |
| 74 | + } |
| 75 | + checkReport(t, validate.report, report.Canceled) |
| 76 | + if len(validate.report.Steps) != 1 { |
| 77 | + t.Fatalf("unexpected steps %+v", validate.report.Steps) |
| 78 | + } |
| 79 | + checkStep(t, validate.report.Steps[0], "validate config", report.Canceled) |
| 80 | +} |
| 81 | + |
| 82 | +func testCommand(t *testing.T, name string, backend validation.Validation) *Command { |
| 83 | + cmd, err := command.ForTest(name, testEnv, t.TempDir()) |
| 84 | + if err != nil { |
| 85 | + t.Fatal(err) |
| 86 | + } |
| 87 | + t.Cleanup(func() { |
| 88 | + cmd.Close() |
| 89 | + }) |
| 90 | + return newCommand(cmd, testConfig, backend) |
| 91 | +} |
| 92 | + |
| 93 | +func checkReport(t *testing.T, report *Report, status report.Status) { |
| 94 | + if report.Status != status { |
| 95 | + t.Fatalf("expected status %q, got %q", status, report.Status) |
| 96 | + } |
| 97 | + if !report.Config.Equal(testConfig) { |
| 98 | + t.Fatalf("expected config %q, got %q", testConfig, report.Config) |
| 99 | + } |
| 100 | + duration := totalDuration(report.Steps) |
| 101 | + if report.Duration != duration { |
| 102 | + t.Fatalf("expected duration %v, got %v", duration, report.Duration) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func checkStep(t *testing.T, step *report.Step, name string, status report.Status) { |
| 107 | + if name != step.Name { |
| 108 | + t.Fatalf("expected step %q, got %q", name, step.Name) |
| 109 | + } |
| 110 | + if status != step.Status { |
| 111 | + t.Fatalf("expected status %q, got %q", status, step.Status) |
| 112 | + } |
| 113 | + // We cannot check duration since it may be zero on windows. |
| 114 | +} |
| 115 | + |
| 116 | +func totalDuration(steps []*report.Step) float64 { |
| 117 | + var total float64 |
| 118 | + for _, step := range steps { |
| 119 | + total += step.Duration |
| 120 | + } |
| 121 | + return total |
| 122 | +} |
0 commit comments