github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/spyglass/lenses/links/links_test.go (about) 1 /* 2 Copyright 2021 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 links 18 19 import ( 20 "reflect" 21 "testing" 22 23 "sigs.k8s.io/prow/pkg/config" 24 ) 25 26 func TestHumanReadableName(t *testing.T) { 27 tests := []struct { 28 input string 29 want string 30 }{ 31 { 32 input: "master-and-node-logs.txt", 33 want: "Master and node logs", 34 }, 35 { 36 input: "dashboard.link.txt", 37 want: "Dashboard", 38 }, 39 { 40 input: "no-suffix", 41 want: "No suffix", 42 }, 43 44 // Malformed inputs 45 { 46 input: ".link.txt", 47 want: "", 48 }, 49 { 50 input: "", 51 want: "", 52 }, 53 } 54 55 for _, tc := range tests { 56 t.Run(tc.input, func(t *testing.T) { 57 if got := humanReadableName(tc.input); got != tc.want { 58 t.Errorf("humanReadableName(%v)=%q, want: %v", tc.input, got, tc.want) 59 } 60 }) 61 } 62 } 63 64 func TestToLink(t *testing.T) { 65 spyglassConfig := config.Spyglass{ 66 GCSBrowserPrefix: "http://gcsbrowser/", 67 } 68 tests := []struct { 69 jobPath string 70 content []byte 71 want link 72 }{ 73 { 74 jobPath: "artifacts/dashboard.link.txt", 75 content: []byte("http://website.com/asdasds?adssasds\n"), 76 want: link{ 77 Name: "Dashboard", 78 URL: "http://website.com/asdasds?adssasds", 79 Link: "https://google.com/url?q=http%3A%2F%2Fwebsite.com%2Fasdasds%3Fadssasds", 80 }, 81 }, 82 { 83 jobPath: "artifacts/master-and-node-logs.link.txt", 84 content: []byte("gs://bucket/asdasd/asdasd\n"), 85 want: link{ 86 Name: "Master and node logs", 87 URL: "gs://bucket/asdasd/asdasd", 88 Link: "http://gcsbrowser/bucket/asdasd/asdasd", 89 }, 90 }, 91 } 92 for _, tc := range tests { 93 t.Run(tc.jobPath, func(t *testing.T) { 94 if got := toLink(tc.jobPath, tc.content, spyglassConfig); got != tc.want { 95 t.Errorf("toLink(%q, %q, %+v)=%q, want: %+v", tc.jobPath, tc.content, spyglassConfig, got, tc.want) 96 } 97 }) 98 } 99 } 100 101 func TestParseLinkFile(t *testing.T) { 102 spyglassConfig := config.Spyglass{ 103 GCSBrowserPrefix: "http://gcsbrowser/", 104 } 105 tests := []struct { 106 jobPath string 107 content []byte 108 hasError bool 109 want linkGroup 110 }{ 111 { 112 jobPath: "artifacts/dashboard.link.txt", 113 content: []byte("http://website.com/asdasds?adssasds\n"), 114 want: linkGroup{ 115 Title: "Dashboard", 116 Links: []link{ 117 { 118 Name: "Dashboard", 119 URL: "http://website.com/asdasds?adssasds", 120 Link: "https://google.com/url?q=http%3A%2F%2Fwebsite.com%2Fasdasds%3Fadssasds", 121 }, 122 }, 123 }, 124 }, 125 { 126 jobPath: "artifacts/debugging-links.link.json", 127 content: []byte(`{ 128 "title": "Debugging links group", 129 "links": [ 130 { 131 "name": "Some random website", 132 "url": "https://example.com" 133 }, 134 { 135 "name": "Link from GCS", 136 "url": "gs://some-bucket/some-file" 137 } 138 ] 139 }`), 140 want: linkGroup{ 141 Title: "Debugging links group", 142 Links: []link{ 143 { 144 Name: "Some random website", 145 URL: "https://example.com", 146 Link: "https://google.com/url?q=https%3A%2F%2Fexample.com", 147 }, 148 { 149 Name: "Link from GCS", 150 URL: "gs://some-bucket/some-file", 151 Link: "http://gcsbrowser/some-bucket/some-file", 152 }, 153 }, 154 }, 155 }, 156 { 157 jobPath: "artifacts/debugging-links.link.json", 158 content: []byte(`some unparseable json file contents`), 159 hasError: true, 160 }, 161 } 162 for _, tc := range tests { 163 t.Run(tc.jobPath, func(t *testing.T) { 164 got, err := parseLinkFile(tc.jobPath, tc.content, spyglassConfig) 165 if err != nil && !tc.hasError { 166 t.Fatalf("unexpected error for parseLinkFile(%q, %q, %+v): %v", tc.jobPath, tc.content, spyglassConfig, err) 167 } 168 if err == nil && tc.hasError { 169 t.Fatalf("expected error for parseLinkFile(%q, %q, %+v), got none", tc.jobPath, tc.content, spyglassConfig) 170 } 171 if !reflect.DeepEqual(got, tc.want) { 172 t.Errorf("parseLinkFile(%q, %q, %+v)=%q, want: %v", tc.jobPath, tc.content, spyglassConfig, got, tc.want) 173 } 174 }) 175 } 176 }