File tree 2 files changed +121
-0
lines changed
2 files changed +121
-0
lines changed Original file line number Diff line number Diff line change
1
+ package tailnet
2
+
3
+ import (
4
+ "strconv"
5
+ "strings"
6
+
7
+ "golang.org/x/xerrors"
8
+ )
9
+
10
+ const (
11
+ CurrentMajor = 2
12
+ CurrentMinor = 0
13
+ )
14
+
15
+ var SupportedMajors = []int {2 , 1 }
16
+
17
+ func ValidateVersion (version string ) error {
18
+ parts := strings .Split (version , "." )
19
+ if len (parts ) != 2 {
20
+ return xerrors .Errorf ("invalid version string: %s" , version )
21
+ }
22
+ major , err := strconv .Atoi (parts [0 ])
23
+ if err != nil {
24
+ return xerrors .Errorf ("invalid major version: %s" , version )
25
+ }
26
+ minor , err := strconv .Atoi (parts [1 ])
27
+ if err != nil {
28
+ return xerrors .Errorf ("invalid minor version: %s" , version )
29
+ }
30
+ if major > CurrentMajor {
31
+ return xerrors .Errorf ("server is at version %d.%d, behind requested version %s" ,
32
+ CurrentMajor , CurrentMinor , version )
33
+ }
34
+ if major == CurrentMajor {
35
+ if minor > CurrentMinor {
36
+ return xerrors .Errorf ("server is at version %d.%d, behind requested version %s" ,
37
+ CurrentMajor , CurrentMinor , version )
38
+ }
39
+ return nil
40
+ }
41
+ for _ , mjr := range SupportedMajors {
42
+ if major == mjr {
43
+ return nil
44
+ }
45
+ }
46
+ return xerrors .Errorf ("version %s is no longer supported" , version )
47
+ }
Original file line number Diff line number Diff line change
1
+ package tailnet_test
2
+
3
+ import (
4
+ "fmt"
5
+ "testing"
6
+
7
+ "github.com/stretchr/testify/require"
8
+
9
+ "github.com/coder/coder/v2/tailnet"
10
+ )
11
+
12
+ func TestValidateVersion (t * testing.T ) {
13
+ t .Parallel ()
14
+ for _ , tc := range []struct {
15
+ name string
16
+ version string
17
+ supported bool
18
+ }{
19
+ {
20
+ name : "Current" ,
21
+ version : fmt .Sprintf ("%d.%d" , tailnet .CurrentMajor , tailnet .CurrentMinor ),
22
+ supported : true ,
23
+ },
24
+ {
25
+ name : "TooNewMinor" ,
26
+ version : fmt .Sprintf ("%d.%d" , tailnet .CurrentMajor , tailnet .CurrentMinor + 1 ),
27
+ },
28
+ {
29
+ name : "TooNewMajor" ,
30
+ version : fmt .Sprintf ("%d.%d" , tailnet .CurrentMajor + 1 , tailnet .CurrentMinor ),
31
+ },
32
+ {
33
+ name : "1.0" ,
34
+ version : "1.0" ,
35
+ supported : true ,
36
+ },
37
+ {
38
+ name : "2.0" ,
39
+ version : "2.0" ,
40
+ supported : true ,
41
+ },
42
+ {
43
+ name : "Malformed0" ,
44
+ version : "cats" ,
45
+ },
46
+ {
47
+ name : "Malformed1" ,
48
+ version : "cats.dogs" ,
49
+ },
50
+ {
51
+ name : "Malformed2" ,
52
+ version : "1.0.1" ,
53
+ },
54
+ {
55
+ name : "Malformed3" ,
56
+ version : "11" ,
57
+ },
58
+ {
59
+ name : "TooOld" ,
60
+ version : "0.8" ,
61
+ },
62
+ } {
63
+ tc := tc
64
+ t .Run (tc .name , func (t * testing.T ) {
65
+ t .Parallel ()
66
+ err := tailnet .ValidateVersion (tc .version )
67
+ if tc .supported {
68
+ require .NoError (t , err )
69
+ } else {
70
+ require .Error (t , err )
71
+ }
72
+ })
73
+ }
74
+ }
You can’t perform that action at this time.
0 commit comments