-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils_test.go
198 lines (171 loc) · 6.21 KB
/
utils_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
193
194
195
196
197
198
package main
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestLog_UnsupportedLevel(t *testing.T) {
logger := NewLogger("info")
defer func() {
if r := recover(); r != nil {
expMsg := "Unsupported log level: unsupported"
if r.(string) != expMsg {
t.Fatal(fmt.Sprintf("Expected panic message: %v, but was: %v", expMsg, r.(string)))
}
}
}()
logger.Log("unsupported", "abc")
t.Fatal("Expected panic, but no panic")
}
func TestLog_Info(t *testing.T) {
logger := NewLogger("info")
// set custom Log output target
var str bytes.Buffer
logger.SetOutput(&str)
logger.Log("info", "hello")
output := strings.TrimSuffix(str.String(), "\n")
assert.Contains(t, output, "INFO")
assert.Contains(t, output, "hello")
}
func TestLog_Debug(t *testing.T) {
logger := NewLogger("debug")
// set custom Log output target
var str bytes.Buffer
logger.SetOutput(&str)
logger.Log("debug", "my debug msg")
output := strings.TrimSuffix(str.String(), "\n")
assert.Contains(t, output, "DEBUG")
assert.Contains(t, output, "my debug msg")
}
func TestLog_MixLevel_DebugUnset(t *testing.T) {
logger := NewLogger("info")
// set custom Log output target
var str bytes.Buffer
logger.SetOutput(&str)
logger.Log("info", "hello")
logger.Log("debug", "my debug msg")
output := strings.TrimSuffix(str.String(), "\n")
assert.Contains(t, output, "INFO")
assert.Contains(t, output, "hello")
assert.NotContains(t, output, "DEBUG")
assert.NotContains(t, output, "my debug msg")
}
func TestLog_MixLevel_DebugSet(t *testing.T) {
logger := NewLogger("debug")
// set custom Log output target
var str bytes.Buffer
logger.SetOutput(&str)
logger.Log("info", "hello")
logger.Log("debug", "my debug msg")
output := strings.TrimSuffix(str.String(), "\n")
assert.Contains(t, output, "INFO")
assert.Contains(t, output, "hello")
assert.Contains(t, output, "DEBUG")
assert.Contains(t, output, "my debug msg")
}
func logSth(logger *Logger) {
logger.Log("debug", "logging sth")
}
// When you run this test separately, you see output.
func TestLog_MixLevel_ForHuman(t *testing.T) {
logger := NewLogger("debug")
// set custom Log output target
var str bytes.Buffer
logger.SetOutput(&str)
logger.Log("info", "hello")
logger.Log("debug", "my debug msg")
logSth(logger)
}
func Test_getRunID(t *testing.T) {
runID := getRunID("false")
assert.Contains(t, runID, "dojo-")
// runID must be lowercase
lowerCaseRunID := strings.ToLower(runID)
assert.Equal(t, lowerCaseRunID, runID)
runID = getRunID("true")
assert.Equal(t, "testdojorunid", runID)
// runID must be lowercase
lowerCaseRunID = strings.ToLower(runID)
assert.Equal(t, lowerCaseRunID, runID)
}
func Test_getRunIDGenerateFromCurrentDir(t *testing.T) {
// lower case letters only
runID := getRunIDGenerateFromCurrentDir("mydir")
assert.True(t, strings.HasPrefix(runID, "dojo-mydir-"))
// lower case and upper case letters
runID = getRunIDGenerateFromCurrentDir("mYdIR")
assert.True(t, strings.HasPrefix(runID, "dojo-mydir-"))
// lower case and upper case letters and spaces
runID = getRunIDGenerateFromCurrentDir("mYdIR with spaces")
assert.True(t, strings.HasPrefix(runID, "dojo-mydirwithspaces-"))
// lower case and upper case letters and spaces and special characters
runID = getRunIDGenerateFromCurrentDir("mYdIR wi#th s(3paces")
assert.True(t, strings.HasPrefix(runID, "dojo-mydirwiths3paces-"))
}
func getTestConfig() Config {
config := getDefaultConfig("somefile")
config.DockerImage = "img:1.2.3"
// set these to some dummy dir, so that tests work also if not run in dojo docker image
config.WorkDirOuter = "/tmp/bla"
config.IdentityDirOuter = "/tmp/myidentity"
return config
}
func Test_removeWhiteSpaces(t *testing.T) {
str := `
aaa
bb
`
actual := removeWhiteSpaces(str)
assert.Equal(t, "aaabb", actual)
}
func Test_getContainerInfo(t *testing.T) {
logger := NewLogger("debug")
commandsReactions := make(map[string]interface{}, 0)
fakeOutput := `1234 /name1 running 133`
commandsReactions["docker inspect --format='{{.Id}} {{.Name}} {{.State.Status}} {{.State.ExitCode}}' 1234"] =
[]string{fakeOutput, "", "0"}
shell := NewMockedShellServiceNotInteractive2(logger, commandsReactions)
info, err := getContainerInfo(shell, "1234")
assert.Equal(t, "1234", info.ID)
assert.Equal(t, "name1", info.Name)
assert.Equal(t, "running", info.Status)
assert.Equal(t, "133", info.ExitCode)
assert.Equal(t, nil, err)
assert.Equal(t, true, info.Exists)
}
func Test_getContainerInfo_NoSuchObject(t *testing.T) {
logger := NewLogger("debug")
commandsReactions := make(map[string]interface{}, 0)
commandsReactions["docker inspect --format='{{.Id}} {{.Name}} {{.State.Status}} {{.State.ExitCode}}' 1234"] =
[]string{"", "Error: No such object: 1234", "1"}
shell := NewMockedShellServiceNotInteractive2(logger, commandsReactions)
info, err := getContainerInfo(shell, "1234")
assert.Equal(t, "", info.ID)
assert.Equal(t, "", info.Status)
assert.Equal(t, nil, err)
assert.Equal(t, false, info.Exists)
}
func Test_toPrintOrNotToPrint(t *testing.T) {
assert.Equal(t, true, toPrintOrNotToPrint("debug", "debug"))
assert.Equal(t, false, toPrintOrNotToPrint("debug", "info"))
assert.Equal(t, false, toPrintOrNotToPrint("debug", "warn"))
assert.Equal(t, false, toPrintOrNotToPrint("debug", "error"))
assert.Equal(t, false, toPrintOrNotToPrint("debug", "silent"))
assert.Equal(t, true, toPrintOrNotToPrint("info", "debug"))
assert.Equal(t, true, toPrintOrNotToPrint("info", "info"))
assert.Equal(t, false, toPrintOrNotToPrint("info", "warn"))
assert.Equal(t, false, toPrintOrNotToPrint("info", "error"))
assert.Equal(t, false, toPrintOrNotToPrint("info", "silent"))
assert.Equal(t, true, toPrintOrNotToPrint("warn", "debug"))
assert.Equal(t, true, toPrintOrNotToPrint("warn", "info"))
assert.Equal(t, true, toPrintOrNotToPrint("warn", "warn"))
assert.Equal(t, false, toPrintOrNotToPrint("warn", "error"))
assert.Equal(t, false, toPrintOrNotToPrint("warn", "silent"))
assert.Equal(t, true, toPrintOrNotToPrint("error", "debug"))
assert.Equal(t, true, toPrintOrNotToPrint("error", "info"))
assert.Equal(t, true, toPrintOrNotToPrint("error", "warn"))
assert.Equal(t, true, toPrintOrNotToPrint("error", "error"))
assert.Equal(t, false, toPrintOrNotToPrint("error", "silent"))
}