|
| 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 | + "io" |
| 21 | + "os" |
| 22 | + "path" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/google/go-cmp/cmp" |
| 26 | + "sigs.k8s.io/controller-tools/pkg/genall" |
| 27 | + "sigs.k8s.io/controller-tools/pkg/loader" |
| 28 | + "sigs.k8s.io/controller-tools/pkg/markers" |
| 29 | +) |
| 30 | + |
| 31 | +func Test_Generate(t *testing.T) { |
| 32 | + cwd, err := os.Getwd() |
| 33 | + if err != nil { |
| 34 | + t.Error(err) |
| 35 | + } |
| 36 | + |
| 37 | + optionsRegistry := &markers.Registry{} |
| 38 | + |
| 39 | + metricGenerator := Generator{} |
| 40 | + if err := metricGenerator.RegisterMarkers(optionsRegistry); err != nil { |
| 41 | + t.Error(err) |
| 42 | + } |
| 43 | + |
| 44 | + out := &outputRule{ |
| 45 | + buf: &bytes.Buffer{}, |
| 46 | + } |
| 47 | + |
| 48 | + // Load the passed packages as roots. |
| 49 | + roots, err := loader.LoadRoots(path.Join(cwd, "testdata", "...")) |
| 50 | + if err != nil { |
| 51 | + t.Errorf("loading packages %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + gen := Generator{} |
| 55 | + |
| 56 | + generationContext := &genall.GenerationContext{ |
| 57 | + Collector: &markers.Collector{Registry: optionsRegistry}, |
| 58 | + Roots: roots, |
| 59 | + Checker: &loader.TypeChecker{}, |
| 60 | + OutputRule: out, |
| 61 | + } |
| 62 | + |
| 63 | + t.Log("Trying to generate a custom resource configuration from the loaded packages") |
| 64 | + |
| 65 | + if err := gen.Generate(generationContext); err != nil { |
| 66 | + t.Error(err) |
| 67 | + } |
| 68 | + output := out.buf.String() |
| 69 | + |
| 70 | + t.Log("Comparing output to testdata to check for regressions") |
| 71 | + |
| 72 | + expectedFile, err := os.ReadFile(path.Clean(path.Join(cwd, "testdata", "foo-config.yaml"))) |
| 73 | + if err != nil { |
| 74 | + t.Error(err) |
| 75 | + } |
| 76 | + |
| 77 | + diff := cmp.Diff(string(expectedFile), output) |
| 78 | + if diff != "" { |
| 79 | + t.Log("output:") |
| 80 | + t.Log(output) |
| 81 | + t.Log("diff:") |
| 82 | + t.Log(diff) |
| 83 | + t.Log("Expected output to match file `testdata/foo-config.yaml` but it does not.") |
| 84 | + t.Log("If the change is intended, use `go generate ./pkg/customresourcestate/generate/generator/testdata` to regenerate the `testdata/foo-config.yaml` file.") |
| 85 | + t.Error("Detected a diff between the output of the integration test and the file `testdata/foo-config.yaml`.") |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +type outputRule struct { |
| 90 | + buf *bytes.Buffer |
| 91 | +} |
| 92 | + |
| 93 | +func (o *outputRule) Open(_ *loader.Package, _ string) (io.WriteCloser, error) { |
| 94 | + return nopCloser{o.buf}, nil |
| 95 | +} |
| 96 | + |
| 97 | +type nopCloser struct { |
| 98 | + io.Writer |
| 99 | +} |
| 100 | + |
| 101 | +func (n nopCloser) Close() error { |
| 102 | + return nil |
| 103 | +} |
0 commit comments