Skip to content

Commit 8bbfe95

Browse files
committed
Add test for span arg validation
This adds a test that a user cannot specify a min-layer-size or span-size that is negative or larger than what can fit into a signed 64-bit integer. Signed-off-by: Kern Walster <[email protected]>
1 parent 16d71c2 commit 8bbfe95

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

integration/create_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"encoding/json"
2222
"fmt"
23+
"math"
2324
"path/filepath"
2425
"strings"
2526
"testing"
@@ -33,6 +34,69 @@ import (
3334
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3435
)
3536

37+
func TestCreateConvertParameterValidation(t *testing.T) {
38+
tests := []struct {
39+
name string
40+
minLayerSize string
41+
spanSize string
42+
expectedError string
43+
}{
44+
{
45+
name: "minLayerSize < 0 fails validation",
46+
minLayerSize: "-1",
47+
spanSize: "0",
48+
expectedError: "min layer size must be >= 0",
49+
},
50+
{
51+
name: "spanSize < 0 fails validation",
52+
minLayerSize: "0",
53+
spanSize: "-1",
54+
expectedError: "span size must be >= 0",
55+
},
56+
{
57+
name: "minLayerSize > int64.MaxValue fails validation",
58+
minLayerSize: fmt.Sprintf("%d", uint64(math.MaxInt64)+1),
59+
spanSize: "",
60+
expectedError: "for flag -min-layer-size: value out of range",
61+
},
62+
{
63+
name: "spanSize > int64.MaxValue fails validation",
64+
minLayerSize: "0",
65+
spanSize: fmt.Sprintf("%d", uint64(math.MaxInt64)+1),
66+
expectedError: "for flag -span-size: value out of range",
67+
},
68+
}
69+
70+
sh, done := newSnapshotterBaseShell(t)
71+
defer done()
72+
rebootContainerd(t, sh, "", "")
73+
image := dockerhub(alpineImage)
74+
sh.X("nerdctl", "pull", image.ref)
75+
for _, test := range tests {
76+
t.Run(test.name, func(t *testing.T) {
77+
t.Run("create", func(t *testing.T) {
78+
b, err := sh.CombinedOLog("soci", "create", "--min-layer-size", test.minLayerSize, "--span-size", test.spanSize, image.ref)
79+
if !strings.Contains(string(b), test.expectedError) {
80+
t.Fatalf("expected error to contain %q, got %q", test.expectedError, string(b))
81+
}
82+
if err == nil {
83+
t.Fatal("expected error but got nil")
84+
}
85+
})
86+
87+
t.Run("convert", func(t *testing.T) {
88+
b, err := sh.CombinedOLog("soci", "convert", "--min-layer-size", test.minLayerSize, "--span-size", test.spanSize, image.ref, image.ref)
89+
if !strings.Contains(string(b), test.expectedError) {
90+
t.Fatalf("convert: expected error to contain %q, got %q", test.expectedError, string(b))
91+
}
92+
if err == nil {
93+
t.Fatal("convert: expected error but got nil")
94+
}
95+
})
96+
})
97+
}
98+
}
99+
36100
func TestSociCreateEmptyIndex(t *testing.T) {
37101
sh, done := newSnapshotterBaseShell(t)
38102
defer done()

soci/soci_index.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ type BuilderOption func(c *builderConfig) error
331331
// WithSpanSize specifies span size.
332332
func WithSpanSize(spanSize int64) BuilderOption {
333333
return func(c *builderConfig) error {
334+
if spanSize < 0 {
335+
return fmt.Errorf("span size must be >= 0")
336+
}
334337
c.spanSize = spanSize
335338
return nil
336339
}
@@ -339,6 +342,9 @@ func WithSpanSize(spanSize int64) BuilderOption {
339342
// WithMinLayerSize specifies min layer size to build a ztoc for a layer.
340343
func WithMinLayerSize(minLayerSize int64) BuilderOption {
341344
return func(c *builderConfig) error {
345+
if minLayerSize < 0 {
346+
return fmt.Errorf("min layer size must be >= 0")
347+
}
342348
c.minLayerSize = minLayerSize
343349
return nil
344350
}

0 commit comments

Comments
 (0)