forked from nytm/go-grafana-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource_test.go
68 lines (55 loc) · 1.31 KB
/
datasource_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
package gapi
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/gobs/pretty"
)
func gapiTestTools(code int, body string) (*httptest.Server, *Client) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, body)
}))
tr := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(server.URL)
},
}
httpClient := &http.Client{Transport: tr}
url := url.URL{
Scheme: "http",
Host: "my-grafana.com",
}
client := &Client{"my-key", url, httpClient}
return server, client
}
func TestNewDataSource(t *testing.T) {
server, client := gapiTestTools(200, createdDataSourceJSON)
defer server.Close()
ds := &DataSource{
Name: "foo",
Type: "cloudwatch",
URL: "http://some-url.com",
Access: "access",
IsDefault: true,
JSONData: JSONData{
AuthType: "keys",
DefaultRegion: "us-east-1",
},
SecureJSONData: SecureJSONData{
AccessKey: "123",
SecretKey: "456",
},
}
created, err := client.NewDataSource(ds)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(created))
if created != 1 {
t.Error("datasource creation response should return the created datasource ID")
}
}