Skip to content

Commit b265455

Browse files
rikatzk8s-publishing-bot
authored andcommitted
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 <[email protected]> --------- Co-authored-by: Antonio Ojea <[email protected]> Kubernetes-commit: c688478a28ba01827cbd1d4dba88547faa6fec0e
1 parent b510e2c commit b265455

File tree

2 files changed

+150
-11
lines changed

2 files changed

+150
-11
lines changed

cmd/prerelease-lifecycle-gen/prerelease-lifecycle-generators/status.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func extractKubeVersionTag(tagName string, t *types.Type) (*tagValue, int, int,
7171
}
7272

7373
splitValue := strings.Split(rawTag.value, ".")
74-
if len(splitValue) != 2 || len(splitValue[0]) == 0 || len(splitValue[0]) == 0 {
74+
if len(splitValue) != 2 || len(splitValue[0]) == 0 || len(splitValue[1]) == 0 {
7575
return nil, -1, -1, fmt.Errorf("%v format must match %v=xx.yy tag", t, tagName)
7676
}
7777
major, err := strconv.ParseInt(splitValue[0], 10, 32)
@@ -159,16 +159,7 @@ func extractTag(tagName string, comments []string) *tagValue {
159159
for i := range parts {
160160
kv := strings.SplitN(parts[i], "=", 2)
161161
k := kv[0]
162-
//v := ""
163-
//if len(kv) == 2 {
164-
// v = kv[1]
165-
//}
166-
switch k {
167-
//case "register":
168-
// if v != "false" {
169-
// tag.register = true
170-
// }
171-
default:
162+
if k != "" {
172163
klog.Fatalf("Unsupported %s param: %q", tagName, parts[i])
173164
}
174165
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package prereleaselifecyclegenerators
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
23+
"k8s.io/gengo/types"
24+
)
25+
26+
var mockType = &types.Type{
27+
CommentLines: []string{
28+
"RandomType defines a random structure in Kubernetes",
29+
"It should be used just when you need something different than 42",
30+
},
31+
SecondClosestCommentLines: []string{},
32+
}
33+
34+
func Test_extractKubeVersionTag(t *testing.T) {
35+
tests := []struct {
36+
name string
37+
tagName string
38+
tagComments []string
39+
wantValue *tagValue
40+
wantMajor int
41+
wantMinor int
42+
wantErr bool
43+
}{
44+
{
45+
name: "not found tag should generate an error",
46+
tagName: "someVersionTag:version",
47+
tagComments: []string{
48+
"+someOtherTag:version=1.5",
49+
},
50+
wantValue: nil,
51+
wantErr: true,
52+
},
53+
{
54+
name: "found tag should return correctly",
55+
tagName: "someVersionTag:version",
56+
tagComments: []string{
57+
"+someVersionTag:version=1.5",
58+
},
59+
wantValue: &tagValue{
60+
value: "1.5",
61+
},
62+
wantMajor: 1,
63+
wantMinor: 5,
64+
wantErr: false,
65+
},
66+
/*{
67+
name: "multiple declarations of same tag should return an error",
68+
tagName: "someVersionTag:version",
69+
tagComments: []string{
70+
"+someVersionTag:version=1.5",
71+
"+someVersionTag:version=v1.7",
72+
},
73+
wantValue: nil,
74+
wantErr: true, // TODO: Today it is klog.Fatal, check how to capture it
75+
},
76+
{
77+
name: "multiple values on same tag should return an error",
78+
tagName: "someVersionTag:version",
79+
tagComments: []string{
80+
"+someVersionTag:version=1.5,something",
81+
},
82+
wantValue: nil,
83+
wantErr: true, // TODO: Today it is klog.Fatal, check how to capture it
84+
},*/
85+
{
86+
name: "wrong tag major value should return an error",
87+
tagName: "someVersionTag:version",
88+
tagComments: []string{
89+
"+someVersionTag:version=.5",
90+
},
91+
wantErr: true,
92+
},
93+
{
94+
name: "wrong tag minor value should return an error",
95+
tagName: "someVersionTag:version",
96+
tagComments: []string{
97+
"+someVersionTag:version=1.",
98+
},
99+
wantErr: true,
100+
},
101+
{
102+
name: "wrong tag format should return an error",
103+
tagName: "someVersionTag:version",
104+
tagComments: []string{
105+
"+someVersionTag:version=1.5.7",
106+
},
107+
wantErr: true,
108+
},
109+
{
110+
name: "wrong tag major int value should return an error",
111+
tagName: "someVersionTag:version",
112+
tagComments: []string{
113+
"+someVersionTag:version=blah.5",
114+
},
115+
wantErr: true,
116+
},
117+
{
118+
name: "wrong tag minor int value should return an error",
119+
tagName: "someVersionTag:version",
120+
tagComments: []string{
121+
"+someVersionTag:version=1.blah",
122+
},
123+
wantErr: true,
124+
},
125+
}
126+
for _, tt := range tests {
127+
t.Run(tt.name, func(t *testing.T) {
128+
mockType.SecondClosestCommentLines = tt.tagComments
129+
gotTag, gotMajor, gotMinor, err := extractKubeVersionTag(tt.tagName, mockType)
130+
if (err != nil) != tt.wantErr {
131+
t.Errorf("extractKubeVersionTag() error = %v, wantErr %v", err, tt.wantErr)
132+
return
133+
}
134+
if tt.wantErr {
135+
return
136+
}
137+
if !reflect.DeepEqual(gotTag, tt.wantValue) {
138+
t.Errorf("extractKubeVersionTag() got = %v, want %v", gotTag, tt.wantValue)
139+
}
140+
if gotMajor != tt.wantMajor {
141+
t.Errorf("extractKubeVersionTag() got1 = %v, want %v", gotMajor, tt.wantMajor)
142+
}
143+
if gotMinor != tt.wantMinor {
144+
t.Errorf("extractKubeVersionTag() got2 = %v, want %v", gotMinor, tt.wantMinor)
145+
}
146+
})
147+
}
148+
}

0 commit comments

Comments
 (0)