|
| 1 | +/* |
| 2 | +Copyright 2025 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "encoding/json" |
| 22 | + "errors" |
| 23 | + "io" |
| 24 | + "net/http" |
| 25 | + "os" |
| 26 | + "sigs.k8s.io/prow/pkg/pluginhelp" |
| 27 | + "strings" |
| 28 | + "testing" |
| 29 | + "time" |
| 30 | +) |
| 31 | + |
| 32 | +type mockRoundTripper struct { |
| 33 | + resp *http.Response |
| 34 | + err error |
| 35 | +} |
| 36 | + |
| 37 | +func (m *mockRoundTripper) RoundTrip(*http.Request) (*http.Response, error) { |
| 38 | + return m.resp, m.err |
| 39 | +} |
| 40 | + |
| 41 | +func TestGetHelp(t *testing.T) { |
| 42 | + type testCase struct { |
| 43 | + name string |
| 44 | + hookPath string |
| 45 | + cert string |
| 46 | + certContent string |
| 47 | + mockResp *http.Response |
| 48 | + mockErr error |
| 49 | + wantDesc string |
| 50 | + wantFileErr bool |
| 51 | + wantErr string |
| 52 | + cacheTest bool |
| 53 | + } |
| 54 | + |
| 55 | + pluginHelp := pluginhelp.PluginHelp{Description: "test"} |
| 56 | + cached := pluginhelp.PluginHelp{Description: "cached"} |
| 57 | + help := pluginhelp.Help{PluginHelp: map[string]pluginhelp.PluginHelp{"example": pluginHelp}} |
| 58 | + cachedHelp := pluginhelp.Help{PluginHelp: map[string]pluginhelp.PluginHelp{"example": cached}} |
| 59 | + helpBytes, _ := json.Marshal(help) |
| 60 | + cachedBytes, _ := json.Marshal(cachedHelp) |
| 61 | + |
| 62 | + cases := []testCase{ |
| 63 | + { |
| 64 | + name: "NoCert_Success", |
| 65 | + hookPath: "http://example.com/help", |
| 66 | + cert: "", |
| 67 | + mockResp: &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader(helpBytes))}, |
| 68 | + wantDesc: "test", |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "WithCert_FileError", |
| 72 | + hookPath: "https://example.com/help", |
| 73 | + cert: "C://badfileformat://", |
| 74 | + wantFileErr: true, |
| 75 | + wantErr: "error decoding cert file: open C://badfileformat://: no such file or directory", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "NoCert_NoHttps", |
| 79 | + hookPath: "http://example.com/help", |
| 80 | + cert: "", |
| 81 | + mockResp: &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader(helpBytes))}, |
| 82 | + wantDesc: "test", |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "WithCert_Success", |
| 86 | + hookPath: "https://example.com/help", |
| 87 | + cert: t.TempDir() + "/cert.pem", |
| 88 | + certContent: "dummy cert content", |
| 89 | + mockResp: &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader(helpBytes))}, |
| 90 | + wantDesc: "test", |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "HTTPError", |
| 94 | + hookPath: "http://example.com/help", |
| 95 | + cert: "", |
| 96 | + mockErr: errors.New("fail"), |
| 97 | + wantErr: "error Getting plugin help: Get \"http://example.com/help\": fail", |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "StatusCodeError", |
| 101 | + hookPath: "http://example.com/help", |
| 102 | + cert: "", |
| 103 | + mockResp: &http.Response{StatusCode: 500, Body: io.NopCloser(bytes.NewReader([]byte{}))}, |
| 104 | + wantErr: "response has status code 500", |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "JSONError", |
| 108 | + hookPath: "http://example.com/help", |
| 109 | + cert: "", |
| 110 | + mockResp: &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader([]byte("notjson")))}, |
| 111 | + wantErr: "error decoding json plugin help:", |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "Cache", |
| 115 | + hookPath: "http://example.com/help", |
| 116 | + cert: "", |
| 117 | + mockResp: &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader(cachedBytes))}, |
| 118 | + wantDesc: "cached", |
| 119 | + cacheTest: true, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + for _, tc := range cases { |
| 124 | + t.Run(tc.name, func(t *testing.T) { |
| 125 | + |
| 126 | + //// Mock the HTTP client |
| 127 | + http.DefaultTransport = &mockRoundTripper{ |
| 128 | + resp: tc.mockResp, |
| 129 | + err: tc.mockErr, |
| 130 | + } |
| 131 | + |
| 132 | + ha := newHelpAgent(tc.hookPath, tc.cert) |
| 133 | + if tc.cert != "" && !tc.wantFileErr { |
| 134 | + err := os.WriteFile(tc.cert, []byte(tc.certContent), 0644) |
| 135 | + if err != nil { |
| 136 | + t.Fatalf("Failed to create test file: %v", err) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if tc.cacheTest { |
| 141 | + // First call populates cache |
| 142 | + got, err := ha.getHelp() |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("unexpected error: %v", err) |
| 145 | + } |
| 146 | + if got.PluginHelp["example"].Description != tc.wantDesc { |
| 147 | + t.Errorf("expected description '%s', got '%s'", tc.wantDesc, got.PluginHelp["example"].Description) |
| 148 | + } |
| 149 | + // Set expiry to future to trigger cache |
| 150 | + ha.expiry = time.Now().Add(time.Minute) |
| 151 | + got2, err2 := ha.getHelp() |
| 152 | + if err2 != nil { |
| 153 | + t.Fatalf("unexpected error: %v", err2) |
| 154 | + } |
| 155 | + if got != got2 { |
| 156 | + t.Errorf("expected cached help, got different pointers") |
| 157 | + } |
| 158 | + return |
| 159 | + } |
| 160 | + got, err := ha.getHelp() |
| 161 | + if tc.wantErr != "" { |
| 162 | + if err == nil || !strings.Contains(err.Error(), tc.wantErr) { |
| 163 | + t.Errorf("expected error containing '%s', got %v", tc.wantErr, err) |
| 164 | + } |
| 165 | + } else { |
| 166 | + if err != nil { |
| 167 | + t.Fatalf("unexpected error: %v", err) |
| 168 | + } |
| 169 | + if got.PluginHelp["example"].Description != tc.wantDesc { |
| 170 | + t.Errorf("expected description '%s', got '%s'", tc.wantDesc, got.PluginHelp["example"].Description) |
| 171 | + } |
| 172 | + } |
| 173 | + }) |
| 174 | + } |
| 175 | +} |
0 commit comments