Skip to content

Commit 26a7d8c

Browse files
committed
implement some unit tests
1 parent 6166b2a commit 26a7d8c

File tree

3 files changed

+263
-2
lines changed

3 files changed

+263
-2
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors All rights reserved.
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+
package markers
17+
18+
import (
19+
"reflect"
20+
"testing"
21+
22+
"k8s.io/kube-state-metrics/v2/pkg/customresourcestate"
23+
)
24+
25+
func Test_jsonPath_Parse(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
j jsonPath
29+
want []string
30+
wantErr bool
31+
}{
32+
{
33+
name: "empty input",
34+
j: "",
35+
want: []string{},
36+
wantErr: false,
37+
},
38+
{
39+
name: "dot input",
40+
j: ".",
41+
want: []string{""},
42+
wantErr: false,
43+
},
44+
{
45+
name: "some path input",
46+
j: ".foo.bar",
47+
want: []string{"foo", "bar"},
48+
wantErr: false,
49+
},
50+
{
51+
name: "invalid character ,",
52+
j: ".foo,.bar",
53+
wantErr: true,
54+
},
55+
{
56+
name: "invalid closure",
57+
j: "{.foo}",
58+
wantErr: true,
59+
},
60+
}
61+
for _, tt := range tests {
62+
t.Run(tt.name, func(t *testing.T) {
63+
got, err := tt.j.Parse()
64+
if (err != nil) != tt.wantErr {
65+
t.Errorf("jsonPath.Parse() error = %v, wantErr %v", err, tt.wantErr)
66+
return
67+
}
68+
if !reflect.DeepEqual(got, tt.want) {
69+
t.Errorf("jsonPath.Parse() = %v, want %v", got, tt.want)
70+
}
71+
})
72+
}
73+
}
74+
75+
func Test_newMetricMeta(t *testing.T) {
76+
tests := []struct {
77+
name string
78+
basePath []string
79+
j jsonPath
80+
jsonLabelsFromPath map[string]jsonPath
81+
want customresourcestate.MetricMeta
82+
}{
83+
{
84+
name: "with basePath and jsonpath, without jsonLabelsFromPath",
85+
basePath: []string{"foo"},
86+
j: jsonPath(".bar"),
87+
jsonLabelsFromPath: map[string]jsonPath{},
88+
want: customresourcestate.MetricMeta{
89+
Path: []string{"foo", "bar"},
90+
LabelsFromPath: map[string][]string{},
91+
},
92+
},
93+
{
94+
name: "with basePath, jsonpath and jsonLabelsFromPath",
95+
basePath: []string{"foo"},
96+
j: jsonPath(".bar"),
97+
jsonLabelsFromPath: map[string]jsonPath{"some": ".label.from.path"},
98+
want: customresourcestate.MetricMeta{
99+
Path: []string{"foo", "bar"},
100+
LabelsFromPath: map[string][]string{
101+
"some": {"label", "from", "path"},
102+
},
103+
},
104+
},
105+
{
106+
name: "no basePath, jsonpath and jsonLabelsFromPath",
107+
basePath: []string{},
108+
j: jsonPath(""),
109+
jsonLabelsFromPath: map[string]jsonPath{},
110+
want: customresourcestate.MetricMeta{
111+
Path: []string{},
112+
LabelsFromPath: map[string][]string{},
113+
},
114+
},
115+
}
116+
for _, tt := range tests {
117+
t.Run(tt.name, func(t *testing.T) {
118+
if got := newMetricMeta(tt.basePath, tt.j, tt.jsonLabelsFromPath); !reflect.DeepEqual(got, tt.want) {
119+
t.Errorf("newMetricMeta() = %v, want %v", got, tt.want)
120+
}
121+
})
122+
}
123+
}

pkg/customresourcestate/generate/markers/labelfrompath.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616
package markers
1717

