|
| 1 | +// Copyright The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package apiurl |
| 15 | + |
| 16 | +import ( |
| 17 | + "net/url" |
| 18 | + "os" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | + |
| 23 | + amcommoncfg "github.com/prometheus/alertmanager/config/common" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + baseURL = "https://slack.com" |
| 28 | + defaultURLWithAPIPath = baseURL + "/api/chat.postMessage" |
| 29 | + defaultURLWithFilePath = baseURL + "/file/chat.postMessage" |
| 30 | + |
| 31 | + addReactionsMethod = "reactions.add" |
| 32 | + updateChatMethod = "chat.update" |
| 33 | +) |
| 34 | + |
| 35 | +func TestResolver_URLForMethod_ValidScenarios(t *testing.T) { |
| 36 | + t.Parallel() |
| 37 | + |
| 38 | + defaultAPIURLFile := createTempAPIURLFile(t, "test_url_for_method", defaultURLWithFilePath) |
| 39 | + |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + apiURL *amcommoncfg.SecretURL |
| 43 | + apiURLFile string |
| 44 | + method string |
| 45 | + want string |
| 46 | + }{ |
| 47 | + { |
| 48 | + name: "empty method with apiURL", |
| 49 | + apiURL: getSecretURL(t, defaultURLWithAPIPath), |
| 50 | + apiURLFile: "", |
| 51 | + method: "", |
| 52 | + want: defaultURLWithAPIPath, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "empty method without apiURL but with apiURLFile", |
| 56 | + apiURL: nil, |
| 57 | + apiURLFile: defaultAPIURLFile.Name(), |
| 58 | + method: "", |
| 59 | + want: defaultURLWithFilePath, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "empty method with apiURL and apiURLFile, should use apiURL", |
| 63 | + apiURL: getSecretURL(t, defaultURLWithAPIPath), |
| 64 | + apiURLFile: defaultAPIURLFile.Name(), |
| 65 | + method: "", |
| 66 | + want: defaultURLWithAPIPath, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "method chat.update with apiURL", |
| 70 | + apiURL: getSecretURL(t, defaultURLWithAPIPath), |
| 71 | + apiURLFile: defaultAPIURLFile.Name(), |
| 72 | + method: updateChatMethod, |
| 73 | + want: baseURL + "/api/" + updateChatMethod, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "method chat.update with apiURL with trailing slash", |
| 77 | + apiURL: getSecretURL(t, defaultURLWithAPIPath+"/"), |
| 78 | + apiURLFile: defaultAPIURLFile.Name(), |
| 79 | + method: updateChatMethod, |
| 80 | + want: baseURL + "/api/" + updateChatMethod, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "method reactions.add with apiURLFile", |
| 84 | + apiURL: nil, |
| 85 | + apiURLFile: defaultAPIURLFile.Name(), |
| 86 | + method: addReactionsMethod, |
| 87 | + want: baseURL + "/file/" + addReactionsMethod, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "method reactions.add with apiURLFile with empty spaces", |
| 91 | + apiURL: nil, |
| 92 | + apiURLFile: createTempAPIURLFile(t, "test_url_for_method_with_new_lines_and_spaces", defaultURLWithFilePath+"\n \n").Name(), |
| 93 | + method: addReactionsMethod, |
| 94 | + want: baseURL + "/file/" + addReactionsMethod, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "method reactions.add with apiURLFile with trailing slash", |
| 98 | + apiURL: nil, |
| 99 | + apiURLFile: createTempAPIURLFile(t, "test_url_for_method_with_trailing_slash", defaultURLWithFilePath+"/").Name(), |
| 100 | + method: addReactionsMethod, |
| 101 | + want: baseURL + "/file/" + addReactionsMethod, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tt := range tests { |
| 106 | + tt := tt |
| 107 | + t.Run(tt.name, func(t *testing.T) { |
| 108 | + t.Parallel() |
| 109 | + |
| 110 | + resolver := New(tt.apiURL, tt.apiURLFile) |
| 111 | + |
| 112 | + got, err := resolver.URLForMethod(tt.method) |
| 113 | + require.NoError(t, err) |
| 114 | + require.Equal(t, tt.want, got) |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestResolver_URLForMethod_InvalidScenarios(t *testing.T) { |
| 120 | + t.Parallel() |
| 121 | + |
| 122 | + invalidURL := amcommoncfg.SecretURL{ |
| 123 | + URL: &url.URL{}, |
| 124 | + } |
| 125 | + tests := []struct { |
| 126 | + name string |
| 127 | + apiURL *amcommoncfg.SecretURL |
| 128 | + apiURLFile string |
| 129 | + method string |
| 130 | + expectedErrorMsg string |
| 131 | + }{ |
| 132 | + { |
| 133 | + name: "invalid URL", |
| 134 | + apiURL: &invalidURL, |
| 135 | + apiURLFile: "", |
| 136 | + method: "", |
| 137 | + expectedErrorMsg: "slack api url is empty", |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "no apiURL nor apiURLFile", |
| 141 | + apiURL: nil, |
| 142 | + apiURLFile: "unknown", |
| 143 | + method: "", |
| 144 | + expectedErrorMsg: "open unknown: no such file or directory", |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + for _, tt := range tests { |
| 149 | + tt := tt |
| 150 | + t.Run(tt.name, func(t *testing.T) { |
| 151 | + t.Parallel() |
| 152 | + |
| 153 | + resolver := New(tt.apiURL, tt.apiURLFile) |
| 154 | + |
| 155 | + _, err := resolver.URLForMethod(tt.method) |
| 156 | + require.Error(t, err) |
| 157 | + require.Contains(t, err.Error(), tt.expectedErrorMsg) |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func getSecretURL(t *testing.T, raw string) *amcommoncfg.SecretURL { |
| 163 | + t.Helper() |
| 164 | + u, err := amcommoncfg.ParseURL(raw) |
| 165 | + require.NoError(t, err) |
| 166 | + s := amcommoncfg.SecretURL(*u) |
| 167 | + return &s |
| 168 | +} |
| 169 | + |
| 170 | +func createTempAPIURLFile(t *testing.T, pattern string, url string) *os.File { |
| 171 | + t.Helper() |
| 172 | + apiURLFileWithNewLines, err := os.CreateTemp(t.TempDir(), pattern) |
| 173 | + require.NoError(t, err) |
| 174 | + _, err = apiURLFileWithNewLines.WriteString(url) |
| 175 | + require.NoError(t, err) |
| 176 | + require.NoError(t, apiURLFileWithNewLines.Close()) |
| 177 | + return apiURLFileWithNewLines |
| 178 | +} |
0 commit comments