Skip to content

Commit 0cb7dbf

Browse files
committed
Add chmod tests
1 parent e453d9a commit 0cb7dbf

File tree

4 files changed

+89
-22
lines changed

4 files changed

+89
-22
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ go mod tidy
2121
```
2222

2323
## Test
24-
uid/gid - default & custom
25-
26-
mode - default & custom
27-
2824
inspect - should export status fields
2925

3026
# Extra
27+
validate options
28+
3129
if _, err := exec.LookPath("mkfs.xfs"); err != nil {
3230
logrus.Fatal("mkfs.xfs is not available, please install xfsprogs to continue")
3331
}

driver/driver.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,18 @@ func (d Driver) Create(req *v.CreateRequest) error {
150150
Msg("will parse as octal")
151151

152152
modeParsed, err := strconv.ParseUint(modeStr, 8, 32)
153-
if err != nil || modeParsed <= 0 || modeParsed > 07777 {
153+
if err != nil {
154154
return errors.Wrapf(err,
155-
"Error creating volume '%s' - cannot parse mode '%s' as 4-position octal",
155+
"Error creating volume '%s' - cannot parse mode '%s' as positive 4-position octal",
156+
req.Name, modeStr)
157+
}
158+
159+
if modeParsed <= 0 || modeParsed > 07777 {
160+
return errors.Errorf(
161+
"Error creating volume '%s' - mode value '%s' does not fall between 0 and 7777 in octal encoding",
156162
req.Name, modeStr)
157163
}
164+
158165
mode = uint32(modeParsed)
159166
}
160167

manager/manager.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,18 @@ func (m Manager) Create(name string, sizeInBytes int64, sparse bool, fs string,
197197
}
198198

199199
// format data file
200-
_, err = exec.Command(fmt.Sprintf("mkfs.%s", fs), append(mkfsFlags, dataFilePath)...).Output()
200+
errBytes, err := exec.Command("mkfs."+fs, append(mkfsFlags, dataFilePath)...).CombinedOutput()
201201
if err != nil {
202+
errStr := strings.TrimSpace(string(errBytes[:]))
202203
_ = os.Remove(dataFilePath) // attempt to cleanup
203204
return errors.Wrapf(err,
204-
"Error creating volume '%s' - cannot format datafile as %s filesystem",
205-
name, fs)
205+
"Error creating volume '%s' - cannot format datafile as %s filesystem: %s",
206+
name, fs, errStr)
206207
}
207208

208-
// At this point we're done - last step is to adjust ownership if required.
209-
if uid >= 0 || gid >= 0 {
209+
// At this point we're done - last step is to adjust ownership and mode if required.
210+
211+
if uid >= 0 || gid >= 0 || mode > 0 {
210212
lease := "driver"
211213

212214
mountPath, err := m.Mount(name, lease)
@@ -216,24 +218,27 @@ func (m Manager) Create(name string, sizeInBytes int64, sparse bool, fs string,
216218
"Error creating volume '%s' - cannot mount volume to adjust its root owner/permissions",
217219
name)
218220
}
219-
220221
if mode > 0 {
221-
err = os.Chmod(mountPath, os.FileMode(mode))
222+
errBytes, err := exec.Command("chmod", fmt.Sprintf("%#o", mode), mountPath).CombinedOutput()
222223
if err != nil {
224+
errStr := strings.TrimSpace(string(errBytes[:]))
223225
_ = m.UnMount(name, lease)
224226
_ = os.Remove(dataFilePath) // attempt to cleanup
225227
return errors.Wrapf(err,
226-
"Error creating volume '%s' - cannot adjust volume root permissions",
227-
name)
228+
"Error creating volume '%s' - cannot adjust volume root permissions: %s",
229+
name, errStr)
228230
}
229231
}
230-
err = os.Chown(mountPath, uid, gid)
231-
if err != nil {
232-
_ = m.UnMount(name, lease)
233-
_ = os.Remove(dataFilePath) // attempt to cleanup
234-
return errors.Wrapf(err,
235-
"Error creating volume '%s' - cannot adjust volume root owner",
236-
name)
232+
233+
if uid >= 0 || gid >= 0 {
234+
err = os.Chown(mountPath, uid, gid)
235+
if err != nil {
236+
_ = m.UnMount(name, lease)
237+
_ = os.Remove(dataFilePath) // attempt to cleanup
238+
return errors.Wrapf(err,
239+
"Error creating volume '%s' - cannot adjust volume root owner",
240+
name)
241+
}
237242
}
238243

239244
err = m.UnMount(name, lease)

tests/test_chmod.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env bash
2+
3+
testDefaultMode() {
4+
local volume mode
5+
# setup
6+
volume=$(docker volume create -d "${DRIVER}")
7+
8+
# stat produces some funny characters at the end so we want to clean them up
9+
mode=$(docker run --rm -it -v "${volume}:/vol" "${IMAGE}" stat -c '%a ' /vol | tr -cd '[0-7]')
10+
11+
# check
12+
assertEquals "Default mode should be 755" "755" "${mode}"
13+
14+
# cleanup
15+
docker volume rm "${volume}" > /dev/null
16+
}
17+
18+
19+
testCustomMode() {
20+
local volume mode
21+
# setup
22+
volume=$(docker volume create -d "${DRIVER}" -o mode=7777)
23+
24+
# stat produces some funny characters at the end so we want to clean them up
25+
mode=$(docker run --rm -it -v "${volume}:/vol" "${IMAGE}" stat -c '%a ' /vol | tr -cd '[0-7]')
26+
27+
# check
28+
assertEquals "Mode should be 7777" "7777" "${mode}"
29+
30+
# cleanup
31+
docker volume rm "${volume}" > /dev/null
32+
}
33+
34+
testZeroMode() {
35+
local volume info error result
36+
# setup
37+
error=$(docker volume create -d "${DRIVER}" -o mode=0 2>&1)
38+
result=$?
39+
40+
# checks
41+
assertEquals "Volume creation should fail" "1" "${result}"
42+
assertContains "${error}" "mode value '0' does not fall between 0 and 7777 in octal encoding"
43+
}
44+
45+
testInvalidMode() {
46+
local volume info error result
47+
# setup
48+
error=$(docker volume create -d "${DRIVER}" -o mode=x 2>&1)
49+
result=$?
50+
51+
# checks
52+
assertEquals "Volume creation should fail" "1" "${result}"
53+
assertContains "${error}" "cannot parse mode 'x' as positive 4-position octal: strconv.ParseUint: parsing \"x\": invalid syntax"
54+
}
55+
56+
57+
. test.sh

0 commit comments

Comments
 (0)