-
Notifications
You must be signed in to change notification settings - Fork 374
/
size_list_test.go
156 lines (140 loc) · 3.47 KB
/
size_list_test.go
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
150
151
152
153
154
155
156
package integration
import (
"fmt"
"net/http"
"net/http/httptest"
"net/http/httputil"
"os/exec"
"strings"
"testing"
"github.com/sclevine/spec"
"github.com/stretchr/testify/require"
)
var _ = suite("compute/size/list", func(t *testing.T, when spec.G, it spec.S) {
var (
expect *require.Assertions
server *httptest.Server
)
it.Before(func() {
expect = require.New(t)
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/v2/sizes":
auth := req.Header.Get("Authorization")
if auth != "Bearer some-magic-token" {
w.WriteHeader(http.StatusUnauthorized)
return
}
if req.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Write([]byte(sizeListResponse))
default:
dump, err := httputil.DumpRequest(req, true)
if err != nil {
t.Fatal("failed to dump request")
}
t.Fatalf("received unknown request: %s", dump)
}
}))
})
when("all required flags are passed", func() {
it("lists sizes", func() {
cmd := exec.Command(builtBinaryPath,
"-t", "some-magic-token",
"-u", server.URL,
"compute",
"size",
"list",
)
output, err := cmd.CombinedOutput()
expect.NoError(err, fmt.Sprintf("received error output: %s", output))
expect.Equal(strings.TrimSpace(sizeListOutput), strings.TrimSpace(string(output)))
})
})
when("passing a format", func() {
it("displays only those columns", func() {
cmd := exec.Command(builtBinaryPath,
"-t", "some-magic-token",
"-u", server.URL,
"compute",
"size",
"list",
"--format", "Slug,PriceMonthly",
)
output, err := cmd.CombinedOutput()
expect.NoError(err, fmt.Sprintf("received error output: %s", output))
expect.Equal(strings.TrimSpace(sizeListFormatOutput), strings.TrimSpace(string(output)))
})
})
when("passing no-header", func() {
it("displays only values, no headers", func() {
cmd := exec.Command(builtBinaryPath,
"-t", "some-magic-token",
"-u", server.URL,
"compute",
"size",
"list",
"--no-header",
)
output, err := cmd.CombinedOutput()
expect.NoError(err, fmt.Sprintf("received error output: %s", output))
expect.Equal(strings.TrimSpace(sizeListNoHeaderOutput), strings.TrimSpace(string(output)))
})
})
})
const (
sizeListResponse = `
{
"sizes": [
{
"slug": "512mb",
"memory": 512,
"description": "Basic",
"vcpus": 1,
"disk": 20,
"transfer": 1,
"price_monthly": 5,
"price_hourly": 0.007439999841153622,
"regions": [
"nyc1"
],
"available": true
},
{
"slug": "s-1vcpu-1gb",
"memory": 1024,
"description": "Basic",
"vcpus": 1,
"disk": 25,
"transfer": 1,
"price_monthly": 5,
"price_hourly": 0.007439999841153622,
"regions": [
"nyc1"
],
"available": true
}
],
"links": {},
"meta": {
"total": 2
}
}
`
sizeListOutput = `
Slug Description Memory VCPUs Disk Price Monthly Price Hourly
512mb Basic 512 1 20 5.00 0.007440
s-1vcpu-1gb Basic 1024 1 25 5.00 0.007440
`
sizeListFormatOutput = `
Slug Price Monthly
512mb 5.00
s-1vcpu-1gb 5.00
`
sizeListNoHeaderOutput = `
512mb Basic 512 1 20 5.00 0.007440
s-1vcpu-1gb Basic 1024 1 25 5.00 0.007440
`
)