Skip to content

Commit fd53871

Browse files
committed
Test listing volumes
1 parent 0cb7dbf commit fd53871

13 files changed

+219
-130
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ go mod vendor
2020
go mod tidy
2121
```
2222

23-
## Test
24-
inspect - should export status fields
25-
2623
# Extra
2724
validate options
2825

Vagrantfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Vagrant.configure(2) do |config|
22
config.vm.box = "ubuntu/trusty64"
33
config.vm.provision :docker
4+
config.vm.provision "shell", inline: "apt update && apt install -y jq xfsprogs"
45
end

driver/driver.go

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package driver
33
import (
44
"fmt"
55
"os"
6+
"sort"
67
"strconv"
78
"strings"
89
"sync"
@@ -30,6 +31,8 @@ type Driver struct {
3031
sync.Mutex
3132
}
3233

34+
var AllowedOptions = []string{"size", "sparse", "fs", "uid", "gid", "mode"}
35+
3336
func NewDriver(cfg Config) (d Driver, err error) {
3437
if cfg.DefaultSize == "" {
3538
err = errors.Errorf("DefaultSize must be specified")
@@ -64,6 +67,26 @@ func (d Driver) Create(req *v.CreateRequest) error {
6467
Str("opts-size", req.Options["size"]).
6568
Logger()
6669

70+
// 1st - let's validate incoming option names
71+
allowedOptionsSet := make(map[string]struct{})
72+
for _, option := range AllowedOptions {
73+
allowedOptionsSet[option] = struct{}{}
74+
}
75+
var wrongOptions []string
76+
for k := range req.Options {
77+
_, allowed := allowedOptionsSet[k]
78+
if !allowed {
79+
wrongOptions = append(wrongOptions, k)
80+
}
81+
}
82+
if len(wrongOptions) > 0 {
83+
sort.Strings(wrongOptions)
84+
return errors.Errorf(
85+
"options '%s' are not among supported ones: %s",
86+
strings.Join(wrongOptions, ", "), strings.Join(AllowedOptions, ", "))
87+
}
88+
89+
// 2nd - parse 'size' option if present
6790
size, sizePresent := req.Options["size"]
6891

6992
if !sizePresent {
@@ -75,22 +98,20 @@ func (d Driver) Create(req *v.CreateRequest) error {
7598

7699
sizeInBytes, err := FromHumanSize(size)
77100
if err != nil {
78-
return errors.Errorf(
79-
"couldn't convert specified size [%s] into bytes",
80-
size)
101+
return errors.Errorf("cannot convert 'size' option value '%s' into bytes", size)
81102
}
82103

104+
// 3rd - parse 'sparse' option if present
83105
sparse := false
84106
sparseStr, sparsePresent := req.Options["sparse"]
85107
if sparsePresent {
86108
sparse, err = strconv.ParseBool(sparseStr)
87109
if err != nil {
88-
return errors.Wrapf(err,
89-
"Error creating volume '%s' - cannot parse 'sparse' option value '%s' as bool",
90-
req.Name, sparseStr)
110+
return errors.Wrapf(err, "cannot parse 'sparse' option value '%s' as bool", sparseStr)
91111
}
92112
}
93113

114+
// 4th - parse 'fs' option if present
94115
var fs string
95116
fsInput, fsPresent := req.Options["fs"]
96117
if fsPresent && len(fsInput) > 0 {
@@ -102,46 +123,41 @@ func (d Driver) Create(req *v.CreateRequest) error {
102123
Msg("no fs opt found, using default")
103124
}
104125

126+
// 5th - parse 'uid' option if present
105127
uid := -1
106128
uidStr, uidPresent := req.Options["uid"]
107129
if uidPresent && len(uidStr) > 0 {
108130
uid, err = strconv.Atoi(uidStr)
109131
if err != nil {
110-
return errors.Wrapf(err,
111-
"Error creating volume '%s' - cannot parse 'uid' option value '%s' as an integer",
112-
req.Name, uidStr)
132+
return errors.Wrapf(err, "cannot parse 'uid' option value '%s' as an integer", uidStr)
113133
}
114134
if uid < 0 {
115-
return errors.Errorf(
116-
"Error creating volume '%s' - 'uid' option should be >= 0 but received '%d'",
117-
req.Name, uid)
135+
return errors.Errorf("'uid' option should be >= 0 but received '%d'", uid)
118136
}
119137

120138
logger.Debug().
121139
Int("uid", uid).
122140
Msg("set volume root uid")
123141
}
124142

143+
// 6th - parse 'gid' option if present
125144
gid := -1
126145
gidStr, gidPresent := req.Options["gid"]
127146
if gidPresent && len(gidStr) > 0 {
128147
gid, err = strconv.Atoi(gidStr)
129148
if err != nil {
130-
return errors.Wrapf(err,
131-
"Error creating volume '%s' - cannot parse 'gid' option value '%s' as an integer",
132-
req.Name, gidStr)
149+
return errors.Wrapf(err, "cannot parse 'gid' option value '%s' as an integer", gidStr)
133150
}
134151
if gid < 0 {
135-
return errors.Errorf(
136-
"Error creating volume '%s' - 'gid' option should be >= 0 but received '%d'",
137-
req.Name, gid)
152+
return errors.Errorf("'gid' option should be >= 0 but received '%d'", gid)
138153
}
139154

140155
logger.Debug().
141156
Int("gid", gid).
142157
Msg("set volume root gid")
143158
}
144159

160+
// 7th - parse 'mode' option if present
145161
var mode uint32
146162
modeStr, modePresent := req.Options["mode"]
147163
if modePresent && len(modeStr) > 0 {
@@ -151,32 +167,30 @@ func (d Driver) Create(req *v.CreateRequest) error {
151167

152168
modeParsed, err := strconv.ParseUint(modeStr, 8, 32)
153169
if err != nil {
154-
return errors.Wrapf(err,
155-
"Error creating volume '%s' - cannot parse mode '%s' as positive 4-position octal",
156-
req.Name, modeStr)
170+
return errors.Wrapf(err, "cannot parse mode '%s' as positive 4-position octal", modeStr)
157171
}
158172

159173
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",
162-
req.Name, modeStr)
174+
return errors.Errorf("mode value '%s' does not fall between 0 and 7777 in octal encoding", modeStr)
163175
}
164176

165177
mode = uint32(modeParsed)
166178
}
167179

180+
// Finally - attempt creating a volume
181+
168182
d.Lock()
169183
defer d.Unlock()
170184

171185
logger.Debug().Msg("starting creation")
172186

173187
err = d.manager.Create(req.Name, sizeInBytes, sparse, fs, uid, gid, mode)
174188
if err != nil {
189+
logger.Debug().Msg("failed creating volume")
175190
return err
176191
}
177192

178193
logger.Debug().Msg("finished creating volume")
179-
180194
return nil
181195
}
182196

0 commit comments

Comments
 (0)