grim/resticide

first round of linting
develop
2016-07-09, Gary Kramlich
63fa19125098
Parents d7fe9754718f
Children cae66556bb62
first round of linting
--- a/loader/json.go Sat Jul 09 13:35:45 2016 -0500
+++ b/loader/json.go Sat Jul 09 14:08:24 2016 -0500
@@ -162,7 +162,7 @@
}
if json, found := req["json"]; found {
- tr.Json = json
+ tr.JSON = json
}
// parse the response
--- a/main.go Sat Jul 09 13:35:45 2016 -0500
+++ b/main.go Sat Jul 09 14:08:24 2016 -0500
@@ -12,7 +12,7 @@
)
func main() {
- test_dir := flag.String("path", "resticide", "The path to search for tests")
+ testDir := flag.String("path", "resticide", "The path to search for tests")
verbose := flag.Bool("verbose", false, "Whether or not to use verbose output")
host := flag.String("host", "", "The hostname to use")
workers := flag.Int("workers", runtime.NumCPU(), "How many workers to use to run tests")
@@ -33,7 +33,7 @@
report := reporter.NewReporter(*verbose)
report.AddHandler(new(reporter.ConsoleReporter))
- tests, err := loader.LoadTests(*test_dir, report)
+ tests, err := loader.LoadTests(*testDir, report)
if err != nil {
fmt.Printf("error %s\n", err.Error())
--- a/reporter/console_reporter.go Sat Jul 09 13:35:45 2016 -0500
+++ b/reporter/console_reporter.go Sat Jul 09 14:08:24 2016 -0500
@@ -46,9 +46,9 @@
writer := new(tabwriter.Writer)
writer.Init(os.Stdout, 2, 2, 2, ' ', 0)
- fmt.Fprintf(writer, "\tStatus Code\t%d\n", res.HttpResponse.StatusCode)
+ fmt.Fprintf(writer, "\tStatus Code\t%d\n", res.HTTPResponse.StatusCode)
fmt.Fprintln(writer, "\tHeaders:")
- for name, value := range res.HttpResponse.Header {
+ for name, value := range res.HTTPResponse.Header {
fmt.Fprintf(writer, "\t\t%s\t%s\n", name, strings.Join(value, ";"))
}
--- a/runner.go Sat Jul 09 13:35:45 2016 -0500
+++ b/runner.go Sat Jul 09 14:08:24 2016 -0500
@@ -8,62 +8,63 @@
"bitbucket.org/rw_grim/resticide/test"
)
-func _test_worker(worker_chan chan test.Test, result_chan chan *test.Result, wait_group *sync.WaitGroup, url *url.URL, reporter *reporter.Reporter) {
- defer wait_group.Done()
+func testWorker(workerChan chan test.Test, resultChan chan *test.Result, waitGroup *sync.WaitGroup, url *url.URL, reporter *reporter.Reporter) {
+ defer waitGroup.Done()
- for test := range worker_chan {
+ for test := range workerChan {
reporter.TestStart(test)
res := test.Run(url)
reporter.TestFinish(test, *res)
- result_chan <- res
+ resultChan <- res
}
}
-func RunTests(tests []test.Test, url *url.URL, n_workers int, reporter *reporter.Reporter) int {
- n_tests := len(tests)
+// RunTests will run all of the given test with the given arguments
+func RunTests(tests []test.Test, url *url.URL, nWorkers int, reporter *reporter.Reporter) int {
+ nTests := len(tests)
passed := 0
- reporter.Start(n_tests)
+ reporter.Start(nTests)
- worker_chan := make(chan test.Test)
- worker_wg := new(sync.WaitGroup)
- result_chan := make(chan *test.Result)
- result_wg := new(sync.WaitGroup)
+ workerChan := make(chan test.Test)
+ workerWg := new(sync.WaitGroup)
+ resultChan := make(chan *test.Result)
+ resultWg := new(sync.WaitGroup)
// create our result processor
- result_wg.Add(1)
- go func(result_wg *sync.WaitGroup, result_chan chan *test.Result) {
- defer result_wg.Done()
+ resultWg.Add(1)
+ go func(resultWg *sync.WaitGroup, resultChan chan *test.Result) {
+ defer resultWg.Done()
- for result := range result_chan {
+ for result := range resultChan {
if result.Passed {
passed++
}
}
- }(result_wg, result_chan)
+ }(resultWg, resultChan)
// create our workers
- for i := 0; i < n_workers; i++ {
- worker_wg.Add(1)
- go _test_worker(worker_chan, result_chan, worker_wg, url, reporter)
+ for i := 0; i < nWorkers; i++ {
+ workerWg.Add(1)
+ go testWorker(workerChan, resultChan, workerWg, url, reporter)
}
// add each test to the channel
for _, test := range tests {
- worker_chan <- test
+ workerChan <- test
}
// clean up the channel and wait for all the workers to finish
- close(worker_chan)
- worker_wg.Wait()
+ close(workerChan)
+ workerWg.Wait()
- close(result_chan)
- result_wg.Wait()
+ close(resultChan)
+ resultWg.Wait()
- reporter.Finish(n_tests, passed, n_tests-passed)
+ failed := nTests - passed
- failed := n_tests - passed
+ reporter.Finish(nTests, passed, failed)
return failed
}
--- a/test/request.go Sat Jul 09 13:35:45 2016 -0500
+++ b/test/request.go Sat Jul 09 14:08:24 2016 -0500
@@ -20,6 +20,7 @@
"gopkg.in/xeipuuv/gojsonschema.v0"
)
+// Request represents a test request
type Request struct {
Path string
Query map[string]string
@@ -29,14 +30,14 @@
FormData map[string]FormData
URLEncoded map[string]string
Binary string
- Json interface{}
+ JSON interface{}
Response Response
}
func (req *Request) buildBody(test *Test) (io.Reader, string) {
body := &bytes.Buffer{}
- content_type := "application/html"
+ contentType := "application/html"
if len(req.Body) > 0 {
body.WriteString(req.Body)
@@ -67,7 +68,7 @@
}
}
- content_type = writer.FormDataContentType()
+ contentType = writer.FormDataContentType()
} else if len(req.URLEncoded) > 0 {
values := url.Values{}
@@ -76,57 +77,57 @@
}
body.WriteString(values.Encode())
- content_type = "application/x-www-form-urlencoded"
+ contentType = "application/x-www-form-urlencoded"
} else if len(req.Binary) > 0 {
dirname := filepath.Dir(test.Filename)
filename := filepath.Join(dirname, req.Binary)
- file_type := mime.TypeByExtension(filepath.Ext(req.Binary))
+ fileType := mime.TypeByExtension(filepath.Ext(req.Binary))
- if len(file_type) > 0 {
- content_type = file_type
+ if len(fileType) > 0 {
+ contentType = fileType
} else {
- content_type = "application/octet-stream"
+ contentType = "application/octet-stream"
}
// This is gross and needs to be cleaned up too..
file, _ := os.Open(filename)
defer file.Close()
io.Copy(body, file)
- } else if req.Json != nil {
- data, _ := json.Marshal(req.Json)
+ } else if req.JSON != nil {
+ data, _ := json.Marshal(req.JSON)
body.WriteString(string(data))
- content_type = "application/json"
+ contentType = "application/json"
} else {
body.WriteString("")
- content_type = ""
+ contentType = ""
}
- return body, content_type
+ return body, contentType
}
func (req *Request) buildRequest(test *Test, url *url.URL) (*http.Request, error) {
- var test_url = *url
+ var testURL = *url
- test_url.Path = req.Path
+ testURL.Path = req.Path
// build the query string
- query := test_url.Query()
+ query := testURL.Query()
for qname, qvalue := range req.Query {
query.Set(qname, qvalue)
}
- test_url.RawQuery = query.Encode()
+ testURL.RawQuery = query.Encode()
- body, content_type := req.buildBody(test)
+ body, contentType := req.buildBody(test)
- hreq, err := http.NewRequest(req.Method, test_url.String(), body)
+ hreq, err := http.NewRequest(req.Method, testURL.String(), body)
if err != nil {
return nil, err
}
// set the Content-Type we determined when building the body
- if len(content_type) > 0 {
- hreq.Header.Add("Content-Type", content_type)
+ if len(contentType) > 0 {
+ hreq.Header.Add("Content-Type", contentType)
}
for name, values := range req.Headers {
@@ -166,21 +167,21 @@
}
// read the body into a byte array
- body_byte, err := ioutil.ReadAll(hresp.Body)
+ bodyByte, err := ioutil.ReadAll(hresp.Body)
if err != nil {
return false, err
}
// check if we have a file block and check it
if len(req.Response.File) > 0 {
- if sha256_digest, ok := req.Response.File["sha256"]; ok {
- sum := sha256.Sum256(body_byte)
+ if sha256Digest, ok := req.Response.File["sha256"]; ok {
+ sum := sha256.Sum256(bodyByte)
actual := fmt.Sprintf("%x", sum)
- if actual != sha256_digest {
+ if actual != sha256Digest {
err := fmt.Errorf(
"Expected file to have a SHA256 of '%s' but received '%s'",
- sha256_digest,
+ sha256Digest,
actual,
)
@@ -188,14 +189,14 @@
}
}
- if md5_digest, ok := req.Response.File["md5"]; ok {
- sum := md5.Sum(body_byte)
+ if md5Digest, ok := req.Response.File["md5"]; ok {
+ sum := md5.Sum(bodyByte)
actual := fmt.Sprintf("%x", sum)
- if actual != md5_digest {
+ if actual != md5Digest {
err := fmt.Errorf(
"Expected file to have a MD5 of '%s' but received '%s'",
- md5_digest,
+ md5Digest,
actual,
)
@@ -209,31 +210,31 @@
return false, err
}
- _, err = file.Write(body_byte)
+ _, err = file.Write(bodyByte)
if err != nil {
return false, err
}
}
} else {
- body := string(body_byte[:])
+ body := string(bodyByte[:])
// check if we have a json schema to validate
if len(req.Response.JSONSchema) != 0 {
- schema_loader := gojsonschema.NewGoLoader(req.Response.JSONSchema)
- body_loader := gojsonschema.NewStringLoader(body)
+ schemaLoader := gojsonschema.NewGoLoader(req.Response.JSONSchema)
+ bodyLoader := gojsonschema.NewStringLoader(body)
- schema_result, err := gojsonschema.Validate(schema_loader, body_loader)
+ schemaResult, err := gojsonschema.Validate(schemaLoader, bodyLoader)
if err != nil {
return false, err
}
- if schema_result.Valid() != true {
+ if schemaResult.Valid() != true {
errors := []string{}
- for _, err := range schema_result.Errors() {
+ for _, err := range schemaResult.Errors() {
errors = append(errors, err.Description())
}
- err_msg := strings.Join(errors, "\n")
- err := fmt.Errorf("Schema validation failed:\n%s", err_msg)
+ errMsg := strings.Join(errors, "\n")
+ err := fmt.Errorf("Schema validation failed:\n%s", errMsg)
return false, err
}
--- a/test/request_test.go Sat Jul 09 13:35:45 2016 -0500
+++ b/test/request_test.go Sat Jul 09 14:08:24 2016 -0500
@@ -7,13 +7,13 @@
"testing"
)
-func buildBody(t *testing.T, r *Request, body string, content_type string) {
+func buildBody(t *testing.T, r *Request, body string, contentType string) {
test := &Test{}
b, ct := r.buildBody(test)
- if ct != content_type {
- t.Errorf("got content-type '%s' expected '%s'", ct, content_type)
+ if ct != contentType {
+ t.Errorf("got content-type '%s' expected '%s'", ct, contentType)
}
buf := new(bytes.Buffer)
@@ -46,7 +46,7 @@
"a": 789,
},
}
- r.Json = j
+ r.JSON = j
test := &Test{}
--- a/test/response.go Sat Jul 09 13:35:45 2016 -0500
+++ b/test/response.go Sat Jul 09 14:08:24 2016 -0500
@@ -1,5 +1,6 @@
package test
+// Response represents an expected response
type Response struct {
StatusCode int
Headers map[string][]string
--- a/test/result.go Sat Jul 09 13:35:45 2016 -0500
+++ b/test/result.go Sat Jul 09 14:08:24 2016 -0500
@@ -4,14 +4,16 @@
"net/http"
)
+// Result is the result of running a Test
type Result struct {
Test *Test
Passed bool
- HttpResponse *http.Response
+ HTTPResponse *http.Response
TestResponse Response
Error error
}
+// NewResult creates a new instance of Result
func NewResult(test *Test) *Result {
res := new(Result)
--- a/test/test.go Sat Jul 09 13:35:45 2016 -0500
+++ b/test/test.go Sat Jul 09 14:08:24 2016 -0500
@@ -6,6 +6,7 @@
"time"
)
+// Test is the run time version of the test
type Test struct {
Filename string
Name string
@@ -15,26 +16,27 @@
Duration time.Duration
}
+// Run runs the given test
func (test *Test) Run(url *url.URL) *Result {
res := NewResult(test)
test.Start = time.Now()
for _, request := range test.Requests {
- http_request, err := request.buildRequest(test, url)
+ httpRequest, err := request.buildRequest(test, url)
if err != nil {
return res
}
client := &http.Client{}
- resp, err := client.Do(http_request)
+ resp, err := client.Do(httpRequest)
if err != nil {
res.Error = err
return res
}
defer resp.Body.Close()
- res.HttpResponse = resp
+ res.HTTPResponse = resp
res.Passed, res.Error = request.compareResponse(resp)
}