Golang Testing
The more I manage devOps the more I need a way to test entire setups as things progress.
Infrastructure testing as code. For example if you run a site at http://example.com/ you want to
make sure it returns status 200.  Create a new file called example_test.go  All golang test files
needs to be *_test.go
package main
import (
        "net/http"
        "testing"
       )
func TestSite(t *testing.T) {
    resp, err := http.Get("https://golang.com")
        if err != nil {
            t.Errorf("could not request http get request against site")
        }
    if resp.StatusCode != 200 {
        t.Errorf("the status code is not 200")
    }
}
Now you can run these test on the terminal by using:
go test
PASS
ok      _/~/git/testing      0.451s
if you want to see what test pass you can use more verbose output:
go test -v
=== RUN   TestSite
--- PASS: TestSite (0.31s)
PASS
ok      _/~/git/testing     0.413s
Now lets write a unit test that fails:
package main
import (
        "net/http"
        "testing"
)
func TestSite(t *testing.T) {
        resp, err := http.Get("https://golang.com")
        defer resp.Body.Close()
        if err != nil {
                t.Errorf("could not request http get request against site")
        }
        if resp.StatusCode != 200 {
                t.Errorf("the status code is not 200")
        }
}
func TestSite1(t *testing.T) {
        resp, err := http.Get("https://google.com")
        defer resp.Body.Close()
        if err != nil {
                t.Errorf("could not request http get request against site")
        }
        if resp.StatusCode != 301 {
                t.Errorf("the status code is not 301")
        }
}
results:
go test
--- FAIL: TestSite1 (0.23s)
    example_test.go:28: the status code is not 301
FAIL
exit status 1
FAIL    _/~/git/testing      0.836s
Now lets add some color to our results.  The git repo that does this is here.
