| 1 | package alert_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/CenturyLinkCloud/clc-sdk/alert" |
| 10 | "github.com/CenturyLinkCloud/clc-sdk/api" |
| 11 | "github.com/stretchr/testify/assert" |
| 12 | "github.com/stretchr/testify/mock" |
| 13 | ) |
| 14 | |
| 15 | func TestGetAlertPolicy(t *testing.T) { |
| 16 | assert := assert.New(t) |
| 17 | |
| 18 | client := NewMockClient() |
| 19 | client.On("Get", "http://localhost/v2/alertPolicies/test/12345", mock.Anything).Return(nil) |
| 20 | service := alert.New(client) |
| 21 | |
| 22 | id := "12345" |
| 23 | resp, err := service.Get(id) |
| 24 | |
| 25 | assert.Nil(err) |
| 26 | assert.Equal(id, resp.ID) |
| 27 | client.AssertExpectations(t) |
| 28 | } |
| 29 | |
| 30 | func TestGetAllAlertPolicies(t *testing.T) { |
| 31 | assert := assert.New(t) |
| 32 | |
| 33 | client := NewMockClient() |
| 34 | client.On("Get", "http://localhost/v2/alertPolicies/test", mock.Anything).Return(nil) |
| 35 | service := alert.New(client) |
| 36 | |
| 37 | resp, err := service.GetAll() |
| 38 | |
| 39 | assert.Nil(err) |
| 40 | assert.Equal(2, len(resp.Items)) |
| 41 | } |
| 42 | |
| 43 | func TestCreateAlertPolicy(t *testing.T) { |
| 44 | assert := assert.New(t) |
| 45 | |
| 46 | client := NewMockClient() |
| 47 | client.On("Post", "http://localhost/v2/alertPolicies/test", mock.Anything, mock.Anything).Return(nil) |
| 48 | service := alert.New(client) |
| 49 | |
| 50 | a := alert.Alert{ |
| 51 | Name: "new alert", |
| 52 | Actions: []alert.Action{ |
| 53 | alert.Action{ |
| 54 | Action: "email", |
| 55 | Setting: alert.Setting{ |
| 56 | Recipients: []string{"[email protected]"}, |
| 57 | }, |
| 58 | }, |
| 59 | }, |
| 60 | Triggers: []alert.Trigger{ |
| 61 | alert.Trigger{ |
| 62 | Metric: "disk", |
| 63 | Duration: "00:05:00", |
| 64 | Threshold: 80.0, |
| 65 | }, |
| 66 | }, |
| 67 | } |
| 68 | resp, err := service.Create(a) |
| 69 | |
| 70 | assert.Nil(err) |
| 71 | assert.Equal(a.Name, resp.Name) |
| 72 | assert.Equal(a.Actions, resp.Actions) |
| 73 | assert.Equal(a.Triggers, resp.Triggers) |
| 74 | client.AssertExpectations(t) |
| 75 | } |
| 76 | |
| 77 | func TestUpdateAlertPolicy(t *testing.T) { |
| 78 | assert := assert.New(t) |
| 79 | |
| 80 | client := NewMockClient() |
| 81 | client.On("Put", "http://localhost/v2/alertPolicies/test/12345", mock.Anything, mock.Anything).Return(nil) |
| 82 | service := alert.New(client) |
| 83 | |
| 84 | a := alert.Alert{ |
| 85 | Name: "update alert", |
| 86 | } |
| 87 | id := "12345" |
| 88 | resp, err := service.Update(id, a) |
| 89 | |
| 90 | assert.Nil(err) |
| 91 | assert.Equal(a.Name, resp.Name) |
| 92 | assert.Equal("12345", resp.ID) |
| 93 | client.AssertExpectations(t) |
| 94 | } |
| 95 | |
| 96 | func TestDeleteAlertPolicy(t *testing.T) { |
| 97 | assert := assert.New(t) |
| 98 | |
| 99 | client := NewMockClient() |
| 100 | client.On("Delete", "http://localhost/v2/alertPolicies/test/12345", mock.Anything).Return(nil) |
| 101 | service := alert.New(client) |
| 102 | |
| 103 | err := service.Delete("12345") |
| 104 | |
| 105 | assert.Nil(err) |
| 106 | client.AssertExpectations(t) |
| 107 | } |
| 108 | |
| 109 | func NewMockClient() *MockClient { |
| 110 | return &MockClient{} |
| 111 | } |
| 112 | |
| 113 | type MockClient struct { |
| 114 | mock.Mock |
| 115 | } |
| 116 | |
| 117 | func (m *MockClient) Get(url string, resp interface{}) error { |
| 118 | if strings.HasSuffix(url, "12345") { |
| 119 | json.Unmarshal([]byte(`{"id":"12345","name":"new alert","actions":[{"action":"email","settings":{"recipients":["[email protected]"]}}],"links":[{"rel":"self","href":"/v2/alertPolicies/test/12345","verbs":["GET","DELETE","PUT"]}],"triggers":[{"metric":"disk","duration":"00:05:00","threshold":80.0}]}`), resp) |
| 120 | } |
| 121 | |
| 122 | json.Unmarshal([]byte(`{"items":[{"id":"999de90f25ab4308a6c346cd03602fef","name":"Memory Above 90%","actions":[{"action":"email","settings":{"recipients":["[email protected]"]}}],"links":[{"rel":"self","href":"/v2/alertPolicies/test/999de90f25ab4308a6c346cd03602fef","verbs":["GET","DELETE","PUT"]}],"triggers":[{"metric":"memory","duration":"00:10:00","threshold":90.0}]},{"id":"175c3b5743d64cea952a5cca03bdb2da","name":"CPU Above 75%","actions":[{"action":"email","settings":{"recipients":["[email protected]"]}}],"links":[{"rel":"self","href":"/v2/alertPolicies/test/175c3b5743d64cea952a5cca03bdb2da","verbs":["GET","DELETE","PUT"]}],"triggers":[{"metric":"cpu","duration":"00:05:00","threshold":75.0}]}],"links":[{"rel":"self","href":"/v2/alertPolicies/test","verbs":["GET","POST"]}]}`), resp) |
| 123 | args := m.Called(url, resp) |
| 124 | return args.Error(0) |
| 125 | } |
| 126 | |
| 127 | func (m *MockClient) Post(url string, body, resp interface{}) error { |
| 128 | json.Unmarshal([]byte(`{"id":"12345","name":"new alert","actions":[{"action":"email","settings":{"recipients":["[email protected]"]}}],"links":[{"rel":"self","href":"/v2/alertPolicies/test/12345","verbs":["GET","DELETE","PUT"]}],"triggers":[{"metric":"disk","duration":"00:05:00","threshold":80.0}]}`), resp) |
| 129 | args := m.Called(url, body, resp) |
| 130 | return args.Error(0) |
| 131 | } |
| 132 | |
| 133 | func (m *MockClient) Put(url string, body, resp interface{}) error { |
| 134 | json.Unmarshal([]byte(`{"id":"12345","name":"update alert","actions":[{"action":"email","settings":{"recipients":["[email protected]"]}}],"links":[{"rel":"self","href":"/v2/alertPolicies/test/12345","verbs":["GET","DELETE","PUT"]}],"triggers":[{"metric":"disk","duration":"00:05:00","threshold":80.0}]}`), resp) |
| 135 | args := m.Called(url, body, resp) |
| 136 | return args.Error(0) |
| 137 | } |
| 138 | |
| 139 | func (m *MockClient) Patch(url string, body, resp interface{}) error { |
| 140 | args := m.Called(url, body, resp) |
| 141 | return args.Error(0) |
| 142 | } |
| 143 | |
| 144 | func (m *MockClient) Delete(url string, resp interface{}) error { |
| 145 | args := m.Called(url, resp) |
| 146 | return args.Error(0) |
| 147 | } |
| 148 | |
| 149 | func (m *MockClient) Config() *api.Config { |
| 150 | u, _ := url.Parse("http://localhost/v2") |
| 151 | return &api.Config{ |
| 152 | User: api.User{ |
| 153 | Username: "test.user", |
| 154 | Password: "s0s3cur3", |
| 155 | }, |
| 156 | Alias: "test", |
| 157 | BaseURL: u, |
| 158 | } |
| 159 | } |