mirror of https://github.com/coder/coder.git
feat: add tailnet ValidateVersion (#11223)
Part of #10532 Adds a method to validate a requested version of the tailnet API
This commit is contained in:
parent
ad3fed72bc
commit
30f032d282
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue