-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
116 lines (97 loc) · 2.99 KB
/
client_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
// Package aiven provides a client for interacting with the Aiven API.
package aiven
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/aiven/go-client-codegen/handler/clickhouse"
"github.com/aiven/go-client-codegen/handler/service"
)
func TestNewClient(t *testing.T) {
token := os.Getenv("AIVEN_TOKEN")
if token == "" {
t.Skip("token is required for the test")
}
c, err := NewClient(DebugOpt(true))
require.NoError(t, err)
ctx := context.Background()
tokens, err := c.AccessTokenList(ctx)
require.NoError(t, err)
found := 0
for _, to := range tokens {
if strings.HasPrefix(token, to.TokenPrefix) {
found++
}
}
assert.Equal(t, 1, found)
}
func TestServiceCreate(t *testing.T) {
var callCount int64
// Creates a test server
mux := http.NewServeMux()
mux.HandleFunc(
"/v1/project/aiven-project/service",
func(w http.ResponseWriter, r *http.Request) {
// Validates request
expectIn := new(service.ServiceCreateIn)
err := json.NewDecoder(r.Body).Decode(expectIn)
assert.NoError(t, err)
assert.Equal(t, "my-clickhouse", expectIn.ServiceName)
assert.Equal(t, "clickhouse", expectIn.ServiceType)
assert.Regexp(t, `go-client-codegen/[0-9\.]+ unit-test`, r.Header["User-Agent"])
// Creates response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte(`{"service": {"plan": "wow", "state": "RUNNING"}}`))
require.NoError(t, err)
atomic.AddInt64(&callCount, 1)
},
)
mux.HandleFunc(
"/v1/project/aiven-project/service/my-clickhouse/clickhouse/query/stats",
func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.URL.RawQuery, "limit=1&order_by=max_time%3Aasc")
// Creates response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"queries": [{"calls": 1}]}`))
require.NoError(t, err)
atomic.AddInt64(&callCount, 1)
},
)
server := httptest.NewServer(mux)
defer server.Close()
// Points a new client to the server url
c, err := NewClient(TokenOpt("token"), HostOpt(server.URL), UserAgentOpt("unit-test"))
require.NotNil(t, c)
require.NoError(t, err)
// Makes create request
in := &service.ServiceCreateIn{
ServiceName: "my-clickhouse",
ServiceType: "clickhouse",
}
ctx := context.Background()
project := "aiven-project"
out, err := c.ServiceCreate(ctx, project, in)
require.NoError(t, err)
require.NotNil(t, out)
assert.Equal(t, "wow", out.Plan)
assert.Equal(t, service.ServiceStateTypeRunning, out.State)
// Validates query params
stats, err := c.ServiceClickHouseQueryStats(
ctx, project, in.ServiceName,
clickhouse.ServiceClickHouseQueryStatsLimit(1),
clickhouse.ServiceClickHouseQueryStatsOrderByType(clickhouse.OrderByTypeMaxTimeasc),
)
require.NoError(t, err)
assert.Len(t, stats, 1)
// All calls are received
assert.EqualValues(t, 2, callCount)
}