-
Notifications
You must be signed in to change notification settings - Fork 902
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
Changes from all commits
Commits
File filter
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
commit 5df34d75dc7e50103aac3049ab43cbc2f5200cfe
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
spikecurtis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.