-
Notifications
You must be signed in to change notification settings - Fork 104
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
5 changed files
with
207 additions
and
149 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package telemetry | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
// parseSemver takes a string and turns it into a semver format. | ||
func parseSemver(s string) (string, error) { | ||
s = strings.TrimSpace(s) | ||
s = strings.TrimPrefix(s, "v") | ||
|
||
if s == "" { | ||
return "", errors.New("string cannot be empty") | ||
} | ||
|
||
parts := strings.SplitN(s, ".", 3) | ||
|
||
if _, err := strconv.Atoi(parts[0]); err != nil { | ||
return "", errors.New("string must have a number as the major version") | ||
} | ||
|
||
if len(parts) == 1 || parts[1] == "" { | ||
return "", errors.New("string must have at least a major and minor version specified") | ||
} | ||
|
||
// in the edge case where the kubeletVersion is missing the patch version and has trailing characters which include | ||
// '.' e.g. "v1.27-gke.1067004" | ||
if _, err := strconv.Atoi(parts[1]); err != nil && len(parts) == 3 { | ||
parts[1] = parts[1] + parts[2] | ||
parts = parts[:len(parts)-1] | ||
} | ||
|
||
lastString := parts[len(parts)-1] | ||
|
||
for index := range lastString { | ||
// cut off trailing characters after the patch version. | ||
// e.g. if kubeletVersion = "1.27.4+500050039", will return "4" | ||
if !unicode.IsDigit(rune(lastString[index])) { | ||
parts[len(parts)-1] = lastString[:index] | ||
break | ||
} | ||
} | ||
|
||
if len(parts) == 2 { | ||
parts = append(parts, "0") | ||
} | ||
|
||
// in the case where lastString was "" | ||
if parts[2] == "" { | ||
parts[2] = "0" | ||
} | ||
|
||
return strings.Join(parts, "."), 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,104 @@ | ||
package telemetry | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestParseKubeletVersion(t *testing.T) { | ||
tests := []struct { | ||
expError error | ||
input string | ||
expected string | ||
name string | ||
}{ | ||
{ | ||
input: "v1.27.9", | ||
expected: "1.27.9", | ||
name: "normal case", | ||
expError: nil, | ||
}, | ||
{ | ||
input: " v1.27.9 ", | ||
expected: "1.27.9", | ||
name: "removes added whitespace", | ||
expError: nil, | ||
}, | ||
{ | ||
input: "v1.27", | ||
expected: "1.27.0", | ||
name: "adds appended 0's if missing semver patch number", | ||
expError: nil, | ||
}, | ||
{ | ||
input: "v1.27.8-gke.1067004", | ||
expected: "1.27.8", | ||
name: "removes trailing characters from semver version", | ||
expError: nil, | ||
}, | ||
{ | ||
input: "v1.27.9+", | ||
expected: "1.27.9", | ||
name: "removes trailing characters from semver version no following characters", | ||
expError: nil, | ||
}, | ||
{ | ||
input: "v1.27-gke.1067004", | ||
expected: "1.27.0", | ||
name: "removes trailing characters from semver version no patch version", | ||
expError: nil, | ||
}, | ||
{ | ||
input: "v1.27.", | ||
expected: "1.27.0", | ||
name: "edge case where patch version is missing but separating period is", | ||
expError: nil, | ||
}, | ||
{ | ||
input: "v1.27.gke+323", | ||
expected: "1.27.0", | ||
name: "edge case where patch version is missing but additional characters are present", | ||
expError: nil, | ||
}, | ||
{ | ||
input: "", | ||
expected: "", | ||
name: "error on empty string", | ||
expError: errors.New("string cannot be empty"), | ||
}, | ||
{ | ||
input: "1", | ||
expected: "", | ||
name: "errors when major and minor version are not present", | ||
expError: errors.New("string must have at least a major and minor version specified"), | ||
}, | ||
{ | ||
input: "1.", | ||
expected: "", | ||
name: "errors when major and minor version are not present", | ||
expError: errors.New("string must have at least a major and minor version specified"), | ||
}, | ||
{ | ||
input: "123gke", | ||
expected: "", | ||
name: "errors when string does not contain a number as the major version", | ||
expError: errors.New("string must have a number as the major version"), | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
result, err := parseSemver(test.input) | ||
g.Expect(result).To(Equal(test.expected)) | ||
|
||
if test.expError != nil { | ||
g.Expect(err).To(MatchError(test.expError)) | ||
} else { | ||
g.Expect(err).To(BeNil()) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.