-
Notifications
You must be signed in to change notification settings - Fork 26
/
main_test.go
150 lines (142 loc) · 4.11 KB
/
main_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
package main
import (
"encoding/json"
"fmt"
"os"
"reflect"
"testing"
)
func TestNormalizeAllowPath(t *testing.T) {
tests := []struct {
input string
useConf bool
expected string
}{
{
input: "data.policy.rule",
useConf: true,
expected: "/policy/rule",
},
{
input: "data.policy.rule",
useConf: false,
expected: "data.policy.rule",
},
{
input: "/policy/rule",
useConf: true,
expected: "/policy/rule",
},
{
input: "/policy/rule",
useConf: false,
expected: "data.policy.rule",
},
{
input: "",
useConf: true,
expected: "",
},
}
for _, tc := range tests {
t.Run("Normalize allowPath", func(t *testing.T) {
result := normalizeAllowPath(tc.input, tc.useConf)
if result != tc.expected {
t.Errorf("Expected %v, got %v", tc.expected, result)
}
})
}
}
func TestListBindMounts(t *testing.T) {
dotDotPath := fmt.Sprintf("%s/../../../../", t.TempDir())
symlinkSourcePath := t.TempDir()
symlinkTargetPath := fmt.Sprintf("%s/target", t.TempDir())
err := os.Symlink(symlinkSourcePath, symlinkTargetPath)
if err != nil {
t.Fatalf("Failed to symlink '%s' to '%s' - got %v", symlinkSourcePath, symlinkTargetPath, err)
}
tests := []struct {
statement string
input string
expected []BindMount
}{
{
statement: "parse a simple bind list",
input: `{ "HostConfig": { "Binds" : [ "/var:/home", "volume:/var/lib/app:ro" ] } }`,
expected: []BindMount{{"/var", false, "/var"}},
},
{
statement: "expand ..",
input: fmt.Sprintf(`{ "HostConfig": { "Binds" : [ "%s:/host" ] } }`, dotDotPath),
expected: []BindMount{{dotDotPath, false, "/"}},
},
{
statement: "resolve symlinks",
input: fmt.Sprintf(`{ "HostConfig": { "Binds" : [ "%s:/host" ] } }`, symlinkTargetPath),
expected: []BindMount{{symlinkTargetPath, false, symlinkSourcePath}},
},
{
statement: "parse the readonly attribute",
input: `{ "HostConfig": { "Binds" : [ "/var:/home:ro", "/var/lib:/mnt:rw" ] } }`,
expected: []BindMount{{"/var", true, "/var"}, {"/var/lib", false, "/var/lib"}},
},
{
statement: "handle when neither bind nor mounts provided",
input: `{ "HostConfig": {} }`,
expected: []BindMount{},
},
{
statement: "handle an invalid binds list",
input: `{ "HostConfig": { "Binds" : null } }`,
expected: []BindMount{},
},
{
statement: "handle an empty binds list",
input: `{ "HostConfig": { "Binds" : [] } }`,
expected: []BindMount{},
},
{
statement: "parse a mount list",
input: `{ "HostConfig": { "Mounts" : [
{ "Source": "/var", "Target": "/mnt", "Type": "bind" },
{ "Source": "vol", "Target": "/vol", "Type": "volume", "Labels":{"color":"red"} }
] } }`,
expected: []BindMount{{"/var", false, "/var"}},
},
{
statement: "parse a readonly mount list",
input: `{ "HostConfig": { "Mounts" : [
{ "Source": "/var", "Target": "/mnt", "Type": "bind", "ReadOnly": true },
{ "Source": "/home", "Target": "/home", "Type": "bind" }
] } }`,
expected: []BindMount{{"/var", true, "/var"}, {"/home", false, "/home"}},
},
{
statement: "ignore an invalid mount list",
input: `{ "HostConfig": { "Mounts" : [
{ "Source": "/var", "Target": "/mnt", "Type": "bind", "ReadOnly": true },
{ "Source1": "/home", "Target": "/home", "Type": "bind" }
] } }`,
expected: []BindMount{{"/var", true, "/var"}},
},
{
statement: "ignore a mount list of the wrong type, whlile reading binds",
input: `{ "HostConfig": { "Binds": ["/var:/mnt/var:ro","/home:/home"],
"Mounts" : null } }`,
expected: []BindMount{{"/var", true, "/var"}, {"/home", false, "/home"}},
},
}
for _, tc := range tests {
t.Run("listBindMounts should "+tc.statement, func(t *testing.T) {
var body map[string]interface{}
err := json.Unmarshal([]byte(tc.input), &body)
if err != nil {
t.Fatalf("Improper JSON input - got %v for '%s'", err, tc.input)
}
result := listBindMounts(body)
if len(result) > 0 && len(tc.expected) > 0 && !reflect.DeepEqual(result, tc.expected) {
t.Errorf("Expected %v, got %v", tc.expected, result)
}
})
}
}