Skip to content

Commit 03a2bf7

Browse files
committed
WIP: Create a mock api testing package
Signed-off-by: Richard Wall <[email protected]>
1 parent 1209e4b commit 03a2bf7

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

pkg/internal/cyberark/dataupload/dataupload.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@ import (
1616
"k8s.io/client-go/transport"
1717

1818
"github.com/jetstack/preflight/api"
19+
"github.com/jetstack/preflight/pkg/internal/cyberark"
1920
"github.com/jetstack/preflight/pkg/version"
2021
)
2122

2223
const (
2324
// maxRetrievePresignedUploadURLBodySize is the maximum allowed size for a response body from the
2425
// Retrieve Presigned Upload URL service.
2526
maxRetrievePresignedUploadURLBodySize = 10 * 1024
26-
27-
// apiPathSnapshotLinks is the URL path of the snapshot-links endpoint of the inventory API.
28-
// This endpoint returns an AWS presigned URL.
29-
// TODO(wallrj): Link to CyberArk API documentation when it is published.
30-
apiPathSnapshotLinks = "/api/ingestions/kubernetes/snapshot-links"
3127
)
3228

3329
type CyberArkClient struct {
@@ -114,7 +110,7 @@ func (c *CyberArkClient) PostDataReadingsWithOptions(ctx context.Context, payloa
114110
}
115111

116112
func (c *CyberArkClient) retrievePresignedUploadURL(ctx context.Context, checksum string, opts Options) (string, error) {
117-
uploadURL, err := url.JoinPath(c.baseURL, apiPathSnapshotLinks)
113+
uploadURL, err := url.JoinPath(c.baseURL, cyberark.EndpointSnapshotLinks)
118114
if err != nil {
119115
return "", err
120116
}

pkg/internal/cyberark/dataupload/dataupload_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dataupload_test
1+
package dataupload
22

33
import (
44
"crypto/x509"
@@ -11,13 +11,13 @@ import (
1111

1212
"github.com/stretchr/testify/require"
1313
"k8s.io/klog/v2"
14-
"k8s.io/klog/v2/ktesting"
1514

1615
"github.com/jetstack/preflight/api"
17-
"github.com/jetstack/preflight/pkg/internal/cyberark/dataupload"
1816
"github.com/jetstack/preflight/pkg/internal/cyberark/identity"
1917
"github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
18+
cyberarktesting "github.com/jetstack/preflight/pkg/internal/cyberark/testing"
2019

20+
"k8s.io/klog/v2/ktesting"
2121
_ "k8s.io/klog/v2/ktesting/init"
2222
)
2323

@@ -39,7 +39,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
3939
},
4040
},
4141
}
42-
defaultOpts := dataupload.Options{
42+
defaultOpts := Options{
4343
ClusterName: "success-cluster-id",
4444
}
4545

@@ -54,7 +54,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
5454
name string
5555
payload api.DataReadingsPost
5656
authenticate func(req *http.Request) error
57-
opts dataupload.Options
57+
opts Options
5858
requireFn func(t *testing.T, err error)
5959
}{
6060
{
@@ -69,7 +69,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
6969
{
7070
name: "error when cluster name is empty",
7171
payload: defaultPayload,
72-
opts: dataupload.Options{ClusterName: ""},
72+
opts: Options{ClusterName: ""},
7373
authenticate: setToken("success-token"),
7474
requireFn: func(t *testing.T, err error) {
7575
require.ErrorContains(t, err, "programmer mistake: the cluster name")
@@ -87,7 +87,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
8787
{
8888
name: "invalid JSON from server (RetrievePresignedUploadURL step)",
8989
payload: defaultPayload,
90-
opts: dataupload.Options{ClusterName: "invalid-json-retrieve-presigned"},
90+
opts: Options{ClusterName: "invalid-json-retrieve-presigned"},
9191
authenticate: setToken("success-token"),
9292
requireFn: func(t *testing.T, err error) {
9393
require.ErrorContains(t, err, "while retrieving snapshot upload URL: rejecting JSON response from server as it was too large or was truncated")
@@ -96,7 +96,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
9696
{
9797
name: "500 from server (RetrievePresignedUploadURL step)",
9898
payload: defaultPayload,
99-
opts: dataupload.Options{ClusterName: "invalid-response-post-data"},
99+
opts: Options{ClusterName: "invalid-response-post-data"},
100100
authenticate: setToken("success-token"),
101101
requireFn: func(t *testing.T, err error) {
102102
require.ErrorContains(t, err, "while retrieving snapshot upload URL: received response with status code 500: mock error")
@@ -106,7 +106,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
106106

107107
for _, tc := range tests {
108108
t.Run(tc.name, func(t *testing.T) {
109-
server := dataupload.MockDataUploadServer()
109+
server := cyberarktesting.MockDataUploadServer()
110110
defer server.Close()
111111

112112
certPool := x509.NewCertPool()
@@ -115,7 +115,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
115115
Bytes: server.Server.TLS.Certificates[0].Certificate[0],
116116
})))
117117

118-
cyberArkClient, err := dataupload.NewCyberArkClient(certPool, server.Server.URL, tc.authenticate)
118+
cyberArkClient, err := NewCyberArkClient(certPool, server.Server.URL, tc.authenticate)
119119
require.NoError(t, err)
120120

121121
err = cyberArkClient.PostDataReadingsWithOptions(t.Context(), tc.payload, tc.opts)
@@ -169,10 +169,10 @@ func TestPostDataReadingsWithOptionsWithRealAPI(t *testing.T) {
169169
err = identityClient.LoginUsernamePassword(ctx, username, []byte(secret))
170170
require.NoError(t, err)
171171

172-
cyberArkClient, err := dataupload.NewCyberArkClient(nil, serviceURL, identityClient.AuthenticateRequest)
172+
cyberArkClient, err := NewCyberArkClient(nil, serviceURL, identityClient.AuthenticateRequest)
173173
require.NoError(t, err)
174174

175-
err = cyberArkClient.PostDataReadingsWithOptions(ctx, api.DataReadingsPost{}, dataupload.Options{
175+
err = cyberArkClient.PostDataReadingsWithOptions(ctx, api.DataReadingsPost{}, Options{
176176
ClusterName: "bb068932-c80d-460d-88df-34bc7f3f3297",
177177
})
178178
require.NoError(t, err)

pkg/internal/cyberark/endpoints.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package cyberark
2+
3+
const (
4+
// EndpointSnapshotLinks is the URL path of the snapshot-links endpoint of the inventory API.
5+
// This endpoint returns an AWS presigned URL.
6+
// TODO(wallrj): Link to CyberArk API documentation when it is published.
7+
EndpointSnapshotLinks = "/api/ingestions/kubernetes/snapshot-links"
8+
)
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dataupload
1+
package testing
22

33
import (
44
"crypto/sha256"
@@ -9,9 +9,8 @@ import (
99
"net/http"
1010
"net/http/httptest"
1111

12+
"github.com/jetstack/preflight/pkg/internal/cyberark"
1213
"github.com/jetstack/preflight/pkg/version"
13-
14-
_ "embed"
1514
)
1615

1716
const (
@@ -37,7 +36,7 @@ func (mds *mockDataUploadServer) Close() {
3736

3837
func (mds *mockDataUploadServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3938
switch r.URL.Path {
40-
case apiPathSnapshotLinks:
39+
case cyberark.EndpointSnapshotLinks:
4140
mds.handleSnapshotLinks(w, r)
4241
return
4342
case "/presigned-upload":

0 commit comments

Comments
 (0)