Skip to content

Commit

Permalink
add go modules, remove dependencies, tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonhancock committed Oct 18, 2023
1 parent 0494638 commit 4257623
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 75 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/pullrequests.yaml
Original file line number Diff line number Diff line change
@@ -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 ./...
28 changes: 28 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"
git config --local user.name "GitHub Action"
git tag -a $RELEASE_VERSION -m $RELEASE_VERSION
git push origin $RELEASE_VERSION
17 changes: 0 additions & 17 deletions .travis.yml

This file was deleted.

7 changes: 3 additions & 4 deletions check_dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"
"testing"

"github.com/cheekybits/is"
"github.com/stretchr/testify/require"
)

func TestCheckDNS(t *testing.T) {
Expand All @@ -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)
}
})
}
Expand Down
10 changes: 4 additions & 6 deletions check_staticmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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() {
Expand Down
10 changes: 4 additions & 6 deletions check_tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 5 additions & 7 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

"github.com/cheekybits/is"
"github.com/stretchr/testify/require"
)

var count int
Expand All @@ -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"])
}
45 changes: 18 additions & 27 deletions checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,81 +28,72 @@ 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()

req = httptest.NewRequest("GET", "http://127.0.0.1/healthz", nil)
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
}

Expand Down
11 changes: 3 additions & 8 deletions examples_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package healthz

import (
"net/http"

"github.com/gorilla/mux"
)
import "net/http"

func Example() {
checker := NewChecker()
Expand All @@ -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)
}
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 4257623

Please sign in to comment.