| 1 | package lb_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/CenturyLinkCloud/clc-sdk/api" |
| 10 | "github.com/CenturyLinkCloud/clc-sdk/lb" |
| 11 | "github.com/stretchr/testify/assert" |
| 12 | "github.com/stretchr/testify/mock" |
| 13 | ) |
| 14 | |
| 15 | func TestGetLB(t *testing.T) { |
| 16 | assert := assert.New(t) |
| 17 | |
| 18 | client := NewMockClient() |
| 19 | client.On("Get", "http://localhost/v2/sharedLoadBalancers/test/dc1/12345", mock.Anything).Return(nil) |
| 20 | service := lb.New(client) |
| 21 | |
| 22 | id := "12345" |
| 23 | resp, err := service.Get("dc1", id) |
| 24 | |
| 25 | assert.Nil(err) |
| 26 | assert.Equal(id, resp.ID) |
| 27 | client.AssertExpectations(t) |
| 28 | } |
| 29 | |
| 30 | func TestGetAllLBs(t *testing.T) { |
| 31 | assert := assert.New(t) |
| 32 | |
| 33 | client := NewMockClient() |
| 34 | client.On("Get", "http://localhost/v2/sharedLoadBalancers/test/dc1", mock.Anything).Return(nil) |
| 35 | service := lb.New(client) |
| 36 | |
| 37 | resp, err := service.GetAll("dc1") |
| 38 | |
| 39 | assert.Nil(err) |
| 40 | assert.Equal(1, len(resp)) |
| 41 | client.AssertExpectations(t) |
| 42 | } |
| 43 | |
| 44 | func TestCreateLB(t *testing.T) { |
| 45 | assert := assert.New(t) |
| 46 | |
| 47 | client := NewMockClient() |
| 48 | client.On("Post", "http://localhost/v2/sharedLoadBalancers/test/dc1", mock.Anything, mock.Anything).Return(nil) |
| 49 | service := lb.New(client) |
| 50 | |
| 51 | lb := lb.LoadBalancer{ |
| 52 | Name: "new", |
| 53 | Description: "balancing load", |
| 54 | } |
| 55 | resp, err := service.Create("dc1", lb) |
| 56 | |
| 57 | assert.Nil(err) |
| 58 | assert.Equal(lb.Name, resp.Name) |
| 59 | assert.Equal("enabled", resp.Status) |
| 60 | assert.NotEmpty(resp.ID) |
| 61 | client.AssertExpectations(t) |
| 62 | } |
| 63 | |
| 64 | func TestUpdateLB(t *testing.T) { |
| 65 | assert := assert.New(t) |
| 66 | |
| 67 | client := NewMockClient() |
| 68 | client.On("Put", "http://localhost/v2/sharedLoadBalancers/test/dc1/12345", mock.Anything, nil).Return(nil) |
| 69 | service := lb.New(client) |
| 70 | |
| 71 | lb := lb.LoadBalancer{ |
| 72 | Name: "new", |
| 73 | Description: "balancing load", |
| 74 | } |
| 75 | err := service.Update("dc1", "12345", lb) |
| 76 | |
| 77 | assert.Nil(err) |
| 78 | client.AssertExpectations(t) |
| 79 | } |
| 80 | |
| 81 | func TestDeleteLB(t *testing.T) { |
| 82 | assert := assert.New(t) |
| 83 | |
| 84 | client := NewMockClient() |
| 85 | client.On("Delete", "http://localhost/v2/sharedLoadBalancers/test/dc1/12345", mock.Anything).Return(nil) |
| 86 | service := lb.New(client) |
| 87 | |
| 88 | id := "12345" |
| 89 | err := service.Delete("dc1", id) |
| 90 | |
| 91 | assert.Nil(err) |
| 92 | client.AssertExpectations(t) |
| 93 | } |
| 94 | |
| 95 | func TestGetLBPool(t *testing.T) { |
| 96 | assert := assert.New(t) |
| 97 | |
| 98 | client := NewMockClient() |
| 99 | client.On("Get", "http://localhost/v2/sharedLoadBalancers/test/dc1/12345/pools/56789", mock.Anything).Return(nil) |
| 100 | service := lb.New(client) |
| 101 | |
| 102 | id := "56789" |
| 103 | resp, err := service.GetPool("dc1", "12345", id) |
| 104 | |
| 105 | assert.Nil(err) |
| 106 | assert.Equal(id, resp.ID) |
| 107 | client.AssertExpectations(t) |
| 108 | } |
| 109 | |
| 110 | func TestGetLBPools(t *testing.T) { |
| 111 | assert := assert.New(t) |
| 112 | |
| 113 | client := NewMockClient() |
| 114 | client.On("Get", "http://localhost/v2/sharedLoadBalancers/test/dc1/12345/pools", mock.Anything).Return(nil) |
| 115 | service := lb.New(client) |
| 116 | |
| 117 | resp, err := service.GetAllPools("dc1", "12345") |
| 118 | |
| 119 | assert.Nil(err) |
| 120 | assert.Equal(1, len(resp)) |
| 121 | client.AssertExpectations(t) |
| 122 | } |
| 123 | |
| 124 | func TestCreateLBPool(t *testing.T) { |
| 125 | assert := assert.New(t) |
| 126 | |
| 127 | client := NewMockClient() |
| 128 | client.On("Post", "http://localhost/v2/sharedLoadBalancers/test/dc1/12345/pools", mock.Anything, mock.Anything).Return(nil) |
| 129 | service := lb.New(client) |
| 130 | |
| 131 | pool := lb.Pool{ |
| 132 | Port: 80, |
| 133 | Method: lb.LeastConn, |
| 134 | Persistence: lb.Sticky, |
| 135 | } |
| 136 | |
| 137 | resp, err := service.CreatePool("dc1", "12345", pool) |
| 138 | |
| 139 | assert.Nil(err) |
| 140 | assert.Equal(pool.Port, resp.Port) |
| 141 | assert.NotEmpty(resp.ID) |
| 142 | client.AssertExpectations(t) |
| 143 | } |
| 144 | |
| 145 | func TestUpdatePool(t *testing.T) { |
| 146 | assert := assert.New(t) |
| 147 | |
| 148 | client := NewMockClient() |
| 149 | client.On("Put", "http://localhost/v2/sharedLoadBalancers/test/dc1/12345/pools/56789", mock.Anything, nil).Return(nil) |
| 150 | service := lb.New(client) |
| 151 | |
| 152 | pool := lb.Pool{ |
| 153 | Method: lb.LeastConn, |
| 154 | Persistence: lb.Sticky, |
| 155 | } |
| 156 | err := service.UpdatePool("dc1", "12345", "56789", pool) |
| 157 | |
| 158 | assert.Nil(err) |
| 159 | client.AssertExpectations(t) |
| 160 | } |
| 161 | |
| 162 | func TestDeleteLBPool(t *testing.T) { |
| 163 | assert := assert.New(t) |
| 164 | |
| 165 | client := NewMockClient() |
| 166 | client.On("Delete", "http://localhost/v2/sharedLoadBalancers/test/dc1/12345/pools/56789", mock.Anything).Return(nil) |
| 167 | service := lb.New(client) |
| 168 | |
| 169 | id := "56789" |
| 170 | err := service.DeletePool("dc1", "12345", id) |
| 171 | |
| 172 | assert.Nil(err) |
| 173 | client.AssertExpectations(t) |
| 174 | } |
| 175 | func TestGetAllNodes(t *testing.T) { |
| 176 | assert := assert.New(t) |
| 177 | |
| 178 | client := NewMockClient() |
| 179 | client.On("Get", "http://localhost/v2/sharedLoadBalancers/test/dc1/12345/pools/56789/nodes", mock.Anything).Return(nil) |
| 180 | service := lb.New(client) |
| 181 | |
| 182 | resp, err := service.GetAllNodes("dc1", "12345", "56789") |
| 183 | |
| 184 | assert.Nil(err) |
| 185 | assert.Equal(2, len(resp)) |
| 186 | client.AssertExpectations(t) |
| 187 | } |
| 188 | |
| 189 | func TestUpdateNodes(t *testing.T) { |
| 190 | assert := assert.New(t) |
| 191 | |
| 192 | client := NewMockClient() |
| 193 | client.On("Put", "http://localhost/v2/sharedLoadBalancers/test/dc1/12345/pools/56789/nodes", mock.Anything, nil).Return(nil) |
| 194 | service := lb.New(client) |
| 195 | |
| 196 | node := lb.Node{ |
| 197 | IPaddress: "10.0.0.0", |
| 198 | PrivatePort: 8080, |
| 199 | } |
| 200 | err := service.UpdateNodes("dc1", "12345", "56789", node) |
| 201 | |
| 202 | assert.Nil(err) |
| 203 | client.AssertExpectations(t) |
| 204 | } |
| 205 | |
| 206 | func NewMockClient() *MockClient { |
| 207 | return &MockClient{} |
| 208 | } |
| 209 | |
| 210 | type MockClient struct { |
| 211 | mock.Mock |
| 212 | } |
| 213 | |
| 214 | func (m *MockClient) Get(url string, resp interface{}) error { |
| 215 | if strings.HasSuffix(url, "12345") { |
| 216 | json.Unmarshal([]byte(`{"id":"12345","name":"new","description":"balancing load","ipAddress":"10.10.10.10","status":"enabled","pools":[],"links":[{"rel":"self","href":"/v2/sharedLoadBalancers/test/dc1/12345","verbs":["GET","PUT","DELETE"]},{"rel":"pools","href":"/v2/sharedLoadBalancers/test/dc1/12345/pools","verbs":["GET","POST"]}]}`), resp) |
| 217 | } else if strings.HasSuffix(url, "56789") { |
| 218 | json.Unmarshal([]byte(`{"id":"56789","port":80,"method":"leastConnection","persistence":"standard","links":[{"rel":"self","href":"/v2/sharedLoadBalancers/t3bk/va1/12345/pools/56789","verbs":["GET","PUT","DELETE"]},{"rel":"nodes","href":"/v2/sharedLoadBalancers/t3bk/va1/12345/pools/56789/nodes","verbs":["GET","PUT"]}]}`), resp) |
| 219 | } else if strings.HasSuffix(url, "nodes") { |
| 220 | json.Unmarshal([]byte(`[{"status":"enabled","ipAddress":"10.11.12.13","privatePort":80},{"status":"enabled","ipAddress":"10.11.12.14","privatePort":80}]`), resp) |
| 221 | } else if strings.Contains(url, "pools") { |
| 222 | json.Unmarshal([]byte(`[{"id":"56789","port":80,"method":"leastConnection","persistence":"standard","links":[{"rel":"self","href":"/v2/sharedLoadBalancers/t3bk/va1/12345/pools/56789","verbs":["GET","PUT","DELETE"]},{"rel":"nodes","href":"/v2/sharedLoadBalancers/t3bk/va1/12345/pools/56789/nodes","verbs":["GET","PUT"]}]}]`), resp) |
| 223 | } else { |
| 224 | json.Unmarshal([]byte(`[{"id":"12345","name":"new","description":"balancing load","ipAddress":"10.10.10.10","status":"enabled","pools":[],"links":[{"rel":"self","href":"/v2/sharedLoadBalancers/test/dc1/12345","verbs":["GET","PUT","DELETE"]},{"rel":"pools","href":"/v2/sharedLoadBalancers/test/dc1/12345/pools","verbs":["GET","POST"]}]}]`), resp) |
| 225 | } |
| 226 | |
| 227 | args := m.Called(url, resp) |
| 228 | return args.Error(0) |
| 229 | } |
| 230 | |
| 231 | func (m *MockClient) Post(url string, body, resp interface{}) error { |
| 232 | if strings.HasSuffix(url, "pools") { |
| 233 | json.Unmarshal([]byte(`{"id":"56789","port":80,"method":"leastConnection","persistence":"sticky","nodes":[],"links":[{"rel":"self","href":"/v2/sharedLoadBalancers/test/dc1/12345/pools/56789","verbs":["GET","PUT","DELETE"]},{"rel":"nodes","href":"/v2/sharedLoadBalancers/test/dc1/12345/pools/56789/nodes","verbs":["GET","PUT"]}]}`), resp) |
| 234 | } else { |
| 235 | json.Unmarshal([]byte(`{"id":"12345","name":"new","description":"balancing load","ipAddress":"10.10.10.10","status":"enabled","pools":[],"links":[{"rel":"self","href":"/v2/sharedLoadBalancers/test/dc1/12345","verbs":["GET","PUT","DELETE"]},{"rel":"pools","href":"/v2/sharedLoadBalancers/test/dc1/12345/pools","verbs":["GET","POST"]}]}`), resp) |
| 236 | } |
| 237 | args := m.Called(url, body, resp) |
| 238 | return args.Error(0) |
| 239 | } |
| 240 | |
| 241 | func (m *MockClient) Put(url string, body, resp interface{}) error { |
| 242 | args := m.Called(url, body, resp) |
| 243 | return args.Error(0) |
| 244 | } |
| 245 | |
| 246 | func (m *MockClient) Patch(url string, body, resp interface{}) error { |
| 247 | args := m.Called(url, body, resp) |
| 248 | return args.Error(0) |
| 249 | } |
| 250 | |
| 251 | func (m *MockClient) Delete(url string, resp interface{}) error { |
| 252 | args := m.Called(url, resp) |
| 253 | return args.Error(0) |
| 254 | } |
| 255 | |
| 256 | func (m *MockClient) Config() *api.Config { |
| 257 | u, _ := url.Parse("http://localhost/v2") |
| 258 | return &api.Config{ |
| 259 | User: api.User{ |
| 260 | Username: "test.user", |
| 261 | Password: "s0s3cur3", |
| 262 | }, |
| 263 | Alias: "test", |
| 264 | BaseURL: u, |
| 265 | } |
| 266 | } |