@@ -8,8 +8,11 @@ import (
88 "net/http"
99 "net/http/httptest"
1010 "strings"
11+ "testing"
1112 "text/template"
1213
14+ "k8s.io/client-go/transport"
15+
1316 "github.com/jetstack/preflight/pkg/version"
1417
1518 _ "embed"
@@ -19,50 +22,51 @@ const (
1922 // MockDiscoverySubdomain is the subdomain for which the MockDiscoveryServer will return a success response
2023 MockDiscoverySubdomain = "venafi-test"
2124
22- defaultIdentityAPIURL = "https://ajp5871.id.integration-cyberark.cloud"
25+ mockIdentityAPIURL = "https://ajp5871.id.integration-cyberark.cloud"
2326)
2427
2528//go:embed testdata/discovery_success.json.template
2629var discoverySuccessTemplate string
2730
2831type mockDiscoveryServer struct {
29- Server * httptest.Server
30-
32+ t testing.TB
3133 successResponse string
3234}
3335
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 {
4150 tmpl := template .Must (template .New ("mockDiscoverySuccess" ).Parse (discoverySuccessTemplate ))
42-
4351 buf := & bytes.Buffer {}
44-
45- err := tmpl .Execute (buf , struct { IdentityAPIURL string }{apiURL })
52+ err := tmpl .Execute (buf , struct { IdentityAPIURL string }{identityAPIURL })
4653 if err != nil {
4754 panic (err )
4855 }
49-
5056 mds := & mockDiscoveryServer {
57+ t : t ,
5158 successResponse : buf .String (),
5259 }
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
6366}
6467
6568func (mds * mockDiscoveryServer ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
69+ mds .t .Log (r .Method , r .RequestURI )
6670 if r .Method != http .MethodGet {
6771 // This was observed by making a POST request to the integration environment
6872 // Normally, we'd expect 405 Method Not Allowed but we match the observed response here
0 commit comments