@@ -6,15 +6,13 @@ import (
6
6
"net/http"
7
7
"os"
8
8
"testing"
9
- "time"
10
9
11
10
"github.com/jetstack/venafi-connection-lib/http_client"
12
11
"github.com/stretchr/testify/require"
13
12
"k8s.io/client-go/transport"
14
13
"k8s.io/klog/v2"
15
14
"k8s.io/klog/v2/ktesting"
16
15
17
- "github.com/jetstack/preflight/api"
18
16
"github.com/jetstack/preflight/pkg/internal/cyberark/dataupload"
19
17
"github.com/jetstack/preflight/pkg/internal/cyberark/identity"
20
18
"github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
@@ -23,28 +21,10 @@ import (
23
21
_ "k8s.io/klog/v2/ktesting/init"
24
22
)
25
23
26
- func TestCyberArkClient_PostDataReadingsWithOptions (t * testing.T ) {
27
- fakeTime := time .Unix (123 , 0 )
28
- defaultPayload := api.DataReadingsPost {
29
- AgentMetadata : & api.AgentMetadata {
30
- Version : "test-version" ,
31
- ClusterID : "test" ,
32
- },
33
- DataGatherTime : fakeTime ,
34
- DataReadings : []* api.DataReading {
35
- {
36
- ClusterID : "success-cluster-id" ,
37
- DataGatherer : "test-gatherer" ,
38
- Timestamp : api.Time {Time : fakeTime },
39
- Data : map [string ]interface {}{"test" : "data" },
40
- SchemaVersion : "v1" ,
41
- },
42
- },
43
- }
44
- defaultOpts := dataupload.Options {
45
- ClusterName : "success-cluster-id" ,
46
- }
47
-
24
+ // TestCyberArkClient_PutSnapshot_MockAPI tests the dataupload code against a
25
+ // mock API server. The mock server is configured to return different responses
26
+ // based on the cluster ID and bearer token used in the request.
27
+ func TestCyberArkClient_PutSnapshot_MockAPI (t * testing.T ) {
48
28
setToken := func (token string ) func (* http.Request ) error {
49
29
return func (req * http.Request ) error {
50
30
req .Header .Set ("Authorization" , fmt .Sprintf ("Bearer %s" , token ))
@@ -54,51 +34,60 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
54
34
55
35
tests := []struct {
56
36
name string
57
- payload api. DataReadingsPost
37
+ snapshot dataupload. Snapshot
58
38
authenticate func (req * http.Request ) error
59
- opts dataupload.Options
60
39
requireFn func (t * testing.T , err error )
61
40
}{
62
41
{
63
- name : "successful upload" ,
64
- payload : defaultPayload ,
65
- opts : defaultOpts ,
42
+ name : "successful upload" ,
43
+ snapshot : dataupload.Snapshot {
44
+ ClusterID : "success-cluster-id" ,
45
+ AgentVersion : "test-version" ,
46
+ },
66
47
authenticate : setToken ("success-token" ),
67
48
requireFn : func (t * testing.T , err error ) {
68
49
require .NoError (t , err )
69
50
},
70
51
},
71
52
{
72
- name : "error when cluster name is empty" ,
73
- payload : defaultPayload ,
74
- opts : dataupload.Options {ClusterName : "" },
53
+ name : "error when cluster ID is empty" ,
54
+ snapshot : dataupload.Snapshot {
55
+ ClusterID : "" ,
56
+ AgentVersion : "test-version" ,
57
+ },
75
58
authenticate : setToken ("success-token" ),
76
59
requireFn : func (t * testing.T , err error ) {
77
- require .ErrorContains (t , err , "programmer mistake: the cluster name " )
60
+ require .ErrorContains (t , err , "programmer mistake: the snapshot cluster ID cannot be left empty " )
78
61
},
79
62
},
80
63
{
81
- name : "error when bearer token is incorrect" ,
82
- payload : defaultPayload ,
83
- opts : defaultOpts ,
64
+ name : "error when bearer token is incorrect" ,
65
+ snapshot : dataupload.Snapshot {
66
+ ClusterID : "test" ,
67
+ AgentVersion : "test-version" ,
68
+ },
84
69
authenticate : setToken ("fail-token" ),
85
70
requireFn : func (t * testing.T , err error ) {
86
71
require .ErrorContains (t , err , "while retrieving snapshot upload URL: received response with status code 500: should authenticate using the correct bearer token" )
87
72
},
88
73
},
89
74
{
90
- name : "invalid JSON from server (RetrievePresignedUploadURL step)" ,
91
- payload : defaultPayload ,
92
- opts : dataupload.Options {ClusterName : "invalid-json-retrieve-presigned" },
75
+ name : "invalid JSON from server (RetrievePresignedUploadURL step)" ,
76
+ snapshot : dataupload.Snapshot {
77
+ ClusterID : "invalid-json-retrieve-presigned" ,
78
+ AgentVersion : "test-version" ,
79
+ },
93
80
authenticate : setToken ("success-token" ),
94
81
requireFn : func (t * testing.T , err error ) {
95
82
require .ErrorContains (t , err , "while retrieving snapshot upload URL: rejecting JSON response from server as it was too large or was truncated" )
96
83
},
97
84
},
98
85
{
99
- name : "500 from server (RetrievePresignedUploadURL step)" ,
100
- payload : defaultPayload ,
101
- opts : dataupload.Options {ClusterName : "invalid-response-post-data" },
86
+ name : "500 from server (RetrievePresignedUploadURL step)" ,
87
+ snapshot : dataupload.Snapshot {
88
+ ClusterID : "invalid-response-post-data" ,
89
+ AgentVersion : "test-version" ,
90
+ },
102
91
authenticate : setToken ("success-token" ),
103
92
requireFn : func (t * testing.T , err error ) {
104
93
require .ErrorContains (t , err , "while retrieving snapshot upload URL: received response with status code 500: mock error" )
@@ -115,13 +104,13 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
115
104
116
105
cyberArkClient := dataupload .New (httpClient , datauploadAPIBaseURL , tc .authenticate )
117
106
118
- err := cyberArkClient .PostDataReadingsWithOptions (ctx , tc .payload , tc . opts )
107
+ err := cyberArkClient .PutSnapshot (ctx , tc .snapshot )
119
108
tc .requireFn (t , err )
120
109
})
121
110
}
122
111
}
123
112
124
- // TestPostDataReadingsWithOptionsWithRealAPI demonstrates that the dataupload code works with the real inventory API.
113
+ // TestCyberArkClient_PutSnapshot_RealAPI demonstrates that the dataupload code works with the real inventory API.
125
114
// An API token is obtained by authenticating with the ARK_USERNAME and ARK_SECRET from the environment.
126
115
// ARK_SUBDOMAIN should be your tenant subdomain.
127
116
//
@@ -131,8 +120,8 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
131
120
// To enable verbose request logging:
132
121
//
133
122
// go test ./pkg/internal/cyberark/dataupload/... \
134
- // -v -count 1 -run TestPostDataReadingsWithOptionsWithRealAPI -args -testing.v 6
135
- func TestPostDataReadingsWithOptionsWithRealAPI (t * testing.T ) {
123
+ // -v -count 1 -run TestCyberArkClient_PutSnapshot_RealAPI -args -testing.v 6
124
+ func TestCyberArkClient_PutSnapshot_RealAPI (t * testing.T ) {
136
125
subdomain := os .Getenv ("ARK_SUBDOMAIN" )
137
126
username := os .Getenv ("ARK_USERNAME" )
138
127
secret := os .Getenv ("ARK_SECRET" )
@@ -159,8 +148,8 @@ func TestPostDataReadingsWithOptionsWithRealAPI(t *testing.T) {
159
148
require .NoError (t , err )
160
149
161
150
cyberArkClient := dataupload .New (httpClient , services .DiscoveryContext .API , identityClient .AuthenticateRequest )
162
- err = cyberArkClient .PostDataReadingsWithOptions (ctx , api. DataReadingsPost {}, dataupload.Options {
163
- ClusterName : "bb068932-c80d-460d-88df-34bc7f3f3297" ,
151
+ err = cyberArkClient .PutSnapshot (ctx , dataupload.Snapshot {
152
+ ClusterID : "bb068932-c80d-460d-88df-34bc7f3f3297" ,
164
153
})
165
154
require .NoError (t , err )
166
155
}
0 commit comments