-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
remove_testutils.go
92 lines (84 loc) · 2.67 KB
/
remove_testutils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2022 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package testutils_test
import (
"fmt"
"strings"
"testing"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
// Given represents the provided inputs for the test case.
type Given struct {
// Items is the given input items.
Items []string
// RemoveArgs are the arguments to pass to the remove command.
RemoveArgs []string
}
// Expected represents the expected outputs of the test case.
type Expected struct {
// Expected is the collection of expected output items.
Items []string
// Deleted is the collection of expected Deleted items (if any).
Deleted []string
// Err represents the error that is expected in the output (if any).
Err error
}
// Case represents a test case to execute.
type Case struct {
// Description is the description of the test case.
Description string
// Given is the provided inputs for the test case.
Given Given
// Expected is the expected outputs for the test case.
Expected Expected
}
// ExecuteRemoveTestCases executes the provided test cases against the specified command
// for a particular collection (e.g. ) tests a command defined by the provided
// collection Name (e.g. transformers or resources) and newRemoveCmdToTest function.
func ExecuteRemoveTestCases(
t *testing.T,
testCases []Case,
collectionName string,
newRemoveCmdToTest func(filesys.FileSystem) *cobra.Command,
) {
t.Helper()
for _, tc := range testCases {
t.Run(tc.Description, func(t *testing.T) {
fSys := filesys.MakeFsInMemory()
WriteTestKustomizationWith(
fSys,
[]byte(fmt.Sprintf("%s:\n - %s",
collectionName,
strings.Join(tc.Given.Items, "\n - "))))
cmd := newRemoveCmdToTest(fSys)
err := cmd.RunE(cmd, tc.Given.RemoveArgs)
if err != nil && tc.Expected.Err == nil {
t.Errorf("unexpected cmd error: %v", err)
} else if tc.Expected.Err != nil {
if err.Error() != tc.Expected.Err.Error() {
t.Errorf("expected error did not occurred. Expected: %v. Actual: %v",
tc.Expected.Err,
err)
}
return
}
content, err := ReadTestKustomization(fSys)
if err != nil {
t.Errorf("unexpected read error: %v", err)
}
for _, itemFileName := range tc.Expected.Items {
if !strings.Contains(string(content), itemFileName) {
t.Errorf("expected item not found in kustomization file.\nItem: %s\nKustomization file:\n%s",
itemFileName, content)
}
}
for _, itemFileName := range tc.Expected.Deleted {
if strings.Contains(string(content), itemFileName) {
t.Errorf("expected deleted item found in kustomization file. Item: %s\nKustomization file:\n%s",
itemFileName, content)
}
}
})
}
}