@@ -8,8 +8,11 @@ import (
8
8
"net/http"
9
9
"net/http/httptest"
10
10
"strings"
11
+ "testing"
11
12
"text/template"
12
13
14
+ "k8s.io/client-go/transport"
15
+
13
16
"github.com/jetstack/preflight/pkg/version"
14
17
15
18
_ "embed"
@@ -19,50 +22,51 @@ const (
19
22
// MockDiscoverySubdomain is the subdomain for which the MockDiscoveryServer will return a success response
20
23
MockDiscoverySubdomain = "venafi-test"
21
24
22
- defaultIdentityAPIURL = "https://ajp5871.id.integration-cyberark.cloud"
25
+ mockIdentityAPIURL = "https://ajp5871.id.integration-cyberark.cloud"
23
26
)
24
27
25
28
//go:embed testdata/discovery_success.json.template
26
29
var discoverySuccessTemplate string
27
30
28
31
type mockDiscoveryServer struct {
29
- Server * httptest.Server
30
-
32
+ t testing.TB
31
33
successResponse string
32
34
}
33
35
34
- // MockDiscoveryServer returns a mocked discovery server with a default value for the Identity API.
35
- // The returned server should be Closed by the caller after use.
36
- func MockDiscoveryServer () * mockDiscoveryServer {
37
- return MockDiscoveryServerWithCustomAPIURL (defaultIdentityAPIURL )
38
- }
39
-
40
- func MockDiscoveryServerWithCustomAPIURL (apiURL string ) * mockDiscoveryServer {
36
+ // MockDiscoveryServer starts a mocked CyberArk service discovery server and
37
+ // returns an HTTP client with the CA certs needed to connect to it.
38
+ //
39
+ // The URL of the mock server is set in the `ARK_DISCOVERY_API` environment
40
+ // variable, so any code using the `servicediscovery.Client` will use this mock
41
+ // server.
42
+ //
43
+ // The mock server will return a successful response when the subdomain is
44
+ // `MockDiscoverySubdomain`, and the identity API URL in that response will be
45
+ // `identityAPIURL`.
46
+ // Other subdomains, can be used to trigger various failure responses.
47
+ // The returned HTTP client has a transport which logs requests and responses
48
+ // depending on log level of the logger supplied in the context.
49
+ func MockDiscoveryServer (t testing.TB , identityAPIURL string ) * http.Client {
41
50
tmpl := template .Must (template .New ("mockDiscoverySuccess" ).Parse (discoverySuccessTemplate ))
42
-
43
51
buf := & bytes.Buffer {}
44
-
45
- err := tmpl .Execute (buf , struct { IdentityAPIURL string }{apiURL })
52
+ err := tmpl .Execute (buf , struct { IdentityAPIURL string }{identityAPIURL })
46
53
if err != nil {
47
54
panic (err )
48
55
}
49
-
50
56
mds := & mockDiscoveryServer {
57
+ t : t ,
51
58
successResponse : buf .String (),
52
59
}
53
-
54
- server := httptest .NewServer (mds )
55
-
56
- mds .Server = server
57
-
58
- return mds
59
- }
60
-
61
- func (mds * mockDiscoveryServer ) Close () {
62
- mds .Server .Close ()
60
+ server := httptest .NewTLSServer (mds )
61
+ t .Cleanup (server .Close )
62
+ t .Setenv ("ARK_DISCOVERY_API" , server .URL )
63
+ httpClient := server .Client ()
64
+ httpClient .Transport = transport .NewDebuggingRoundTripper (httpClient .Transport , transport .DebugByContext )
65
+ return httpClient
63
66
}
64
67
65
68
func (mds * mockDiscoveryServer ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
69
+ mds .t .Log (r .Method , r .RequestURI )
66
70
if r .Method != http .MethodGet {
67
71
// This was observed by making a POST request to the integration environment
68
72
// Normally, we'd expect 405 Method Not Allowed but we match the observed response here
0 commit comments