Skip to content

WIP: Create a mock api testing package #691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions pkg/internal/cyberark/dataupload/dataupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@ import (
"k8s.io/client-go/transport"

"github.com/jetstack/preflight/api"
"github.com/jetstack/preflight/pkg/internal/cyberark"
"github.com/jetstack/preflight/pkg/version"
)

const (
// maxRetrievePresignedUploadURLBodySize is the maximum allowed size for a response body from the
// Retrieve Presigned Upload URL service.
maxRetrievePresignedUploadURLBodySize = 10 * 1024

// apiPathSnapshotLinks is the URL path of the snapshot-links endpoint of the inventory API.
// This endpoint returns an AWS presigned URL.
// TODO(wallrj): Link to CyberArk API documentation when it is published.
apiPathSnapshotLinks = "/api/ingestions/kubernetes/snapshot-links"
)

type CyberArkClient struct {
Expand Down Expand Up @@ -114,7 +110,7 @@ func (c *CyberArkClient) PostDataReadingsWithOptions(ctx context.Context, payloa
}

func (c *CyberArkClient) retrievePresignedUploadURL(ctx context.Context, checksum string, opts Options) (string, error) {
uploadURL, err := url.JoinPath(c.baseURL, apiPathSnapshotLinks)
uploadURL, err := url.JoinPath(c.baseURL, cyberark.EndpointSnapshotLinks)
if err != nil {
return "", err
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/internal/cyberark/dataupload/dataupload_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dataupload_test
package dataupload

import (
"crypto/x509"
Expand All @@ -10,14 +10,14 @@
"time"

"github.com/stretchr/testify/require"
"k8s.io/klog/v2"

Check failure on line 13 in pkg/internal/cyberark/dataupload/dataupload_test.go

View workflow job for this annotation

GitHub Actions / verify

File is not properly formatted (gci)
"k8s.io/klog/v2/ktesting"

"github.com/jetstack/preflight/api"
"github.com/jetstack/preflight/pkg/internal/cyberark/dataupload"
"github.com/jetstack/preflight/pkg/internal/cyberark/identity"
"github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
cyberarktesting "github.com/jetstack/preflight/pkg/internal/cyberark/testing"

"k8s.io/klog/v2/ktesting"
_ "k8s.io/klog/v2/ktesting/init"
)

Expand All @@ -39,7 +39,7 @@
},
},
}
defaultOpts := dataupload.Options{
defaultOpts := Options{
ClusterName: "success-cluster-id",
}

Expand All @@ -54,7 +54,7 @@
name string
payload api.DataReadingsPost
authenticate func(req *http.Request) error
opts dataupload.Options
opts Options
requireFn func(t *testing.T, err error)
}{
{
Expand All @@ -69,7 +69,7 @@
{
name: "error when cluster name is empty",
payload: defaultPayload,
opts: dataupload.Options{ClusterName: ""},
opts: Options{ClusterName: ""},
authenticate: setToken("success-token"),
requireFn: func(t *testing.T, err error) {
require.ErrorContains(t, err, "programmer mistake: the cluster name")
Expand All @@ -87,7 +87,7 @@
{
name: "invalid JSON from server (RetrievePresignedUploadURL step)",
payload: defaultPayload,
opts: dataupload.Options{ClusterName: "invalid-json-retrieve-presigned"},
opts: Options{ClusterName: "invalid-json-retrieve-presigned"},
authenticate: setToken("success-token"),
requireFn: func(t *testing.T, err error) {
require.ErrorContains(t, err, "while retrieving snapshot upload URL: rejecting JSON response from server as it was too large or was truncated")
Expand All @@ -96,7 +96,7 @@
{
name: "500 from server (RetrievePresignedUploadURL step)",
payload: defaultPayload,
opts: dataupload.Options{ClusterName: "invalid-response-post-data"},
opts: Options{ClusterName: "invalid-response-post-data"},
authenticate: setToken("success-token"),
requireFn: func(t *testing.T, err error) {
require.ErrorContains(t, err, "while retrieving snapshot upload URL: received response with status code 500: mock error")
Expand All @@ -106,7 +106,7 @@

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
server := dataupload.MockDataUploadServer()
server := cyberarktesting.MockDataUploadServer()
defer server.Close()

certPool := x509.NewCertPool()
Expand All @@ -115,7 +115,7 @@
Bytes: server.Server.TLS.Certificates[0].Certificate[0],
})))

cyberArkClient, err := dataupload.NewCyberArkClient(certPool, server.Server.URL, tc.authenticate)
cyberArkClient, err := NewCyberArkClient(certPool, server.Server.URL, tc.authenticate)
require.NoError(t, err)

err = cyberArkClient.PostDataReadingsWithOptions(t.Context(), tc.payload, tc.opts)
Expand Down Expand Up @@ -169,10 +169,10 @@
err = identityClient.LoginUsernamePassword(ctx, username, []byte(secret))
require.NoError(t, err)

cyberArkClient, err := dataupload.NewCyberArkClient(nil, serviceURL, identityClient.AuthenticateRequest)
cyberArkClient, err := NewCyberArkClient(nil, serviceURL, identityClient.AuthenticateRequest)
require.NoError(t, err)

err = cyberArkClient.PostDataReadingsWithOptions(ctx, api.DataReadingsPost{}, dataupload.Options{
err = cyberArkClient.PostDataReadingsWithOptions(ctx, api.DataReadingsPost{}, Options{
ClusterName: "bb068932-c80d-460d-88df-34bc7f3f3297",
})
require.NoError(t, err)
Expand Down
8 changes: 8 additions & 0 deletions pkg/internal/cyberark/endpoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cyberark

const (
// EndpointSnapshotLinks is the URL path of the snapshot-links endpoint of the inventory API.
// This endpoint returns an AWS presigned URL.
// TODO(wallrj): Link to CyberArk API documentation when it is published.
EndpointSnapshotLinks = "/api/ingestions/kubernetes/snapshot-links"
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dataupload
package testing

import (
"crypto/sha256"
Expand All @@ -9,9 +9,8 @@ import (
"net/http"
"net/http/httptest"

"github.com/jetstack/preflight/pkg/internal/cyberark"
"github.com/jetstack/preflight/pkg/version"

_ "embed"
)

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

func (mds *mockDataUploadServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case apiPathSnapshotLinks:
case cyberark.EndpointSnapshotLinks:
mds.handleSnapshotLinks(w, r)
return
case "/presigned-upload":
Expand Down
Loading