Openstatus www.openstatus.dev

bug dns checker assertion (#1647)

authored by

Thibault Le Ouay and committed by
GitHub
26210b89 b4fbb971

+38 -31
+7 -11
apps/checker/handlers/checker_test.go
··· 109 109 }) 110 110 } 111 111 112 - 113 112 func TestEvaluateAssertions_raw(t *testing.T) { 114 113 // Helper to marshal assertion 115 114 marshal := func(a any) json.RawMessage { ··· 133 132 target := struct { 134 133 request.Assertion 135 134 Comparator request.StringComparator `json:"compare"` 136 - Key string `json:"key"` 137 - Target string `json:"target"` 135 + Key string `json:"key"` 136 + Target string `json:"target"` 138 137 }{ 139 138 assertion, 140 139 request.StringContains, ··· 156 155 target := struct { 157 156 request.Assertion 158 157 Comparator request.StringComparator `json:"compare"` 159 - Key string `json:"key"` 160 - Target string `json:"target"` 158 + Key string `json:"key"` 159 + Target string `json:"target"` 161 160 }{ 162 161 assertion, 163 162 request.StringContains, ··· 180 179 target := struct { 181 180 request.Assertion 182 181 Comparator request.StringComparator `json:"compare"` 183 - Target string `json:"target"` 182 + Target string `json:"target"` 184 183 }{ 185 184 assertion, 186 185 request.StringEquals, ··· 190 189 raw := []json.RawMessage{rawMsg} 191 190 data := handlers.PingData{Body: "ok"} 192 191 res := checker.Response{Status: 200} 193 - 194 192 195 193 ok, err := handlers.EvaluateHTTPAssertions(raw, data, res) 196 194 assert.False(t, ok) ··· 203 201 target := struct { 204 202 request.Assertion 205 203 Comparator request.StringComparator `json:"compare"` 206 - Target string `json:"target"` 204 + Target string `json:"target"` 207 205 }{ 208 206 assertion, 209 207 request.StringEquals, ··· 214 212 data := handlers.PingData{Body: "success"} 215 213 res := checker.Response{Status: 200} 216 214 217 - 218 215 ok, err := handlers.EvaluateHTTPAssertions(raw, data, res) 219 216 assert.True(t, ok) 220 217 assert.NoError(t, err) ··· 225 222 target := struct { 226 223 request.Assertion 227 224 Comparator request.NumberComparator `json:"compare"` 228 - Target int64 `json:"target"` 225 + Target int64 `json:"target"` 229 226 }{ 230 227 assertion, 231 228 request.NumberEquals, ··· 235 232 raw := []json.RawMessage{rawMsg} 236 233 data := handlers.PingData{} 237 234 res := checker.Response{Status: 200} 238 - 239 235 240 236 ok, err := handlers.EvaluateHTTPAssertions(raw, data, res) 241 237 assert.True(t, ok)
+14 -13
apps/checker/handlers/dns.go
··· 164 164 165 165 // Status update logic 166 166 switch { 167 - case !isSuccessful && req.Status != "error": 167 + case !isSuccessful: 168 168 log.Ctx(ctx).Debug().Msg("DNS check failed assertions") 169 - checker.UpdateStatus(ctx, checker.UpdateData{ 170 - MonitorId: req.MonitorID, 171 - Status: "error", 172 - Region: h.Region, 173 - Message: err.Error(), 174 - CronTimestamp: req.CronTimestamp, 175 - Latency: latency, 176 - }) 177 169 data.RequestStatus = "error" 178 170 data.Error = 1 179 171 data.ErrorMessage = err.Error() 172 + if req.Status != "error" { 173 + checker.UpdateStatus(ctx, checker.UpdateData{ 174 + MonitorId: req.MonitorID, 175 + Status: "error", 176 + Region: h.Region, 177 + Message: err.Error(), 178 + CronTimestamp: req.CronTimestamp, 179 + Latency: latency, 180 + }) 181 + } 180 182 case isSuccessful && req.DegradedAfter > 0 && latency > req.DegradedAfter && req.Status != "degraded": 181 183 checker.UpdateStatus(ctx, checker.UpdateData{ 182 184 MonitorId: req.MonitorID, ··· 233 235 return 234 236 } 235 237 236 - 237 238 retry := defaultRetry 238 239 if req.Retry != 0 { 239 240 retry = int(req.Retry) ··· 245 246 return 246 247 } 247 248 248 - workspaceId , _ := strconv.Atoi(req.WorkspaceID) 249 + workspaceId, _ := strconv.Atoi(req.WorkspaceID) 249 250 250 251 statusMap := map[string]string{ 251 252 "active": "success", ··· 366 367 return false, fmt.Errorf("unable to parse assertion: %w", err) 367 368 } 368 369 var isSuccessfull bool 369 - switch assert.Record { 370 + switch assert.Key { 370 371 case request.RecordA: 371 372 isSuccessfull = assert.RecordEvaluate(response.A) 372 373 case request.RecordAAAA: ··· 380 381 case request.RecordTXT: 381 382 isSuccessfull = assert.RecordEvaluate(response.TXT) 382 383 default: 383 - return false, fmt.Errorf("unknown record type in assertion: %s", assert.Record) 384 + return false, fmt.Errorf("unknown record type in assertion: %s", assert.Key) 384 385 } 385 386 if !isSuccessfull { 386 387 return false, nil
+16 -6
apps/checker/handlers/dns_test.go
··· 21 21 TXT []string 22 22 } 23 23 24 - 25 - 26 24 func TestFormatDNSResult(t *testing.T) { 27 25 tests := []struct { 28 26 name string ··· 105 103 } 106 104 } 107 105 108 - 109 106 func TestEvaluateDNSAssertions(t *testing.T) { 110 107 type args struct { 111 108 rawAssertions []json.RawMessage ··· 121 118 name: "A record matches", 122 119 args: args{ 123 120 rawAssertions: []json.RawMessage{ 124 - json.RawMessage(`{"record":"A","compare":"eq","target":"1.2.3.4"}`), 121 + json.RawMessage(`{"key":"A","compare":"eq","target":"1.2.3.4"}`), 125 122 }, 126 123 response: &checker.DnsResponse{ 127 124 A: []string{"1.2.3.4", "5.6.7.8"}, ··· 134 131 name: "CNAME does not match", 135 132 args: args{ 136 133 rawAssertions: []json.RawMessage{ 137 - json.RawMessage(`{"record":"CNAME","compare":"eq","target":"not-example.com"}`), 134 + json.RawMessage(`{"key":"CNAME","compare":"eq","target":"not-example.com"}`), 138 135 }, 139 136 response: &checker.DnsResponse{ 140 137 CNAME: "example.com", ··· 144 141 wantErr: false, 145 142 }, 146 143 { 144 + name: "CNAME Contains", 145 + args: args{ 146 + rawAssertions: []json.RawMessage{ 147 + json.RawMessage(`{"version":"v1","type":"dnsRecord","key":"CNAME","compare":"contains","target":"openstatus.dev"}`), 148 + }, 149 + response: &checker.DnsResponse{ 150 + CNAME: "openstatus.dev.", 151 + }, 152 + }, 153 + wantSuccess: true, 154 + wantErr: false, 155 + }, 156 + { 147 157 name: "Unknown record type", 148 158 args: args{ 149 159 rawAssertions: []json.RawMessage{ 150 - json.RawMessage(`{"record":"FOO","compare":"eq","target":"bar"}`), 160 + json.RawMessage(`{"key":"FOO","compare":"eq","target":"bar"}`), 151 161 }, 152 162 response: &checker.DnsResponse{}, 153 163 },
+1 -1
apps/checker/pkg/assertions/assertions.go
··· 36 36 type RecordTarget struct { 37 37 Comparator request.RecordComparator `json:"compare"` 38 38 Target string `json:"target"` 39 - Record request.Record `json:"record"` 39 + Key request.Record `json:"key"` 40 40 } 41 41 42 42 func (target StringTargetType) StringEvaluate(s string) bool {