@@ -8,9 +8,10 @@ import (
88 "net/http"
99 "os"
1010 "testing"
11- "time"
1211
1312 "github.com/stretchr/testify/require"
13+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
14+ "k8s.io/apimachinery/pkg/types"
1415 "k8s.io/klog/v2"
1516 "k8s.io/klog/v2/ktesting"
1617
@@ -23,20 +24,41 @@ import (
2324 _ "k8s.io/klog/v2/ktesting/init"
2425)
2526
26- func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI (t * testing.T ) {
27- fakeTime := time .Unix (123 , 0 )
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" ,
27+ func genNamespace (name string ) * unstructured.Unstructured {
28+ o := & unstructured.Unstructured {}
29+ o .SetAPIVersion ("" )
30+ o .SetKind ("Namespace" )
31+ o .SetName (name )
32+ return o
33+ }
34+ func genArkNamespacesDataReading (clusterID types.UID ) * api.DataReading {
35+ kubeSystemNamespace := genNamespace ("kube-system" )
36+ kubeSystemNamespace .SetUID (clusterID )
37+ return & api.DataReading {
38+ ClusterID : "ignored-tlspk-cluster-id" ,
39+ DataGatherer : "ark/namespaces" ,
40+ Data : & api.DynamicData {
41+ Items : []* api.GatheredResource {
42+ {
43+ Resource : kubeSystemNamespace ,
44+ },
45+ {
46+ Resource : genNamespace ("kube-public" ),
47+ },
48+ {
49+ Resource : genNamespace ("venafi" ),
50+ },
51+ {
52+ Resource : genNamespace ("cert-manager" ),
53+ },
54+ },
3555 },
56+ SchemaVersion : "v1" ,
3657 }
37-
38- defaultOpts := dataupload.Options {
39- ClusterName : "success-cluster-id" ,
58+ }
59+ func TestCyberArkClient_PostDataReadings_MockAPI (t * testing.T ) {
60+ defaultDataReadings := []* api.DataReading {
61+ genArkNamespacesDataReading ("success-cluster-id" ),
4062 }
4163
4264 setToken := func (token string ) func (* http.Request ) error {
@@ -50,31 +72,27 @@ func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI(t *testing.T) {
5072 name string
5173 readings []* api.DataReading
5274 authenticate func (req * http.Request ) error
53- opts dataupload.Options
5475 requireFn func (t * testing.T , err error )
5576 }{
5677 {
5778 name : "successful upload" ,
5879 readings : defaultDataReadings ,
59- opts : defaultOpts ,
6080 authenticate : setToken ("success-token" ),
6181 requireFn : func (t * testing.T , err error ) {
6282 require .NoError (t , err )
6383 },
6484 },
6585 {
66- name : "error when cluster name is empty" ,
67- readings : defaultDataReadings ,
68- opts : dataupload.Options {ClusterName : "" },
86+ name : "error when cluster ID not found among data readings" ,
87+ readings : nil ,
6988 authenticate : setToken ("success-token" ),
7089 requireFn : func (t * testing.T , err error ) {
71- require .ErrorContains (t , err , "programmer mistake: the cluster name " )
90+ require .ErrorContains (t , err , "while converting datareadings to Cyberark snapshot format: failed to compute a clusterID from the data-readings " )
7291 },
7392 },
7493 {
7594 name : "error when bearer token is incorrect" ,
7695 readings : defaultDataReadings ,
77- opts : defaultOpts ,
7896 authenticate : setToken ("fail-token" ),
7997 requireFn : func (t * testing.T , err error ) {
8098 require .ErrorContains (t , err , "while retrieving snapshot upload URL: received response with status code 500: should authenticate using the correct bearer token" )
@@ -83,7 +101,6 @@ func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI(t *testing.T) {
83101 {
84102 name : "error contains authenticate error" ,
85103 readings : defaultDataReadings ,
86- opts : defaultOpts ,
87104 authenticate : func (_ * http.Request ) error {
88105 return errors .New ("simulated-authenticate-error" )
89106 },
@@ -92,18 +109,20 @@ func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI(t *testing.T) {
92109 },
93110 },
94111 {
95- name : "invalid JSON from server (RetrievePresignedUploadURL step)" ,
96- readings : defaultDataReadings ,
97- opts : dataupload.Options {ClusterName : "invalid-json-retrieve-presigned" },
112+ name : "invalid JSON from server (RetrievePresignedUploadURL step)" ,
113+ readings : []* api.DataReading {
114+ genArkNamespacesDataReading ("invalid-json-retrieve-presigned" ),
115+ },
98116 authenticate : setToken ("success-token" ),
99117 requireFn : func (t * testing.T , err error ) {
100118 require .ErrorContains (t , err , "while retrieving snapshot upload URL: rejecting JSON response from server as it was too large or was truncated" )
101119 },
102120 },
103121 {
104- name : "500 from server (RetrievePresignedUploadURL step)" ,
105- readings : defaultDataReadings ,
106- opts : dataupload.Options {ClusterName : "invalid-response-post-data" },
122+ name : "500 from server (RetrievePresignedUploadURL step)" ,
123+ readings : []* api.DataReading {
124+ genArkNamespacesDataReading ("invalid-response-post-data" ),
125+ },
107126 authenticate : setToken ("success-token" ),
108127 requireFn : func (t * testing.T , err error ) {
109128 require .ErrorContains (t , err , "while retrieving snapshot upload URL: received response with status code 500: mock error" )
@@ -128,22 +147,22 @@ func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI(t *testing.T) {
128147 cyberArkClient , err := dataupload .NewCyberArkClient (certPool , server .Server .URL , tc .authenticate )
129148 require .NoError (t , err )
130149
131- err = cyberArkClient .PostDataReadingsWithOptions (ctx , tc .readings , tc . opts )
150+ err = cyberArkClient .PostDataReadings (ctx , tc .readings )
132151 tc .requireFn (t , err )
133152 })
134153 }
135154}
136155
137- // TestCyberArkClient_PostDataReadingsWithOptions_RealAPI demonstrates that the dataupload code works with the real inventory API.
156+ // TestCyberArkClient_PostDataReadings_RealAPI demonstrates that the dataupload code works with the real inventory API.
138157// An API token is obtained by authenticating with the ARK_USERNAME and ARK_SECRET from the environment.
139158// ARK_SUBDOMAIN should be your tenant subdomain.
140159// ARK_PLATFORM_DOMAIN should be either integration-cyberark.cloud or cyberark.cloud
141160//
142161// To enable verbose request logging:
143162//
144163// go test ./pkg/internal/cyberark/dataupload/... \
145- // -v -count 1 -run TestCyberArkClient_PostDataReadingsWithOptions_RealAPI -args -testing.v 6
146- func TestCyberArkClient_PostDataReadingsWithOptions_RealAPI (t * testing.T ) {
164+ // -v -count 1 -run TestCyberArkClient_PostDataReadings_RealAPI -args -testing.v 6
165+ func TestCyberArkClient_PostDataReadings_RealAPI (t * testing.T ) {
147166 platformDomain := os .Getenv ("ARK_PLATFORM_DOMAIN" )
148167 subdomain := os .Getenv ("ARK_SUBDOMAIN" )
149168 username := os .Getenv ("ARK_USERNAME" )
@@ -183,12 +202,9 @@ func TestCyberArkClient_PostDataReadingsWithOptions_RealAPI(t *testing.T) {
183202 require .NoError (t , err )
184203
185204 dataReadings := testutil .ParseDataReadings (t , testutil .ReadGZIP (t , "testdata/example-1/datareadings.json.gz" ))
186- err = cyberArkClient .PostDataReadingsWithOptions (
205+ err = cyberArkClient .PostDataReadings (
187206 ctx ,
188207 dataReadings ,
189- dataupload.Options {
190- ClusterName : "bb068932-c80d-460d-88df-34bc7f3f3297" ,
191- },
192208 )
193209 require .NoError (t , err )
194210}
0 commit comments