-
Notifications
You must be signed in to change notification settings - Fork 841
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
[Internal] Add methods for configuring tests to work with test proxy #15259
Changes from 20 commits
43e875b
ce8dfa6
8b716cd
2d34f68
858f929
0e78180
abfd495
ae991cc
ab34658
464bb90
5170886
1bb884d
6355a03
ccf37a8
29bed05
f33a232
0ea12a0
def4c27
42e667a
0455394
0f87900
2921e52
66158cb
3de4936
5feef38
e752a6f
8ec2e2a
32c6570
297b899
ecc9563
7867ab1
1453a3d
2cee416
b3f7949
bd98736
9ca55c8
9421675
e2b0ded
6c3d638
b62e797
f40b00d
a009528
2c8e939
cdca98a
2d96846
1fd955b
d3719af
0a56f19
6aba571
a20591d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
}, | ||
{ | ||
"Name": "internal", | ||
"CoverageGoal": 0.83 | ||
"CoverageGoal": 0.90 | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -439,7 +439,7 @@ var modeMap = map[RecordMode]recorder.Mode{ | |
Playback: recorder.ModeReplaying, | ||
} | ||
|
||
var recordMode, _ = os.LookupEnv("AZURE_RECORD_MODE") | ||
var recordMode = os.Getenv("AZURE_RECORD_MODE") | ||
|
||
const ( | ||
modeRecording = "record" | ||
|
@@ -451,7 +451,7 @@ const ( | |
UpstreamUriHeader = "x-recording-upstream-base-uri" | ||
) | ||
|
||
var recordingId string | ||
var recordingIds = map[string]string{} | ||
|
||
var tr = &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
|
@@ -466,10 +466,6 @@ type RecordingOptions struct { | |
Scheme string | ||
} | ||
|
||
func resetRecordingId() { | ||
recordingId = "" | ||
} | ||
|
||
func defaultOptions() *RecordingOptions { | ||
return &RecordingOptions{ | ||
UseHTTPS: true, | ||
|
@@ -486,20 +482,22 @@ func (r RecordingOptions) HostScheme() string { | |
} | ||
|
||
func getTestId(pathToRecordings string, t *testing.T) string { | ||
if strings.HasSuffix(pathToRecordings, "/") { | ||
seankane-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pathToRecordings = strings.TrimRight(pathToRecordings, "/") | ||
} | ||
return pathToRecordings + "/recordings/" + t.Name() + ".json" | ||
seankane-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func StartRecording(t *testing.T, pathToRecordings string, options *RecordingOptions) error { | ||
richardpark-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if options == nil { | ||
options = defaultOptions() | ||
} | ||
if recordMode == "" { | ||
t.Log("AZURE_RECORD_MODE was not set, options are \"record\" or \"playback\". \nDefaulting to playback") | ||
recordMode = "playback" | ||
if !(recordMode == modeRecording || recordMode == modePlayback) { | ||
return fmt.Errorf("AZURE_RECORD_MODE was not understood, options are \"record\" or \"playback\". Received: %v", recordMode) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No biggie, but could '%s' and use the actual constants in your error message since you're already Errorf'ing. |
||
} | ||
testId := getTestId(pathToRecordings, t) | ||
seankane-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
url := fmt.Sprintf("%v/%v/start", options.HostScheme(), recordMode) | ||
url := fmt.Sprintf("%s/%s/start", options.HostScheme(), recordMode) | ||
|
||
req, err := http.NewRequest("POST", url, nil) | ||
if err != nil { | ||
|
@@ -512,13 +510,19 @@ func StartRecording(t *testing.T, pathToRecordings string, options *RecordingOpt | |
if err != nil { | ||
return err | ||
} | ||
recordingId = resp.Header.Get(IdHeader) | ||
recId := resp.Header.Get(IdHeader) | ||
if recId == "" { | ||
b, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
return fmt.Errorf("Recording ID was not returned by the response. Response body: %s", b) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: would it be helpful to say that the specific header was not found? Could aid in diagnostics if someone is trying to figure out what's wrong strictly by the error message. |
||
} | ||
recordingIds[t.Name()] = recId | ||
return nil | ||
} | ||
|
||
func StopRecording(t *testing.T, options *RecordingOptions) error { | ||
defer resetRecordingId() | ||
|
||
if options == nil { | ||
options = defaultOptions() | ||
} | ||
|
@@ -528,10 +532,12 @@ func StopRecording(t *testing.T, options *RecordingOptions) error { | |
if err != nil { | ||
return err | ||
} | ||
if recordingId == "" { | ||
var recId string | ||
var ok bool | ||
if recId, ok = recordingIds[t.Name()]; !ok { | ||
return errors.New("Recording ID was never set. Did you call StartRecording?") | ||
} | ||
req.Header.Set("x-recording-id", recordingId) | ||
req.Header.Set("x-recording-id", recId) | ||
_, err = client.Do(req) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
|
@@ -591,58 +597,51 @@ func LiveOnly(t *testing.T) { | |
|
||
// Function for sleeping during a test for `duration` seconds. This method will only execute when | ||
// AZURE_RECORD_MODE = "record", if a test is running in playback this will be a noop. | ||
func Sleep(duration int) { | ||
func Sleep(duration time.Duration) { | ||
if GetRecordMode() == modeRecording { | ||
time.Sleep(time.Duration(duration) * time.Second) | ||
time.Sleep(duration) | ||
} | ||
} | ||
|
||
func GetRecordingId() string { | ||
return recordingId | ||
func GetRecordingId(t *testing.T) string { | ||
return recordingIds[t.Name()] | ||
} | ||
|
||
func GetRecordMode() string { | ||
return recordMode | ||
} | ||
|
||
func InPlayback() bool { | ||
return GetRecordMode() == modePlayback | ||
} | ||
|
||
func InRecord() bool { | ||
return GetRecordMode() == modeRecording | ||
} | ||
|
||
func getRootCas() (*x509.CertPool, error) { | ||
func getRootCas(t *testing.T) (*x509.CertPool, error) { | ||
localFile, ok := os.LookupEnv("PROXY_CERT") | ||
|
||
rootCAs, err := x509.SystemCertPool() | ||
if err != nil { | ||
if err != nil && strings.Contains(err.Error(), "system root pool is not available on Windows") { | ||
rootCAs = x509.NewCertPool() | ||
} else if err != nil { | ||
return rootCAs, err | ||
} | ||
|
||
if !ok { | ||
fmt.Println("Could not find path to proxy certificate, set the environment variable 'PROXY_CERT' to the location of your certificate") | ||
t.Log("Could not find path to proxy certificate, set the environment variable 'PROXY_CERT' to the location of your certificate") | ||
return rootCAs, nil | ||
} | ||
|
||
cert, err := ioutil.ReadFile(localFile) | ||
if err != nil { | ||
fmt.Println("error opening cert file") | ||
return nil, err | ||
} | ||
|
||
if ok := rootCAs.AppendCertsFromPEM(cert); !ok { | ||
fmt.Println("No certs appended, using system certs only") | ||
t.Log("No certs appended, using system certs only") | ||
} | ||
|
||
return rootCAs, nil | ||
} | ||
|
||
func GetHTTPClient() (*http.Client, error) { | ||
func GetHTTPClient(t *testing.T) (*http.Client, error) { | ||
transport := http.DefaultTransport.(*http.Transport).Clone() | ||
|
||
rootCAs, err := getRootCas() | ||
rootCAs, err := getRootCas(t) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This here is a positive indicator. :)