-
Notifications
You must be signed in to change notification settings - Fork 463
/
sonobuoy.go
342 lines (265 loc) · 7.58 KB
/
sonobuoy.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Package sonobuoy provides functions to to run Kubernetes e2e tests.
package sonobuoy
import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/coreos/go-semver/semver"
"github.com/vmware-tanzu/sonobuoy/cmd/sonobuoy/app"
"github.com/vmware-tanzu/sonobuoy/pkg/client"
"github.com/vmware-tanzu/sonobuoy/pkg/config"
sonodynamic "github.com/vmware-tanzu/sonobuoy/pkg/dynamic"
yaml "gopkg.in/yaml.v3"
"github.com/talos-systems/talos/pkg/cluster"
"github.com/talos-systems/talos/pkg/machinery/constants"
)
// Options for the tests.
type Options struct {
RunTests []string
Parallel bool
RunTimeout time.Duration
DeleteTimeout time.Duration
KubernetesVersion string
UseSpinner bool
RetrieveResults bool
ResultsPath string
}
// DefaultOptions with hand-picked tests, timeouts, etc.
func DefaultOptions() *Options {
return &Options{
RunTests: []string{ // list of tests to cover basic kubernetes operations
"Pods should be submitted and removed",
"Services should serve a basic endpoint from pods",
"Services should be able to change the type from ExternalName to ClusterIP",
},
RunTimeout: 10 * time.Minute,
DeleteTimeout: 3 * time.Minute,
KubernetesVersion: constants.DefaultKubernetesVersion,
}
}
// FastConformance runs conformance suite in two passes: parallel + serial for non parallel-safe tests.
func FastConformance(ctx context.Context, cluster cluster.K8sProvider) error {
optionsList := []Options{
{
RunTests: []string{`\[Conformance\]`},
Parallel: true,
RunTimeout: time.Hour,
DeleteTimeout: 5 * time.Minute,
KubernetesVersion: constants.DefaultKubernetesVersion,
},
{
RunTests: []string{`\[Serial\].*\[Conformance\]`},
Parallel: false,
RunTimeout: time.Hour,
DeleteTimeout: 5 * time.Minute,
KubernetesVersion: constants.DefaultKubernetesVersion,
},
}
for _, options := range optionsList {
options := options
if err := Run(ctx, cluster, &options); err != nil {
return err
}
}
return nil
}
// CertifiedConformance runs conformance suite in certified mode collecting all the results.
func CertifiedConformance(ctx context.Context, cluster cluster.K8sProvider) error {
options := Options{
RunTests: []string{`\[Conformance\]`},
Parallel: false,
RunTimeout: 2 * time.Hour,
DeleteTimeout: 5 * time.Minute,
KubernetesVersion: constants.DefaultKubernetesVersion,
RetrieveResults: true,
}
k8sVersion, err := semver.NewVersion(options.KubernetesVersion)
if err != nil {
return err
}
options.ResultsPath = fmt.Sprintf("v%d.%d/talos", k8sVersion.Major, k8sVersion.Minor)
if err = os.MkdirAll(options.ResultsPath, 0o755); err != nil {
return err
}
return Run(ctx, cluster, &options)
}
// Run the e2e test against cluster with provided options.
//
//nolint:gocyclo,cyclop
func Run(ctx context.Context, cluster cluster.K8sProvider, options *Options) error {
var waitOutput string
if options.UseSpinner {
waitOutput = string(app.SpinnerOutputMode)
} else {
waitOutput = string(app.SilentOutputMode)
}
cfg, err := cluster.K8sRestConfig(ctx)
if err != nil {
return fmt.Errorf("error getting kubernetes config: %w", err)
}
skc, err := sonodynamic.NewAPIHelperFromRESTConfig(cfg)
if err != nil {
return fmt.Errorf("couldn't get sonobuoy api helper: %w", err)
}
e2ePassed := false
sclient, err := client.NewSonobuoyClient(cfg, skc)
if err != nil {
return fmt.Errorf("error building sonobuoy client: %w", err)
}
logReader, err := sclient.LogReader(&client.LogConfig{
Namespace: config.DefaultNamespace,
Follow: true,
Plugin: "e2e",
})
if err != nil {
return fmt.Errorf("error setting up log reader: %w", err)
}
logF, err := ioutil.TempFile("", "talos")
if err != nil {
return fmt.Errorf("error creating temporary file for logs: %w", err)
}
defer logF.Close() //nolint:errcheck
go func() {
io.Copy(logF, logReader) //nolint:errcheck
}()
cleanup := func() error {
os.Remove(logF.Name()) //nolint:errcheck
return sclient.Delete(&client.DeleteConfig{
Namespace: config.DefaultNamespace,
DeleteAll: true,
Wait: options.DeleteTimeout,
WaitOutput: waitOutput,
})
}
defer cleanup() //nolint:errcheck
runConfig := client.NewRunConfig()
runConfig.Wait = options.RunTimeout
runConfig.WaitOutput = waitOutput
runConfig.DynamicPlugins = []string{"e2e"}
runConfig.PluginEnvOverrides = map[string]map[string]string{
"e2e": {
"E2E_FOCUS": strings.Join(options.RunTests, "|"),
"E2E_PARALLEL": fmt.Sprintf("%v", options.Parallel),
},
}
runConfig.KubeVersion = fmt.Sprintf("v%s", options.KubernetesVersion)
if err = sclient.Run(runConfig); err != nil {
return fmt.Errorf("sonobuoy run failed: %w", err)
}
status, err := sclient.GetStatus(&client.StatusConfig{
Namespace: config.DefaultNamespace,
})
if err != nil {
return fmt.Errorf("error getting test status: %w", err)
}
for _, pluginStatus := range status.Plugins {
if pluginStatus.Plugin == "e2e" {
fmt.Print("e2e status ")
for label, count := range pluginStatus.ResultStatusCounts {
fmt.Printf("%s:%d ", label, count)
}
fmt.Println()
if pluginStatus.ResultStatus == "passed" {
e2ePassed = true
break
}
fmt.Println("\ne2e plugin logs:")
logF.Seek(0, io.SeekStart) //nolint:errcheck
io.Copy(os.Stdout, logF) //nolint:errcheck
return fmt.Errorf("e2e plugin status: %q", pluginStatus.ResultStatus)
}
}
if !e2ePassed {
return fmt.Errorf("missing e2e plugin status")
}
if options.RetrieveResults {
resultR, errCh, err := sclient.RetrieveResults(&client.RetrieveConfig{
Namespace: config.DefaultNamespace,
})
if err != nil {
return fmt.Errorf("error retrieving results: %w", err)
}
if resultR == nil {
return fmt.Errorf("no result reader")
}
gzipR, err := gzip.NewReader(resultR)
if err != nil {
return err
}
defer gzipR.Close() //nolint:errcheck
tarR := tar.NewReader(gzipR)
for {
var header *tar.Header
header, err = tarR.Next()
if err != nil {
if err == io.EOF {
break
}
return err
}
matched, _ := filepath.Match("*_sonobuoy_*.tar.gz", header.Name) //nolint:errcheck
if !matched {
continue
}
var innerGzipR *gzip.Reader
innerGzipR, err = gzip.NewReader(tarR)
if err != nil {
return err
}
defer innerGzipR.Close() //nolint:errcheck
innnerTarR := tar.NewReader(innerGzipR)
for {
header, err = innnerTarR.Next()
if err != nil {
if err == io.EOF {
break
}
return err
}
writeFile := func(name string) error {
var data []byte
data, err = io.ReadAll(innnerTarR)
if err != nil {
return err
}
return os.WriteFile(filepath.Join(options.ResultsPath, name), data, 0o644)
}
switch header.Name {
case "plugins/e2e/results/global/junit_01.xml":
if err = writeFile("junit_01.xml"); err != nil {
return err
}
case "plugins/e2e/results/global/e2e.log":
if err = writeFile("e2e.log"); err != nil {
return err
}
}
}
}
select {
case err = <-errCh:
if err != nil {
return err
}
default:
}
productInfo, err := yaml.Marshal(talos)
if err != nil {
return fmt.Errorf("error marshaling product info: %w", err)
}
if err = os.WriteFile(filepath.Join(options.ResultsPath, "PRODUCT.yaml"), productInfo, 0o644); err != nil {
return err
}
}
return cleanup()
}