Skip to content

Commit 8cac891

Browse files
authored
Merge pull request kubernetes#2298 from shekhar-rajak/query_test
query_test.go added - TestValidate method defined
2 parents c18e33a + 1e22f0b commit 8cac891

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

pkg/kepctl/query.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ type QueryOpts struct {
5555
Output string
5656
}
5757

58-
// Validate checks the args and cleans them up if needed
59-
func (c *QueryOpts) Validate(args []string) error {
58+
59+
func (c *QueryOpts) Validate() error {
6060
if len(c.SIG) > 0 {
6161
sigs, err := selectByRegexp(util.Groups(), c.SIG)
6262
if err != nil {

pkg/kepctl/query_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2021 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 kepctl
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
"github.com/stretchr/testify/require"
24+
)
25+
26+
27+
func TestValidate(t *testing.T) {
28+
testcases := []struct {
29+
name string
30+
queryOpts QueryOpts
31+
err error
32+
}{
33+
{
34+
name: "Valid SIG",
35+
queryOpts: QueryOpts{
36+
CommonArgs: CommonArgs {
37+
Name: "1011-test",
38+
},
39+
SIG: []string{"sig-multicluster"},
40+
IncludePRs: true,
41+
Output: "json",
42+
},
43+
err: nil,
44+
},
45+
{
46+
name: "Invalid SIG",
47+
queryOpts: QueryOpts{
48+
CommonArgs: CommonArgs {
49+
Name: "1011-test-xyz",
50+
},
51+
SIG: []string{"sig-xyz"},
52+
IncludePRs: true,
53+
Output: "json",
54+
},
55+
err: fmt.Errorf("No SIG matches any of the passed regular expressions"),
56+
},
57+
{
58+
name: "Unsupported Output format",
59+
queryOpts: QueryOpts{
60+
CommonArgs: CommonArgs {
61+
Name: "1011-test-testing",
62+
},
63+
SIG: []string{"sig-testing"},
64+
IncludePRs: true,
65+
Output: "PDF",
66+
},
67+
err: fmt.Errorf("unsupported output format: PDF. Valid values: [table json yaml]"),
68+
},
69+
}
70+
71+
for _, tc := range testcases {
72+
t.Run(tc.name, func(t *testing.T) {
73+
var queryOpts = tc.queryOpts
74+
var err = queryOpts.Validate()
75+
if tc.err == nil {
76+
require.Nil(t, err)
77+
} else {
78+
require.NotNil(t, err, tc.err.Error())
79+
}
80+
81+
})
82+
}
83+
}

0 commit comments

Comments
 (0)