github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/spyglass/lenses/podinfo/podinfo_test.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 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 17 package podinfo 18 19 import ( 20 "encoding/json" 21 "os" 22 "path" 23 "testing" 24 25 "github.com/sirupsen/logrus/hooks/test" 26 "sigs.k8s.io/prow/pkg/config" 27 "sigs.k8s.io/prow/pkg/spyglass/api" 28 "sigs.k8s.io/prow/pkg/spyglass/lenses/fake" 29 ) 30 31 func TestBody(t *testing.T) { 32 tests := []struct { 33 name string 34 artifacts []api.Artifact 35 tmpl string 36 ownConfig *ownConfig 37 want string 38 }{ 39 { 40 name: "base", 41 artifacts: []api.Artifact{ 42 &fake.Artifact{ 43 Path: "podinfo.json", 44 Content: []byte(`{ 45 "pod": { 46 "metadata": { 47 "name": "abc-123" 48 } 49 } 50 }`), 51 }, 52 &fake.Artifact{ 53 Path: "prowjob.json", 54 Content: []byte(`{ 55 "spec": { 56 "cluster": "bar" 57 } 58 }`), 59 }, 60 }, 61 ownConfig: &ownConfig{ 62 RunnerConfigs: map[string]RunnerConfig{ 63 "bar": { 64 PodLinkTemplate: "http://somewhere/pod/{{ .Name }}", 65 }, 66 }, 67 }, 68 }, 69 { 70 name: "no-prowjob-json", 71 artifacts: []api.Artifact{ 72 &fake.Artifact{ 73 Path: "podinfo.json", 74 Content: []byte(`{ 75 "pod": { 76 "metadata": { 77 "name": "abc-123" 78 } 79 } 80 }`), 81 }, 82 }, 83 ownConfig: &ownConfig{ 84 RunnerConfigs: map[string]RunnerConfig{ 85 "bar": { 86 PodLinkTemplate: "http://somewhere/pod/{{ .Name }}", 87 }, 88 }, 89 }, 90 }, 91 { 92 name: "no-cluster-info", 93 artifacts: []api.Artifact{ 94 &fake.Artifact{ 95 Path: "podinfo.json", 96 Content: []byte(`{ 97 "pod": { 98 "metadata": { 99 "name": "abc-123" 100 } 101 } 102 }`), 103 }, 104 &fake.Artifact{ 105 Path: "prowjob.json", 106 Content: []byte(`{ 107 "spec": { 108 "cluster": "bar" 109 } 110 }`), 111 }, 112 }, 113 ownConfig: nil, 114 }, 115 } 116 117 logHook := test.NewGlobal() 118 for _, tc := range tests { 119 t.Run(tc.name, func(t *testing.T) { 120 logHook.Reset() 121 wantFile := path.Join("testdata", "test_"+tc.name+".html") 122 wantBytes, err := os.ReadFile(wantFile) 123 if err != nil { 124 t.Fatalf("Failed reading output file %s: %v", wantFile, err) 125 } 126 var oc []byte 127 if tc.ownConfig != nil { 128 oc, err = json.Marshal(tc.ownConfig) 129 if err != nil { 130 t.Fatal(err) 131 } 132 } 133 got, want := Lens{}.Body(tc.artifacts, ".", "", json.RawMessage(oc), config.Spyglass{}), string(wantBytes) 134 if got != want { 135 t.Errorf("Output mismatch\nwant: %s\n got: %s", want, got) 136 } 137 if entries := logHook.AllEntries(); len(entries) > 0 { 138 var logs []string 139 for _, entry := range entries { 140 log, _ := entry.String() 141 logs = append(logs, log) 142 } 143 t.Errorf("Unexpected log messages: %v", logs) 144 } 145 }) 146 } 147 }