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