|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients" |
| 7 | + "github.com/opentelekomcloud/gophertelekomcloud/openstack/ces/v2/templates" |
| 8 | + th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" |
| 9 | +) |
| 10 | + |
| 11 | +func TestAlarmTemplatesCRUD(t *testing.T) { |
| 12 | + client, err := clients.NewCesV2Client() |
| 13 | + th.AssertNoErr(t, err) |
| 14 | + |
| 15 | + t.Log("Attempting to create alarm template") |
| 16 | + createOpts := templates.CreateOpts{ |
| 17 | + TemplateName: "test-template-acc", |
| 18 | + TemplateDescription: "Test alarm template for acceptance tests", |
| 19 | + Policies: []templates.Policy{ |
| 20 | + { |
| 21 | + Namespace: "SYS.ECS", |
| 22 | + DimensionName: "instance_id", |
| 23 | + MetricName: "cpu_util", |
| 24 | + Period: 300, |
| 25 | + Filter: "average", |
| 26 | + ComparisonOperator: ">", |
| 27 | + Value: 80, |
| 28 | + Unit: "%", |
| 29 | + Count: 3, |
| 30 | + AlarmLevel: 2, |
| 31 | + SuppressDuration: 300, |
| 32 | + }, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + templateId, err := templates.Create(client, createOpts) |
| 37 | + th.AssertNoErr(t, err) |
| 38 | + t.Logf("Created alarm template: %s", templateId) |
| 39 | + |
| 40 | + t.Cleanup(func() { |
| 41 | + t.Log("Attempting to delete alarm template") |
| 42 | + _, err := templates.Delete(client, templates.DeleteOpts{ |
| 43 | + TemplateIds: []string{templateId}, |
| 44 | + DeleteAssociateAlarm: false, |
| 45 | + }) |
| 46 | + th.AssertNoErr(t, err) |
| 47 | + }) |
| 48 | + |
| 49 | + t.Log("Attempting to get alarm template details") |
| 50 | + template, err := templates.Get(client, templateId) |
| 51 | + th.AssertNoErr(t, err) |
| 52 | + th.AssertEquals(t, template.TemplateName, "test-template-acc") |
| 53 | + th.AssertEquals(t, template.TemplateDescription, "Test alarm template for acceptance tests") |
| 54 | + th.AssertEquals(t, len(template.Policies), 1) |
| 55 | + th.AssertEquals(t, template.Policies[0].MetricName, "cpu_util") |
| 56 | + |
| 57 | + t.Log("Attempting to list alarm templates") |
| 58 | + listResp, err := templates.List(client, templates.ListOpts{ |
| 59 | + TemplateName: "test-template-acc", |
| 60 | + TemplateType: "custom", |
| 61 | + }) |
| 62 | + th.AssertNoErr(t, err) |
| 63 | + th.AssertEquals(t, listResp.Count >= 1, true) |
| 64 | + |
| 65 | + t.Log("Attempting to update alarm template") |
| 66 | + err = templates.Update(client, templateId, templates.UpdateOpts{ |
| 67 | + TemplateName: "test-template-acc-updated", |
| 68 | + TemplateDescription: "Updated description", |
| 69 | + Policies: []templates.Policy{ |
| 70 | + { |
| 71 | + Namespace: "SYS.ECS", |
| 72 | + DimensionName: "instance_id", |
| 73 | + MetricName: "cpu_util", |
| 74 | + Period: 300, |
| 75 | + Filter: "average", |
| 76 | + ComparisonOperator: ">=", |
| 77 | + Value: 90, |
| 78 | + Unit: "%", |
| 79 | + Count: 5, |
| 80 | + AlarmLevel: 1, |
| 81 | + SuppressDuration: 600, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }) |
| 85 | + th.AssertNoErr(t, err) |
| 86 | + |
| 87 | + t.Log("Attempting to verify template was updated") |
| 88 | + template, err = templates.Get(client, templateId) |
| 89 | + th.AssertNoErr(t, err) |
| 90 | + th.AssertEquals(t, template.TemplateName, "test-template-acc-updated") |
| 91 | + th.AssertEquals(t, template.TemplateDescription, "Updated description") |
| 92 | + th.AssertEquals(t, template.Policies[0].ComparisonOperator, ">=") |
| 93 | + th.AssertEquals(t, template.Policies[0].Value, float64(90)) |
| 94 | + th.AssertEquals(t, template.Policies[0].AlarmLevel, 1) |
| 95 | +} |
| 96 | + |
| 97 | +func TestAlarmTemplatesList(t *testing.T) { |
| 98 | + client, err := clients.NewCesV2Client() |
| 99 | + th.AssertNoErr(t, err) |
| 100 | + |
| 101 | + t.Log("Attempting to list system alarm templates") |
| 102 | + listResp, err := templates.List(client, templates.ListOpts{ |
| 103 | + TemplateType: "system", |
| 104 | + Limit: 10, |
| 105 | + }) |
| 106 | + th.AssertNoErr(t, err) |
| 107 | + |
| 108 | + t.Logf("Found %d system alarm templates", listResp.Count) |
| 109 | + for _, tmpl := range listResp.AlarmTemplates { |
| 110 | + t.Logf("Template ID: %s, Name: %s, Type: %s", |
| 111 | + tmpl.TemplateId, tmpl.TemplateName, tmpl.TemplateType) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestAlarmTemplateAssociationAlarms(t *testing.T) { |
| 116 | + client, err := clients.NewCesV2Client() |
| 117 | + th.AssertNoErr(t, err) |
| 118 | + |
| 119 | + t.Log("Attempting to create alarm template") |
| 120 | + createOpts := templates.CreateOpts{ |
| 121 | + TemplateName: "test-template-assoc-acc", |
| 122 | + TemplateDescription: "Test template for association alarms", |
| 123 | + Policies: []templates.Policy{ |
| 124 | + { |
| 125 | + Namespace: "SYS.ECS", |
| 126 | + DimensionName: "instance_id", |
| 127 | + MetricName: "cpu_util", |
| 128 | + Period: 300, |
| 129 | + Filter: "average", |
| 130 | + ComparisonOperator: ">", |
| 131 | + Value: 80, |
| 132 | + Unit: "%", |
| 133 | + Count: 3, |
| 134 | + AlarmLevel: 2, |
| 135 | + SuppressDuration: 300, |
| 136 | + }, |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + templateId, err := templates.Create(client, createOpts) |
| 141 | + th.AssertNoErr(t, err) |
| 142 | + |
| 143 | + t.Cleanup(func() { |
| 144 | + t.Log("Attempting to delete alarm template") |
| 145 | + _, err := templates.Delete(client, templates.DeleteOpts{ |
| 146 | + TemplateIds: []string{templateId}, |
| 147 | + DeleteAssociateAlarm: true, |
| 148 | + }) |
| 149 | + th.AssertNoErr(t, err) |
| 150 | + }) |
| 151 | + |
| 152 | + t.Log("Attempting to list association alarms") |
| 153 | + listResp, err := templates.ListAssociationAlarms(client, templateId, templates.ListAssociationAlarmsOpts{ |
| 154 | + Limit: 10, |
| 155 | + }) |
| 156 | + th.AssertNoErr(t, err) |
| 157 | + |
| 158 | + t.Logf("Found %d associated alarm rules", listResp.Count) |
| 159 | + for _, alarm := range listResp.Alarms { |
| 160 | + t.Logf("Alarm ID: %s, Name: %s", alarm.AlarmId, alarm.Name) |
| 161 | + } |
| 162 | +} |
0 commit comments