Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit c739733

Browse files
committed
prometheus-operator: Add conversion tests
- This commit adds new conversion test to verify if the `foldersFromFilesStructure` is put in the configmap correctly. Signed-off-by: Suraj Deshmukh <suraj@kinvolk.io>
1 parent c7507f0 commit c739733

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ require (
6262
google.golang.org/genproto v0.0.0-20200312145019-da6875a35672 // indirect
6363
google.golang.org/grpc v1.28.0 // indirect
6464
gopkg.in/ini.v1 v1.54.0 // indirect
65+
gopkg.in/yaml.v2 v2.2.8 // indirect
6566
gotest.tools/v3 v3.0.2 // indirect
6667
helm.sh/helm/v3 v3.1.2
6768
k8s.io/api v0.18.0
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package prometheus //nolint:testpackage
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/hashicorp/hcl/v2"
8+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
9+
yamlserializer "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
10+
"k8s.io/client-go/util/jsonpath"
11+
12+
"github.com/kinvolk/lokomotive/pkg/components/util"
13+
)
14+
15+
func renderManifests(t *testing.T, configHCL string) map[string]string {
16+
name := "prometheus-operator"
17+
18+
component := newComponent()
19+
20+
body, diagnostics := util.GetComponentBody(configHCL, name)
21+
if diagnostics.HasErrors() {
22+
t.Fatalf("Getting component body: %v", diagnostics.Errs())
23+
}
24+
25+
diagnostics = component.LoadConfig(body, &hcl.EvalContext{})
26+
if diagnostics.HasErrors() {
27+
t.Fatalf("Valid config should not return an error, got: %v", diagnostics)
28+
}
29+
30+
ret, err := component.RenderManifests()
31+
if err != nil {
32+
t.Fatalf("Rendering manifests with valid config should succeed, got: %s", err)
33+
}
34+
35+
return ret
36+
}
37+
38+
//nolint:funlen
39+
func TestConversion(t *testing.T) {
40+
testCases := []struct {
41+
Name string
42+
InputConfig string
43+
ExpectedConfigFileName string
44+
Expected reflect.Value
45+
JSONPath string
46+
}{
47+
{
48+
Name: "verify foldersFromFilesStructure in configmap",
49+
InputConfig: `component "prometheus-operator" {}`,
50+
ExpectedConfigFileName: "prometheus-operator/charts/grafana/templates/configmap-dashboard-provider.yaml",
51+
Expected: reflect.ValueOf(`apiVersion: 1
52+
providers:
53+
- name: 'sidecarProvider'
54+
orgId: 1
55+
folder: ''
56+
type: file
57+
disableDeletion: false
58+
allowUiUpdates: false
59+
options:
60+
foldersFromFilesStructure: true
61+
path: /tmp/dashboards`),
62+
JSONPath: "{.data.provider\\.yaml}",
63+
},
64+
}
65+
66+
for _, tc := range testCases {
67+
tc := tc
68+
t.Run(tc.Name, func(t *testing.T) {
69+
t.Parallel()
70+
71+
m := renderManifests(t, tc.InputConfig)
72+
if len(m) == 0 {
73+
t.Fatalf("Rendered manifests shouldn't be empty")
74+
}
75+
76+
gotConfig, ok := m[tc.ExpectedConfigFileName]
77+
if !ok {
78+
t.Fatalf("Config not found with filename: %q", tc.ExpectedConfigFileName)
79+
}
80+
81+
u := getUnstructredObj(t, gotConfig)
82+
got := getValFromObject(t, tc.JSONPath, u)
83+
84+
switch got.Kind() { //nolint:exhaustive
85+
case reflect.Interface:
86+
switch gotVal := got.Interface().(type) {
87+
// Add more cases as the expected values become heterogeneous.
88+
// case bool:
89+
case string:
90+
expVal, ok := tc.Expected.Interface().(string)
91+
if !ok {
92+
t.Fatalf("expected value is not string")
93+
}
94+
95+
if gotVal != expVal {
96+
t.Fatalf("expected: %s, got: %s", expVal, gotVal)
97+
}
98+
99+
default:
100+
t.Fatalf("Unknown type of the interface object: %T", got.Interface())
101+
}
102+
default:
103+
t.Fatalf("Unknown type of the object extracted from the converted YAML: %v", got.Kind())
104+
}
105+
})
106+
}
107+
}
108+
109+
func getUnstructredObj(t *testing.T, yamlObj string) *unstructured.Unstructured {
110+
u := &unstructured.Unstructured{}
111+
112+
// Decode YAML into `unstructured.Unstructured`.
113+
dec := yamlserializer.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
114+
if _, _, err := dec.Decode([]byte(yamlObj), nil, u); err != nil {
115+
t.Fatalf("Converting config to unstructured.Unstructured: %v", err)
116+
}
117+
118+
return u
119+
}
120+
121+
func getValFromObject(t *testing.T, jp string, obj *unstructured.Unstructured) reflect.Value {
122+
jPath := jsonpath.New("parse")
123+
if err := jPath.Parse(jp); err != nil {
124+
t.Fatalf("Parsing JSONPath: %v", err)
125+
}
126+
127+
v, err := jPath.FindResults(obj.Object)
128+
if err != nil {
129+
t.Fatalf("Finding results using JSONPath in the YAML file: %v", err)
130+
}
131+
132+
if len(v) == 0 || len(v[0]) == 0 {
133+
t.Fatalf("No result found")
134+
}
135+
136+
return v[0][0]
137+
}

pkg/components/prometheus-operator/component_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ component "prometheus-operator" {
6262
}`,
6363
wantErr: true,
6464
},
65+
{
66+
desc: "prometheus ingress and external_url given and are different",
67+
hcl: `
68+
component "prometheus-operator" {
69+
prometheus {
70+
external_url = "https://prometheus.notmydomain.net"
71+
ingress {
72+
host = "prometheus.mydomain.net"
73+
}
74+
}
75+
}
76+
`,
77+
wantErr: true,
78+
},
79+
{
80+
desc: "prometheus ingress and external_url given and are same",
81+
hcl: `
82+
component "prometheus-operator" {
83+
prometheus {
84+
external_url = "https://prometheus.mydomain.net"
85+
ingress {
86+
host = "prometheus.mydomain.net"
87+
}
88+
}
89+
}
90+
`,
91+
wantErr: false,
92+
},
6593
}
6694

6795
for _, tc := range tests {

vendor/k8s.io/apimachinery/pkg/runtime/serializer/yaml/yaml.go

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ k8s.io/apimachinery/pkg/runtime/serializer/protobuf
575575
k8s.io/apimachinery/pkg/runtime/serializer/recognizer
576576
k8s.io/apimachinery/pkg/runtime/serializer/streaming
577577
k8s.io/apimachinery/pkg/runtime/serializer/versioning
578+
k8s.io/apimachinery/pkg/runtime/serializer/yaml
578579
k8s.io/apimachinery/pkg/selection
579580
k8s.io/apimachinery/pkg/types
580581
k8s.io/apimachinery/pkg/util/cache

0 commit comments

Comments
 (0)