-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
588 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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,39 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// ErrVersionParse is an error which is raised when srlinux version is failed to parse. | ||
var ErrVersionParse = errors.New("version parsing failed") | ||
|
||
// SrlVersion represents an sr linux version as a set of fields. | ||
type SrlVersion struct { | ||
Major string `json:"major,omitempty"` | ||
Minor string `json:"minor,omitempty"` | ||
Patch string `json:"patch,omitempty"` | ||
Build string `json:"build,omitempty"` | ||
Commit string `json:"commit,omitempty"` | ||
} | ||
|
||
func parseVersionString(s string) (*SrlVersion, error) { | ||
// for latest or missing tag we consider the version to be an engineering build | ||
// with major = 0 | ||
if strings.ToLower(s) == "latest" || s == "" { | ||
return &SrlVersion{"0", "", "", "", ""}, nil | ||
} | ||
|
||
// https://regex101.com/r/eWS6Ms/1 | ||
re := regexp.MustCompile( | ||
`^v?(?P<major>\d{1,3})\.(?P<minor>\d{1,2})\.?(?P<patch>\d{1,2})?-?(?P<build>\d{1,10})?-?(?P<commit>\S+)?`, | ||
) | ||
|
||
v := re.FindStringSubmatch(s) | ||
if v == nil { | ||
return nil, ErrVersionParse | ||
} | ||
|
||
return &SrlVersion{v[1], v[2], v[3], v[4], v[5]}, nil | ||
} |
This file contains 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,98 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestParseVersionString(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
got string | ||
want *SrlVersion | ||
err error | ||
}{ | ||
{ | ||
desc: "maj, minor, patch", | ||
got: "21.6.4", | ||
want: &SrlVersion{"21", "6", "4", "", ""}, | ||
}, | ||
{ | ||
desc: "maj, minor", | ||
got: "21.6", | ||
want: &SrlVersion{"21", "6", "", "", ""}, | ||
}, | ||
{ | ||
desc: "maj, minor and extra string", | ||
got: "21.6-test", | ||
want: &SrlVersion{"21", "6", "", "", "test"}, | ||
}, | ||
{ | ||
desc: "maj, minor, patch and extra string", | ||
got: "21.6.11-test", | ||
want: &SrlVersion{"21", "6", "11", "", "test"}, | ||
}, | ||
{ | ||
desc: "maj, minor, patch, build and extra string", | ||
got: "21.6.11-235-test", | ||
want: &SrlVersion{"21", "6", "11", "235", "test"}, | ||
}, | ||
{ | ||
desc: "maj, minor, patch and build", | ||
got: "21.6.11-235", | ||
want: &SrlVersion{"21", "6", "11", "235", ""}, | ||
}, | ||
{ | ||
desc: "0.0", | ||
got: "0.0", | ||
want: &SrlVersion{"0", "0", "", "", ""}, | ||
}, | ||
{ | ||
desc: "0.0.0", | ||
got: "0.0.0", | ||
want: &SrlVersion{"0", "0", "0", "", ""}, | ||
}, | ||
{ | ||
desc: "0.0.0-34652", | ||
got: "0.0.0-34652", | ||
want: &SrlVersion{"0", "0", "0", "34652", ""}, | ||
}, | ||
{ | ||
desc: "latest", | ||
got: "latest", | ||
want: &SrlVersion{"0", "", "", "", ""}, | ||
}, | ||
{ | ||
desc: "empty", | ||
got: "", | ||
want: &SrlVersion{"0", "", "", "", ""}, | ||
}, | ||
{ | ||
desc: "invalid1", | ||
got: "abcd", | ||
want: nil, | ||
err: ErrVersionParse, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.desc, func(t *testing.T) { | ||
ver, err := parseVersionString(tt.got) | ||
if !errors.Is(err, tt.err) { | ||
t.Fatalf("got error '%v' but expected '%v'", err, tt.err) | ||
} | ||
|
||
if !cmp.Equal(ver, tt.want) { | ||
t.Fatalf( | ||
"%s: actual and expected inputs do not match\nactual: %+v\nexpected:%+v", | ||
tt.desc, | ||
ver, | ||
tt.want, | ||
) | ||
} | ||
}, | ||
) | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.