Skip to content

Commit

Permalink
Code style and cosmetics (#45)
Browse files Browse the repository at this point in the history
- Add interface check for logger
- Add JSON spec tests for dns_lookup
- Add JSON spec tests for assigned_address
- Add JSON spec tests for http_2xx
- Add JSON spec tests for icmp_ping
- Add JSON spec tests for tls_certificate

---------

Signed-off-by: Igor Shishkin <me@teran.dev>
  • Loading branch information
teran authored Jun 8, 2024
1 parent f298762 commit 126c4cc
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 4 deletions.
2 changes: 2 additions & 0 deletions announcer/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
log "github.com/sirupsen/logrus"
)

var _ gobgpLog.Logger = (*Logger)(nil)

type Logger struct {
Logger *log.Logger
}
Expand Down
16 changes: 16 additions & 0 deletions checkers/assigned_address/assigned_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package assigned_address

import (
"context"
"encoding/json"
"os"
"testing"

log "github.com/sirupsen/logrus"
Expand All @@ -13,6 +15,20 @@ func init() {
log.SetLevel(log.TraceLevel)
}

func TestSpec(t *testing.T) {
r := require.New(t)

data, err := os.ReadFile("testdata/spec.json")
r.NoError(err)

c, err := NewFromSpec(json.RawMessage(data))
r.NoError(err)

aa := c.(*assigned_address)
r.Equal("127.0.0.33", aa.ipv4)
r.Equal("dummy0", *aa.iface)
}

func TestCheckHappyPath(t *testing.T) {
r := require.New(t)

Expand Down
1 change: 0 additions & 1 deletion checkers/assigned_address/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/pkg/errors"
"github.com/stretchr/testify/require"

ptr "github.com/teran/go-ptr"
)

Expand Down
4 changes: 4 additions & 0 deletions checkers/assigned_address/testdata/spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"interface": "dummy0",
"ipv4": "127.0.0.33"
}
32 changes: 32 additions & 0 deletions checkers/dns_lookup/dns_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,48 @@ package dns_lookup

import (
"context"
"encoding/json"
"net"
"os"
"testing"
"time"

"github.com/pkg/errors"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/teran/anycastd/config"
)

func TestMkResolver(t *testing.T) {
r := require.New(t)

res := mkResolver("127.0.0.1:53", 3*time.Second)
r.NotNil(res)

nativeResolver := res.(*net.Resolver)
r.True(nativeResolver.PreferGo)
r.False(nativeResolver.StrictErrors)
}

func TestSpec(t *testing.T) {
r := require.New(t)

data, err := os.ReadFile("testdata/spec.json")
r.NoError(err)

c, err := NewFromSpec(json.RawMessage(data))
r.NoError(err)

dl := c.(*dns_lookup)
r.Equal("example.com", dl.query)
r.Equal("127.0.0.1:53", dl.resolver)
r.Equal(uint8(3), dl.tries)
r.Equal(300*time.Millisecond, dl.interval)
r.Equal(3*time.Second, dl.timeout)
}

func (s *checkTestSuite) TestHappyPath() {
l, err := New(spec{
Query: "example.com",
Expand Down
7 changes: 7 additions & 0 deletions checkers/dns_lookup/testdata/spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"query": "example.com",
"resolver": "127.0.0.1:53",
"tries": 3,
"interval": "300ms",
"timeout": "3s"
}
1 change: 1 addition & 0 deletions checkers/http_2xx/http_2xx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/teran/anycastd/checkers"
)

Expand Down
21 changes: 21 additions & 0 deletions checkers/http_2xx/http_2xx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,37 @@ package http_2xx

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/teran/anycastd/config"
)

func TestSpec(t *testing.T) {
r := require.New(t)

data, err := os.ReadFile("testdata/spec.json")
r.NoError(err)

c, err := NewFromSpec(json.RawMessage(data))
r.NoError(err)

h := c.(*http_2xx)
r.Equal("example.com", h.url)
r.Equal("GET", h.method)
r.Equal(uint8(10), h.tries)
r.Equal(1*time.Second, h.interval)
r.Equal(10*time.Second, h.client.Timeout)
}

