-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
104 lines (83 loc) · 2.47 KB
/
main.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
// Copyright 2020 Authors of Cilium
// SPDX-License-Identifier: Apache-2.0
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"runtime"
"github.com/GoogleContainerTools/container-structure-test/cmd/container-structure-test/app/cmd/test"
"github.com/GoogleContainerTools/container-structure-test/pkg/color"
"github.com/GoogleContainerTools/container-structure-test/pkg/drivers"
"github.com/GoogleContainerTools/container-structure-test/pkg/types/unversioned"
)
const (
configFile = "spec.yaml"
)
/* container-structure-test can be used inside a container, however multiple flags have to be set and
metadata file has to be provided also, namely:
/usr/local/bin/container-structure-test --force --driver host --metadata /tmp/metadata.json --config /test/spec.yaml
this version eliminates all of the flags, stubs out metadata and expects to find test specs at
/test/spec.yaml, so the invocation is as simple as:
/test/bin/cst
*/
func main() {
version := flag.Bool("V", false, "print version and exit")
testDir := flag.String("C", "/test", "directory to chdir, and read `spec.yaml`")
flag.Parse()
if *version {
fmt.Printf("%s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH)
os.Exit(0)
}
color.NoColor = true
if err := os.Chdir(*testDir); err != nil {
fmt.Printf("unable to run tests: %s\n", err)
os.Exit(5)
}
fakeMetadataPath, err := fakeMetadata()
if err != nil {
fmt.Printf("unable to write fake metadata: %s\n", err)
os.Exit(4)
}
defer os.Remove(fakeMetadataPath)
driverConfig := &drivers.DriverConfig{
Metadata: fakeMetadataPath,
}
channel := make(chan interface{}, 1)
go func() {
tests, err := test.Parse(configFile, driverConfig, drivers.InitDriverImpl(drivers.Host))
if err != nil {
channel <- &unversioned.TestResult{
Errors: []string{
fmt.Sprintf("error parsing config file: %s", err),
},
}
fmt.Printf("failed to load test spec: %s\n", err)
os.Exit(3)
}
if tests == nil {
fmt.Printf("failed to test: no tests\n")
os.Exit(2)
}
tests.RunAll(channel, configFile)
close(channel)
}()
if err := test.ProcessResults(os.Stdout, unversioned.Text, channel); err != nil {
os.Exit(1)
}
}
func fakeMetadata() (string, error) {
content := []byte(`{ "config": {} }`)
file, err := ioutil.TempFile("", "metadata")
if err != nil {
return "", err
}
if _, err := file.Write(content); err != nil {
return "", err
}
if err := file.Close(); err != nil {
return "", err
}
return file.Name(), nil
}