Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions runc.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,9 @@ func (r *Runc) Update(context context.Context, id string, resources *specs.Linux
return r.runOrError(cmd)
}

// ErrParseRuncVersion is used when the runc version can't be parsed
// ErrParseRuncVersion is kept for backward compatibility with older versions.
//
// Deprecated: ErrParseRuncVersion is never emitted, and should not be used.
var ErrParseRuncVersion = errors.New("unable to parse runc version")

// Version represents the runc version information
Expand All @@ -747,16 +749,16 @@ func (r *Runc) Version(context context.Context) (Version, error) {
if err != nil {
return Version{}, err
}
return parseVersion(data.Bytes())
return parseVersion(data.Bytes()), nil
}

func parseVersion(data []byte) (Version, error) {
func parseVersion(data []byte) Version {
var v Version
parts := strings.Split(strings.TrimSpace(string(data)), "\n")

if len(parts) > 0 {
if !strings.HasPrefix(parts[0], "runc version ") {
return v, nil
return v
}
v.Runc = parts[0][13:]

Expand All @@ -769,7 +771,7 @@ func parseVersion(data []byte) (Version, error) {
}
}

return v, nil
return v
}

// Features shows the features implemented by the runtime.
Expand Down
99 changes: 46 additions & 53 deletions runc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,63 +31,56 @@ import (
)

func TestParseVersion(t *testing.T) {
testParseVersion := func(t *testing.T, input string, expected Version) {
actual, err := parseVersion([]byte(input))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if expected != actual {
t.Fatalf("expected: %v, actual: %v", expected, actual)
}
}

t.Run("Full", func(t *testing.T) {
input := `runc version 1.0.0-rc3
tests := []struct {
doc, input string
expected Version
}{
{
doc: "Full",
input: `runc version 1.0.0-rc3
commit: 17f3e2a07439a024e54566774d597df9177ee216
spec: 1.0.0-rc5-dev
`
expected := Version{
Runc: "1.0.0-rc3",
Commit: "17f3e2a07439a024e54566774d597df9177ee216",
Spec: "1.0.0-rc5-dev",
}
testParseVersion(t, input, expected)
})

t.Run("WithoutCommit", func(t *testing.T) {
input := `runc version 1.0.0-rc9
`,
expected: Version{
Runc: "1.0.0-rc3",
Commit: "17f3e2a07439a024e54566774d597df9177ee216",
Spec: "1.0.0-rc5-dev",
},
},
{
doc: "WithoutCommit",
input: `runc version 1.0.0-rc9
spec: 1.0.1-dev
`
expected := Version{
Runc: "1.0.0-rc9",
Commit: "",
Spec: "1.0.1-dev",
}
testParseVersion(t, input, expected)
})

t.Run("Oneline", func(t *testing.T) {
input := `runc version 1.0.0-rc8+dev
`
expected := Version{
Runc: "1.0.0-rc8+dev",
Commit: "",
Spec: "",
}
testParseVersion(t, input, expected)
})

t.Run("Garbage", func(t *testing.T) {
input := `Garbage
`,
expected: Version{
Runc: "1.0.0-rc9",
Spec: "1.0.1-dev",
},
},
{
doc: "Oneline",
input: `runc version 1.0.0-rc8+dev
`,
expected: Version{
Runc: "1.0.0-rc8+dev",
},
},
{
doc: "Garbage",
input: `Garbage
spec: nope
`
expected := Version{
Runc: "",
Commit: "",
Spec: "",
}
testParseVersion(t, input, expected)
})
`,
expected: Version{},
},
}
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
actual := parseVersion([]byte(tc.input))
if tc.expected != actual {
t.Fatalf("expected: %v, actual: %v", tc.expected, actual)
}
})
}
}

func TestParallelCmds(t *testing.T) {
Expand Down
Loading