@@ -3,6 +3,7 @@ package dataupload_test
33import (
44 "crypto/x509"
55 "encoding/pem"
6+ "errors"
67 "fmt"
78 "net/http"
89 "os"
@@ -17,28 +18,23 @@ import (
1718 "github.com/jetstack/preflight/pkg/internal/cyberark/dataupload"
1819 "github.com/jetstack/preflight/pkg/internal/cyberark/identity"
1920 "github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
21+ "github.com/jetstack/preflight/pkg/testutil"
2022
2123 _ "k8s.io/klog/v2/ktesting/init"
2224)
2325
24- func TestCyberArkClient_PostDataReadingsWithOptions (t * testing.T ) {
26+ func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI (t * testing.T ) {
2527 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" ,
4035 },
4136 }
37+
4238 defaultOpts := dataupload.Options {
4339 ClusterName : "success-cluster-id" ,
4440 }
@@ -52,14 +48,14 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
5248
5349 tests := []struct {
5450 name string
55- payload api.DataReadingsPost
51+ readings [] * api.DataReading
5652 authenticate func (req * http.Request ) error
5753 opts dataupload.Options
5854 requireFn func (t * testing.T , err error )
5955 }{
6056 {
6157 name : "successful upload" ,
62- payload : defaultPayload ,
58+ readings : defaultDataReadings ,
6359 opts : defaultOpts ,
6460 authenticate : setToken ("success-token" ),
6561 requireFn : func (t * testing.T , err error ) {
@@ -68,7 +64,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
6864 },
6965 {
7066 name : "error when cluster name is empty" ,
71- payload : defaultPayload ,
67+ readings : defaultDataReadings ,
7268 opts : dataupload.Options {ClusterName : "" },
7369 authenticate : setToken ("success-token" ),
7470 requireFn : func (t * testing.T , err error ) {
@@ -77,16 +73,27 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
7773 },
7874 {
7975 name : "error when bearer token is incorrect" ,
80- payload : defaultPayload ,
76+ readings : defaultDataReadings ,
8177 opts : defaultOpts ,
8278 authenticate : setToken ("fail-token" ),
8379 requireFn : func (t * testing.T , err error ) {
8480 require .ErrorContains (t , err , "while retrieving snapshot upload URL: received response with status code 500: should authenticate using the correct bearer token" )
8581 },
8682 },
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+ },
8794 {
8895 name : "invalid JSON from server (RetrievePresignedUploadURL step)" ,
89- payload : defaultPayload ,
96+ readings : defaultDataReadings ,
9097 opts : dataupload.Options {ClusterName : "invalid-json-retrieve-presigned" },
9198 authenticate : setToken ("success-token" ),
9299 requireFn : func (t * testing.T , err error ) {
@@ -95,7 +102,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
95102 },
96103 {
97104 name : "500 from server (RetrievePresignedUploadURL step)" ,
98- payload : defaultPayload ,
105+ readings : defaultDataReadings ,
99106 opts : dataupload.Options {ClusterName : "invalid-response-post-data" },
100107 authenticate : setToken ("success-token" ),
101108 requireFn : func (t * testing.T , err error ) {
@@ -106,6 +113,9 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
106113
107114 for _ , tc := range tests {
108115 t .Run (tc .name , func (t * testing.T ) {
116+ logger := ktesting .NewLogger (t , ktesting .DefaultConfig )
117+ ctx := klog .NewContext (t .Context (), logger )
118+
109119 server := dataupload .MockDataUploadServer ()
110120 defer server .Close ()
111121
@@ -118,22 +128,22 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
118128 cyberArkClient , err := dataupload .NewCyberArkClient (certPool , server .Server .URL , tc .authenticate )
119129 require .NoError (t , err )
120130
121- err = cyberArkClient .PostDataReadingsWithOptions (t . Context () , tc .payload , tc .opts )
131+ err = cyberArkClient .PostDataReadingsWithOptions (ctx , tc .readings , tc .opts )
122132 tc .requireFn (t , err )
123133 })
124134 }
125135}
126136
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.
128138// An API token is obtained by authenticating with the ARK_USERNAME and ARK_SECRET from the environment.
129139// ARK_SUBDOMAIN should be your tenant subdomain.
130140// ARK_PLATFORM_DOMAIN should be either integration-cyberark.cloud or cyberark.cloud
131141//
132142// To enable verbose request logging:
133143//
134144// 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 ) {
137147 platformDomain := os .Getenv ("ARK_PLATFORM_DOMAIN" )
138148 subdomain := os .Getenv ("ARK_SUBDOMAIN" )
139149 username := os .Getenv ("ARK_USERNAME" )
@@ -172,8 +182,13 @@ func TestPostDataReadingsWithOptionsWithRealAPI(t *testing.T) {
172182 cyberArkClient , err := dataupload .NewCyberArkClient (nil , serviceURL , identityClient .AuthenticateRequest )
173183 require .NoError (t , err )
174184
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+ )
178193 require .NoError (t , err )
179194}
0 commit comments