Skip to content

Commit 236719b

Browse files
committed
feat: Add tailnet ValidateVersion
1 parent ba75bcc commit 236719b

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

tailnet/service.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

tailnet/service_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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: "Malformed0",
39+
version: "cats",
40+
},
41+
{
42+
name: "Malformed1",
43+
version: "cats.dogs",
44+
},
45+
{
46+
name: "Malformed2",
47+
version: "1.0.1",
48+
},
49+
{
50+
name: "Malformed3",
51+
version: "11",
52+
},
53+
} {
54+
tc := tc
55+
t.Run(tc.name, func(t *testing.T) {
56+
t.Parallel()
57+
err := tailnet.ValidateVersion(tc.version)
58+
if tc.supported {
59+
require.NoError(t, err)
60+
} else {
61+
require.Error(t, err)
62+
}
63+
})
64+
}
65+
}

0 commit comments

Comments
 (0)