Skip to content

Commit

Permalink
Rename Check => SmokeTest (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
symm authored Jul 2, 2017
1 parent 2c1308d commit 1a432fd
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Create a `Vapefile` file in the format:
]
```

then execute `vape http://your.domain` to run the checks
then execute `vape http://your.domain` to run the tests

## As a container

Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ func main() {
os.Exit(0)
}

statusCodeChecks, err := parseVapefile(*vapeFile)
smokeTests, err := parseVapefile(*vapeFile)
if err != nil {
log.Fatal(err)
}

checksLen := len(statusCodeChecks)
resCh, errCh := make(chan CheckResult, checksLen), make(chan error, checksLen)
testsLen := len(smokeTests)
resCh, errCh := make(chan SmokeTestResult, testsLen), make(chan error, testsLen)
vape := NewVape(DefaultClient, baseURL, resCh, errCh)
vape.Process(statusCodeChecks)
vape.Process(smokeTests)

for i := 0; i < checksLen; i++ {
for i := 0; i < testsLen; i++ {
select {
case res := <-resCh:
output := fmt.Sprintf("%s (expected: %d, actual: %d)", res.Check.URI, res.Check.ExpectedStatusCode, res.ActualStatusCode)
output := fmt.Sprintf("%s (expected: %d, actual: %d)", res.Test.URI, res.Test.ExpectedStatusCode, res.ActualStatusCode)
fmt.Println(parseOutput(output, res.Pass))
case err := <-errCh:
fmt.Println(err)
Expand Down
10 changes: 5 additions & 5 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import (
)

// parseVapefile reads a given Vapefile and returns the contents.
func parseVapefile(file string) (StatusCodeChecks, error) {
func parseVapefile(file string) (SmokeTests, error) {
raw, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}

var checks StatusCodeChecks
err = json.Unmarshal(raw, &checks)
var tests SmokeTests
err = json.Unmarshal(raw, &tests)
if err != nil {
return nil, err
}
return checks, nil
return tests, nil
}

// parseBaseURL checks a given URL is valid.
// parseBaseURL tests a given URL is valid.
func parseBaseURL(baseURL string) (*url.URL, error) {
u, err := url.Parse(baseURL)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func tmpFile(data string) (*os.File, func(), error) {
if err != nil {
return nil, nil, err
}
if _, err := tmpfile.Write([]byte(data)); err != nil {
if _, err = tmpfile.Write([]byte(data)); err != nil {
return nil, nil, err
}
if err = tmpfile.Close(); err != nil {
Expand Down
42 changes: 21 additions & 21 deletions vape.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ import (
"path"
)

// StatusCodeCheck contains a URI and expected status code.
type StatusCodeCheck struct {
// SmokeTest contains a URI and expected status code.
type SmokeTest struct {
URI string `json:"uri"`
ExpectedStatusCode int `json:"expected_status_code"`
}

// CheckResult is the result of a StatusCodeCheck.
type CheckResult struct {
Check StatusCodeCheck
// SmokeTestResult is the result of a SmokeTest.
type SmokeTestResult struct {
Test SmokeTest
ActualStatusCode int
Pass bool
}

// StatusCodeChecks is a slice of checks to perform.
type StatusCodeChecks []StatusCodeCheck
// SmokeTests is a slice of smoke tests to perform.
type SmokeTests []SmokeTest

// Vape contains dependencies used to run the application.
type Vape struct {
client HTTPClient
baseURL *url.URL
resCh chan CheckResult
resCh chan SmokeTestResult
errCh chan error
}

// NewVape builds a Vape from the given dependencies.
func NewVape(client HTTPClient, baseURL *url.URL, resCh chan CheckResult, errCh chan error) Vape {
func NewVape(client HTTPClient, baseURL *url.URL, resCh chan SmokeTestResult, errCh chan error) Vape {
return Vape{
client: client,
baseURL: baseURL,
Expand All @@ -40,35 +40,35 @@ func NewVape(client HTTPClient, baseURL *url.URL, resCh chan CheckResult, errCh
}

// Process takes a list of URIs and concurrently performs a smoke test on each.
func (v Vape) Process(statusCodeChecks StatusCodeChecks) {
func (v Vape) Process(SmokeTests SmokeTests) {
// TODO: limit the numer of concurrent requests so we don't DoS the server
go func() {
for _, check := range statusCodeChecks {
go func(check StatusCodeCheck) {
result, err := v.performCheck(check)
for _, test := range SmokeTests {
go func(test SmokeTest) {
result, err := v.performTest(test)
if err != nil {
v.errCh <- err
return
}
v.resCh <- result
}(check)
}(test)
}
}()
}

// performCheck checks the status code of a HTTP request of a given URI.
func (v Vape) performCheck(check StatusCodeCheck) (CheckResult, error) {
// performTest tests the status code of a HTTP request of a given URI.
func (v Vape) performTest(test SmokeTest) (SmokeTestResult, error) {
url := *v.baseURL
url.Path = path.Join(url.Path, check.URI)
url.Path = path.Join(url.Path, test.URI)

resp, err := v.client.Get(url.String())
if err != nil {
return CheckResult{}, err
return SmokeTestResult{}, err
}

return CheckResult{
return SmokeTestResult{
ActualStatusCode: resp.StatusCode,
Check: check,
Pass: check.ExpectedStatusCode == resp.StatusCode,
Test: test,
Pass: test.ExpectedStatusCode == resp.StatusCode,
}, nil
}
14 changes: 7 additions & 7 deletions vape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ func (m mockHTTPClient) Get(url string) (*http.Response, error) {

var httpClient = new(mockHTTPClient)

var check = StatusCodeCheck{
var test = SmokeTest{
URI: "test",
ExpectedStatusCode: 200,
}

func TestProcess(t *testing.T) {
resCh, errCh := make(chan CheckResult, 1), make(chan error, 1)
resCh, errCh := make(chan SmokeTestResult, 1), make(chan error, 1)
baseURL, err := url.Parse("http://base.url")
if err != nil {
t.Fatal(err)
Expand All @@ -34,7 +34,7 @@ func TestProcess(t *testing.T) {
httpClient.GetFunc = func(url string) (*http.Response, error) {
return nil, errors.New("HTTP error")
}
vape.Process(StatusCodeChecks{check})
vape.Process(SmokeTests{test})
select {
case <-resCh:
t.Error("expected to recieve on error chan, not result chan")
Expand All @@ -48,7 +48,7 @@ func TestProcess(t *testing.T) {
StatusCode: 200,
}, nil
}
vape.Process(StatusCodeChecks{check})
vape.Process(SmokeTests{test})
select {
case <-resCh:
case <-errCh:
Expand All @@ -57,7 +57,7 @@ func TestProcess(t *testing.T) {
})
}

func TestPerformCheck(t *testing.T) {
func TestPerformTest(t *testing.T) {
baseURL, err := url.Parse("http://base.url")
if err != nil {
t.Fatal(err)
Expand All @@ -69,7 +69,7 @@ func TestPerformCheck(t *testing.T) {
return nil, errors.New("HTTP error")
}

_, err := vape.performCheck(check)
_, err := vape.performTest(test)
if err == nil {
t.Error("expected error: 'HTTP error', got: nil")
}
Expand All @@ -82,7 +82,7 @@ func TestPerformCheck(t *testing.T) {
}, nil
}

result, err := vape.performCheck(check)
result, err := vape.performTest(test)
if err != nil {
t.Errorf("expected error: nil, got: %v", err)
}
Expand Down

0 comments on commit 1a432fd

Please sign in to comment.