Skip to content

Commit 68ae0ec

Browse files
committed
fix latest version deploy check
1 parent 39db367 commit 68ae0ec

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed

google.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func StartRollingUpdate(c *computeBeta.Service, d Deploy, instanceTemplateURL st
153153

154154
// TODO consider making the following check a configuration flag
155155
latestVersion := findLatestInstanceGroupManagerVersion(ig.Versions)
156-
if latestVersion != "" && latestVersion >= d.InstanceTemplate {
156+
if latestVersion != "" && !VersionLessThan(latestVersion, d.InstanceTemplate) {
157157
return fmt.Errorf("update instance group: instance template '%v' is too old, because '%v' is the latest instance template.", d.InstanceTemplate, latestVersion)
158158
}
159159

util.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"regexp"
5+
"strconv"
6+
)
7+
8+
// VersionLessThan returns true if a < b
9+
func VersionLessThan(a, b string) bool {
10+
xa, err := extractInts(a)
11+
if err != nil {
12+
panic(err)
13+
}
14+
15+
xb, err := extractInts(b)
16+
if err != nil {
17+
panic(err)
18+
}
19+
20+
for i := 0; i < len(xa); i++ {
21+
if len(xb) >= i {
22+
if xa[i] == xb[i] {
23+
continue
24+
}
25+
26+
return xa[i] < xb[i]
27+
}
28+
}
29+
30+
return false
31+
}
32+
33+
// VersionGreaterThan returns true if a > b
34+
func VersionGreaterThan(a, b string) bool {
35+
return !VersionLessThan(a, b)
36+
}
37+
38+
func extractInts(v string) ([]int, error) {
39+
re := regexp.MustCompile("[0-9]+")
40+
matches := re.FindAllString(v, -1)
41+
42+
r := make([]int, 0)
43+
for _, m := range matches {
44+
n, err := strconv.Atoi(m)
45+
if err != nil {
46+
return nil, err
47+
}
48+
r = append(r, n)
49+
}
50+
51+
return r, nil
52+
}

util_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestVersionLessThan(t *testing.T) {
10+
table := []struct {
11+
a, b string
12+
expect bool
13+
}{
14+
// a < b
15+
16+
{"1", "1", false},
17+
{"2", "1", false},
18+
{"1", "2", true},
19+
20+
{"v1.2.3", "v1.2.3", false},
21+
{"v1.2.3", "v1.3.3", true},
22+
{"v1.2.3", "v1.2.4", true},
23+
{"v1.2.3", "v2.2.3", true},
24+
25+
{"instance-12", "instance-12", false},
26+
{"instance-12", "instance-11", false},
27+
{"instance-12", "instance-13", true},
28+
29+
{"1.1", "2", true},
30+
{"1.2.3", "2", true},
31+
{"2", "1.1", false},
32+
{"2", "1.2.3", false},
33+
34+
{"instance-9-14be32d", "instance-17-1df06f1", true},
35+
}
36+
37+
for _, test := range table {
38+
out := VersionLessThan(test.a, test.b)
39+
require.Equal(t, test.expect, out)
40+
41+
out = VersionGreaterThan(test.a, test.b)
42+
require.Equal(t, !test.expect, out)
43+
}
44+
}
45+
46+
func TestExtractInts(t *testing.T) {
47+
table := []struct {
48+
in string
49+
expect []int
50+
}{
51+
{"123", []int{123}},
52+
{"abc-123", []int{123}},
53+
{"abc-123-def", []int{123}},
54+
{"abc-123-def-456", []int{123, 456}},
55+
{"v1.2.3", []int{1, 2, 3}},
56+
{"v.1.2.3", []int{1, 2, 3}},
57+
{"1.2.3", []int{1, 2, 3}},
58+
{"1/2/3", []int{1, 2, 3}},
59+
{"1_2_3", []int{1, 2, 3}},
60+
{"01_02_03", []int{1, 2, 3}},
61+
{"+1", []int{1}},
62+
{"-1", []int{1}},
63+
{"+0", []int{0}},
64+
{"-0", []int{0}},
65+
{"0", []int{0}},
66+
}
67+
68+
for _, test := range table {
69+
out, err := extractInts(test.in)
70+
require.NoError(t, err, test.in)
71+
require.Equal(t, test.expect, out, test.in)
72+
}
73+
}

0 commit comments

Comments
 (0)