Skip to content

Commit d5aaa4b

Browse files
committed
chore(limits): modify limits unit verification
1 parent 2c9c104 commit d5aaa4b

5 files changed

Lines changed: 18 additions & 10 deletions

File tree

cmd/limits.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func parseLimits(limits []string) (map[string]interface{}, error) {
145145
}
146146

147147
func parseLimit(limit string) (string, string, error) {
148-
regex := regexp.MustCompile("^([a-z0-9]+(?:-[a-z0-9]+)*)=(([0-9]+[bkmgBKMG]{1,2}|[0-9.]{1,5}|[0-9.]{1,5}m?))$")
148+
regex := regexp.MustCompile("^([a-z0-9]+(?:-[a-z0-9]+)*)=(([1-9][0-9]*[mgMG]|[1-9][0-9]*m?))$")
149149

150150
if !regex.MatchString(limit) {
151151
return "", "", fmt.Errorf(`%s doesn't fit format type=#unit or type=#

cmd/limits_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ Examples: web=2G worker=500M db=1G`
2929
{"web=2G", "web", "2G", false, ""},
3030
{"web=2", "web", "2", false, ""},
3131
{"web=100m", "web", "100m", false, ""},
32-
{"web=0.1", "web", "0.1", false, ""},
33-
{"web=.123", "web", ".123", false, ""},
3432
{"web1=2G", "web1", "2G", false, ""},
3533
{"web-server=2G", "web-server", "2G", false, ""},
3634
{"web-server1=2G", "web-server1", "2G", false, ""},
35+
{"web=0.1", "", "", true, "web=0.1" + errorHint},
36+
{"web=.123", "", "", true, "web=.123" + errorHint},
3737
{"=1", "", "", true, "=1" + errorHint},
3838
{"web=", "", "", true, "web=" + errorHint},
3939
{"1=", "", "", true, "1=" + errorHint},
@@ -307,7 +307,7 @@ Default
307307
CPU: map[string]interface{}{
308308
"web": "2",
309309
"worker": "300m",
310-
"db": "5.6",
310+
"db": "5",
311311
},
312312
}, r)
313313
}
@@ -319,7 +319,7 @@ Default
319319
"cpu": {
320320
"web": "2",
321321
"worker": "300m",
322-
"db": "5.6"
322+
"db": "5"
323323
},
324324
"cpu": {},
325325
"tags": {},
@@ -331,7 +331,7 @@ Default
331331
})
332332
b.Reset()
333333

334-
err = cmdr.LimitsSet("phew", []string{"web=2", "worker=300m", "db=5.6"}, "cpu")
334+
err = cmdr.LimitsSet("phew", []string{"web=2", "worker=300m", "db=5"}, "cpu")
335335
assert.NoErr(t, err)
336336

337337
assert.Equal(t, testutil.StripProgress(b.String()), `Applying limits... done
@@ -342,7 +342,7 @@ Default
342342
Default
343343
344344
--- CPU
345-
db 5.6
345+
db 5
346346
web 2
347347
worker 300m
348348
`, "output")

cmd/volumes.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ func (d *DryccCmd) VolumesCreate(appID, name string, size string) error {
7474
if err != nil {
7575
return err
7676
}
77+
regex := regexp.MustCompile("^([1-9][0-9]*[mgMG])$")
78+
if !regex.MatchString(size) {
79+
return fmt.Errorf(`%s doesn't fit format #unit
80+
Examples: 2G 2g 500M 500m`, size)
81+
}
7782

7883
d.Printf("Creating %s to %s... ", name, appID)
7984

@@ -152,7 +157,6 @@ func (d *DryccCmd) VolumesUnmount(appID string, name string, volumeVars []string
152157
return err
153158
}
154159

155-
// volumeMap, err := parseVolume(volumeVars)
156160
valuesMap := make(map[string]interface{})
157161
for _, volumeVar := range volumeVars {
158162
valuesMap[volumeVar] = nil

cmd/volumes_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ func TestVolumesCreate(t *testing.T) {
7272

7373
err = cmdr.VolumesCreate("example-go", "myvolume", "500M")
7474
assert.NoErr(t, err)
75+
err = cmdr.VolumesCreate("example-go", "myvolume", "500K")
76+
expected := `500K doesn't fit format #unit
77+
Examples: 2G 2g 500M 500m`
78+
assert.Equal(t, err.Error(), expected, "output")
7579

7680
assert.Equal(t, testutil.StripProgress(b.String()), "Creating myvolume to example-go... done\n", "output")
7781
}

parser/limits.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ Arguments:
7878
Can be in <limit> format eg. web=2G db=1G
7979
You can only set one type of limit per call.
8080
81-
With --memory, units are represented in Bytes (B), Kilobytes (K), Megabytes
82-
(M), or Gigabytes (G). For example, 'drycc limit:set cmd=1G' will restrict all
81+
With --memory, units are represented in Megabytes(M), or Gigabytes (G).
82+
For example, 'drycc limit:set cmd=1G' will restrict all
8383
"cmd" processes to a maximum of 1 Gigabyte of memory each.
8484
8585
With --cpu, units are represented in the number of CPUs. For example,

0 commit comments

Comments
 (0)