Skip to content

Commit

Permalink
Merge pull request #60 from twz123/parse-runc-version
Browse files Browse the repository at this point in the history
Parse runc version even if commit is missing
  • Loading branch information
AkihiroSuda committed Feb 20, 2020
2 parents 8aa1fd6 + 9e92203 commit 7016d3c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 39 deletions.
38 changes: 14 additions & 24 deletions runc.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,32 +623,22 @@ func (r *Runc) Version(context context.Context) (Version, error) {
func parseVersion(data []byte) (Version, error) {
var v Version
parts := strings.Split(strings.TrimSpace(string(data)), "\n")
if len(parts) != 3 {
return v, nil
}
for i, p := range []struct {
dest *string
split string
}{
{
dest: &v.Runc,
split: "version ",
},
{
dest: &v.Commit,
split: ": ",
},
{
dest: &v.Spec,
split: ": ",
},
} {
p2 := strings.Split(parts[i], p.split)
if len(p2) != 2 {
return v, fmt.Errorf("unable to parse version line %q", parts[i])

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

for _, part := range parts[1:] {
if strings.HasPrefix(part, "commit: ") {
v.Commit = part[8:]
} else if strings.HasPrefix(part, "spec: ") {
v.Spec = part[6:]
}
}
*p.dest = p2[1]
}

return v, nil
}

Expand Down
71 changes: 56 additions & 15 deletions runc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,64 @@ import (
)

func TestParseVersion(t *testing.T) {
const data = `runc version 1.0.0-rc3
commit: 17f3e2a07439a024e54566774d597df9177ee216
spec: 1.0.0-rc5-dev`

v, err := parseVersion([]byte(data))
if err != nil {
t.Fatal(err)
}
if v.Runc != "1.0.0-rc3" {
t.Errorf("expected runc version 1.0.0-rc3 but received %s", v.Runc)
}
if v.Commit != "17f3e2a07439a024e54566774d597df9177ee216" {
t.Errorf("expected commit 17f3e2a07439a024e54566774d597df9177ee216 but received %s", v.Commit)
}
if v.Spec != "1.0.0-rc5-dev" {
t.Errorf("expected spec version 1.0.0-rc5-dev but received %s", v.Spec)
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
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
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
spec: nope
`
expected := Version{
Runc: "",
Commit: "",
Spec: "",
}
testParseVersion(t, input, expected)
})
}

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

0 comments on commit 7016d3c

Please sign in to comment.