Skip to content

Commit aefb309

Browse files
committed
make test: fix pkg/legacy
These are on their way out, so tempted to outright delete the tests. But for now, do just enough to keep them passing - dates get parsed as time.Time by go-yaml v3 - fix milestone parsing regex, and assume string keys
1 parent 1ea8997 commit aefb309

File tree

4 files changed

+27
-31
lines changed

4 files changed

+27
-31
lines changed

pkg/legacy/keps/validations/yaml.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ limitations under the License.
1717
package validations
1818

1919
import (
20+
"fmt"
2021
"regexp"
2122
"sort"
2223
"strings"
24+
"time"
2325

2426
"k8s.io/enhancements/pkg/legacy/util"
2527
)
@@ -30,7 +32,7 @@ var (
3032
reStatus = regexp.MustCompile(strings.Join(statuses, "|"))
3133
stages = []string{"alpha", "beta", "stable"}
3234
reStages = regexp.MustCompile(strings.Join(stages, "|"))
33-
reMilestone = regexp.MustCompile(`v1\\.[1-9][0-9]*`)
35+
reMilestone = regexp.MustCompile(`v1\.[1-9][0-9]*`)
3436
)
3537

3638
// TODO(lint): cyclomatic complexity 50 of func `ValidateStructure` is high (> 30) (gocyclo)
@@ -107,9 +109,14 @@ func ValidateStructure(parsed map[interface{}]interface{}) error {
107109
continue
108110
}
109111

110-
fallthrough
111-
112-
case "title", "creation-date", "last-updated":
112+
case "creation-date", "last-updated":
113+
switch v := value.(type) {
114+
case time.Time:
115+
continue
116+
default:
117+
return fmt.Errorf("%s's value (%v) must parse to time.Time, got: %v", k, value, v)
118+
}
119+
case "title":
113120
// TODO(lint): singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
114121
//nolint:gocritic
115122
switch v := value.(type) {
@@ -201,13 +208,13 @@ func ValidateStructure(parsed map[interface{}]interface{}) error {
201208
// TODO(lint): Error return value is not checked (errcheck)
202209
//nolint:errcheck
203210
v, _ := value.(string)
204-
if !reMilestone.Match([]byte(v)) {
211+
if !reMilestone.MatchString(v) {
205212
return util.NewValueMustBeMilestone(k, v)
206213
}
207214

208215
case "milestone":
209216
switch v := value.(type) {
210-
case map[interface{}]interface{}:
217+
case map[string]interface{}:
211218
if err := validateMilestone(v); err != nil {
212219
return err
213220
}
@@ -221,14 +228,8 @@ func ValidateStructure(parsed map[interface{}]interface{}) error {
221228
return nil
222229
}
223230

224-
func validateMilestone(parsed map[interface{}]interface{}) error {
225-
for key, value := range parsed {
226-
// First off the key has to be a string. fact.
227-
k, ok := key.(string)
228-
if !ok {
229-
return util.NewKeyMustBeString(k)
230-
}
231-
231+
func validateMilestone(parsed map[string]interface{}) error {
232+
for k, value := range parsed {
232233
// TODO(lint): singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
233234
//nolint:gocritic
234235
switch strings.ToLower(k) {

pkg/legacy/keps/validations/yaml_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ func TestUnmarshalSuccess(t *testing.T) {
107107
OwningSIG: "sig-architecture",
108108
Status: "provisional",
109109
Approvers: []string{"my approvers"},
110-
LastUpdated: "at some point",
111-
CreationDate: "a while ago",
110+
LastUpdated: "2020-03-04",
111+
CreationDate: "2020-01-02",
112112
}
113113
p := map[interface{}]interface{}{}
114114

pkg/legacy/prrs/validations/yaml.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,24 @@ func ValidateStructure(parsed map[interface{}]interface{}) error {
4444
switch strings.ToLower(k) {
4545
case "alpha", "beta", "stable":
4646
switch v := value.(type) {
47-
case map[interface{}]interface{}:
47+
case map[string]interface{}:
4848
if err := validateMilestone(v); err != nil {
4949
return fmt.Errorf("invalid %s field: %v", k, err)
5050
}
51-
case interface{}:
52-
return util.NewValueMustBeStruct(k, v)
51+
default:
52+
return fmt.Errorf("field %s value '%v' is of invalid type %v", key, value, v)
5353
}
5454
}
5555
}
5656
return nil
5757
}
5858

59-
func validateMilestone(parsed map[interface{}]interface{}) error {
59+
func validateMilestone(parsed map[string]interface{}) error {
6060
// prrApprovers must be sorted to use SearchStrings down below...
6161
prrApprovers := util.PRRApprovers()
6262
sort.Strings(prrApprovers)
6363

64-
for key, value := range parsed {
65-
// First off the key has to be a string. fact.
66-
k, ok := key.(string)
67-
if !ok {
68-
return util.NewKeyMustBeString(k)
69-
}
64+
for k, value := range parsed {
7065

7166
// figure out the types
7267
// TODO(lint): singleCaseSwitch: should rewrite switch statement to if statement (gocritic)

pkg/legacy/prrs/validations/yaml_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,29 @@ func TestValidateStructureSuccess(t *testing.T) {
3232
{
3333
name: "just alpha",
3434
input: map[interface{}]interface{}{
35-
"alpha": map[interface{}]interface{}{
35+
"alpha": map[string]interface{}{
3636
"approver": "@wojtek-t",
3737
},
3838
},
3939
},
4040
{
4141
name: "all milestones",
4242
input: map[interface{}]interface{}{
43-
"alpha": map[interface{}]interface{}{
43+
"alpha": map[string]interface{}{
4444
"approver": "@wojtek-t",
4545
},
46-
"beta": map[interface{}]interface{}{
46+
"beta": map[string]interface{}{
4747
"approver": "@wojtek-t",
4848
},
49-
"stable": map[interface{}]interface{}{
49+
"stable": map[string]interface{}{
5050
"approver": "@wojtek-t",
5151
},
5252
},
5353
},
5454
{
5555
name: "just stable",
5656
input: map[interface{}]interface{}{
57-
"stable": map[interface{}]interface{}{
57+
"stable": map[string]interface{}{
5858
"approver": "@wojtek-t",
5959
},
6060
},

0 commit comments

Comments
 (0)