Skip to content

Commit dc67936

Browse files
committed
validate: Test validate clusters
Add fast tests using a mock backend, validating that the report represent the command result. Signed-off-by: Nir Soffer <nsoffer@redhat.com>
1 parent 72bac76 commit dc67936

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

pkg/validate/command_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
var (
20+
testConfig = &config.Config{}
21+
22+
testEnv = &types.Env{
23+
Hub: &types.Cluster{Name: "hub"},
24+
C1: &types.Cluster{Name: "c1"},
25+
C2: &types.Cluster{Name: "c2"},
26+
}
27+
28+
validateConfigFailed = &validation.Mock{
29+
ValidateFunc: func(ctx validation.Context) error {
30+
return errors.New("No validate for you!")
31+
},
32+
}
33+
34+
validateConfigCanceled = &validation.Mock{
35+
ValidateFunc: func(ctx validation.Context) error {
36+
return context.Canceled
37+
},
38+
}
39+
)
40+
41+
func TestValidateClustersPassed(t *testing.T) {
42+
validate := testCommand(t, "validate-clusters", &validation.Mock{})
43+
if err := validate.Clusters(); err != nil {
44+
t.Fatal(err)
45+
}
46+
checkReport(t, validate.report, report.Passed)
47+
if len(validate.report.Steps) != 2 {
48+
t.Fatalf("unexpected steps %+v", validate.report.Steps)
49+
}
50+
checkStep(t, validate.report.Steps[0], "validate config", report.Passed)
51+
checkStep(t, validate.report.Steps[1], "validate clusters", report.Passed)
52+
}
53+
54+
func TestValidateClustersFailed(t *testing.T) {
55+
validate := testCommand(t, "validate-clusters", validateConfigFailed)
56+
if err := validate.Clusters(); err == nil {
57+
t.Fatal("command did not fail")
58+
}
59+
checkReport(t, validate.report, report.Failed)
60+
if len(validate.report.Steps) != 1 {
61+
t.Fatalf("unexpected steps %+v", validate.report.Steps)
62+
}
63+
checkStep(t, validate.report.Steps[0], "validate config", report.Failed)
64+
}
65+
66+
func TestValidateClustersCanceled(t *testing.T) {
67+
validate := testCommand(t, "validate-clusters", validateConfigCanceled)
68+
if err := validate.Clusters(); err == nil {
69+
t.Fatal("command did not fail")
70+
}
71+
checkReport(t, validate.report, report.Canceled)
72+
if len(validate.report.Steps) != 1 {
73+
t.Fatalf("unexpected steps %+v", validate.report.Steps)
74+
}
75+
checkStep(t, validate.report.Steps[0], "validate config", report.Canceled)
76+
}
77+
78+
func testCommand(t *testing.T, name string, backend validation.Validation) *Command {
79+
cmd, err := command.ForTest(name, testEnv, t.TempDir())
80+
if err != nil {
81+
t.Fatal(err)
82+
}
83+
t.Cleanup(func() {
84+
cmd.Close()
85+
})
86+
return newCommand(cmd, testConfig, backend)
87+
}
88+
89+
func checkReport(t *testing.T, report *Report, status report.Status) {
90+
if report.Status != status {
91+
t.Fatalf("expected status %q, got %q", status, report.Status)
92+
}
93+
if !report.Config.Equal(testConfig) {
94+
t.Fatalf("expected config %q, got %q", testConfig, report.Config)
95+
}
96+
duration := totalDuration(report.Steps)
97+
if report.Duration != duration {
98+
t.Fatalf("expected duration %v, got %v", duration, report.Duration)
99+
}
100+
}
101+
102+
func checkStep(t *testing.T, step *report.Step, name string, status report.Status) {
103+
if name != step.Name {
104+
t.Fatalf("expected step %q, got %q", name, step.Name)
105+
}
106+
if status != step.Status {
107+
t.Fatalf("expected status %q, got %q", status, step.Status)
108+
}
109+
// We cannot check duration since it may be zero on windows.
110+
}
111+
112+
func totalDuration(steps []*report.Step) float64 {
113+
var total float64
114+
for _, step := range steps {
115+
total += step.Duration
116+
}
117+
return total
118+
}

0 commit comments

Comments
 (0)