forked from containers/common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault_test.go
134 lines (124 loc) · 3.75 KB
/
default_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
package config
import (
"errors"
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func prepareProbeBinary(t *testing.T, expectedOutput string) (path string) {
f, err := ioutil.TempFile("", "conmon-")
require.Nil(t, err)
defer func() { require.Nil(t, f.Close()) }()
err = f.Chmod(os.FileMode(0o755))
require.Nil(t, err)
_, err = f.WriteString(fmt.Sprintf("#!/usr/bin/env sh\necho %q", expectedOutput))
require.Nil(t, err)
return f.Name()
}
func TestProbeConmon(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
msg string
output string
assert func(error, string)
}{
{
msg: "success conmon 2",
output: "conmon version 2.1.0",
assert: func(err error, msg string) {
assert.Nil(t, err, msg)
},
},
{
msg: "success conmon 3",
output: "conmon version 3.0.0-dev",
assert: func(x error, msg string) {
assert.Nil(t, x, msg)
},
},
{
msg: "failure outdated version",
output: "conmon version 1.0.0",
assert: func(err error, msg string) {
assert.Equal(t, err, ErrConmonOutdated, msg)
},
},
{
msg: "failure invalid format",
output: "invalid",
assert: func(err error, msg string) {
expectedErr := fmt.Errorf(_conmonVersionFormatErr, errors.New("invalid version format"))
assert.Equal(t, err, expectedErr, msg)
},
},
} {
filePath := prepareProbeBinary(t, tc.output)
defer os.RemoveAll(filePath)
err := probeConmon(filePath)
tc.assert(err, tc.msg)
}
}
func TestMachineVolumes(t *testing.T) {
t.Parallel()
os.Setenv("env1", "/test1")
os.Setenv("env2", "/test2")
for _, tc := range []struct {
msg string
volumes []string
output []string
assert func(err error, in, out []string, msg string)
}{
{
volumes: []string{},
output: []string{},
assert: func(err error, in, out []string, msg string) {
assert.Equal(t, in, out)
assert.Nil(t, err, msg)
},
},
{
volumes: []string{"/foobar:/foobar", "/foobar1:/foobardest:ro", "$env1:/env1", "$env1:$env1", "$env2:$env1"},
output: []string{"/foobar:/foobar", "/foobar1:/foobardest:ro", "/test1:/env1", "/test1:/test1", "/test2:/test1"},
assert: func(err error, in, out []string, msg string) {
assert.Equal(t, in, out)
assert.Nil(t, err, msg)
},
},
{
volumes: []string{"/foobar:/foobar", "/foobar1:/foobardest:ro", "$env1:/env1", "$env1:$env1", "$env3:$env1"},
output: []string{"/foobar:/foobar", "/foobar1:/foobardest:ro", "/test1:/env1", "/test1:/test1", "/test2:/test1"},
assert: func(err error, in, out []string, msg string) {
assert.EqualError(t, err, "invalid machine volume $env3:$env1, fields must container data", msg)
},
},
{
volumes: []string{"/foobar:/foobar", "/foobar1:/foobardest:ro", "$env1:/env1", "$env1:$env4", "$env1:$env1"},
output: []string{"/foobar:/foobar", "/foobar1:/foobardest:ro", "/test1:/env1", "/test1:/test1", "/test2:/test1"},
assert: func(err error, in, out []string, msg string) {
assert.EqualError(t, err, "invalid machine volume $env1:$env4, fields must container data", msg)
},
},
{
msg: "This is broken",
volumes: []string{"/foobar:/foobar:ro:badopt"},
output: []string{"/foobar:/foobar:ro"},
assert: func(err error, in, out []string, msg string) {
assert.EqualError(t, err, "invalid machine volume /foobar:/foobar:ro:badopt, 2 or 3 fields required", msg)
},
},
{
msg: "This is broken2",
volumes: []string{"/foobar"},
output: []string{"/foobar:/foobar:ro"},
assert: func(err error, in, out []string, msg string) {
assert.EqualError(t, err, "invalid machine volume /foobar, 2 or 3 fields required", msg)
},
},
} {
output, err := machineVolumes(tc.volumes)
tc.assert(err, tc.output, output, tc.msg)
}
}