This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
5 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
pkg/components/prometheus-operator/component_conversion_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package prometheus //nolint:testpackage | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
yamlserializer "k8s.io/apimachinery/pkg/runtime/serializer/yaml" | ||
"k8s.io/client-go/util/jsonpath" | ||
|
||
"github.com/kinvolk/lokomotive/pkg/components/util" | ||
) | ||
|
||
func renderManifests(t *testing.T, configHCL string) map[string]string { | ||
name := "prometheus-operator" | ||
|
||
component := newComponent() | ||
|
||
body, diagnostics := util.GetComponentBody(configHCL, name) | ||
if diagnostics.HasErrors() { | ||
t.Fatalf("Getting component body: %v", diagnostics.Errs()) | ||
} | ||
|
||
diagnostics = component.LoadConfig(body, &hcl.EvalContext{}) | ||
if diagnostics.HasErrors() { | ||
t.Fatalf("Valid config should not return an error, got: %v", diagnostics) | ||
} | ||
|
||
ret, err := component.RenderManifests() | ||
if err != nil { | ||
t.Fatalf("Rendering manifests with valid config should succeed, got: %s", err) | ||
} | ||
|
||
return ret | ||
} | ||
|
||
//nolint:funlen | ||
func TestConversion(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
InputConfig string | ||
ExpectedConfigFileName string | ||
Expected reflect.Value | ||
JSONPath string | ||
}{ | ||
{ | ||
Name: "verify foldersFromFilesStructure in configmap", | ||
InputConfig: `component "prometheus-operator" {}`, | ||
ExpectedConfigFileName: "prometheus-operator/charts/grafana/templates/configmap-dashboard-provider.yaml", | ||
Expected: reflect.ValueOf(`apiVersion: 1 | ||
providers: | ||
- name: 'sidecarProvider' | ||
orgId: 1 | ||
folder: '' | ||
type: file | ||
disableDeletion: false | ||
allowUiUpdates: false | ||
options: | ||
foldersFromFilesStructure: true | ||
path: /tmp/dashboards`), | ||
JSONPath: "{.data.provider\\.yaml}", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
t.Run(tc.Name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
m := renderManifests(t, tc.InputConfig) | ||
if len(m) == 0 { | ||
t.Fatalf("Rendered manifests shouldn't be empty") | ||
} | ||
|
||
gotConfig, ok := m[tc.ExpectedConfigFileName] | ||
if !ok { | ||
t.Fatalf("Config not found with filename: %q", tc.ExpectedConfigFileName) | ||
} | ||
|
||
u := getUnstructredObj(t, gotConfig) | ||
got := getValFromObject(t, tc.JSONPath, u) | ||
|
||
switch got.Kind() { //nolint:exhaustive | ||
case reflect.Interface: | ||
switch gotVal := got.Interface().(type) { | ||
// Add more cases as the expected values become heterogeneous. | ||
// case bool: | ||
case string: | ||
expVal, ok := tc.Expected.Interface().(string) | ||
if !ok { | ||
t.Fatalf("expected value is not string") | ||
} | ||
|
||
if gotVal != expVal { | ||
t.Fatalf("expected: %s, got: %s", expVal, gotVal) | ||
} | ||
|
||
default: | ||
t.Fatalf("Unknown type of the interface object: %T", got.Interface()) | ||
} | ||
default: | ||
t.Fatalf("Unknown type of the object extracted from the converted YAML: %v", got.Kind()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func getUnstructredObj(t *testing.T, yamlObj string) *unstructured.Unstructured { | ||
u := &unstructured.Unstructured{} | ||
|
||
// Decode YAML into `unstructured.Unstructured`. | ||
dec := yamlserializer.NewDecodingSerializer(unstructured.UnstructuredJSONScheme) | ||
if _, _, err := dec.Decode([]byte(yamlObj), nil, u); err != nil { | ||
t.Fatalf("Converting config to unstructured.Unstructured: %v", err) | ||
} | ||
|
||
return u | ||
} | ||
|
||
func getValFromObject(t *testing.T, jp string, obj *unstructured.Unstructured) reflect.Value { | ||
jPath := jsonpath.New("parse") | ||
if err := jPath.Parse(jp); err != nil { | ||
t.Fatalf("Parsing JSONPath: %v", err) | ||
} | ||
|
||
v, err := jPath.FindResults(obj.Object) | ||
if err != nil { | ||
t.Fatalf("Finding results using JSONPath in the YAML file: %v", err) | ||
} | ||
|
||
if len(v) == 0 || len(v[0]) == 0 { | ||
t.Fatalf("No result found") | ||
} | ||
|
||
return v[0][0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
vendor/k8s.io/apimachinery/pkg/runtime/serializer/yaml/yaml.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters