-
Notifications
You must be signed in to change notification settings - Fork 193
/
command.go
136 lines (123 loc) · 4.29 KB
/
command.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
// Copyright 2017 Google Inc. All rights reserved.
// 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 v2
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/GoogleContainerTools/container-structure-test/pkg/drivers"
types "github.com/GoogleContainerTools/container-structure-test/pkg/types/unversioned"
"github.com/GoogleContainerTools/container-structure-test/pkg/utils"
)
type CommandTest struct {
Name string `yaml:"name"`
Setup [][]string `yaml:"setup"`
Teardown [][]string `yaml:"teardown"`
EnvVars []types.EnvVar `yaml:"envVars"`
ExitCode int `yaml:"exitCode"`
Command string `yaml:"command"`
Args []string `yaml:"args"`
ExpectedOutput []string `yaml:"expectedOutput"`
ExcludedOutput []string `yaml:"excludedOutput"`
ExpectedError []string `yaml:"expectedError"`
ExcludedError []string `yaml:"excludedError"` // excluded error from running command
}
func (ct *CommandTest) Validate(channel chan interface{}) bool {
res := &types.TestResult{}
if ct.Name == "" {
res.Error("Please provide a valid name for every test")
}
res.Name = ct.Name
if ct.Command == "" {
res.Errorf("Please provide a valid command to run for test %s", ct.Name)
}
if ct.Setup != nil {
for _, c := range ct.Setup {
if len(c) == 0 {
res.Error("Error in setup command configuration encountered; please check formatting and remove all empty setup commands")
}
}
}
if ct.Teardown != nil {
for _, c := range ct.Teardown {
if len(c) == 0 {
res.Error("Error in teardown command configuration encountered; please check formatting and remove all empty teardown commands")
}
}
}
if ct.EnvVars != nil {
for _, envVar := range ct.EnvVars {
if envVar.Key == "" || envVar.Value == "" {
res.Error("Please provide non-empty keys and values for all specified env vars")
}
}
}
if len(res.Errors) > 0 {
channel <- res
return false
}
return true
}
func (ct *CommandTest) LogName() string {
return fmt.Sprintf("Command Test: %s", ct.Name)
}
func (ct *CommandTest) Run(driver drivers.Driver) *types.TestResult {
logrus.Debug(ct.LogName())
config, err := driver.GetConfig()
if err != nil {
logrus.Errorf("error retrieving image config: %s", err.Error())
}
fullCommand := utils.SubstituteEnvVars(append([]string{ct.Command}, ct.Args...), config.Env)
stdout, stderr, exitcode, err := driver.ProcessCommand(ct.EnvVars, fullCommand)
result := &types.TestResult{
Name: ct.LogName(),
Pass: true,
Errors: make([]string, 0),
Stderr: stderr,
Stdout: stdout,
}
if err != nil {
result.Fail()
result.Error(err.Error())
return result
}
ct.CheckOutput(result, stdout, stderr, exitcode)
return result
}
func (ct *CommandTest) CheckOutput(result *types.TestResult, stdout string, stderr string, exitCode int) {
for _, errStr := range ct.ExpectedError {
if !utils.CompileAndRunRegex(errStr, stderr, true) {
result.Errorf("Expected string '%s' not found in error '%s'", errStr, stderr)
result.Fail()
}
}
for _, errStr := range ct.ExcludedError {
if !utils.CompileAndRunRegex(errStr, stderr, false) {
result.Errorf("Excluded string '%s' found in error '%s'", errStr, stderr)
result.Fail()
}
}
for _, outStr := range ct.ExpectedOutput {
if !utils.CompileAndRunRegex(outStr, stdout, true) {
result.Errorf("Expected string '%s' not found in output '%s'", outStr, stdout)
result.Fail()
}
}
for _, outStr := range ct.ExcludedOutput {
if !utils.CompileAndRunRegex(outStr, stdout, false) {
result.Errorf("Excluded string '%s' found in output '%s'", outStr, stdout)
result.Fail()
}
}
if ct.ExitCode != exitCode {
result.Errorf("Test '%s' exited with incorrect error code. Expected: %d, Actual: %d", ct.Name, ct.ExitCode, exitCode)
result.Fail()
}
}