Fix lifecycle generator to check the version correctly (#119268)

* Fix lifecycle generator to check the version correctly

* Fix file header

Co-authored-by: Antonio Ojea <antonio.ojea.garcia@gmail.com>

---------

Co-authored-by: Antonio Ojea <antonio.ojea.garcia@gmail.com>
This commit is contained in:
Ricardo Katz 2023-07-13 13:56:49 -03:00 committed by GitHub
parent 20e60a03df
commit c688478a28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 150 additions and 11 deletions

View File

@ -71,7 +71,7 @@ func extractKubeVersionTag(tagName string, t *types.Type) (*tagValue, int, int,
}
splitValue := strings.Split(rawTag.value, ".")
if len(splitValue) != 2 || len(splitValue[0]) == 0 || len(splitValue[0]) == 0 {
if len(splitValue) != 2 || len(splitValue[0]) == 0 || len(splitValue[1]) == 0 {
return nil, -1, -1, fmt.Errorf("%v format must match %v=xx.yy tag", t, tagName)
}
major, err := strconv.ParseInt(splitValue[0], 10, 32)
@ -159,16 +159,7 @@ func extractTag(tagName string, comments []string) *tagValue {
for i := range parts {
kv := strings.SplitN(parts[i], "=", 2)
k := kv[0]
//v := ""
//if len(kv) == 2 {
// v = kv[1]
//}
switch k {
//case "register":
// if v != "false" {
// tag.register = true
// }
default:
if k != "" {
klog.Fatalf("Unsupported %s param: %q", tagName, parts[i])
}
}

View File

@ -0,0 +1,148 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package prereleaselifecyclegenerators
import (
"reflect"
"testing"
"k8s.io/gengo/types"
)
var mockType = &types.Type{
CommentLines: []string{
"RandomType defines a random structure in Kubernetes",
"It should be used just when you need something different than 42",
},
SecondClosestCommentLines: []string{},
}
func Test_extractKubeVersionTag(t *testing.T) {
tests := []struct {
name string
tagName string
tagComments []string
wantValue *tagValue
wantMajor int
wantMinor int
wantErr bool
}{
{
name: "not found tag should generate an error",
tagName: "someVersionTag:version",
tagComments: []string{
"+someOtherTag:version=1.5",
},
wantValue: nil,
wantErr: true,
},
{
name: "found tag should return correctly",
tagName: "someVersionTag:version",
tagComments: []string{
"+someVersionTag:version=1.5",
},
wantValue: &tagValue{
value: "1.5",
},
wantMajor: 1,
wantMinor: 5,
wantErr: false,
},
/*{
name: "multiple declarations of same tag should return an error",
tagName: "someVersionTag:version",
tagComments: []string{
"+someVersionTag:version=1.5",
"+someVersionTag:version=v1.7",
},
wantValue: nil,
wantErr: true, // TODO: Today it is klog.Fatal, check how to capture it
},
{
name: "multiple values on same tag should return an error",
tagName: "someVersionTag:version",
tagComments: []string{
"+someVersionTag:version=1.5,something",
},
wantValue: nil,
wantErr: true, // TODO: Today it is klog.Fatal, check how to capture it
},*/
{
name: "wrong tag major value should return an error",
tagName: "someVersionTag:version",
tagComments: []string{
"+someVersionTag:version=.5",
},
wantErr: true,
},
{
name: "wrong tag minor value should return an error",
tagName: "someVersionTag:version",
tagComments: []string{
"+someVersionTag:version=1.",
},
wantErr: true,
},
{
name: "wrong tag format should return an error",
tagName: "someVersionTag:version",
tagComments: []string{
"+someVersionTag:version=1.5.7",
},
wantErr: true,
},
{
name: "wrong tag major int value should return an error",
tagName: "someVersionTag:version",
tagComments: []string{
"+someVersionTag:version=blah.5",
},
wantErr: true,
},
{
name: "wrong tag minor int value should return an error",
tagName: "someVersionTag:version",
tagComments: []string{
"+someVersionTag:version=1.blah",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockType.SecondClosestCommentLines = tt.tagComments
gotTag, gotMajor, gotMinor, err := extractKubeVersionTag(tt.tagName, mockType)
if (err != nil) != tt.wantErr {
t.Errorf("extractKubeVersionTag() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
return
}
if !reflect.DeepEqual(gotTag, tt.wantValue) {
t.Errorf("extractKubeVersionTag() got = %v, want %v", gotTag, tt.wantValue)
}
if gotMajor != tt.wantMajor {
t.Errorf("extractKubeVersionTag() got1 = %v, want %v", gotMajor, tt.wantMajor)
}
if gotMinor != tt.wantMinor {
t.Errorf("extractKubeVersionTag() got2 = %v, want %v", gotMinor, tt.wantMinor)
}
})
}
}