summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/scaleio/client/client_test.go
blob: ea82814c20c573ad6056bdda260741f146b5687f (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package client

import (
	"net/http/httptest"
	"testing"

	"github.com/netdata/netdata/go/go.d.plugin/pkg/web"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestNew(t *testing.T) {
	_, err := New(web.Client{}, web.Request{})
	assert.NoError(t, err)
}

func TestClient_Login(t *testing.T) {
	srv, client := prepareSrvClient(t)
	defer srv.Close()

	assert.NoError(t, client.Login())
	assert.Equal(t, testToken, client.token.get())
}

func TestClient_Logout(t *testing.T) {
	srv, client := prepareSrvClient(t)
	defer srv.Close()

	require.NoError(t, client.Login())

	assert.NoError(t, client.Logout())
	assert.False(t, client.token.isSet())

}

func TestClient_LoggedIn(t *testing.T) {
	srv, client := prepareSrvClient(t)
	defer srv.Close()

	assert.False(t, client.LoggedIn())
	assert.NoError(t, client.Login())
	assert.True(t, client.LoggedIn())
}

func TestClient_APIVersion(t *testing.T) {
	srv, client := prepareSrvClient(t)
	defer srv.Close()

	err := client.Login()
	require.NoError(t, err)

	version, err := client.APIVersion()
	assert.NoError(t, err)
	assert.Equal(t, Version{Major: 2, Minor: 5}, version)
}

func TestClient_Instances(t *testing.T) {
	srv, client := prepareSrvClient(t)
	defer srv.Close()

	err := client.Login()
	require.NoError(t, err)

	instances, err := client.Instances()
	assert.NoError(t, err)
	assert.Equal(t, testInstances, instances)
}

func TestClient_Instances_RetryOnExpiredToken(t *testing.T) {
	srv, client := prepareSrvClient(t)
	defer srv.Close()

	instances, err := client.Instances()
	assert.NoError(t, err)
	assert.Equal(t, testInstances, instances)
}

func TestClient_SelectedStatistics(t *testing.T) {
	srv, client := prepareSrvClient(t)
	defer srv.Close()

	err := client.Login()
	require.NoError(t, err)

	stats, err := client.SelectedStatistics(SelectedStatisticsQuery{})
	assert.NoError(t, err)
	assert.Equal(t, testStatistics, stats)
}

func TestClient_SelectedStatistics_RetryOnExpiredToken(t *testing.T) {
	srv, client := prepareSrvClient(t)
	defer srv.Close()

	stats, err := client.SelectedStatistics(SelectedStatisticsQuery{})
	assert.Equal(t, testStatistics, stats)
	assert.NoError(t, err)
	assert.Equal(t, testStatistics, stats)
}

func prepareSrvClient(t *testing.T) (*httptest.Server, *Client) {
	t.Helper()
	srv := httptest.NewServer(MockScaleIOAPIServer{
		User:       testUser,
		Password:   testPassword,
		Version:    testVersion,
		Token:      testToken,
		Instances:  testInstances,
		Statistics: testStatistics,
	})
	client, err := New(web.Client{}, web.Request{
		URL:      srv.URL,
		Username: testUser,
		Password: testPassword,
	})
	assert.NoError(t, err)
	return srv, client
}

var (
	testUser      = "user"
	testPassword  = "password"
	testVersion   = "2.5"
	testToken     = "token"
	testInstances = Instances{
		StoragePoolList: []StoragePool{
			{ID: "id1", Name: "Marketing", SparePercentage: 10},
			{ID: "id2", Name: "Finance", SparePercentage: 10},
		},
		SdcList: []Sdc{
			{ID: "id1", SdcIp: "10.0.0.1", MdmConnectionState: "Connected"},
			{ID: "id2", SdcIp: "10.0.0.2", MdmConnectionState: "Connected"},
		},
	}
	testStatistics = SelectedStatistics{
		System:      SystemStatistics{NumOfDevices: 1},
		Sdc:         map[string]SdcStatistics{"id1": {}, "id2": {}},
		StoragePool: map[string]StoragePoolStatistics{"id1": {}, "id2": {}},
	}
)