| 1 | package status_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/CenturyLinkCloud/clc-sdk/api" |
| 12 | "github.com/CenturyLinkCloud/clc-sdk/status" |
| 13 | "github.com/stretchr/testify/assert" |
| 14 | "github.com/stretchr/testify/mock" |
| 15 | ) |
| 16 | |
| 17 | func TestGetStatus(t *testing.T) { |
| 18 | assert := assert.New(t) |
| 19 | |
| 20 | client := NewMockClient() |
| 21 | client.On("Get", "http://localhost/v2/operations/test/status/12345", mock.Anything).Return(nil) |
| 22 | service := status.New(client) |
| 23 | resp, err := service.Get("12345") |
| 24 | |
| 25 | assert.Nil(err) |
| 26 | assert.True(resp.Running()) |
| 27 | client.AssertExpectations(t) |
| 28 | } |
| 29 | |
| 30 | func TestPollStatus(t *testing.T) { |
| 31 | assert := assert.New(t) |
| 32 | |
| 33 | client := NewMockClient() |
| 34 | client.On("Get", "http://localhost/v2/operations/test/status/12345", mock.Anything).Return(nil) |
| 35 | service := status.New(client) |
| 36 | service.PollInterval = 1 * time.Microsecond |
| 37 | |
| 38 | poll := make(chan *status.Response, 1) |
| 39 | err := service.Poll("12345", poll) |
| 40 | |
| 41 | status := <-poll |
| 42 | |
| 43 | assert.Nil(err) |
| 44 | assert.True(status.Complete()) |
| 45 | client.AssertExpectations(t) |
| 46 | } |
| 47 | |
| 48 | func TestPollStatus_ErrorGettingStatus(t *testing.T) { |
| 49 | assert := assert.New(t) |
| 50 | |
| 51 | client := NewMockClient() |
| 52 | client.On("Get", "http://localhost/v2/operations/test/status/12345", mock.Anything).Return(errors.New("")) |
| 53 | service := status.New(client) |
| 54 | service.PollInterval = 1 * time.Microsecond |
| 55 | |
| 56 | err := service.Poll("12345", make(chan *status.Response, 1)) |
| 57 | |
| 58 | assert.NotNil(err) |
| 59 | } |
| 60 | |
| 61 | func TestStatusBlueprintOperation(t *testing.T) { |
| 62 | assert := assert.New(t) |
| 63 | |
| 64 | client := NewMockClient() |
| 65 | client.On("Get", "http://localhost/v2/operations/test/status/blueprint", mock.Anything).Return(nil) |
| 66 | service := status.New(client) |
| 67 | |
| 68 | resp, err := service.GetBlueprint("blueprint") |
| 69 | |
| 70 | assert.Nil(err) |
| 71 | assert.Equal("blueprintOperation", resp.RequestType) |
| 72 | assert.Equal("network", resp.Summary.Links[0].Rel) |
| 73 | } |
| 74 | |
| 75 | func NewMockClient() *MockClient { |
| 76 | return &MockClient{} |
| 77 | } |
| 78 | |
| 79 | type MockClient struct { |
| 80 | mock.Mock |
| 81 | |
| 82 | count int |
| 83 | } |
| 84 | |
| 85 | func (m *MockClient) Get(url string, resp interface{}) error { |
| 86 | if strings.HasSuffix(url, "blueprint") { |
| 87 | json.Unmarshal([]byte(respBlueprintOperation), resp) |
| 88 | } else { |
| 89 | if m.count <= 1 { |
| 90 | json.Unmarshal([]byte(`{"status":"executing"}`), resp) |
| 91 | } else { |
| 92 | json.Unmarshal([]byte(`{"status":"succeeded"}`), resp) |
| 93 | } |
| 94 | m.count++ |
| 95 | } |
| 96 | args := m.Called(url, resp) |
| 97 | return args.Error(0) |
| 98 | } |
| 99 | |
| 100 | func (m *MockClient) Post(url string, body, resp interface{}) error { |
| 101 | args := m.Called(url, body, resp) |
| 102 | return args.Error(0) |
| 103 | } |
| 104 | |
| 105 | func (m *MockClient) Put(url string, body, resp interface{}) error { |
| 106 | args := m.Called(url, body, resp) |
| 107 | return args.Error(0) |
| 108 | } |
| 109 | |
| 110 | func (m *MockClient) Patch(url string, body, resp interface{}) error { |
| 111 | args := m.Called(url, body, resp) |
| 112 | return args.Error(0) |
| 113 | } |
| 114 | |
| 115 | func (m *MockClient) Delete(url string, resp interface{}) error { |
| 116 | args := m.Called(url, resp) |
| 117 | return args.Error(0) |
| 118 | } |
| 119 | |
| 120 | func (m *MockClient) Config() *api.Config { |
| 121 | u, _ := url.Parse("http://localhost/v2") |
| 122 | return &api.Config{ |
| 123 | User: api.User{ |
| 124 | Username: "test.user", |
| 125 | Password: "s0s3cur3", |
| 126 | }, |
| 127 | Alias: "test", |
| 128 | BaseURL: u, |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | var respBlueprintOperation = ` |
| 133 | { |
| 134 | "requestType":"blueprintOperation", |
| 135 | "status":"succeeded", |
| 136 | "summary":{ |
| 137 | "blueprintId":51229, |
| 138 | "locationId":"CA1", |
| 139 | "links":[ |
| 140 | { |
| 141 | "rel":"network", |
| 142 | "href":"/v2-experimental/networks/ZZBB/CA1/6955e7c39b5648df91bfb32e5d0aa24b", |
| 143 | "id":"6955e7c39b5648df91bfb32e5d0aa24b" |
| 144 | } |
| 145 | ] |
| 146 | }, |
| 147 | "source":{"userName":"ack","requestedAt":"2016-03-24T16:47:04Z"} |
| 148 | } |
| 149 | ` |