github.com/netdata/go.d.plugin@v0.58.1/modules/scaleio/client/client_test.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package client 4 5 import ( 6 "net/http/httptest" 7 "testing" 8 9 "github.com/netdata/go.d.plugin/pkg/web" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestNew(t *testing.T) { 16 _, err := New(web.Client{}, web.Request{}) 17 assert.NoError(t, err) 18 } 19 20 func TestClient_Login(t *testing.T) { 21 srv, client := prepareSrvClient(t) 22 defer srv.Close() 23 24 assert.NoError(t, client.Login()) 25 assert.Equal(t, testToken, client.token.get()) 26 } 27 28 func TestClient_Logout(t *testing.T) { 29 srv, client := prepareSrvClient(t) 30 defer srv.Close() 31 32 require.NoError(t, client.Login()) 33 34 assert.NoError(t, client.Logout()) 35 assert.False(t, client.token.isSet()) 36 37 } 38 39 func TestClient_LoggedIn(t *testing.T) { 40 srv, client := prepareSrvClient(t) 41 defer srv.Close() 42 43 assert.False(t, client.LoggedIn()) 44 assert.NoError(t, client.Login()) 45 assert.True(t, client.LoggedIn()) 46 } 47 48 func TestClient_APIVersion(t *testing.T) { 49 srv, client := prepareSrvClient(t) 50 defer srv.Close() 51 52 err := client.Login() 53 require.NoError(t, err) 54 55 version, err := client.APIVersion() 56 assert.NoError(t, err) 57 assert.Equal(t, Version{Major: 2, Minor: 5}, version) 58 } 59 60 func TestClient_Instances(t *testing.T) { 61 srv, client := prepareSrvClient(t) 62 defer srv.Close() 63 64 err := client.Login() 65 require.NoError(t, err) 66 67 instances, err := client.Instances() 68 assert.NoError(t, err) 69 assert.Equal(t, testInstances, instances) 70 } 71 72 func TestClient_Instances_RetryOnExpiredToken(t *testing.T) { 73 srv, client := prepareSrvClient(t) 74 defer srv.Close() 75 76 instances, err := client.Instances() 77 assert.NoError(t, err) 78 assert.Equal(t, testInstances, instances) 79 } 80 81 func TestClient_SelectedStatistics(t *testing.T) { 82 srv, client := prepareSrvClient(t) 83 defer srv.Close() 84 85 err := client.Login() 86 require.NoError(t, err) 87 88 stats, err := client.SelectedStatistics(SelectedStatisticsQuery{}) 89 assert.NoError(t, err) 90 assert.Equal(t, testStatistics, stats) 91 } 92 93 func TestClient_SelectedStatistics_RetryOnExpiredToken(t *testing.T) { 94 srv, client := prepareSrvClient(t) 95 defer srv.Close() 96 97 stats, err := client.SelectedStatistics(SelectedStatisticsQuery{}) 98 assert.Equal(t, testStatistics, stats) 99 assert.NoError(t, err) 100 assert.Equal(t, testStatistics, stats) 101 } 102 103 func prepareSrvClient(t *testing.T) (*httptest.Server, *Client) { 104 t.Helper() 105 srv := httptest.NewServer(MockScaleIOAPIServer{ 106 User: testUser, 107 Password: testPassword, 108 Version: testVersion, 109 Token: testToken, 110 Instances: testInstances, 111 Statistics: testStatistics, 112 }) 113 client, err := New(web.Client{}, web.Request{ 114 URL: srv.URL, 115 Username: testUser, 116 Password: testPassword, 117 }) 118 assert.NoError(t, err) 119 return srv, client 120 } 121 122 var ( 123 testUser = "user" 124 testPassword = "password" 125 testVersion = "2.5" 126 testToken = "token" 127 testInstances = Instances{ 128 StoragePoolList: []StoragePool{ 129 {ID: "id1", Name: "Marketing", SparePercentage: 10}, 130 {ID: "id2", Name: "Finance", SparePercentage: 10}, 131 }, 132 SdcList: []Sdc{ 133 {ID: "id1", SdcIp: "10.0.0.1", MdmConnectionState: "Connected"}, 134 {ID: "id2", SdcIp: "10.0.0.2", MdmConnectionState: "Connected"}, 135 }, 136 } 137 testStatistics = SelectedStatistics{ 138 System: SystemStatistics{NumOfDevices: 1}, 139 Sdc: map[string]SdcStatistics{"id1": {}, "id2": {}}, 140 StoragePool: map[string]StoragePoolStatistics{"id1": {}, "id2": {}}, 141 } 142 )