|
| 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 | +} |
0 commit comments