github.com/newrelic/newrelic-client-go@v1.1.0/pkg/alerts/nrql_conditions_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package alerts 5 6 import ( 7 "net/http" 8 "testing" 9 10 "github.com/newrelic/newrelic-client-go/pkg/common" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 var ( 16 testListNrqlConditionsResponseJSON = `{ 17 "nrql_conditions": [ 18 { 19 "type": "static", 20 "id": 12345, 21 "name": "NRQL Test Alert", 22 "enabled": true, 23 "value_function": "single_value", 24 "violation_time_limit_seconds": 3600, 25 "terms": [ 26 { 27 "duration": "5", 28 "operator": "above", 29 "priority": "critical", 30 "threshold": "1", 31 "time_function": "all" 32 } 33 ], 34 "nrql": { 35 "query": "SELECT count(*) FROM Transactions", 36 "since_value": "3" 37 }, 38 "entity_guid": "NDAwQzA0rEFST1BTfENPTkRJVElPTnw3MzUzNjL" 39 } 40 ] 41 }` 42 43 testNrqlConditionJSON = `{ 44 "nrql_condition": { 45 "type": "static", 46 "id": 12345, 47 "name": "NRQL Test Alert", 48 "enabled": true, 49 "value_function": "single_value", 50 "violation_time_limit_seconds": 3600, 51 "terms": [ 52 { 53 "duration": "5", 54 "operator": "above", 55 "priority": "critical", 56 "threshold": "1", 57 "time_function": "all" 58 } 59 ], 60 "nrql": { 61 "query": "SELECT count(*) FROM Transactions", 62 "since_value": "3" 63 }, 64 "entity_guid": "NDAwQzA0rEFST1BTfENPTkRJVElPTnw3MzUzNjL" 65 } 66 }` 67 68 testNrqlConditionCreateJSON = `{ 69 "nrql_condition": { 70 "type": "static", 71 "id": 12345, 72 "name": "NRQL Test Alert", 73 "enabled": true, 74 "value_function": "single_value", 75 "violation_time_limit_seconds": 3600, 76 "terms": [ 77 { 78 "duration": "5", 79 "operator": "above", 80 "priority": "critical", 81 "threshold": "1", 82 "time_function": "all" 83 } 84 ], 85 "nrql": { 86 "query": "SELECT count(*) FROM Transactions", 87 "since_value": "3" 88 } 89 } 90 }` 91 92 testNrqlConditionUpdatedJSON = `{ 93 "nrql_condition": { 94 "type": "static", 95 "id": 12345, 96 "name": "NRQL Test Alert Updated", 97 "enabled": false, 98 "value_function": "single_value", 99 "violation_time_limit_seconds": 3600, 100 "terms": [ 101 { 102 "duration": "5", 103 "operator": "below", 104 "priority": "critical", 105 "threshold": "1", 106 "time_function": "all" 107 } 108 ], 109 "nrql": { 110 "query": "SELECT count(*) FROM Transactions", 111 "since_value": "3" 112 }, 113 "runbook_url": "https://www.example.com/docs" 114 } 115 }` 116 117 testNrqlConditionEntityGUID = common.EntityGUID("NDAwQzA0rEFST1BTfENPTkRJVElPTnw3MzUzNjL") 118 ) 119 120 func TestListNrqlConditions(t *testing.T) { 121 t.Parallel() 122 alerts := newMockResponse(t, testListNrqlConditionsResponseJSON, http.StatusOK) 123 124 expected := []*NrqlCondition{ 125 { 126 Nrql: NrqlQuery{ 127 Query: "SELECT count(*) FROM Transactions", 128 SinceValue: "3", 129 }, 130 Terms: []ConditionTerm{ 131 { 132 Duration: 5, 133 Operator: "above", 134 Priority: "critical", 135 Threshold: 1, 136 TimeFunction: "all", 137 }, 138 }, 139 Type: "static", 140 Name: "NRQL Test Alert", 141 RunbookURL: "", 142 ValueFunction: "single_value", 143 ID: 12345, 144 ViolationCloseTimer: 3600, 145 Enabled: true, 146 EntityGUID: &testNrqlConditionEntityGUID, 147 }, 148 } 149 150 actual, err := alerts.ListNrqlConditions(123) 151 152 assert.NoError(t, err) 153 assert.NotNil(t, actual) 154 assert.Equal(t, expected, actual) 155 } 156 157 func TestGetNrqlCondition(t *testing.T) { 158 t.Parallel() 159 alerts := newMockResponse(t, testListNrqlConditionsResponseJSON, http.StatusOK) 160 161 expected := &NrqlCondition{ 162 Nrql: NrqlQuery{ 163 Query: "SELECT count(*) FROM Transactions", 164 SinceValue: "3", 165 }, 166 Terms: []ConditionTerm{ 167 { 168 Duration: 5, 169 Operator: "above", 170 Priority: "critical", 171 Threshold: 1, 172 TimeFunction: "all", 173 }, 174 }, 175 Type: "static", 176 Name: "NRQL Test Alert", 177 RunbookURL: "", 178 ValueFunction: "single_value", 179 ID: 12345, 180 ViolationCloseTimer: 3600, 181 Enabled: true, 182 EntityGUID: &testNrqlConditionEntityGUID, 183 } 184 185 actual, err := alerts.GetNrqlCondition(123, 12345) 186 187 assert.NoError(t, err) 188 assert.NotNil(t, actual) 189 assert.Equal(t, expected, actual) 190 } 191 192 func TestCreateNrqlCondition(t *testing.T) { 193 t.Parallel() 194 alerts := newMockResponse(t, testNrqlConditionCreateJSON, http.StatusCreated) 195 policyID := 333 196 197 condition := NrqlCondition{ 198 Nrql: NrqlQuery{ 199 Query: "SELECT count(*) FROM Transactions", 200 SinceValue: "3", 201 }, 202 Terms: []ConditionTerm{ 203 { 204 Duration: 5, 205 Operator: "above", 206 Priority: "critical", 207 Threshold: 1, 208 TimeFunction: "all", 209 }, 210 }, 211 Type: "static", 212 Name: "NRQL Test Alert", 213 RunbookURL: "", 214 ValueFunction: "single_value", 215 ID: 12345, 216 ViolationCloseTimer: 3600, 217 Enabled: true, 218 } 219 220 expected := &condition 221 222 actual, err := alerts.CreateNrqlCondition(policyID, condition) 223 224 assert.NoError(t, err) 225 assert.NotNil(t, actual) 226 assert.Equal(t, expected, actual) 227 } 228 229 func TestUpdateNrqlCondition(t *testing.T) { 230 t.Parallel() 231 alerts := newMockResponse(t, testNrqlConditionUpdatedJSON, http.StatusCreated) 232 233 condition := NrqlCondition{ 234 Nrql: NrqlQuery{ 235 Query: "SELECT count(*) FROM Transactions", 236 SinceValue: "3", 237 }, 238 Terms: []ConditionTerm{ 239 { 240 Duration: 5, 241 Operator: "above", 242 Priority: "critical", 243 Threshold: 1, 244 TimeFunction: "all", 245 }, 246 }, 247 Type: "static", 248 Name: "NRQL Test Alert", 249 RunbookURL: "", 250 ValueFunction: "single_value", 251 ID: 12345, 252 ViolationCloseTimer: 3600, 253 Enabled: true, 254 EntityGUID: &testNrqlConditionEntityGUID, 255 } 256 257 expected := &NrqlCondition{ 258 Nrql: NrqlQuery{ 259 Query: "SELECT count(*) FROM Transactions", 260 SinceValue: "3", 261 }, 262 Terms: []ConditionTerm{ 263 { 264 Duration: 5, 265 Operator: "below", 266 Priority: "critical", 267 Threshold: 1, 268 TimeFunction: "all", 269 }, 270 }, 271 Type: "static", 272 Name: "NRQL Test Alert Updated", 273 RunbookURL: "https://www.example.com/docs", 274 ValueFunction: "single_value", 275 ID: 12345, 276 ViolationCloseTimer: 3600, 277 Enabled: false, 278 } 279 280 actual, err := alerts.UpdateNrqlCondition(condition) 281 282 assert.NoError(t, err) 283 assert.NotNil(t, actual) 284 assert.Equal(t, expected, actual) 285 } 286 287 func TestDeleteNrqlCondition(t *testing.T) { 288 t.Parallel() 289 alerts := newMockResponse(t, testNrqlConditionJSON, http.StatusOK) 290 expected := &NrqlCondition{ 291 Nrql: NrqlQuery{ 292 Query: "SELECT count(*) FROM Transactions", 293 SinceValue: "3", 294 }, 295 Terms: []ConditionTerm{ 296 { 297 Duration: 5, 298 Operator: "above", 299 Priority: "critical", 300 Threshold: 1, 301 TimeFunction: "all", 302 }, 303 }, 304 Type: "static", 305 Name: "NRQL Test Alert", 306 RunbookURL: "", 307 ValueFunction: "single_value", 308 ID: 12345, 309 ViolationCloseTimer: 3600, 310 Enabled: true, 311 EntityGUID: &testNrqlConditionEntityGUID, 312 } 313 314 actual, err := alerts.DeleteNrqlCondition(12345) 315 316 assert.NoError(t, err) 317 assert.NotNil(t, actual) 318 assert.Equal(t, expected, actual) 319 }