-
Notifications
You must be signed in to change notification settings - Fork 2
/
suite_test.go
156 lines (125 loc) · 4.52 KB
/
suite_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
148
149
150
151
152
153
154
155
156
/*
* Copyright 2022-2024 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is 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 (
"context"
"flag"
"fmt"
"os"
"testing"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/NearNodeFlash/nnf-integration-test/internal"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
zapcr "sigs.k8s.io/controller-runtime/pkg/log/zap"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
log "sigs.k8s.io/controller-runtime/pkg/log"
dwsv1alpha2 "github.com/DataWorkflowServices/dws/api/v1alpha2"
lusv1alpha1 "github.com/NearNodeFlash/lustre-fs-operator/api/v1alpha1"
nnfv1alpha4 "github.com/NearNodeFlash/nnf-sos/api/v1alpha4"
)
var (
ignoreReservation bool
ctx context.Context
cancel context.CancelFunc
testEnv *envtest.Environment
k8sClient client.Client
)
func init() {
flag.BoolVar(&ignoreReservation, "ignore-reservation", false, "Ignore any reservations on the system that might prevent test execution")
}
func TestEverything(t *testing.T) {
RegisterFailHandler(FailHandler)
RunSpecs(t, "Integration Test Suite")
}
var _ = BeforeSuite(func() {
encoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
zaplogger := zapcr.New(zapcr.WriteTo(GinkgoWriter), zapcr.Encoder(encoder), zapcr.UseDevMode(true))
log.SetLogger(zaplogger)
ctx, cancel = context.WithCancel(context.Background())
By("Retrieving Workflow State Timeouts")
// given a default timeout and a env var name, get the duration string and parse it
getTimeoutDuration := func(d, env string) time.Duration {
s := d
// if set, use the env variable
if e := os.Getenv(env); e != "" {
s = e
}
// parse it
t, err := time.ParseDuration(s)
if err != nil {
panic(fmt.Sprintf("cannot prase timeout: %v", err))
}
return t
}
lowTimeoutDuration := getTimeoutDuration(lowTimeout, "LTIMEOUT")
highTimeoutDuration := getTimeoutDuration(highTimeout, "HTIMEOUT")
ctx = context.WithValue(ctx, "lowTimeout", lowTimeoutDuration)
ctx = context.WithValue(ctx, "highTimeout", highTimeoutDuration)
ctx = context.WithValue(ctx, "highTimeoutStates", highTimeoutStates)
fmt.Printf("Using a low timeout of '%s'\n", lowTimeoutDuration)
fmt.Printf("Using a high timeout of '%s' for the following states: %v\n", highTimeoutDuration, highTimeoutStates)
By("Bootstrapping Test Env")
useExistingClustre := true
testEnv = &envtest.Environment{UseExistingCluster: &useExistingClustre}
cfg, err := testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
By("Adding Schemes")
err = dwsv1alpha2.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
err = lusv1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
err = nnfv1alpha4.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
By("Creating Client")
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
// Check if the system is currently in need of tirage and prevent test execution if so
if IsSystemInNeedOfTriage(ctx, k8sClient) {
AbortSuite(fmt.Sprintf("System requires triage. Delete the '%s' namespace when finished", TriageNamespaceName))
}
// Check if the system is being reserved by a developer
if !ignoreReservation {
By("Checking for system reservation")
reserved, developer, err := IsSystemReserved(ctx, k8sClient)
Expect(err).NotTo(HaveOccurred())
if reserved {
AbortSuite(fmt.Sprintf("System is current reserved by '%s'", developer))
}
}
})
var _ = AfterSuite(func() {
cancel()
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
func FailHandler(message string, callerSkip ...int) {
if ctx != nil && k8sClient != nil {
if err := SetSystemInNeedOfTriage(ctx, k8sClient); err != nil {
log.Log.Error(err, "Failed to configure the system for triage")
}
}
Fail(message, callerSkip...)
}