@@ -8,9 +8,10 @@ import (
8
8
"net/http"
9
9
"os"
10
10
"testing"
11
- "time"
12
11
13
12
"github.com/stretchr/testify/require"
13
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
14
+ "k8s.io/apimachinery/pkg/types"
14
15
"k8s.io/klog/v2"
15
16
"k8s.io/klog/v2/ktesting"
16
17
@@ -23,20 +24,41 @@ import (
23
24
_ "k8s.io/klog/v2/ktesting/init"
24
25
)
25
26
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
+ },
35
55
},
56
+ SchemaVersion : "v1" ,
36
57
}
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" ),
40
62
}
41
63
42
64
setToken := func (token string ) func (* http.Request ) error {
@@ -50,31 +72,27 @@ func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI(t *testing.T) {
50
72
name string
51
73
readings []* api.DataReading
52
74
authenticate func (req * http.Request ) error
53
- opts dataupload.Options
54
75
requireFn func (t * testing.T , err error )
55
76
}{
56
77
{
57
78
name : "successful upload" ,
58
79
readings : defaultDataReadings ,
59
- opts : defaultOpts ,
60
80
authenticate : setToken ("success-token" ),
61
81
requireFn : func (t * testing.T , err error ) {
62
82
require .NoError (t , err )
63
83
},
64
84
},
65
85
{
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 ,
69
88
authenticate : setToken ("success-token" ),
70
89
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 " )
72
91
},
73
92
},
74
93
{
75
94
name : "error when bearer token is incorrect" ,
76
95
readings : defaultDataReadings ,
77
- opts : defaultOpts ,
78
96
authenticate : setToken ("fail-token" ),
79
97
requireFn : func (t * testing.T , err error ) {
80
98
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) {
83
101
{
84
102
name : "error contains authenticate error" ,
85
103
readings : defaultDataReadings ,
86
- opts : defaultOpts ,
87
104
authenticate : func (_ * http.Request ) error {
88
105
return errors .New ("simulated-authenticate-error" )
89
106
},
@@ -92,18 +109,20 @@ func TestCyberArkClient_PostDataReadingsWithOptions_MockAPI(t *testing.T) {
92
109
},
93
110
},
94
111
{
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
+ },
98
116
authenticate : setToken ("success-token" ),
99
117
requireFn : func (t * testing.T , err error ) {
100
118
require .ErrorContains (t , err , "while retrieving snapshot upload URL: rejecting JSON response from server as it was too large or was truncated" )
101
119
},
102
120
},
103
121
{
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
+ },
107
126
authenticate : setToken ("success-token" ),
108
127
requireFn : func (t * testing.T , err error ) {
109
128
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) {
128
147
cyberArkClient , err := dataupload .NewCyberArkClient (certPool , server .Server .URL , tc .authenticate )
129
148
require .NoError (t , err )
130
149
131
- err = cyberArkClient .PostDataReadingsWithOptions (ctx , tc .readings , tc . opts )
150
+ err = cyberArkClient .PostDataReadings (ctx , tc .readings )
132
151
tc .requireFn (t , err )
133
152
})
134
153
}
135
154
}
136
155
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.
138
157
// An API token is obtained by authenticating with the ARK_USERNAME and ARK_SECRET from the environment.
139
158
// ARK_SUBDOMAIN should be your tenant subdomain.
140
159
// ARK_PLATFORM_DOMAIN should be either integration-cyberark.cloud or cyberark.cloud
141
160
//
142
161
// To enable verbose request logging:
143
162
//
144
163
// 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 ) {
147
166
platformDomain := os .Getenv ("ARK_PLATFORM_DOMAIN" )
148
167
subdomain := os .Getenv ("ARK_SUBDOMAIN" )
149
168
username := os .Getenv ("ARK_USERNAME" )
@@ -183,12 +202,9 @@ func TestCyberArkClient_PostDataReadingsWithOptions_RealAPI(t *testing.T) {
183
202
require .NoError (t , err )
184
203
185
204
dataReadings := testutil .ParseDataReadings (t , testutil .ReadGZIP (t , "testdata/example-1/datareadings.json.gz" ))
186
- err = cyberArkClient .PostDataReadingsWithOptions (
205
+ err = cyberArkClient .PostDataReadings (
187
206
ctx ,
188
207
dataReadings ,
189
- dataupload.Options {
190
- ClusterName : "bb068932-c80d-460d-88df-34bc7f3f3297" ,
191
- },
192
208
)
193
209
require .NoError (t , err )
194
210
}
0 commit comments