github.com/stakater/IngressMonitorController@v1.0.103/pkg/monitors/appinsights/appinsights-monitor_test.go (about) 1 // Package AppInsightsMonitor adds Azure AppInsights webtest support in IngressMonitorController 2 package appinsights 3 4 import ( 5 "context" 6 "fmt" 7 "reflect" 8 "testing" 9 10 "github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights" 11 insightsAlert "github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2018-03-01/insights" 12 "github.com/stakater/IngressMonitorController/pkg/models" 13 ) 14 15 func TestAppinsightsMonitorService_createWebTest(t *testing.T) { 16 17 location := "westeurope" 18 webtestName := "foo" 19 name := "foo-appinsights" 20 resourceGroup := "demoRG" 21 geoLocation := "us-tx-sn1-azr" 22 frequency := int32(300) 23 isEnabled := true 24 isRetryEnabled := true 25 tag := make(map[string]*string) 26 tagValue := "Resource" 27 tag["hidden-link:/subscriptions/99cb99da-9cf9-9999-9999-9eacc5d36a65/resourceGroups/demoRG/providers/microsoft.insights/components/foo-appinsights"] = &tagValue 28 webTestConfig := "<WebTest xmlns=\"http://microsoft.com/schemas/VisualStudio/TeamTest/2010\" Name=\"\" Enabled=\"true\" Timeout=\"120\" Description=\"foo webtest is created by Ingress Monitor controller\" StopOnError=\"true\"><Items><Request Method=\"GET\" Version=\"1.1\" Url=\"https://microsoft.com\" ThinkTime=\"\" Timeout=\"0\" Encoding=\"utf-8\" ExpectedHttpStatusCode=\"200\" ExpectedResponseUrl=\"\" IgnoreHttpStatusCode=\"false\"></Request></Items></WebTest>" 29 30 type fields struct { 31 insightsClient insights.WebTestsClient 32 alertrulesClient insightsAlert.AlertRulesClient 33 name string 34 location string 35 resourceGroup string 36 geoLocation []interface{} 37 emailAction []string 38 webhookAction string 39 emailToOwners bool 40 subscriptionID string 41 ctx context.Context 42 } 43 type args struct { 44 monitor models.Monitor 45 } 46 tests := []struct { 47 name string 48 fields fields 49 args args 50 want insights.WebTest 51 }{ 52 { 53 name: webtestName, 54 fields: fields{ 55 name: name, 56 location: location, 57 emailToOwners: true, 58 subscriptionID: "99cb99da-9cf9-9999-9999-9eacc5d36a65", 59 resourceGroup: resourceGroup, 60 geoLocation: []interface{}{geoLocation}, 61 emailAction: []string{"mail@cizer.dev"}, 62 webhookAction: "https://webhook.io", 63 ctx: context.Background(), 64 }, 65 args: args{ 66 monitor: models.Monitor{ 67 URL: "https://microsoft.com", 68 Name: "foo", 69 Annotations: map[string]string{ 70 "AppInsightsStatusCodeAnnotation": "200", 71 "AppInsightsFrequency": string(frequency), 72 "AppInsightsRetryEnabledAnnotation": "true", 73 }, 74 ID: "", 75 }, 76 }, 77 want: insights.WebTest{ 78 Name: &webtestName, 79 Location: &location, 80 Kind: insights.Ping, // forcing type of webtest to 'ping',this could be as replace with provider configuration 81 WebTestProperties: &insights.WebTestProperties{ 82 SyntheticMonitorID: &webtestName, 83 WebTestName: &webtestName, 84 WebTestKind: insights.Ping, 85 RetryEnabled: &isRetryEnabled, 86 Enabled: &isEnabled, 87 Frequency: &frequency, 88 Locations: &[]insights.WebTestGeolocation{ 89 {Location: &geoLocation}, 90 }, 91 Configuration: &insights.WebTestPropertiesConfiguration{ 92 WebTest: &webTestConfig, 93 }, 94 }, 95 Tags: tag, 96 }, 97 }, 98 } 99 for _, tt := range tests { 100 t.Run(tt.name, func(t *testing.T) { 101 aiService := &AppinsightsMonitorService{ 102 insightsClient: tt.fields.insightsClient, 103 alertrulesClient: tt.fields.alertrulesClient, 104 name: tt.fields.name, 105 location: tt.fields.location, 106 resourceGroup: tt.fields.resourceGroup, 107 geoLocation: tt.fields.geoLocation, 108 emailAction: tt.fields.emailAction, 109 webhookAction: tt.fields.webhookAction, 110 emailToOwners: tt.fields.emailToOwners, 111 subscriptionID: tt.fields.subscriptionID, 112 ctx: tt.fields.ctx, 113 } 114 if got := aiService.createWebTest(tt.args.monitor); !reflect.DeepEqual(got, tt.want) { 115 t.Errorf("AppinsightsMonitorService.createWebTest() = %v, want %v", got, tt.want) 116 } 117 }) 118 } 119 } 120 121 func TestAppinsightsMonitorService_createAlertRuleResource(t *testing.T) { 122 123 location := "westeurope" 124 name := "foo-appinsights" 125 failedLocationCount := int32(1) 126 period := "PT5M" 127 webtestName := "foo" 128 alertName := fmt.Sprintf("%s-alert", webtestName) 129 resourceGroup := "demoRG" 130 geoLocation := []interface{}{"us-tx-sn1-azr"} 131 frequency := int32(300) 132 isEnabled := true 133 subID := "99cb99da-9cf9-9999-9999-9eacc5d36a65" 134 emailAction := []string{"mail@cizer.dev"} 135 webhookAction := "https://webhook.io" 136 emailToOwners := false 137 description := fmt.Sprintf("%s-alert alert is created using Ingress Monitor Controller", webtestName) 138 resourceUri := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/microsoft.insights/webtests/%s", subID, resourceGroup, webtestName) 139 tag := make(map[string]*string) 140 tagValue := "Resource" 141 tag["hidden-link:/subscriptions/99cb99da-9cf9-9999-9999-9eacc5d36a65/resourceGroups/demoRG/providers/microsoft.insights/components/foo-appinsights"] = &tagValue 142 tag["hidden-link:/subscriptions/99cb99da-9cf9-9999-9999-9eacc5d36a65/resourceGroups/demoRG/providers/microsoft.insights/webtests/foo"] = &tagValue 143 144 actions := []insightsAlert.BasicRuleAction{ 145 insightsAlert.RuleEmailAction{ 146 SendToServiceOwners: &emailToOwners, 147 CustomEmails: &(emailAction), 148 }, 149 insightsAlert.RuleWebhookAction{ 150 ServiceURI: &webhookAction, 151 }, 152 } 153 154 type fields struct { 155 insightsClient insights.WebTestsClient 156 alertrulesClient insightsAlert.AlertRulesClient 157 name string 158 location string 159 resourceGroup string 160 geoLocation []interface{} 161 emailAction []string 162 webhookAction string 163 emailToOwners bool 164 subscriptionID string 165 ctx context.Context 166 } 167 type args struct { 168 monitor models.Monitor 169 } 170 tests := []struct { 171 name string 172 fields fields 173 args args 174 want insightsAlert.AlertRuleResource 175 }{ 176 { 177 name: webtestName, 178 fields: fields{ 179 name: name, 180 location: location, 181 emailToOwners: emailToOwners, 182 subscriptionID: subID, 183 resourceGroup: resourceGroup, 184 geoLocation: geoLocation, 185 emailAction: emailAction, 186 webhookAction: webhookAction, 187 ctx: context.Background(), 188 }, 189 args: args{ 190 monitor: models.Monitor{ 191 URL: "https://microsoft.com", 192 Name: "foo", 193 Annotations: map[string]string{ 194 "AppInsightsStatusCodeAnnotation": "200", 195 "AppInsightsFrequency": string(frequency), 196 "AppInsightsRetryEnabledAnnotation": "true", 197 }, 198 ID: "", 199 }, 200 }, 201 want: insightsAlert.AlertRuleResource{ 202 Name: &webtestName, 203 Location: &location, 204 ID: &resourceUri, 205 AlertRule: &insightsAlert.AlertRule{ 206 Name: &alertName, 207 IsEnabled: &isEnabled, 208 Description: &description, 209 Condition: &insightsAlert.LocationThresholdRuleCondition{ 210 DataSource: insightsAlert.RuleMetricDataSource{ 211 ResourceURI: &resourceUri, 212 OdataType: insightsAlert.OdataTypeMicrosoftAzureManagementInsightsModelsRuleMetricDataSource, 213 MetricName: &alertName, 214 }, 215 FailedLocationCount: &failedLocationCount, 216 WindowSize: &period, 217 OdataType: insightsAlert.OdataTypeMicrosoftAzureManagementInsightsModelsLocationThresholdRuleCondition, 218 }, 219 Actions: &actions, 220 }, 221 Tags: tag, 222 }, 223 }, 224 } 225 for _, tt := range tests { 226 t.Run(tt.name, func(t *testing.T) { 227 aiService := &AppinsightsMonitorService{ 228 insightsClient: tt.fields.insightsClient, 229 alertrulesClient: tt.fields.alertrulesClient, 230 name: tt.fields.name, 231 location: tt.fields.location, 232 resourceGroup: tt.fields.resourceGroup, 233 geoLocation: tt.fields.geoLocation, 234 emailAction: tt.fields.emailAction, 235 webhookAction: tt.fields.webhookAction, 236 emailToOwners: tt.fields.emailToOwners, 237 subscriptionID: tt.fields.subscriptionID, 238 ctx: tt.fields.ctx, 239 } 240 if got := aiService.createAlertRuleResource(tt.args.monitor); !reflect.DeepEqual(got, tt.want) { 241 t.Errorf("%+v", got) 242 t.Errorf("%+v", tt.want) 243 t.Errorf("AppinsightsMonitorService.createAlertRuleResource() = %v, want %v", got, tt.want) 244 } 245 }) 246 } 247 }