From 4257623b7f7ab7ea269d9be1aa28a500358ef0eb Mon Sep 17 00:00:00 2001 From: Jason Hancock Date: Wed, 18 Oct 2023 13:43:03 -0700 Subject: [PATCH] add go modules, remove dependencies, tidy up --- .github/workflows/pullrequests.yaml | 17 +++++++++++ .github/workflows/release.yaml | 28 ++++++++++++++++++ .travis.yml | 17 ----------- check_dns_test.go | 7 ++--- check_staticmd_test.go | 10 +++---- check_tcp_test.go | 10 +++---- check_test.go | 12 ++++---- checker_test.go | 45 ++++++++++++----------------- examples_test.go | 11 ++----- go.mod | 11 +++++++ go.sum | 10 +++++++ 11 files changed, 103 insertions(+), 75 deletions(-) create mode 100644 .github/workflows/pullrequests.yaml create mode 100644 .github/workflows/release.yaml delete mode 100644 .travis.yml create mode 100644 go.mod create mode 100644 go.sum diff --git a/.github/workflows/pullrequests.yaml b/.github/workflows/pullrequests.yaml new file mode 100644 index 0000000..b39255e --- /dev/null +++ b/.github/workflows/pullrequests.yaml @@ -0,0 +1,17 @@ +name: Tests +on: [pull_request] + +jobs: + test: + name: Test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Setup Go + uses: actions/setup-go@v3 + with: + go-version: 'stable' + + - name: Go Tests + run: go test -v ./... diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..d1ed3c3 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,28 @@ +name: release +on: + push: + branches: + - main + +jobs: + release: + name: Build and Release + runs-on: [ubuntu-latest] + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Release Version + id: version + run: | + export RELEASE_VERSION=v0.0.${{ github.run_number }} + echo "RELEASE_VERSION=$RELEASE_VERSION" >> $GITHUB_ENV + echo "release_version=$RELEASE_VERSION" >> $GITHUB_OUTPUT + + - name: create tag + run: | + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + git tag -a $RELEASE_VERSION -m $RELEASE_VERSION + git push origin $RELEASE_VERSION + diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 9bec584..0000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -language: go - -sudo: false - -go: - - 1.10.x - - 1.11.x - -before_install: - - go get github.com/golang/lint/golint - -before_script: - - go vet ./... - - golint -set_exit_status ./... - -script: - - go test -v ./... diff --git a/check_dns_test.go b/check_dns_test.go index 5aba5bf..eff3a8d 100644 --- a/check_dns_test.go +++ b/check_dns_test.go @@ -6,7 +6,7 @@ import ( "net/http" "testing" - "github.com/cheekybits/is" + "github.com/stretchr/testify/require" ) func TestCheckDNS(t *testing.T) { @@ -24,13 +24,12 @@ func TestCheckDNS(t *testing.T) { for _, tt := range tests { t.Run(fmt.Sprintf("%s-%s", tt.address, tt.network), func(t *testing.T) { - is := is.New(t) c := NewCheckDNS(tt.network, tt.address) result := c.Check(context.Background()) if tt.expectError { - is.Err(result.Error) + require.Error(t, result.Error) } else { - is.NoErr(result.Error) + require.NoError(t, result.Error) } }) } diff --git a/check_staticmd_test.go b/check_staticmd_test.go index 37afcfa..c7fa2db 100644 --- a/check_staticmd_test.go +++ b/check_staticmd_test.go @@ -6,12 +6,10 @@ import ( "runtime" "testing" - "github.com/cheekybits/is" + "github.com/stretchr/testify/require" ) func TestCheckStaticMD(t *testing.T) { - is := is.New(t) - md := map[string]string{ "key1": "value1", "key2": "value2", @@ -20,10 +18,10 @@ func TestCheckStaticMD(t *testing.T) { c := NewCheckStaticMD(md) result := c.Check(context.Background()) - is.Equal(2, len(result.Metadata)) + require.Len(t, result.Metadata, 2) value1, ok := result.Metadata["key1"] - is.OK(ok) - is.Equal("value1", value1) + require.True(t, ok) + require.Equal(t, "value1", value1) } func ExampleCheckStaticMD() { diff --git a/check_tcp_test.go b/check_tcp_test.go index d21023d..be085f5 100644 --- a/check_tcp_test.go +++ b/check_tcp_test.go @@ -8,26 +8,24 @@ import ( "testing" "time" - "github.com/cheekybits/is" + "github.com/stretchr/testify/require" ) func TestCheckTCP(t *testing.T) { - is := is.New(t) - server := httptest.NewServer(nil) defer server.Close() u, err := url.Parse(server.URL) - is.NoErr(err) + require.NoError(t, err) c := NewCheckTCP(u.Host, 5*time.Second) result := c.Check(context.Background()) - is.NoErr(result.Error) + require.NoError(t, result.Error) // Shut the server down, expect an error server.Close() result = c.Check(context.Background()) - is.Err(result.Error) + require.Error(t, result.Error) } func ExampleCheckTCP() { diff --git a/check_test.go b/check_test.go index ee23a75..705be98 100644 --- a/check_test.go +++ b/check_test.go @@ -5,7 +5,7 @@ import ( "fmt" "testing" - "github.com/cheekybits/is" + "github.com/stretchr/testify/require" ) var count int @@ -22,18 +22,16 @@ func (c testCheck) Check(ctx context.Context) *Response { } func TestCheck(t *testing.T) { - is := is.New(t) - count = 0 c := testCheck{} ctx := context.Background() r := c.Check(ctx) - is.Equal(count, 1) - is.Equal(r.Metadata["count"], "1") + require.Equal(t, 1, count) + require.Equal(t, "1", r.Metadata["count"]) r = c.Check(ctx) - is.Equal(count, 2) - is.Equal(r.Metadata["count"], "2") + require.Equal(t, 2, count) + require.Equal(t, "2", r.Metadata["count"]) } diff --git a/checker_test.go b/checker_test.go index cfdd573..0cecd00 100644 --- a/checker_test.go +++ b/checker_test.go @@ -4,12 +4,12 @@ import ( "context" "encoding/json" "io" - "io/ioutil" + "net/http" "net/http/httptest" "testing" "time" - "github.com/cheekybits/is" + "github.com/stretchr/testify/require" ) type sleep struct { @@ -28,29 +28,26 @@ func (c sleep) Check(ctx context.Context) *Response { } func TestChecker(t *testing.T) { - is := is.New(t) - c := NewChecker(WithTimeout(100 * time.Millisecond)) err := c.AddCheck("test", testCheck{}) - is.NoErr(err) + require.NoError(t, err) req := httptest.NewRequest("GET", "http://127.0.0.1/healthz", nil) w := httptest.NewRecorder() c.ServeHTTP(w, req) - is.Equal(w.Code, 200) + require.Equal(t, http.StatusOK, w.Code) m, err := parseResponse(w.Body) - is.NoErr(err) + require.NoError(t, err) v, ok := m["test"] - is.OK(ok) - is.Equal(1, len(v.Metadata)) - is.Equal("", v.ErrorMessage) + require.True(t, ok) + require.Len(t, v.Metadata, 1) + require.Equal(t, "", v.ErrorMessage) // Add another check, one designed to fail - err = c.AddCheck("sleep", newSleep(200*time.Millisecond)) - is.NoErr(err) + require.NoError(t, c.AddCheck("sleep", newSleep(200*time.Millisecond))) w = httptest.NewRecorder() @@ -58,51 +55,45 @@ func TestChecker(t *testing.T) { w = httptest.NewRecorder() c.ServeHTTP(w, req) - is.Equal(w.Code, 500) + require.Equal(t, http.StatusInternalServerError, w.Code) m, err = parseResponse(w.Body) - is.NoErr(err) + require.NoError(t, err) v, ok = m["sleep"] - is.OK(ok) - is.Equal(ErrorCheckTimedOut.Error(), v.ErrorMessage) + require.True(t, ok) + require.Equal(t, ErrorCheckTimedOut.Error(), v.ErrorMessage) } func TestCheckerEmpty(t *testing.T) { - is := is.New(t) - c := NewChecker(WithTimeout(100 * time.Millisecond)) req := httptest.NewRequest("GET", "http://127.0.0.1/healthz", nil) w := httptest.NewRecorder() c.ServeHTTP(w, req) - is.Equal(w.Code, 200) + require.Equal(t, http.StatusOK, w.Code) } func TestCheckerUnknownCheck(t *testing.T) { - is := is.New(t) - c := NewChecker(WithTimeout(100 * time.Millisecond)) - err := c.AddCheck("test", testCheck{}) - is.NoErr(err) + require.NoError(t, c.AddCheck("test", testCheck{})) req := httptest.NewRequest("GET", "http://127.0.0.1/healthz/foo", nil) w := httptest.NewRecorder() c.ServeHTTP(w, req) - is.Equal(w.Code, 404) + require.Equal(t, http.StatusNotFound, w.Code) } func parseResponse(r io.Reader) (map[string]Response, error) { - bytes, err := ioutil.ReadAll(r) + bytes, err := io.ReadAll(r) if err != nil { return nil, err } m := make(map[string]Response) - err = json.Unmarshal(bytes, &m) - if err != nil { + if err = json.Unmarshal(bytes, &m); err != nil { return nil, err } diff --git a/examples_test.go b/examples_test.go index 1bd544d..26692d8 100644 --- a/examples_test.go +++ b/examples_test.go @@ -1,10 +1,6 @@ package healthz -import ( - "net/http" - - "github.com/gorilla/mux" -) +import "net/http" func Example() { checker := NewChecker() @@ -27,8 +23,7 @@ func Example_gorilla() { "key2": "a different value", })) - r := mux.NewRouter() - r.PathPrefix("/healthz").Handler(checker) + http.Handle("/healthz", checker) - http.ListenAndServe(":8080", r) + http.ListenAndServe(":8080", nil) } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..15a4aa4 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module github.com/jasonhancock/healthz + +go 1.21.3 + +require github.com/stretchr/testify v1.8.4 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..fa4b6e6 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=