-
Notifications
You must be signed in to change notification settings - Fork 45
/
test.go
147 lines (126 loc) · 3.71 KB
/
test.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
// Copyright 2024 Red Hat, 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 test
import (
"fmt"
"os/exec"
"strings"
"testing"
"time"
"github.com/maistra/maistra-test-tool/pkg/util/env"
"github.com/maistra/maistra-test-tool/pkg/util/version"
)
type TestGroup string
const (
ARM TestGroup = "arm64"
Full TestGroup = "full"
Smoke TestGroup = "smoke"
InterOp TestGroup = "interop"
Disconnected TestGroup = "disconnected"
)
type Test interface {
Run(f func(t TestHelper))
}
type TopLevelTest interface {
Test
Groups(groups ...TestGroup) TopLevelTest
MinVersion(v version.Version) TopLevelTest
MaxVersion(v version.Version) TopLevelTest
Id(id string) TopLevelTest
}
func NewTest(t *testing.T) TopLevelTest {
return &topLevelTest{t: t}
}
var _ Test = &topLevelTest{}
type topLevelTest struct {
t *testing.T
id string
groups []TestGroup
minVersion *version.Version
maxVersion *version.Version
}
func (t *topLevelTest) Groups(groups ...TestGroup) TopLevelTest {
t.groups = groups
return t
}
func (t *topLevelTest) MinVersion(v version.Version) TopLevelTest {
t.minVersion = &v
return t
}
func (t *topLevelTest) MaxVersion(v version.Version) TopLevelTest {
t.maxVersion = &v
return t
}
func (t *topLevelTest) Id(id string) TopLevelTest {
t.id = id
return t
}
func (t *topLevelTest) Run(f func(t TestHelper)) {
t.t.Helper()
t.skipIfNecessary()
start := time.Now()
th := &testHelper{t: t.t}
defer func() {
recoverPanic(t.t)
t.t.Log()
if th.Failed() {
t.t.Logf("Test failed in %.2fs (excluding cleanup)", time.Now().Sub(start).Seconds())
if env.IsMustGatherEnabled() {
captureMustGather(t.t)
}
} else {
t.t.Logf("Test completed in %.2fs (excluding cleanup)", time.Now().Sub(start).Seconds())
}
}()
f(th)
}
func captureMustGather(t *testing.T) {
image := env.GetMustGatherImage()
dir := fmt.Sprintf("%s/failures-must-gather/%s-%s",
env.GetOutputDir(),
time.Now().Format("20060102150405"),
strings.ReplaceAll(t.Name(), "/", "-"))
t.Logf("Capturing cluster state using must-gather %s", image)
cmd := exec.Command("sh", "-c", fmt.Sprintf(`rm -rf %s; mkdir -p %s; oc adm must-gather --dest-dir=%s --image=%s`, dir, dir, dir, image))
_, err := cmd.CombinedOutput()
if err == nil {
t.Log(dir)
} else {
t.Logf("failed to create must-gather: %v", err)
}
}
func (t *topLevelTest) skipIfNecessary() {
testGroup := TestGroup(env.GetTestGroup())
if env.GetArch() == "arm64" {
testGroup = "arm64"
}
if !t.isPartOfGroup(testGroup) {
t.t.Skipf("This test is being skipped because it is not part of the %q test group", testGroup)
}
smcpVersion := env.GetSMCPVersion()
if t.minVersion != nil && smcpVersion.LessThan(*t.minVersion) {
t.t.Skipf("This test is being skipped because it doesn't support the current SMCP version %s (min version is %s)", smcpVersion, t.minVersion)
}
if t.maxVersion != nil && smcpVersion.GreaterThan(*t.maxVersion) {
t.t.Skipf("This test is being skipped because it doesn't support the current SMCP version %s (max version is %s)", smcpVersion, t.maxVersion)
}
}
func (t *topLevelTest) isPartOfGroup(group TestGroup) bool {
for _, g := range t.groups {
if g == group {
return true
}
}
return false
}