/
verify_alert_links_to_SOPs.go
176 lines (143 loc) · 3.91 KB
/
verify_alert_links_to_SOPs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package common
import (
"encoding/json"
prometheusv1 "github.com/prometheus/client_golang/api/prometheus/v1"
"io"
"net/http"
"os"
"strings"
"sync"
)
var (
gitlabToken = os.Getenv("GITLAB_TOKEN")
failedSOPurls = make(chan string)
wg sync.WaitGroup
)
func TestSOPUrls(t TestingTB, ctx *TestingContext) {
if gitlabToken == "" {
t.Skip("Gitlab token not provided, use GITLAB_TOKEN environment variable to specify it")
}
var sopUrls []string
// test connection to Github API, with single url
testUrl := "https://gitlab.cee.redhat.com/rhcloudservices/integreatly-help/-/blob/master/sops/rhoam/alerts/AddonManagedApiServiceParameters.asciidoc"
validateGitlabToken(t, testUrl)
output, err := execToPod("wget -qO - localhost:9090/api/v1/rules",
ObservabilityPrometheusPodName,
ObservabilityProductNamespace,
"prometheus", ctx)
if err != nil {
t.Fatal("failed to exec to pod:", err)
}
var ApiOutput prometheusAPIResponse
err = json.Unmarshal([]byte(output), &ApiOutput)
if err != nil {
t.Fatal("failed to unmarshal json", err)
}
var rulesResult prometheusv1.RulesResult
err = json.Unmarshal(ApiOutput.Data, &rulesResult)
if err != nil {
t.Fatal("failed to unmarshal json", err)
}
for _, group := range rulesResult.Groups {
for _, rule := range group.Rules {
switch v := rule.(type) {
case prometheusv1.RecordingRule:
case prometheusv1.AlertingRule:
for annotation, sopUrl := range v.Annotations {
if annotation == "sop_url" && sopUrl != "" {
sopUrls = append(sopUrls, string(sopUrl))
}
}
default:
t.Log("Unknown rule type %s", v)
}
}
}
sopUrls = unique(sopUrls)
validateSOPurls(t, sopUrls)
}
// modify raw link to Github API verison
func convertToGitlabApiUrl(sopUrl string) (apiSOPUrl string) {
r := strings.NewReplacer(
"com/rhcloudservices/integreatly-help/-/blob/master/sops/rhoam/alerts/", "com/api/v4/projects/64861/repository/files/sops%2Frhoam%2Falerts%2F",
".asciidoc", "%2Easciidoc?ref=master",
)
apiSOPUrl = r.Replace(sopUrl)
return
}
// remove duplicate links
func unique(s []string) []string {
inResult := make(map[string]bool)
var result []string
for _, str := range s {
if _, ok := inResult[str]; !ok {
inResult[str] = true
result = append(result, str)
}
}
return result
}
func validateGitlabToken(t TestingTB, testUrl string) {
apiUrl := convertToGitlabApiUrl(testUrl)
client := &http.Client{}
req, err := http.NewRequest("GET", apiUrl, nil)
if err != nil {
t.Fatal("%s", err)
}
req.Header.Add("Accept", `application/json`)
req.Header.Add("PRIVATE-TOKEN", gitlabToken)
testResp, err := client.Do(req)
if err != nil {
t.Skip(err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
t.Log("failed to close body")
}
}(testResp.Body)
if testResp.StatusCode != 200 {
t.Fatal("Response status: ", apiUrl, testResp.Status, "Given token does not allow access to SOP URLs")
}
}
// validate concurrently that links are accessible
func validateSOPurls(t TestingTB, sopUrls []string) {
for _, url := range sopUrls {
wg.Add(1)
go getSOPAlertLinkStatus(t, url, failedSOPurls)
}
go func() {
wg.Wait()
close(failedSOPurls)
}()
if len(failedSOPurls) != 0 {
for failedSOPUrl := range failedSOPurls {
t.Log("failed to connect to url: ", failedSOPUrl)
}
t.Fatal("test failed due to the invalid url")
}
}
func getSOPAlertLinkStatus(t TestingTB, url string, failedSOPUrls chan string) {
defer wg.Done()
apiUrl := convertToGitlabApiUrl(url)
client := &http.Client{}
req, err := http.NewRequest("GET", apiUrl, nil)
if err != nil {
t.Log("%s", err)
}
req.Header.Add("Accept", `application/json`)
req.Header.Add("PRIVATE-TOKEN", gitlabToken)
resp, err := client.Do(req)
if err != nil {
t.Log(err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
t.Log("failed to close body")
}
}(resp.Body)
if resp.StatusCode != 200 {
failedSOPUrls <- url
}
}