Debian Codegraph @

launcher/internal/launchermount/tmpfs_test.go

raw
1 package launchermount
2
3 import (
4 "regexp"
5 "testing"
6
7 "github.com/google/go-cmp/cmp"
8 "github.com/opencontainers/runtime-spec/specs-go"
9 )
10
11 func TestCreateTmpfsMountAndSpecsMount(t *testing.T) {
12 var testCases = []struct {
13 testName string
14 mountMap map[string]string
15 expectedTmpfs TmpfsMount
16 expectedSpecsMount specs.Mount
17 }{
18 {
19 "Basic Tmpfs Mount",
20 map[string]string{
21 "type": "tmpfs",
22 "source": "tmpfs",
23 "destination": "/d",
24 },
25 TmpfsMount{Destination: "/d"},
26 specs.Mount{Type: TypeTmpfs,
27 Source: TypeTmpfs,
28 Destination: "/d",
29 Options: []string{"nosuid", "noexec", "nodev"},
30 },
31 },
32 {
33 "Tmpfs Mount with Size",
34 map[string]string{
35 "type": "tmpfs",
36 "source": "tmpfs",
37 "destination": "/my dest",
38 "size": "21342314",
39 },
40 TmpfsMount{Destination: "/my dest", Size: 21342314},
41 specs.Mount{Type: TypeTmpfs,
42 Source: TypeTmpfs,
43 Destination: "/my dest",
44 Options: []string{"nosuid", "noexec", "nodev", "size=21342314"},
45 },
46 },
47 {
48 "Tmpfs Mount with Relative Dst",
49 map[string]string{
50 "type": "tmpfs",
51 "source": "tmpfs",
52 "destination": "my dest",
53 "size": "21342314",
54 },
55 TmpfsMount{Destination: "/my dest", Size: 21342314},
56 specs.Mount{Type: TypeTmpfs,
57 Source: TypeTmpfs,
58 Destination: "/my dest",
59 Options: []string{"nosuid", "noexec", "nodev", "size=21342314"},
60 },
61 },
62 {
63 "Tmpfs Mount with Relative Dst More Complex Filepath",
64 map[string]string{
65 "type": "tmpfs",
66 "source": "tmpfs",
67 "destination": "grandparent dir/parentDir/my dest",
68 },
69 TmpfsMount{Destination: "/grandparent dir/parentDir/my dest"},
70 specs.Mount{Type: TypeTmpfs,
71 Source: TypeTmpfs,
72 Destination: "/grandparent dir/parentDir/my dest",
73 Options: []string{"nosuid", "noexec", "nodev"},
74 },
75 },
76 {
77 "Tmpfs Mount with Dst Internal Rel Parent",
78 map[string]string{
79 "type": "tmpfs",
80 "source": "tmpfs",
81 "destination": "grandparent dir/parentDir/../../my dest",
82 },
83 TmpfsMount{Destination: "/my dest"},
84 specs.Mount{Type: TypeTmpfs,
85 Source: TypeTmpfs,
86 Destination: "/my dest",
87 Options: []string{"nosuid", "noexec", "nodev"},
88 },
89 },
90 {
91 "Tmpfs Mount with Relative Dst Internal Cwd",
92 map[string]string{
93 "type": "tmpfs",
94 "source": "tmpfs",
95 "destination": "grandparent dir/parentDir/.././my dest",
96 },
97 TmpfsMount{Destination: "/grandparent dir/my dest"},
98 specs.Mount{Type: TypeTmpfs,
99 Source: TypeTmpfs,
100 Destination: "/grandparent dir/my dest",
101 Options: []string{"nosuid", "noexec", "nodev"},
102 },
103 },
104 {
105 "Tmpfs Mount with Malformed Relative Dst",
106 map[string]string{
107 "type": "tmpfs",
108 "source": "tmpfs",
109 "destination": "grandparent dir/parentDir/.../.../my dest",
110 },
111 TmpfsMount{Destination: "/grandparent dir/parentDir/.../.../my dest"},
112 specs.Mount{Type: TypeTmpfs,
113 Source: TypeTmpfs,
114 Destination: "/grandparent dir/parentDir/.../.../my dest",
115 Options: []string{"nosuid", "noexec", "nodev"},
116 },
117 },
118 {
119 "Tmpfs Mount with Parent Relative Dst",
120 map[string]string{
121 "type": "tmpfs",
122 "source": "tmpfs",
123 "destination": "../my dest",
124 },
125 TmpfsMount{Destination: "/my dest"},
126 specs.Mount{Type: TypeTmpfs,
127 Source: TypeTmpfs,
128 Destination: "/my dest",
129 Options: []string{"nosuid", "noexec", "nodev"},
130 },
131 },
132 {
133 "Tmpfs Mount with Grandparent Relative Dst",
134 map[string]string{
135 "type": "tmpfs",
136 "source": "tmpfs",
137 "destination": "../../my dest",
138 },
139 TmpfsMount{Destination: "/my dest"},
140 specs.Mount{Type: TypeTmpfs,
141 Source: TypeTmpfs,
142 Destination: "/my dest",
143 Options: []string{"nosuid", "noexec", "nodev"},
144 },
145 },
146 }
147 for _, testcase := range testCases {
148 t.Run(testcase.testName, func(t *testing.T) {
149 mnt, err := CreateTmpfsMount(testcase.mountMap)
150 if err != nil {
151 t.Errorf("got non-nil error %v, want nil error", err)
152 }
153 if diff := cmp.Diff(mnt, testcase.expectedTmpfs); diff != "" {
154 t.Errorf("got %v, want %v:\ndiff: %v", mnt, testcase.expectedTmpfs, diff)
155 }
156 spMnt := mnt.SpecsMount()
157 if diff := cmp.Diff(spMnt, testcase.expectedSpecsMount); diff != "" {
158 t.Errorf("got %v, want %v:\ndiff: %v", spMnt, testcase.expectedSpecsMount, diff)
159 }
160 })
161 }
162 }
163
164 func TestCreateTmpfsMountFail(t *testing.T) {
165 var testCases = []struct {
166 testName string
167 mountMap map[string]string
168 wantErr string
169 }{
170 {
171 "Bad Mount Type",
172 map[string]string{
173 "type": "tfs",
174 },
175 "received wrong mount type",
176 },
177 {
178 "Bad Mount Src",
179 map[string]string{
180 "type": "tmpfs",
181 "source": "tfffffs",
182 },
183 "received wrong mount source",
184 },
185 {
186 "No Dest",
187 map[string]string{
188 "type": "tmpfs",
189 "source": "tmpfs",
190 },
191 errTmpfsMustHaveDest.Error(),
192 },
193 {
194 "Bad Size",
195 map[string]string{
196 "type": "tmpfs",
197 "source": "tmpfs",
198 "destination": "dst",
199 "size": "notanum",
200 },
201 "failed to convert size option",
202 },
203 {
204 "Unknown Opts",
205 map[string]string{
206 "type": "tmpfs",
207 "source": "tmpfs",
208 "destination": "dst",
209 "size": "111",
210 "rw": "true",
211 },
212 "received unknown mount options for tmpfs mount",
213 },
214 }
215 for _, testcase := range testCases {
216 t.Run(testcase.testName, func(t *testing.T) {
217 if _, err := CreateTmpfsMount(testcase.mountMap); err == nil {
218 t.Errorf("got nil error, want non-nil error \"%v\"", testcase.wantErr)
219 } else {
220 if match, _ := regexp.MatchString(testcase.wantErr, err.Error()); !match {
221 t.Errorf("got error \"%v\", but expected \"%v\"", err, testcase.wantErr)
222 }
223 }
224 })
225 }
226 }