-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
199 lines (171 loc) · 5.06 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
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
package main
import (
"fmt"
"io/ioutil"
"log"
"os/exec"
"strings"
"gopkg.in/yaml.v2"
)
// Type of test what define at the Test.Name
const (
TestAuth = "[Authentication]"
TestCreation = "[Creation]"
TestStability = "[Stability]"
TestForbidden = "[Forbidden]"
)
type Config struct {
GlobalVariables map[string]string `yaml:"globalVariables"`
Tests []Test `yaml:"testsForDockerContainerAuthPlugin"`
}
type Test struct {
Number int `yaml:"number"`
Name string `yaml:"name"`
Value string `yaml:"value"`
Result string `yaml:"result"`
}
func PrintLines() {
fmt.Println("-----------------------------------")
}
func Performer(command string) string {
args := strings.Fields(command)
cmd := exec.Command(args[0], args[1:]...)
// Get output
output, _ := cmd.CombinedOutput()
return string(output)
}
func Testing(caseOfTest Test, containerid string) []string {
lines := strings.Split(caseOfTest.Value, "\n")
var results []string
for _, i := range lines {
var command string
if i == "" {
continue
}
// Is it a string, where i need to put a containerid
if strings.Contains(i, "%s") {
command = fmt.Sprintf(i, containerid)
} else {
command = i
}
// do this command
result := Performer(command)
result = strings.TrimRight(result, "\n")
linesOfResult := strings.Split(result, "\n")
if len(linesOfResult) > 1 {
for index, info := range linesOfResult {
// if docker daemon give us "See docker --help" See test #19
if strings.Contains(info, "--help") {
linesOfResult = append(linesOfResult[:index], linesOfResult[index+1:]...)
}
}
results = append(results, linesOfResult...)
} else {
results = append(results, result)
}
}
return results
}
func arraysEqual(arr1, arr2 []string) bool {
if len(arr1) != len(arr2) {
return false
}
for i := 0; i < len(arr1); i++ {
if arr1[i] != arr2[i] {
return false
}
}
return true
}
func findNotEqualArrays(arrays [][]string) (int, int) {
for i := 0; i < len(arrays); i++ {
for j := i + 1; j < len(arrays); j++ {
if !arraysEqual(arrays[i], arrays[j]) {
return i, j
}
}
}
return -1, -1
}
func main() {
yfile, err := ioutil.ReadFile("tests/testCases.yaml")
if err != nil {
log.Fatalf("cannot open: %v", err)
}
var config Config
err = yaml.Unmarshal(yfile, &config)
if err != nil {
log.Fatalf("cannot unmarshal data: %v", err)
}
containerid := config.GlobalVariables["ContainerID"]
nameOfContainer := config.GlobalVariables["NameOfContainer"]
fullContainerID := config.GlobalVariables["FullContainerID"]
for _, test := range config.Tests {
var wait string
var results []string
if strings.HasPrefix(test.Name, TestAuth) {
wait = fmt.Sprintf(test.Result, config.GlobalVariables["ErrorAuthMessage"])
wait = strings.TrimRight(wait, "\n")
// Check a lot of types to call container
results = Testing(test, containerid)
resultsOfCallnameOfContainer := Testing(test, nameOfContainer)
resultsOfCallFullContainerID := Testing(test, fullContainerID)
resultsOfCallLessThan12 := Testing(test, fullContainerID[:8])
resultsOfCallLessThan64 := Testing(test, fullContainerID[:32])
arrays := [][]string{results, resultsOfCallnameOfContainer,
resultsOfCallFullContainerID, resultsOfCallLessThan12, resultsOfCallLessThan64}
i, j := findNotEqualArrays(arrays)
if i != -1 && j != -1 {
fmt.Printf("Массивы %s \n\tи %s не равны.\n", arrays[i+1], arrays[j+1])
}
}
if strings.HasPrefix(test.Name, TestCreation) {
template := strings.TrimRight(config.GlobalVariables["ErrorCreationMessage"], "\n")
wait = fmt.Sprintf(test.Result, template)
wait = strings.TrimRight(wait, "\n")
results = Testing(test, containerid)
}
if strings.HasPrefix(test.Name, TestStability) {
template := strings.TrimRight(config.GlobalVariables["WhatIdontExpectFromStabilitisTests"], "\n")
wait = fmt.Sprintf(test.Result, template)
wait = strings.TrimRight(wait, "\n")
results = Testing(test, containerid)
}
if strings.HasPrefix(test.Name, TestForbidden) {
template := strings.TrimRight(config.GlobalVariables["ForbiddenMessage"], "\n")
wait = fmt.Sprintf(test.Result, template)
wait = strings.TrimRight(wait, "\n")
results = Testing(test, containerid)
}
testIsOK := false
isItTestInStability := strings.Contains(test.Name, TestStability)
for i, _ := range results {
// forbbiden make this if with contains
if results[i] == wait {
if isItTestInStability {
PrintLines()
msg := fmt.Sprintf("Test number #%d FAILED! I've got: %s, \n\tbut expect: %s", test.Number, results, wait)
fmt.Println(msg)
testIsOK = false
break
}
PrintLines()
msg := fmt.Sprintf("Test number #%d OK!", test.Number)
fmt.Println(msg)
testIsOK = true
break
}
}
if !testIsOK {
if isItTestInStability {
PrintLines()
msg := fmt.Sprintf("Test number #%d OK!", test.Number)
fmt.Println(msg)
continue
}
PrintLines()
msg := fmt.Sprintf("Test number #%d FAILED! I've got: %s, \n\tbut expect: %s", test.Number, results, wait)
fmt.Println(msg)
}
}
}