Skip to content

Commit

Permalink
Enable tests to run without a GitHub token
Browse files Browse the repository at this point in the history
This means that we no longer need to provide a GitHub `INPUT_REPO_TOKEN`
environment variable when running the tests (fixes #85).

The tests now use Hoverfly[1] by default, which proxies the calls to the
GitHub API and returns simulated responses[2] instead.
Also used Hoverfly to capture[3] these simulated responses.

The tests can also be run with an -integration flag, which means they
will make calls to the real GitHub API, requring a GitHub
`INPUT_REPO_TOKEN` environment variable to have been set.

Steps to install Hoverfly locally (you must do this on your local
machine before you can run the tests):

1. Install Hoverfly[4]
2. Download the Hoverfly default cert[5]
3. Add and trust the Hoverfly default cert[6] (how to add and trust
   a cert)[7]

[1] https://docs.hoverfly.io/
[2] https://docs.hoverfly.io/en/latest/pages/keyconcepts/simulations/simulations.html
[3] https://docs.hoverfly.io/en/latest/pages/keyconcepts/modes/capture.html
[4] https://docs.hoverfly.io/en/latest/pages/introduction/downloadinstallation.html
[5] https://raw.githubusercontent.com/SpectoLabs/hoverfly/master/core/cert.pem
[6] https://docs.hoverfly.io/en/latest/pages/tutorials/advanced/configuressl/configuressl.html
[7] https://manuals.gfi.com/en/kerio/connect/content/server-configuration/ssl-certificates/adding-trusted-root-certificates-to-the-server-1605.html
  • Loading branch information
johnboyes committed Jul 16, 2020
1 parent 7e8da19 commit 81ee842
Show file tree
Hide file tree
Showing 2 changed files with 514 additions and 0 deletions.
51 changes: 51 additions & 0 deletions label_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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))
}

Expand All @@ -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{}
Expand Down
Loading

0 comments on commit 81ee842

Please sign in to comment.