-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_suite_test.go
192 lines (161 loc) · 5.29 KB
/
test_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
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
187
188
189
190
191
192
package kuberouterTest
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/cloudnativelabs/kube-router-e2e/config"
. "github.com/cloudnativelabs/kube-router-e2e/ginkgo-ext"
ginkgoext "github.com/cloudnativelabs/kube-router-e2e/ginkgo-ext"
"github.com/cloudnativelabs/kube-router-e2e/helpers"
gops "github.com/google/gops/agent"
"github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/format"
"github.com/sirupsen/logrus"
)
var (
log = logrus.New()
DefaultSettings = map[string]string{
"K8S_VERSION": "1.13",
}
k8sNodesEnv = "K8S_NODES"
commandsLogFileName = "cmds.log"
)
func init() {
// Open socket for using gops to get stacktraces in case the tests deadlock.
if err := gops.Listen(gops.Options{}); err != nil {
errorString := fmt.Sprintf("unable to start gops: %s", err)
fmt.Println(errorString)
os.Exit(-1)
}
for k, v := range DefaultSettings {
getOrSetEnvVar(k, v)
}
config.KuberouterTestConfig.ParseFlags()
os.RemoveAll(helpers.TestResultsPath)
format.UseStringerRepresentation = true
}
func configLogsOutput() {
log.SetLevel(logrus.DebugLevel)
log.Out = &config.TestLogWriter
logrus.SetFormatter(&config.Formatter)
log.Formatter = &config.Formatter
log.Hooks.Add(&config.LogHook{})
ginkgoext.GinkgoWriter = NewWriter(log.Out)
}
func ShowCommands() {
if !config.KuberouterTestConfig.ShowCommands {
return
}
helpers.SSHMetaLogs = ginkgoext.NewWriter(os.Stdout)
}
func TestTest(t *testing.T) {
if config.KuberouterTestConfig.TestScope != "" {
helpers.UserDefinedScope = config.KuberouterTestConfig.TestScope
fmt.Printf("User specified the scope: %q\n", config.KuberouterTestConfig.TestScope)
}
configLogsOutput()
ShowCommands()
if config.KuberouterTestConfig.HoldEnvironment {
RegisterFailHandler(helpers.Fail)
} else {
RegisterFailHandler(Fail)
}
RunSpecsWithDefaultAndCustomReporters(
t, helpers.GetScopeWithVersion(), nil)
}
var _ = BeforeAll(func() {
var err error
logger := log.WithFields(logrus.Fields{"testName": "BeforeAll"})
scope, err := helpers.GetScope()
if err != nil {
Fail(fmt.Sprintf(
"Cannot get the scope for running test, please use --kuberouter.testScope option: %s",
err))
}
// Boot / provision VMs if specified by configuration.
if config.KuberouterTestConfig.Reprovision {
Fail("Provisioning is not supported at this time.")
}
if config.KuberouterTestConfig.Provisioner != "vagrant" {
Fail("Only provisioner supported at this time is vagrant")
}
if config.KuberouterTestConfig.SSHConfig == "" {
Fail(fmt.Sprintf(
"No ssh config specified, please use --kuberouter.SSHConfig option: %s",
err))
}
switch scope {
case helpers.Runtime:
vm := helpers.InitRuntimeHelper(helpers.K8s1VMName(), logger)
go vm.PprofReport()
case helpers.IPv6C:
// TODO: if kuberouter.provision, create an IPv6 cluster
kubectl := helpers.CreateKubectl(helpers.K8s1VMName(), logger)
go kubectl.PprofReport()
case helpers.IPv4C:
// TODO: if kuberouter.provision, create an IPv6 cluster
}
return
})
var _ = AfterSuite(func() {
if !helpers.IsRunningOnJenkins() {
GinkgoPrint("AfterSuite: not running on Jenkins; leaving VMs running for debugging")
return
}
// Errors are not checked here because it should fail on BeforeAll
scope, _ := helpers.GetScope()
GinkgoPrint("cleaning up VMs started for %s tests", scope)
switch scope {
case helpers.Runtime:
// helpers.DestroyVM(helpers.Runtime)
case helpers.IPv6C:
// kubectl := helpers.CreateKubectl(helpers.K8s1VMName(), logger)
// kubectl.Apply(helpers.GetFilePath("./examples/kubernetes/addons/prometheus/prometheus.yaml"))
// go kubectl.PprofReport()
case helpers.IPv4C:
// TODO
}
return
})
func getOrSetEnvVar(key, value string) {
if val := os.Getenv(key); val == "" {
log.Infof("environment variable %q was not set; setting to default value %q", key, value)
os.Setenv(key, value)
}
}
var _ = AfterEach(func() {
// Send the Checks output to Junit report to be render on Jenkins.
defer helpers.CheckLogs.Reset()
GinkgoPrint("<Checks>\n%s\n</Checks>\n", helpers.CheckLogs.Buffer.String())
defer config.TestLogWriterReset()
err := helpers.CreateLogFile(config.TestLogFileName, config.TestLogWriter.Bytes())
if err != nil {
log.WithError(err).Errorf("cannot create log file '%s'", config.TestLogFileName)
return
}
defer helpers.SSHMetaLogs.Reset()
err = helpers.CreateLogFile(commandsLogFileName, helpers.SSHMetaLogs.Bytes())
if err != nil {
log.WithError(err).Errorf("cannot create log file '%s'", commandsLogFileName)
return
}
// This piece of code is to enable zip attachments on Junit Output.
if ginkgo.CurrentGinkgoTestDescription().Failed && helpers.IsRunningOnJenkins() {
// ReportDirectory is already created. No check the error
path, _ := helpers.CreateReportDirectory()
zipFileName := fmt.Sprintf("%s_%s.zip", helpers.MakeUID(), ginkgoext.GetTestName())
zipFilePath := filepath.Join(helpers.TestResultsPath, zipFileName)
_, err := exec.Command(
"/bin/bash", "-c",
fmt.Sprintf("zip -qr %s %s", zipFilePath, path)).CombinedOutput()
if err != nil {
log.WithError(err).Errorf("cannot create zip file '%s'", zipFilePath)
}
// @eloy- This part is only if you use Jenkins Attachment plugin, has
// been super useful for us.
ginkgoext.GinkgoPrint("[[ATTACHMENT|%s]]", zipFileName)
}
})