func (s *http2xxTestSuite) TestTrivial() {
s.handlerMock.On("ServeHTTP", http.MethodGet, "/ping").Return(http.StatusOK).Once()

Expand Down
1 change: 1 addition & 0 deletions checkers/http_2xx/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/teran/anycastd/config"
)

Expand Down
7 changes: 7 additions & 0 deletions checkers/http_2xx/testdata/spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"url": "example.com",
"method": "GET",
"tries": 10,
"interval": "1s",
"timeout": "10s"
}
19 changes: 19 additions & 0 deletions checkers/icmp_ping/icmp_ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@ package icmp_ping

import (
"context"
"encoding/json"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/teran/anycastd/config"
)

func TestSpec(t *testing.T) {
r := require.New(t)

data, err := os.ReadFile("testdata/spec.json")
r.NoError(err)

c, err := NewFromSpec(json.RawMessage(data))
r.NoError(err)

p := c.(*icmp_ping)
r.Equal("127.0.0.33", p.host)
r.Equal(uint8(5), p.tries)
r.Equal(100*time.Millisecond, p.interval)
r.Equal(5*time.Second, p.timeout)
}

func TestCheck(t *testing.T) {
r := require.New(t)

Expand Down
1 change: 1 addition & 0 deletions checkers/icmp_ping/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/teran/anycastd/config"
)

Expand Down
8 changes: 8 additions & 0 deletions checkers/icmp_ping/testdata/spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"static": {
"host": "127.0.0.33"
},
"tries": 5,
"interval": "100ms",
"timeout": "5s"
}
1 change: 0 additions & 1 deletion checkers/tls_certificate/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/pkg/errors"
"github.com/stretchr/testify/require"

ptr "github.com/teran/go-ptr"
)

Expand Down
14 changes: 14 additions & 0 deletions checkers/tls_certificate/testdata/spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"local": {
"path": "/etc/ssl/cert.pem"
},
"common_name": "test cert",
"dns_names": [
"test-host.example.org",
"test-host-2.example.org"
],
"ip_addresses": [
"127.0.0.34"
],
"issuer": "test issuer"
}
10 changes: 8 additions & 2 deletions checkers/tls_certificate/tls_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,20 @@ func newWithCertificateRetriever(s spec, fn func() ([]*x509.Certificate, error))
return nil, err
}

return &tls_certificate{
tc := &tls_certificate{
retrieveCertificates: fn,

commonName: s.CommonName,
dnsNames: s.DNSNames,
ipAddresses: s.IPAddresses,
issuer: s.Issuer,
}, nil
}

if s.Local != nil {
tc.path = s.Local.Path
}

return tc, nil
}

func NewFromSpec(in json.RawMessage) (checkers.Checker, error) {
Expand Down
22 changes: 22 additions & 0 deletions checkers/tls_certificate/tls_certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package tls_certificate

import (
"context"
"encoding/json"
"os"
"testing"
"time"

Expand All @@ -15,6 +17,26 @@ func init() {
log.SetLevel(log.TraceLevel)
}

func TestSpec(t *testing.T) {
r := require.New(t)

data, err := os.ReadFile("testdata/spec.json")
r.NoError(err)

c, err := NewFromSpec(json.RawMessage(data))
r.NoError(err)

tc := c.(*tls_certificate)
r.Equal("/etc/ssl/cert.pem", tc.path)
r.Equal(ptr.String("test cert"), tc.commonName)
r.Equal([]string{
"test-host.example.org",
"test-host-2.example.org",
}, tc.dnsNames)
r.Equal([]string{"127.0.0.34"}, tc.ipAddresses)
r.Equal(ptr.String("test issuer"), tc.issuer)
}

func TestTLSCertificateLocal(t *testing.T) {
type testCase struct {
name string
Expand Down

0 comments on commit 126c4cc

Please sign in to comment.