@@ -3,6 +3,7 @@ package dataupload_test
3
3
import (
4
4
"crypto/x509"
5
5
"encoding/pem"
6
+ "errors"
6
7
"fmt"
7
8
"net/http"
8
9
"os"
@@ -17,28 +18,23 @@ import (
17
18
"github.com/jetstack/preflight/pkg/internal/cyberark/dataupload"
18
19
"github.com/jetstack/preflight/pkg/internal/cyberark/identity"
19
20
"github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
21
+ "github.com/jetstack/preflight/pkg/testutil"
20
22
21
23
_ "k8s.io/klog/v2/ktesting/init"
22
24
)
23
25
24
- func TestCyberArkClient_PostDataReadingsWithOptions (t * testing.T ) {
26
+ func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI (t * testing.T ) {
25
27
fakeTime := time .Unix (123 , 0 )
26
- defaultPayload := api.DataReadingsPost {
27
- AgentMetadata : & api.AgentMetadata {
28
- Version : "test-version" ,
29
- ClusterID : "test" ,
30
- },
31
- DataGatherTime : fakeTime ,
32
- DataReadings : []* api.DataReading {
33
- {
34
- ClusterID : "success-cluster-id" ,
35
- DataGatherer : "test-gatherer" ,
36
- Timestamp : api.Time {Time : fakeTime },
37
- Data : map [string ]interface {}{"test" : "data" },
38
- SchemaVersion : "v1" ,
39
- },
28
+ defaultDataReadings := []* api.DataReading {
29
+ {
30
+ ClusterID : "success-cluster-id" ,
31
+ DataGatherer : "test-gatherer" ,
32
+ Timestamp : api.Time {Time : fakeTime },
33
+ Data : map [string ]interface {}{"test" : "data" },
34
+ SchemaVersion : "v1" ,
40
35
},
41
36
}
37
+
42
38
defaultOpts := dataupload.Options {
43
39
ClusterName : "success-cluster-id" ,
44
40
}
@@ -52,14 +48,14 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
52
48
53
49
tests := []struct {
54
50
name string
55
- payload api.DataReadingsPost
51
+ readings [] * api.DataReading
56
52
authenticate func (req * http.Request ) error
57
53
opts dataupload.Options
58
54
requireFn func (t * testing.T , err error )
59
55
}{
60
56
{
61
57
name : "successful upload" ,
62
- payload : defaultPayload ,
58
+ readings : defaultDataReadings ,
63
59
opts : defaultOpts ,
64
60
authenticate : setToken ("success-token" ),
65
61
requireFn : func (t * testing.T , err error ) {
@@ -68,7 +64,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
68
64
},
69
65
{
70
66
name : "error when cluster name is empty" ,
71
- payload : defaultPayload ,
67
+ readings : defaultDataReadings ,
72
68
opts : dataupload.Options {ClusterName : "" },
73
69
authenticate : setToken ("success-token" ),
74
70
requireFn : func (t * testing.T , err error ) {
@@ -77,16 +73,27 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
77
73
},
78
74
{
79
75
name : "error when bearer token is incorrect" ,
80
- payload : defaultPayload ,
76
+ readings : defaultDataReadings ,
81
77
opts : defaultOpts ,
82
78
authenticate : setToken ("fail-token" ),
83
79
requireFn : func (t * testing.T , err error ) {
84
80
require .ErrorContains (t , err , "while retrieving snapshot upload URL: received response with status code 500: should authenticate using the correct bearer token" )
85
81
},
86
82
},
83
+ {
84
+ name : "error contains authenticate error" ,
85
+ readings : defaultDataReadings ,
86
+ opts : defaultOpts ,
87
+ authenticate : func (_ * http.Request ) error {
88
+ return errors .New ("simulated-authenticate-error" )
89
+ },
90
+ requireFn : func (t * testing.T , err error ) {
91
+ require .ErrorContains (t , err , "while retrieving snapshot upload URL: failed to authenticate request: simulated-authenticate-error" )
92
+ },
93
+ },
87
94
{
88
95
name : "invalid JSON from server (RetrievePresignedUploadURL step)" ,
89
- payload : defaultPayload ,
96
+ readings : defaultDataReadings ,
90
97
opts : dataupload.Options {ClusterName : "invalid-json-retrieve-presigned" },
91
98
authenticate : setToken ("success-token" ),
92
99
requireFn : func (t * testing.T , err error ) {
@@ -95,7 +102,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
95
102
},
96
103
{
97
104
name : "500 from server (RetrievePresignedUploadURL step)" ,
98
- payload : defaultPayload ,
105
+ readings : defaultDataReadings ,
99
106
opts : dataupload.Options {ClusterName : "invalid-response-post-data" },
100
107
authenticate : setToken ("success-token" ),
101
108
requireFn : func (t * testing.T , err error ) {
@@ -106,6 +113,9 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
106
113
107
114
for _ , tc := range tests {
108
115
t .Run (tc .name , func (t * testing.T ) {
116
+ logger := ktesting .NewLogger (t , ktesting .DefaultConfig )
117
+ ctx := klog .NewContext (t .Context (), logger )
118
+
109
119
server := dataupload .MockDataUploadServer ()
110
120
defer server .Close ()
111
121
@@ -118,22 +128,22 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
118
128
cyberArkClient , err := dataupload .NewCyberArkClient (certPool , server .Server .URL , tc .authenticate )
119
129
require .NoError (t , err )
120
130
121
- err = cyberArkClient .PostDataReadingsWithOptions (t . Context () , tc .payload , tc .opts )
131
+ err = cyberArkClient .PostDataReadingsWithOptions (ctx , tc .readings , tc .opts )
122
132
tc .requireFn (t , err )
123
133
})
124
134
}
125
135
}
126
136
127
- // TestPostDataReadingsWithOptionsWithRealAPI demonstrates that the dataupload code works with the real inventory API.
137
+ // TestCyberArkClient_PostDataReadingsWithOptions_RealAPI demonstrates that the dataupload code works with the real inventory API.
128
138
// An API token is obtained by authenticating with the ARK_USERNAME and ARK_SECRET from the environment.
129
139
// ARK_SUBDOMAIN should be your tenant subdomain.
130
140
// ARK_PLATFORM_DOMAIN should be either integration-cyberark.cloud or cyberark.cloud
131
141
//
132
142
// To enable verbose request logging:
133
143
//
134
144
// go test ./pkg/internal/cyberark/dataupload/... \
135
- // -v -count 1 -run TestPostDataReadingsWithOptionsWithRealAPI -args -testing.v 6
136
- func TestPostDataReadingsWithOptionsWithRealAPI (t * testing.T ) {
145
+ // -v -count 1 -run TestCyberArkClient_PostDataReadingsWithOptions_RealAPI -args -testing.v 6
146
+ func TestCyberArkClient_PostDataReadingsWithOptions_RealAPI (t * testing.T ) {
137
147
platformDomain := os .Getenv ("ARK_PLATFORM_DOMAIN" )
138
148
subdomain := os .Getenv ("ARK_SUBDOMAIN" )
139
149
username := os .Getenv ("ARK_USERNAME" )
@@ -172,8 +182,13 @@ func TestPostDataReadingsWithOptionsWithRealAPI(t *testing.T) {
172
182
cyberArkClient , err := dataupload .NewCyberArkClient (nil , serviceURL , identityClient .AuthenticateRequest )
173
183
require .NoError (t , err )
174
184
175
- err = cyberArkClient .PostDataReadingsWithOptions (ctx , api.DataReadingsPost {}, dataupload.Options {
176
- ClusterName : "bb068932-c80d-460d-88df-34bc7f3f3297" ,
177
- })
185
+ dataReadings := testutil .ParseDataReadings (t , testutil .ReadGZIP (t , "testdata/example-1/datareadings.json.gz" ))
186
+ err = cyberArkClient .PostDataReadingsWithOptions (
187
+ ctx ,
188
+ dataReadings ,
189
+ dataupload.Options {
190
+ ClusterName : "bb068932-c80d-460d-88df-34bc7f3f3297" ,
191
+ },
192
+ )
178
193
require .NoError (t , err )
179
194
}
0 commit comments