-
Notifications
You must be signed in to change notification settings - Fork 23
/
tester.go
186 lines (161 loc) · 5.73 KB
/
tester.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2023 Upbound Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
import (
"bufio"
"encoding/json"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/upbound/uptest/internal/config"
"github.com/upbound/uptest/internal/templates"
)
func newTester(ms []config.Manifest, opts *config.AutomatedTest) *tester {
return &tester{
options: opts,
manifests: ms,
}
}
type tester struct {
options *config.AutomatedTest
manifests []config.Manifest
}
func (t *tester) executeTests() error {
if err := t.writeKuttlFiles(); err != nil {
return errors.Wrap(err, "cannot write kuttl test files")
}
fmt.Println("Running kuttl tests at " + t.options.Directory)
cmd := exec.Command("bash", "-c", fmt.Sprintf(`"${KUTTL}" test --start-kind=false --skip-cluster-delete %s --timeout %d 2>&1`, t.options.Directory, t.options.DefaultTimeout)) // #nosec G204
stdout, _ := cmd.StdoutPipe()
if err := cmd.Start(); err != nil {
return errors.Wrap(err, "cannot start kuttl")
}
sc := bufio.NewScanner(stdout)
sc.Split(bufio.ScanLines)
for sc.Scan() {
fmt.Println(sc.Text())
}
return errors.Wrap(cmd.Wait(), "kuttl failed")
}
func (t *tester) prepareConfig() (*config.TestCase, []config.Resource, error) { //nolint:gocyclo // TODO: can we break this?
tc := &config.TestCase{
Timeout: t.options.DefaultTimeout,
SetupScriptPath: t.options.SetupScriptPath,
TeardownScriptPath: t.options.TeardownScriptPath,
}
examples := make([]config.Resource, 0, len(t.manifests))
for _, m := range t.manifests {
obj := m.Object
groupVersionKind := obj.GroupVersionKind()
kg := strings.ToLower(groupVersionKind.Kind + "." + groupVersionKind.Group)
example := config.Resource{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
KindGroup: kg,
YAML: m.YAML,
Timeout: t.options.DefaultTimeout,
Conditions: t.options.DefaultConditions,
}
var err error
annotations := obj.GetAnnotations()
if v, ok := annotations[config.AnnotationKeyTimeout]; ok {
example.Timeout, err = strconv.Atoi(v)
if err != nil {
return nil, nil, errors.Wrap(err, "timeout value is not valid")
}
if example.Timeout > tc.Timeout {
tc.Timeout = example.Timeout
}
}
if v, ok := annotations[config.AnnotationKeyConditions]; ok {
example.Conditions = strings.Split(v, ",")
}
if v, ok := annotations[config.AnnotationKeyPreAssertHook]; ok {
example.PreAssertScriptPath, err = filepath.Abs(filepath.Join(filepath.Dir(m.FilePath), filepath.Clean(v)))
if err != nil {
return nil, nil, errors.Wrap(err, "cannot find absolute path for pre assert hook")
}
}
if v, ok := annotations[config.AnnotationKeyPostAssertHook]; ok {
example.PostAssertScriptPath, err = filepath.Abs(filepath.Join(filepath.Dir(m.FilePath), filepath.Clean(v)))
if err != nil {
return nil, nil, errors.Wrap(err, "cannot find absolute path for post assert hook")
}
}
if v, ok := annotations[config.AnnotationKeyPreDeleteHook]; ok {
example.PreDeleteScriptPath, err = filepath.Abs(filepath.Join(filepath.Dir(m.FilePath), filepath.Clean(v)))
if err != nil {
return nil, nil, errors.Wrap(err, "cannot find absolute path for pre delete hook")
}
}
if v, ok := annotations[config.AnnotationKeyPostDeleteHook]; ok {
example.PostDeleteScriptPath, err = filepath.Abs(filepath.Join(filepath.Dir(m.FilePath), filepath.Clean(v)))
if err != nil {
return nil, nil, errors.Wrap(err, "cannot find absolute path for post delete hook")
}
}
updateParameter, ok := annotations[config.AnnotationKeyUpdateParameter]
if !ok {
updateParameter = os.Getenv("UPTEST_UPDATE_PARAMETER")
}
if updateParameter != "" {
example.UpdateParameter = updateParameter
var data map[string]interface{}
if err := json.Unmarshal([]byte(updateParameter), &data); err != nil {
return nil, nil, errors.Wrapf(err, "cannot unmarshal JSON object: %s", updateParameter)
}
example.UpdateAssertKey, example.UpdateAssertValue = convertToJSONPath(data, "")
}
if exampleID, ok := annotations[config.AnnotationKeyExampleID]; ok {
if exampleID == strings.ToLower(fmt.Sprintf("%s/%s/%s", strings.Split(groupVersionKind.Group, ".")[0], groupVersionKind.Version, groupVersionKind.Kind)) {
example.Root = true
}
}
examples = append(examples, example)
}
return tc, examples, nil
}
func (t *tester) writeKuttlFiles() error {
tc, examples, err := t.prepareConfig()
if err != nil {
return errors.Wrap(err, "cannot build examples config")
}
files, err := templates.Render(tc, examples)
if err != nil {
return errors.Wrap(err, "cannot render kuttl templates")
}
for k, v := range files {
if err := os.WriteFile(filepath.Join(filepath.Join(t.options.Directory, caseDirectory), k), []byte(v), fs.ModePerm); err != nil {
return errors.Wrapf(err, "cannot write file %q", k)
}
}
return nil
}
func convertToJSONPath(data map[string]interface{}, currentPath string) (string, string) {
for key, value := range data {
newPath := currentPath + "." + key
switch v := value.(type) {
case map[string]interface{}:
return convertToJSONPath(v, newPath)
default:
return newPath, fmt.Sprintf("%v", v)
}
}
return currentPath, ""
}