Skip to content

Commit 7016d3c

Browse files
authored
Merge pull request containerd#60 from twz123/parse-runc-version
Parse runc version even if commit is missing
2 parents 8aa1fd6 + 9e92203 commit 7016d3c

File tree

2 files changed

+70
-39
lines changed

2 files changed

+70
-39
lines changed

runc.go

+14-24
Original file line numberDiff line numberDiff line change
@@ -623,32 +623,22 @@ func (r *Runc) Version(context context.Context) (Version, error) {
623623
func parseVersion(data []byte) (Version, error) {
624624
var v Version
625625
parts := strings.Split(strings.TrimSpace(string(data)), "\n")
626-
if len(parts) != 3 {
627-
return v, nil
628-
}
629-
for i, p := range []struct {
630-
dest *string
631-
split string
632-
}{
633-
{
634-
dest: &v.Runc,
635-
split: "version ",
636-
},
637-
{
638-
dest: &v.Commit,
639-
split: ": ",
640-
},
641-
{
642-
dest: &v.Spec,
643-
split: ": ",
644-
},
645-
} {
646-
p2 := strings.Split(parts[i], p.split)
647-
if len(p2) != 2 {
648-
return v, fmt.Errorf("unable to parse version line %q", parts[i])
626+
627+
if len(parts) > 0 {
628+
if !strings.HasPrefix(parts[0], "runc version ") {
629+
return v, nil
630+
}
631+
v.Runc = parts[0][13:]
632+
633+
for _, part := range parts[1:] {
634+
if strings.HasPrefix(part, "commit: ") {
635+
v.Commit = part[8:]
636+
} else if strings.HasPrefix(part, "spec: ") {
637+
v.Spec = part[6:]
638+
}
649639
}
650-
*p.dest = p2[1]
651640
}
641+
652642
return v, nil
653643
}
654644

runc_test.go

+56-15
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,64 @@ import (
2323
)
2424

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

30-
v, err := parseVersion([]byte(data))
31-
if err != nil {
32-
t.Fatal(err)
33-
}
34-
if v.Runc != "1.0.0-rc3" {
35-
t.Errorf("expected runc version 1.0.0-rc3 but received %s", v.Runc)
36-
}
37-
if v.Commit != "17f3e2a07439a024e54566774d597df9177ee216" {
38-
t.Errorf("expected commit 17f3e2a07439a024e54566774d597df9177ee216 but received %s", v.Commit)
39-
}
40-
if v.Spec != "1.0.0-rc5-dev" {
41-
t.Errorf("expected spec version 1.0.0-rc5-dev but received %s", v.Spec)
27+
testParseVersion := func(t *testing.T, input string, expected Version) {
28+
actual, err := parseVersion([]byte(input))
29+
if err != nil {
30+
t.Fatalf("unexpected error: %v", err)
31+
}
32+
if expected != actual {
33+
t.Fatalf("expected: %v, actual: %v", expected, actual)
34+
}
4235
}
36+
37+
t.Run("Full", func(t *testing.T) {
38+
input := `runc version 1.0.0-rc3
39+
commit: 17f3e2a07439a024e54566774d597df9177ee216
40+
spec: 1.0.0-rc5-dev
41+
`
42+
expected := Version{
43+
Runc: "1.0.0-rc3",
44+
Commit: "17f3e2a07439a024e54566774d597df9177ee216",
45+
Spec: "1.0.0-rc5-dev",
46+
}
47+
testParseVersion(t, input, expected)
48+
})
49+
50+
t.Run("WithoutCommit", func(t *testing.T) {
51+
input := `runc version 1.0.0-rc9
52+
spec: 1.0.1-dev
53+
`
54+
expected := Version{
55+
Runc: "1.0.0-rc9",
56+
Commit: "",
57+
Spec: "1.0.1-dev",
58+
}
59+
testParseVersion(t, input, expected)
60+
})
61+
62+
t.Run("Oneline", func(t *testing.T) {
63+
input := `runc version 1.0.0-rc8+dev
64+
`
65+
expected := Version{
66+
Runc: "1.0.0-rc8+dev",
67+
Commit: "",
68+
Spec: "",
69+
}
70+
testParseVersion(t, input, expected)
71+
})
72+
73+
t.Run("Garbage", func(t *testing.T) {
74+
input := `Garbage
75+
spec: nope
76+
`
77+
expected := Version{
78+
Runc: "",
79+
Commit: "",
80+
Spec: "",
81+
}
82+
testParseVersion(t, input, expected)
83+
})
4384
}
4485

4586
func TestParallelCmds(t *testing.T) {

0 commit comments

Comments
 (0)