-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
88 lines (70 loc) · 1.93 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
package netpalmgo
import (
"context"
"github.com/stretchr/testify/assert"
testing2 "go.bnck.me/netpalm/internal/testing"
"go.bnck.me/netpalm/models"
"net/http"
"net/http/httptest"
"os"
"testing"
)
var client *Client
func GetClient() *Client {
if client != nil {
return client
}
client = New(os.Getenv("NETPALM_APIURL"), os.Getenv("NETPALM_APIKEY"))
return client
}
func TestNew(t *testing.T) {
cl := New(os.Getenv("NETPALM_APIURL"), os.Getenv("NETPALM_APIKEY"))
if cl.transport == nil {
t.Error("Resty client is empty")
}
}
func TestNewWithClient(t *testing.T) {
cl := NewWithClient(os.Getenv("NETPALM_APIURL"), os.Getenv("NETPALM_APIKEY"), &http.Client{})
if cl.transport == nil {
t.Error("Resty client is empty")
}
}
func TestWaitForResult(t *testing.T) {
reqcounter := 0
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
// Send response to be tested
if reqcounter > 2 {
rw.Write(testing2.GetTaskResponseJSON("123", models.TaskStatusFinished))
} else {
rw.Write(testing2.GetTaskResponseJSON("123", models.TaskStatusQueued))
}
reqcounter++
}))
cl := NewWithClient(server.URL, "123", server.Client())
resp, err := cl.GetConfig().WithCommand(context.Background(), "show int", models.LibraryNetmiko, testing2.GetConnectionArgs())
assert.NoError(t, err)
resp, err = cl.WaitForResult(context.Background(), resp)
assert.NoError(t, err)
assert.Equal(t, models.TaskStatusFinished, resp.Data.TaskStatus)
}
func TestClient_GetConfig(t *testing.T) {
cl := GetClient()
cfgAPI := cl.GetConfig()
if cfgAPI == nil {
t.Error("Failed to get 'GetConfig' endpoint")
}
}
func TestClient_SetConfig(t *testing.T) {
cl := GetClient()
cfgAPI := cl.SetConfig()
if cfgAPI == nil {
t.Error("Failed to get 'SetConfig' endpoint")
}
}
func TestClient_Task(t *testing.T) {
cl := GetClient()
taskAPI := cl.Task()
if taskAPI == nil {
t.Error("Failed to get 'Task' endpoint")
}
}