diff --git a/label_checker_test.go b/label_checker_test.go index e9c285c9..789324f8 100644 --- a/label_checker_test.go +++ b/label_checker_test.go @@ -2,15 +2,25 @@ package test import ( "bytes" + "flag" "fmt" "io/ioutil" + "log" "os" + "os/exec" "path/filepath" "testing" + "github.com/agilepathway/label-checker/internal/error/panic" "github.com/magefile/mage/mage" ) +//nolint: gochecknoglobals +var integration = flag.Bool( + "integration", + false, + "Make calls to real external services. Requires INPUT_REPO_TOKEN environment variable.") + // nolint: lll const ( EnvGitHubRepository = "GITHUB_REPOSITORY" @@ -19,6 +29,7 @@ const ( EnvRequireNoneOf = "INPUT_NONE_OF" EnvRequireAllOf = "INPUT_ALL_OF" EnvRequireAnyOf = "INPUT_ANY_OF" + EnvHTTPSProxy = "HTTPS_PROXY" GitHubTestRepo = "agilepathway/test-label-checker-consumer" NoLabelsPR = 1 // https://github.com/agilepathway/test-label-checker-consumer/pull/1 OneLabelPR = 2 // https://github.com/agilepathway/test-label-checker-consumer/pull/2 @@ -27,6 +38,7 @@ const ( GitHubEventJSONDir = "testdata/temp" GitHubEventJSONFilename = "github_event.json" MagefileVerbose = "MAGEFILE_VERBOSE" + HoverflyProxyAddress = "127.0.0.1:8500" NeedNoneGotNone = "Label check successful: required none of major, minor, patch, and found 0.\n" NeedNoneGotOne = "Label check failed: required none of major, minor, patch, but found 1: minor\n" NeedNoneGotTwo = "Label check failed: required none of major, minor, patch, but found 2: minor, patch\n" @@ -119,10 +131,12 @@ func TestSplit(t *testing.T) { } func TestMain(m *testing.M) { + flag.Parse() os.Mkdir(GitHubEventJSONDir, os.ModePerm) //nolint os.Setenv(EnvGitHubRepository, GitHubTestRepo) //nolint os.Setenv(EnvGitHubEventPath, gitHubEventFullPath()) //nolint os.Setenv(MagefileVerbose, "1") //nolint + setupVirtualServicesIfNotInIntegrationMode() os.Exit(testMainWrapper(m)) } @@ -133,11 +147,48 @@ func testMainWrapper(m *testing.M) int { os.Unsetenv(EnvGitHubRepository) os.Unsetenv(EnvGitHubEventPath) os.Unsetenv(MagefileVerbose) + teardownVirtualServicesIfNotInIntegrationMode() }() return m.Run() } +func setupVirtualServicesIfNotInIntegrationMode() { + if !*integration { + startHoverflyInSpyMode() + os.Setenv(EnvHTTPSProxy, HoverflyProxyAddress) //nolint + importGitHubSimulations() + } +} + +func teardownVirtualServicesIfNotInIntegrationMode() { + if !*integration { + os.Unsetenv(EnvHTTPSProxy) //nolint + stopHoverfly() + } +} + +func execHoverCtl(arg ...string) { + // #nosec 204 https://github.com/securego/gosec/issues/343 + cmd := exec.Command("hoverctl", arg...) + stdout, err := cmd.Output() + panic.IfError(err) + log.Println(string(stdout)) +} + +func startHoverflyInSpyMode() { + execHoverCtl("start") + execHoverCtl("mode", "spy") +} + +func importGitHubSimulations() { + execHoverCtl("import", "./testdata/github_api.json") +} + +func stopHoverfly() { + execHoverCtl("stop") +} + func checkLabels() (int, *bytes.Buffer, *bytes.Buffer) { stdout := &bytes.Buffer{} stderr := &bytes.Buffer{} diff --git a/testdata/github_api.json b/testdata/github_api.json new file mode 100644 index 00000000..82f6edd3 --- /dev/null +++ b/testdata/github_api.json @@ -0,0 +1,463 @@ +{ + "data": { + "pairs": [ + { + "request": { + "path": [ + { + "matcher": "exact", + "value": "/graphql" + } + ], + "method": [ + { + "matcher": "exact", + "value": "POST" + } + ], + "destination": [ + { + "matcher": "exact", + "value": "api.github.com" + } + ], + "scheme": [ + { + "matcher": "exact", + "value": "https" + } + ], + "body": [ + { + "matcher": "json", + "value": "{\"query\":\"query($name:String!$owner:String!$pullRequestNumber:Int!){repository(owner: $owner, name: $name){pullRequest(number: $pullRequestNumber){labels(first: 100){nodes{name}}}}}\",\"variables\":{\"name\":\"test-label-checker-consumer\",\"owner\":\"agilepathway\",\"pullRequestNumber\":1}}\n" + } + ] + }, + "response": { + "status": 200, + "body": "H4sIAAAAAAAAA6tWSkksSVSyqlYqSi3IL84syS+qBPEKSnNyglILS1OLS0DcnMSk1JxiECsvPyUVyIiOrQUBABUCAvE/AAAA", + "encodedBody": true, + "headers": { + "Access-Control-Allow-Origin": [ + "*" + ], + "Access-Control-Expose-Headers": [ + "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset" + ], + "Cache-Control": [ + "no-cache" + ], + "Content-Encoding": [ + "gzip" + ], + "Content-Security-Policy": [ + "default-src 'none'" + ], + "Content-Type": [ + "application/json; charset=utf-8" + ], + "Date": [ + "Tue, 07 Jul 2020 06:01:34 GMT" + ], + "Hoverfly": [ + "Was-Here" + ], + "Referrer-Policy": [ + "origin-when-cross-origin, strict-origin-when-cross-origin" + ], + "Server": [ + "GitHub.com" + ], + "Status": [ + "200 OK" + ], + "Strict-Transport-Security": [ + "max-age=31536000; includeSubdomains; preload" + ], + "Vary": [ + "Accept-Encoding, Accept, X-Requested-With", + "Accept-Encoding" + ], + "X-Accepted-Oauth-Scopes": [ + "repo" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-Frame-Options": [ + "deny" + ], + "X-Github-Media-Type": [ + "github.v4; format=json" + ], + "X-Github-Request-Id": [ + "DD25:5656:CAF466:106A982:5F040FBE" + ], + "X-Oauth-Scopes": [ + "repo" + ], + "X-Ratelimit-Limit": [ + "5000" + ], + "X-Ratelimit-Remaining": [ + "4996" + ], + "X-Ratelimit-Reset": [ + "1594102919" + ], + "X-Xss-Protection": [ + "1; mode=block" + ] + }, + "templated": false + } + }, + { + "request": { + "path": [ + { + "matcher": "exact", + "value": "/graphql" + } + ], + "method": [ + { + "matcher": "exact", + "value": "POST" + } + ], + "destination": [ + { + "matcher": "exact", + "value": "api.github.com" + } + ], + "scheme": [ + { + "matcher": "exact", + "value": "https" + } + ], + "body": [ + { + "matcher": "json", + "value": "{\"query\":\"query($name:String!$owner:String!$pullRequestNumber:Int!){repository(owner: $owner, name: $name){pullRequest(number: $pullRequestNumber){labels(first: 100){nodes{name}}}}}\",\"variables\":{\"name\":\"test-label-checker-consumer\",\"owner\":\"agilepathway\",\"pullRequestNumber\":2}}\n" + } + ] + }, + "response": { + "status": 200, + "body": "H4sIAAAAAAAAAxXHMQ7AIAhA0btwDI/RtelAI4MJigIOjfHulT+9vyCjI6QFSl2suOgX1yfzRWOSeSzjS2yhJpkO7iOsBAlqaaKwnx39lLu4dU8AAAA=", + "encodedBody": true, + "headers": { + "Access-Control-Allow-Origin": [ + "*" + ], + "Access-Control-Expose-Headers": [ + "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset" + ], + "Cache-Control": [ + "no-cache" + ], + "Content-Encoding": [ + "gzip" + ], + "Content-Security-Policy": [ + "default-src 'none'" + ], + "Content-Type": [ + "application/json; charset=utf-8" + ], + "Date": [ + "Tue, 07 Jul 2020 06:01:37 GMT" + ], + "Hoverfly": [ + "Was-Here" + ], + "Referrer-Policy": [ + "origin-when-cross-origin, strict-origin-when-cross-origin" + ], + "Server": [ + "GitHub.com" + ], + "Status": [ + "200 OK" + ], + "Strict-Transport-Security": [ + "max-age=31536000; includeSubdomains; preload" + ], + "Vary": [ + "Accept-Encoding, Accept, X-Requested-With", + "Accept-Encoding" + ], + "X-Accepted-Oauth-Scopes": [ + "repo" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-Frame-Options": [ + "deny" + ], + "X-Github-Media-Type": [ + "github.v4; format=json" + ], + "X-Github-Request-Id": [ + "DD25:5656:CAF4F1:106A996:5F040FBE" + ], + "X-Oauth-Scopes": [ + "repo" + ], + "X-Ratelimit-Limit": [ + "5000" + ], + "X-Ratelimit-Remaining": [ + "4995" + ], + "X-Ratelimit-Reset": [ + "1594102919" + ], + "X-Xss-Protection": [ + "1; mode=block" + ] + }, + "templated": false + } + }, + { + "request": { + "path": [ + { + "matcher": "exact", + "value": "/graphql" + } + ], + "method": [ + { + "matcher": "exact", + "value": "POST" + } + ], + "destination": [ + { + "matcher": "exact", + "value": "api.github.com" + } + ], + "scheme": [ + { + "matcher": "exact", + "value": "https" + } + ], + "body": [ + { + "matcher": "json", + "value": "{\"query\":\"query($name:String!$owner:String!$pullRequestNumber:Int!){repository(owner: $owner, name: $name){pullRequest(number: $pullRequestNumber){labels(first: 100){nodes{name}}}}}\",\"variables\":{\"name\":\"test-label-checker-consumer\",\"owner\":\"agilepathway\",\"pullRequestNumber\":3}}\n" + } + ] + }, + "response": { + "status": 200, + "body": "H4sIAAAAAAAAA6tWSkksSVSyqlYqSi3IL84syS+qBPEKSnNyglILS1OLS0DcnMSk1JxiECsvPyUVyIgGshJzU5WslHIz8/KLlGp14AIFiSXJGUq1sbUgAAANI+uxYAAAAA==", + "encodedBody": true, + "headers": { + "Access-Control-Allow-Origin": [ + "*" + ], + "Access-Control-Expose-Headers": [ + "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset" + ], + "Cache-Control": [ + "no-cache" + ], + "Content-Encoding": [ + "gzip" + ], + "Content-Security-Policy": [ + "default-src 'none'" + ], + "Content-Type": [ + "application/json; charset=utf-8" + ], + "Date": [ + "Tue, 07 Jul 2020 06:01:54 GMT" + ], + "Hoverfly": [ + "Was-Here" + ], + "Referrer-Policy": [ + "origin-when-cross-origin, strict-origin-when-cross-origin" + ], + "Server": [ + "GitHub.com" + ], + "Status": [ + "200 OK" + ], + "Strict-Transport-Security": [ + "max-age=31536000; includeSubdomains; preload" + ], + "Vary": [ + "Accept-Encoding, Accept, X-Requested-With", + "Accept-Encoding" + ], + "X-Accepted-Oauth-Scopes": [ + "repo" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-Frame-Options": [ + "deny" + ], + "X-Github-Media-Type": [ + "github.v4; format=json" + ], + "X-Github-Request-Id": [ + "DD25:5656:CAF87A:106ADC2:5F040FCF" + ], + "X-Oauth-Scopes": [ + "repo" + ], + "X-Ratelimit-Limit": [ + "5000" + ], + "X-Ratelimit-Remaining": [ + "4990" + ], + "X-Ratelimit-Reset": [ + "1594102919" + ], + "X-Xss-Protection": [ + "1; mode=block" + ] + }, + "templated": false + } + }, + { + "request": { + "path": [ + { + "matcher": "exact", + "value": "/graphql" + } + ], + "method": [ + { + "matcher": "exact", + "value": "POST" + } + ], + "destination": [ + { + "matcher": "exact", + "value": "api.github.com" + } + ], + "scheme": [ + { + "matcher": "exact", + "value": "https" + } + ], + "body": [ + { + "matcher": "json", + "value": "{\"query\":\"query($name:String!$owner:String!$pullRequestNumber:Int!){repository(owner: $owner, name: $name){pullRequest(number: $pullRequestNumber){labels(first: 100){nodes{name}}}}}\",\"variables\":{\"name\":\"test-label-checker-consumer\",\"owner\":\"agilepathway\",\"pullRequestNumber\":4}}\n" + } + ] + }, + "response": { + "status": 200, + "body": "H4sIAAAAAAAAA1WIMQ6AIAwA/9LZF/AMV+NQpYmYShHKYAh/l07Gm+6ugUdFcA0yJSlBJT9WqTLPdFcqasm4ERezKJ6GLMPwInBw4SkZ+vSNEP8joe4H9LUbLyUQYixxAAAA", + "encodedBody": true, + "headers": { + "Access-Control-Allow-Origin": [ + "*" + ], + "Access-Control-Expose-Headers": [ + "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset" + ], + "Cache-Control": [ + "no-cache" + ], + "Content-Encoding": [ + "gzip" + ], + "Content-Security-Policy": [ + "default-src 'none'" + ], + "Content-Type": [ + "application/json; charset=utf-8" + ], + "Date": [ + "Tue, 07 Jul 2020 06:01:41 GMT" + ], + "Hoverfly": [ + "Was-Here" + ], + "Referrer-Policy": [ + "origin-when-cross-origin, strict-origin-when-cross-origin" + ], + "Server": [ + "GitHub.com" + ], + "Status": [ + "200 OK" + ], + "Strict-Transport-Security": [ + "max-age=31536000; includeSubdomains; preload" + ], + "Vary": [ + "Accept-Encoding, Accept, X-Requested-With", + "Accept-Encoding" + ], + "X-Accepted-Oauth-Scopes": [ + "repo" + ], + "X-Content-Type-Options": [ + "nosniff" + ], + "X-Frame-Options": [ + "deny" + ], + "X-Github-Media-Type": [ + "github.v4; format=json" + ], + "X-Github-Request-Id": [ + "DD25:5656:CAF5A1:106AA4E:5F040FC1" + ], + "X-Oauth-Scopes": [ + "repo" + ], + "X-Ratelimit-Limit": [ + "5000" + ], + "X-Ratelimit-Remaining": [ + "4994" + ], + "X-Ratelimit-Reset": [ + "1594102919" + ], + "X-Xss-Protection": [ + "1; mode=block" + ] + }, + "templated": false + } + } + ], + "globalActions": { + "delays": [], + "delaysLogNormal": [] + } + }, + "meta": { + "schemaVersion": "v5.1", + "hoverflyVersion": "v1.3.0", + "timeExported": "2020-07-07T13:03:21+07:00" + } +}