|
| 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 metrics |
| 17 | + |
| 18 | +import ( |
| 19 | + "bytes" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "os" |
| 23 | + "path" |
| 24 | + "strings" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/google/go-cmp/cmp" |
| 28 | + "sigs.k8s.io/controller-tools/pkg/genall" |
| 29 | + "sigs.k8s.io/controller-tools/pkg/loader" |
| 30 | + "sigs.k8s.io/controller-tools/pkg/markers" |
| 31 | + "sigs.k8s.io/controller-tools/pkg/metrics/internal/config" |
| 32 | +) |
| 33 | + |
| 34 | +func Test_Generate(t *testing.T) { |
| 35 | + cwd, err := os.Getwd() |
| 36 | + if err != nil { |
| 37 | + t.Error(err) |
| 38 | + } |
| 39 | + |
| 40 | + optionsRegistry := &markers.Registry{} |
| 41 | + |
| 42 | + metricGenerator := Generator{} |
| 43 | + if err := metricGenerator.RegisterMarkers(optionsRegistry); err != nil { |
| 44 | + t.Error(err) |
| 45 | + } |
| 46 | + |
| 47 | + out := &outputRule{ |
| 48 | + buf: &bytes.Buffer{}, |
| 49 | + } |
| 50 | + |
| 51 | + // Load the passed packages as roots. |
| 52 | + roots, err := loader.LoadRoots(path.Join(cwd, "testdata", "...")) |
| 53 | + if err != nil { |
| 54 | + t.Errorf("loading packages %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + gen := Generator{} |
| 58 | + |
| 59 | + generationContext := &genall.GenerationContext{ |
| 60 | + Collector: &markers.Collector{Registry: optionsRegistry}, |
| 61 | + Roots: roots, |
| 62 | + Checker: &loader.TypeChecker{}, |
| 63 | + OutputRule: out, |
| 64 | + } |
| 65 | + |
| 66 | + t.Log("Trying to generate a custom resource configuration from the loaded packages") |
| 67 | + |
| 68 | + if err := gen.Generate(generationContext); err != nil { |
| 69 | + t.Error(err) |
| 70 | + } |
| 71 | + |
| 72 | + output := strings.Split(out.buf.String(), "\n---\n") |
| 73 | + |
| 74 | + header := fmt.Sprintf(headerText, "(devel)", config.KubeStateMetricsVersion) |
| 75 | + |
| 76 | + if len(output) != 3 { |
| 77 | + t.Error("Expected two output files, metrics configuration followed by rbac.") |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + generatedData := map[string]string{ |
| 82 | + "metrics.yaml": header + "---\n" + string(output[1]), |
| 83 | + "rbac.yaml": "---\n" + string(output[2]), |
| 84 | + } |
| 85 | + |
| 86 | + t.Log("Comparing output to testdata to check for regressions") |
| 87 | + |
| 88 | + for _, golden := range []string{"metrics.yaml", "rbac.yaml"} { |
| 89 | + // generatedRaw := strings.TrimSpace(output[i]) |
| 90 | + |
| 91 | + expectedRaw, err := os.ReadFile(path.Clean(path.Join(cwd, "testdata", golden))) |
| 92 | + if err != nil { |
| 93 | + t.Error(err) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + // Remove leading `---` and trim newlines |
| 98 | + generated := strings.TrimSpace(strings.TrimPrefix(generatedData[golden], "---")) |
| 99 | + expected := strings.TrimSpace(strings.TrimPrefix(string(expectedRaw), "---")) |
| 100 | + |
| 101 | + diff := cmp.Diff(expected, generated) |
| 102 | + if diff != "" { |
| 103 | + t.Log("generated:") |
| 104 | + t.Log(generated) |
| 105 | + t.Log("diff:") |
| 106 | + t.Log(diff) |
| 107 | + t.Logf("Expected output to match file `testdata/%s` but it does not.", golden) |
| 108 | + t.Logf("If the change is intended, use `go generate ./pkg/metrics/testdata` to regenerate the `testdata/%s` file.", golden) |
| 109 | + t.Errorf("Detected a diff between the output of the integration test and the file `testdata/%s`.", golden) |
| 110 | + return |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +type outputRule struct { |
| 116 | + buf *bytes.Buffer |
| 117 | +} |
| 118 | + |
| 119 | +func (o *outputRule) Open(_ *loader.Package, _ string) (io.WriteCloser, error) { |
| 120 | + return nopCloser{o.buf}, nil |
| 121 | +} |
| 122 | + |
| 123 | +type nopCloser struct { |
| 124 | + io.Writer |
| 125 | +} |
| 126 | + |
| 127 | +func (n nopCloser) Close() error { |
| 128 | + return nil |
| 129 | +} |
0 commit comments