1818
import (
19+
"errors"
1920
"fmt"
2021

2122
"sigs.k8s.io/controller-tools/pkg/markers"
@@ -59,14 +60,19 @@ func (labelFromPathMarker) Help() *markers.DefinitionHelp {
5960
}
6061

6162
func (n labelFromPathMarker) ApplyToResource(resource *customresourcestate.Resource) error {
62-
if resource.LabelsFromPath == nil {
63-
resource.LabelsFromPath = map[string][]string{}
63+
if resource == nil {
64+
return errors.New("expected resource to not be nil")
6465
}
66+
6567
jsonPathElems, err := n.JSONPath.Parse()
6668
if err != nil {
6769
return err
6870
}
6971

72+
if resource.LabelsFromPath == nil {
73+
resource.LabelsFromPath = map[string][]string{}
74+
}
75+
7076
if jsonPath, labelExists := resource.LabelsFromPath[n.Name]; labelExists {
7177
if len(jsonPathElems) != len(jsonPath) {
7278
return fmt.Errorf("duplicate definition for label %q", n.Name)
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors All rights reserved.
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+
package markers
17+
18+
import (
19+
"reflect"
20+
"testing"
21+
22+
"k8s.io/kube-state-metrics/v2/pkg/customresourcestate"
23+
)
24+
25+
func Test_labelFromPathMarker_ApplyToResource(t *testing.T) {
26+
type fields struct {
27+
Name string
28+
JSONPath jsonPath
29+
}
30+
tests := []struct {
31+
name string
32+
fields fields
33+
resource *customresourcestate.Resource
34+
wantResource *customresourcestate.Resource
35+
wantErr bool
36+
}{
37+
{
38+
name: "happy path",
39+
fields: fields{
40+
Name: "foo",
41+
JSONPath: ".bar",
42+
},
43+
resource: &customresourcestate.Resource{},
44+
wantResource: &customresourcestate.Resource{
45+
Labels: customresourcestate.Labels{
46+
LabelsFromPath: map[string][]string{
47+
"foo": {"bar"},
48+
},
49+
},
50+
},
51+
wantErr: false,
52+
},
53+
{
54+
name: "label already exists with same path length",
55+
fields: fields{
56+
Name: "foo",
57+
JSONPath: ".bar",
58+
},
59+
resource: &customresourcestate.Resource{
60+
Labels: customresourcestate.Labels{
61+
LabelsFromPath: map[string][]string{
62+
"foo": {"other"},
63+
},
64+
},
65+
},
66+
wantResource: &customresourcestate.Resource{
67+
Labels: customresourcestate.Labels{
68+
LabelsFromPath: map[string][]string{
69+
"foo": {"other"},
70+
},
71+
},
72+
},
73+
wantErr: true,
74+
},
75+
{
76+
name: "label already exists with different path length",
77+
fields: fields{
78+
Name: "foo",
79+
JSONPath: ".bar",
80+
},
81+
resource: &customresourcestate.Resource{
82+
Labels: customresourcestate.Labels{
83+
LabelsFromPath: map[string][]string{
84+
"foo": {"other", "path"},
85+
},
86+
},
87+
},
88+
wantResource: &customresourcestate.Resource{
89+
Labels: customresourcestate.Labels{
90+
LabelsFromPath: map[string][]string{
91+
"foo": {"other", "path"},
92+
},
93+
},
94+
},
95+
wantErr: true,
96+
},
97+
{
98+
name: "invalid json path",
99+
fields: fields{
100+
Name: "foo",
101+
JSONPath: "{.bar}",
102+
},
103+
resource: &customresourcestate.Resource{},
104+
wantResource: &customresourcestate.Resource{},
105+
wantErr: true,
106+
},
107+
{
108+
name: "nil resource",
109+
fields: fields{
110+
Name: "foo",
111+
JSONPath: "{.bar}",
112+
},
113+
resource: nil,
114+
wantErr: true,
115+
},
116+
}
117+
for _, tt := range tests {
118+
t.Run(tt.name, func(t *testing.T) {
119+
n := labelFromPathMarker{
120+
Name: tt.fields.Name,
121+
JSONPath: tt.fields.JSONPath,
122+
}
123+
if err := n.ApplyToResource(tt.resource); (err != nil) != tt.wantErr {
124+
t.Errorf("labelFromPathMarker.ApplyToResource() error = %v, wantErr %v", err, tt.wantErr)
125+
}
126+
if !reflect.DeepEqual(tt.resource, tt.wantResource) {
127+
t.Errorf("labelFromPathMarker.ApplyToResource() = %v, want %v", tt.resource, tt.wantResource)
128+
}
129+
130+
})
131+
}
132+
}

0 commit comments

Comments
 (0)