This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
forked from kudobuilder/kuttl
-
Notifications
You must be signed in to change notification settings - Fork 5
/
assert.go
131 lines (110 loc) · 2.91 KB
/
assert.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
package test
import (
"errors"
"fmt"
"time"
"k8s.io/client-go/discovery"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
testutils "github.com/kyverno/kuttl/pkg/test/utils"
)
// Assert checks all provided assert files against a namespace. Upon assert failure, it prints the failures and returns an error
func Assert(namespace string, timeout int, assertFiles ...string) error {
var objects []client.Object
for _, file := range assertFiles {
o, err := ObjectsFromPath(file, "")
if err != nil {
return err
}
objects = append(objects, o...)
}
// feels like the wrong abstraction, need to do some refactoring
s := &Step{
Timeout: 0,
Client: Client,
DiscoveryClient: DiscoveryClient,
}
var testErrors []error
for i := 0; i < timeout; i++ {
// start fresh
testErrors = []error{}
for _, expected := range objects {
testErrors = append(testErrors, s.CheckResource(expected, namespace, nil)...)
}
if len(testErrors) == 0 {
break
}
time.Sleep(time.Second)
}
if len(testErrors) == 0 {
fmt.Printf("assert is valid\n")
return nil
}
for _, testError := range testErrors {
fmt.Println(testError)
}
return errors.New("asserts not valid")
}
// Errors checks all provided errors files against a namespace. Upon assert failure, it prints the failures and returns an error
func Errors(namespace string, timeout int, errorFiles ...string) error {
var objects []client.Object
for _, file := range errorFiles {
o, err := ObjectsFromPath(file, "")
if err != nil {
return err
}
objects = append(objects, o...)
}
// feels like the wrong abstraction, need to do some refactoring
s := &Step{
Timeout: 0,
Client: Client,
DiscoveryClient: DiscoveryClient,
}
var testErrors []error
for i := 0; i < timeout; i++ {
// start fresh
testErrors = []error{}
for _, expected := range objects {
if err := s.CheckResourceAbsent(expected, namespace); err != nil {
testErrors = append(testErrors, err)
}
}
if len(testErrors) == 0 {
break
}
time.Sleep(time.Second)
}
if len(testErrors) == 0 {
fmt.Printf("error assert is valid\n")
return nil
}
for _, testError := range testErrors {
fmt.Println(testError)
}
return errors.New("error asserts not valid")
}
func Client(forceNew bool) (client.Client, error) {
cfg, err := config.GetConfig()
if err != nil {
return nil, err
}
client, err := testutils.NewRetryClient(cfg, client.Options{
Scheme: testutils.Scheme(),
})
if err != nil {
return nil, fmt.Errorf("fatal error getting client: %v", err)
}
return client, nil
}
func DiscoveryClient() (discovery.DiscoveryInterface, error) {
cfg, err := config.GetConfig()
if err != nil {
return nil, err
}
dclient, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
return nil, fmt.Errorf("fatal error getting discovery client: %v", err)
}
return dclient, nil
}