Skip to content

Commit 3999a44

Browse files
committed
fix(version): rename HasGKEPrefix→HasRawGKESuffix; remove dead HasGKESuffix; add tests
- Rename HasGKEPrefix → HasRawGKESuffix (clearer: operates on Extras which is itself a suffix; 'prefix' referred ambiguously to the -gke. leader) - Remove HasGKESuffix (no production callers; use ExtractGKEBuild directly) - Fix stale godoc referencing the removed HasGKESuffix - Add TestHasRawGKESuffix covering valid, invalid, and non-GKE inputs - Add no-space lone-= (=ubuntu → OperatorEQ/ubuntu) and empty lone-= error (= → ErrCodeInvalidRequest) tests to constraint_test.go Signed-off-by: Kevin Hawkins <khawkins@nvidia.com>
1 parent b270346 commit 3999a44

4 files changed

Lines changed: 36 additions & 14 deletions

File tree

pkg/constraints/constraint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func ParseConstraintExpression(expr string) (*ParsedConstraint, error) {
126126
pc.Operator == OperatorLTE || pc.Operator == OperatorLT
127127
if isComparisonOp {
128128
if parsed, err := version.ParseVersion(pc.Value); err == nil {
129-
if version.HasGKEPrefix(parsed.Extras) {
129+
if version.HasRawGKESuffix(parsed.Extras) {
130130
if _, ok := version.ExtractGKEBuild(parsed.Extras); !ok {
131131
return nil, errors.New(errors.ErrCodeInvalidRequest,
132132
fmt.Sprintf("constraint value %q has a malformed GKE build suffix (must be -gke.N with N >= 0)", pc.Value))

pkg/constraints/constraint_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ func TestParseConstraintExpression(t *testing.T) {
4040
{name: "less than", expression: "< 2.0", wantOp: OperatorLT, wantValue: "2.0"},
4141
{name: "equal op", expression: "== ubuntu", wantOp: OperatorEQ, wantValue: "ubuntu"},
4242
{name: "lone = is alias for ==", expression: "= ubuntu", wantOp: OperatorEQ, wantValue: "ubuntu"},
43+
{name: "lone = no-space is alias for ==", expression: "=ubuntu", wantOp: OperatorEQ, wantValue: "ubuntu"},
44+
{name: "lone = with empty value errors", expression: "=", expectError: true, wantErrCode: errors.ErrCodeInvalidRequest},
4345
{name: "not equal", expression: "!= rhel", wantOp: OperatorNE, wantValue: "rhel"},
4446

4547
// Exact match (no operator)

pkg/version/version.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
)
2525

2626
// gkeSuffixPrefix is the bare prefix that identifies GKE-specific build number
27-
// extras of the form "-gke.NNNNNN". Unexported: use HasGKESuffix or
27+
// extras of the form "-gke.NNNNNN". Unexported: use HasRawGKESuffix or
2828
// ExtractGKEBuild for cross-package consumption.
2929
const gkeSuffixPrefix = "gke."
3030

@@ -202,18 +202,12 @@ func ExtractGKEBuild(extras string) (int64, bool) {
202202
return n, true
203203
}
204204

205-
// HasGKESuffix reports whether the Extras field carries a valid GKE build
206-
// suffix ("-gke.N" with N >= 0). Returns false for malformed or non-GKE extras.
207-
func HasGKESuffix(extras string) bool {
208-
_, ok := ExtractGKEBuild(extras)
209-
return ok
210-
}
211-
212-
// HasGKEPrefix reports whether the Extras field begins with the "-gke." prefix,
213-
// regardless of whether the trailing build number is valid. Use this when you
214-
// need to distinguish "has a GKE prefix but invalid number" from "no GKE prefix
215-
// at all" (e.g. to reject malformed constraint values at parse time).
216-
func HasGKEPrefix(extras string) bool {
205+
// HasRawGKESuffix reports whether the Extras field begins with the "-gke."
206+
// prefix, regardless of whether the trailing build number is valid. Use this
207+
// when you need to distinguish "has the GKE suffix leader but an invalid build
208+
// number" from "no GKE suffix at all" (e.g. to reject malformed constraint
209+
// values at parse time). For full validity checking combine with ExtractGKEBuild.
210+
func HasRawGKESuffix(extras string) bool {
217211
return strings.HasPrefix(strings.TrimPrefix(extras, "-"), gkeSuffixPrefix)
218212
}
219213

pkg/version/version_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,32 @@ func TestExtractGKEBuild(t *testing.T) {
901901
}
902902
}
903903

904+
// TestHasRawGKESuffix verifies that HasRawGKESuffix returns true whenever the
905+
// -gke. prefix is present, regardless of build-number validity.
906+
func TestHasRawGKESuffix(t *testing.T) {
907+
t.Parallel()
908+
909+
tests := []struct {
910+
extras string
911+
want bool
912+
}{
913+
{"-gke.1318000", true}, // valid
914+
{"-gke.-1", true}, // invalid build but prefix present
915+
{"-gke.abc", true}, // invalid build but prefix present
916+
{"-gke.", true}, // empty build but prefix present
917+
{"-eks-3025e55", false}, // non-GKE
918+
{"", false},
919+
}
920+
for _, tt := range tests {
921+
t.Run(tt.extras, func(t *testing.T) {
922+
t.Parallel()
923+
if got := HasRawGKESuffix(tt.extras); got != tt.want {
924+
t.Errorf("HasRawGKESuffix(%q) = %v, want %v", tt.extras, got, tt.want)
925+
}
926+
})
927+
}
928+
}
929+
904930
// ExampleVersion_Compare demonstrates sorting versions
905931
func ExampleVersion_Compare() {
906932
v1, _ := ParseVersion("1.2.0")

0 commit comments

Comments
 (0)