Skip to content

feat: add tailnet ValidateVersion #11223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: Add tailnet ValidateVersion
  • Loading branch information
spikecurtis committed Dec 15, 2023
commit 5df34d75dc7e50103aac3049ab43cbc2f5200cfe
47 changes: 47 additions & 0 deletions tailnet/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package tailnet

import (
"strconv"
"strings"

"golang.org/x/xerrors"
)

const (
CurrentMajor = 2
CurrentMinor = 0
)

var SupportedMajors = []int{2, 1}

func ValidateVersion(version string) error {
parts := strings.Split(version, ".")
if len(parts) != 2 {
return xerrors.Errorf("invalid version string: %s", version)
}
major, err := strconv.Atoi(parts[0])
if err != nil {
return xerrors.Errorf("invalid major version: %s", version)
}
minor, err := strconv.Atoi(parts[1])
if err != nil {
return xerrors.Errorf("invalid minor version: %s", version)
}
if major > CurrentMajor {
return xerrors.Errorf("server is at version %d.%d, behind requested version %s",
CurrentMajor, CurrentMinor, version)
}
if major == CurrentMajor {
if minor > CurrentMinor {
return xerrors.Errorf("server is at version %d.%d, behind requested version %s",
CurrentMajor, CurrentMinor, version)
}
return nil
}
for _, mjr := range SupportedMajors {
if major == mjr {
return nil
}
}
return xerrors.Errorf("version %s is no longer supported", version)
}
74 changes: 74 additions & 0 deletions tailnet/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package tailnet_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/tailnet"
)

func TestValidateVersion(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
version string
supported bool
}{
{
name: "Current",
version: fmt.Sprintf("%d.%d", tailnet.CurrentMajor, tailnet.CurrentMinor),
supported: true,
},
{
name: "TooNewMinor",
version: fmt.Sprintf("%d.%d", tailnet.CurrentMajor, tailnet.CurrentMinor+1),
},
{
name: "TooNewMajor",
version: fmt.Sprintf("%d.%d", tailnet.CurrentMajor+1, tailnet.CurrentMinor),
},
{
name: "1.0",
version: "1.0",
supported: true,
},
{
name: "2.0",
version: "2.0",
supported: true,
},
{
name: "Malformed0",
version: "cats",
},
{
name: "Malformed1",
version: "cats.dogs",
},
{
name: "Malformed2",
version: "1.0.1",
},
{
name: "Malformed3",
version: "11",
},
{
name: "TooOld",
version: "0.8",
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
err := tailnet.ValidateVersion(tc.version)
if tc.supported {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}
}