Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleaned up linted code #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions checkers/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"fmt"

. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -56,6 +57,7 @@ func TestDo(t *testing.T) {
Expect(err).To(HaveOccurred())
Expect(res).To(BeNil())
Expect(err.Error()).To(ContainSubstring("error parsing payload"))
res.Close()
})

t.Run("Should error if request can't be created", func(t *testing.T) {
Expand All @@ -72,6 +74,7 @@ func TestDo(t *testing.T) {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Unable to create new HTTP request for HTTPMonitor check"))
Expect(res).To(BeNil())
res.Close()
})
}

Expand Down
32 changes: 16 additions & 16 deletions checkers/memcache/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
// "Timeout" defines timeout for socket write/read (useful for servers hosted on different machine)
// "Ping" is optional; Ping establishes tcp connection to memcached server.
type MemcachedConfig struct {
Url string
URL string
Timeout int32
Ping bool
Set *MemcachedSetOptions
Expand Down Expand Up @@ -75,7 +75,7 @@ func NewMemcached(cfg *MemcachedConfig) (*Memcached, error) {
return nil, fmt.Errorf("unable to validate memcached config: %v", err)
}

mcWrapper := &MemcachedClientWrapper{memcache.New(cfg.Url)}
mcWrapper := &MemcachedClientWrapper{memcache.New(cfg.URL)}

return &Memcached{
Config: cfg,
Expand All @@ -86,15 +86,15 @@ func NewMemcached(cfg *MemcachedConfig) (*Memcached, error) {
func (mc *Memcached) Status() (interface{}, error) {

if mc.Config.Ping {
if _, err := net.Dial("tcp", mc.Config.Url); err != nil {
return nil, fmt.Errorf("Ping failed: %v", err)
if _, err := net.Dial("tcp", mc.Config.URL); err != nil {
return nil, fmt.Errorf("ping failed: %v", err)
}
}

if mc.Config.Set != nil {
err := mc.wrapper.GetClient().Set(&memcache.Item{Key: mc.Config.Set.Key, Value: []byte(mc.Config.Set.Value), Expiration: mc.Config.Set.Expiration})
if err != nil {
return nil, fmt.Errorf("Unable to complete set: %v", err)
return nil, fmt.Errorf("unable to complete set: %v", err)
}
}

Expand All @@ -103,16 +103,16 @@ func (mc *Memcached) Status() (interface{}, error) {
if err != nil {
if err == memcache.ErrCacheMiss {
if !mc.Config.Get.NoErrorMissingKey {
return nil, fmt.Errorf("Unable to complete get: '%v' not found", mc.Config.Get.Key)
return nil, fmt.Errorf("unable to complete get: '%v' not found", mc.Config.Get.Key)
}
} else {
return nil, fmt.Errorf("Unable to complete get: %v", err)
return nil, fmt.Errorf("unable to complete get: %v", err)
}
}

if mc.Config.Get.Expect != nil {
if !bytes.Equal(mc.Config.Get.Expect, val.Value) {
return nil, fmt.Errorf("Unable to complete get: returned value '%v' does not match expected value '%v'",
return nil, fmt.Errorf("unable to complete get: returned value '%v' does not match expected value '%v'",
val, mc.Config.Get.Expect)
}
}
Expand All @@ -123,26 +123,26 @@ func (mc *Memcached) Status() (interface{}, error) {

func validateMemcachedConfig(cfg *MemcachedConfig) error {
if cfg == nil {
return fmt.Errorf("Main config cannot be nil")
return fmt.Errorf("main config cannot be nil")
}

if cfg.Url == "" {
return fmt.Errorf("Url string must be set in config")
if cfg.URL == "" {
return fmt.Errorf("url string must be set in config")
}

if _, err := url.Parse(cfg.Url); err != nil {
return fmt.Errorf("Unable to parse URL: %v", err)
if _, err := url.Parse(cfg.URL); err != nil {
return fmt.Errorf("unable to parse URL: %v", err)
}

// At least one check method must be set
if !cfg.Ping && cfg.Set == nil && cfg.Get == nil {
return fmt.Errorf("At minimum, either cfg.Ping, cfg.Set or cfg.Get must be set")
return fmt.Errorf("at minimum, either cfg.Ping, cfg.Set or cfg.Get must be set")
}

// If .Set is set, verify that at minimum .Key is set
if cfg.Set != nil {
if cfg.Set.Key == "" {
return fmt.Errorf("If cfg.Set is used, cfg.Set.Key must be set")
return fmt.Errorf("if cfg.Set is used, cfg.Set.Key must be set")
}

if cfg.Set.Value == "" {
Expand All @@ -153,7 +153,7 @@ func validateMemcachedConfig(cfg *MemcachedConfig) error {
// If .Get is set, verify that at minimum .Key is set
if cfg.Get != nil {
if cfg.Get.Key == "" {
return fmt.Errorf("If cfg.Get is used, cfg.Get.Key must be set")
return fmt.Errorf("if cfg.Get is used, cfg.Get.Key must be set")
}
}

Expand Down
38 changes: 19 additions & 19 deletions checkers/memcache/memcached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
testUrl = "localhost:11011"
testURL = "localhost:11011"
)

var emulateServerShutdown bool
Expand All @@ -20,9 +20,9 @@ func TestNewMemcached(t *testing.T) {
RegisterTestingT(t)

t.Run("Happy path", func(t *testing.T) {
url := testUrl
url := testURL
cfg := &MemcachedConfig{
Url: url,
URL: url,
Ping: true,
}
mc, server, err := setupMemcached(cfg)
Expand All @@ -42,9 +42,9 @@ func TestNewMemcached(t *testing.T) {
})

t.Run("Memcached should contain Client and Config", func(t *testing.T) {
url := testUrl
url := testURL
cfg := &MemcachedConfig{
Url: url,
URL: url,
Ping: true,
}
mc, err := NewMemcached(cfg)
Expand All @@ -62,41 +62,41 @@ func TestValidateMemcachedConfig(t *testing.T) {
var cfg *MemcachedConfig
err := validateMemcachedConfig(cfg)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Main config cannot be nil"))
Expect(err.Error()).To(ContainSubstring("main config cannot be nil"))
})

t.Run("Config must have an url set", func(t *testing.T) {
cfg := &MemcachedConfig{}

err := validateMemcachedConfig(cfg)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Url string must be set in config"))
Expect(err.Error()).To(ContainSubstring("url string must be set in config"))
})

t.Run("Should error if none of the check methods are enabled", func(t *testing.T) {
cfg := &MemcachedConfig{
Url: testUrl,
URL: testURL,
}

err := validateMemcachedConfig(cfg)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("At minimum, either cfg.Ping, cfg.Set or cfg.Get must be set"))
Expect(err.Error()).To(ContainSubstring("at minimum, either cfg.Ping, cfg.Set or cfg.Get must be set"))
})

t.Run("Should error if .Set is used but key is undefined", func(t *testing.T) {
cfg := &MemcachedConfig{
Url: testUrl,
URL: testURL,
Set: &MemcachedSetOptions{},
}

err := validateMemcachedConfig(cfg)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("If cfg.Set is used, cfg.Set.Key must be set"))
Expect(err.Error()).To(ContainSubstring("if cfg.Set is used, cfg.Set.Key must be set"))
})

t.Run("Should error if .Get is used but key is undefined", func(t *testing.T) {
cfg := &MemcachedConfig{
Url: testUrl,
URL: testURL,
Get: &MemcachedGetOptions{},
}

Expand All @@ -107,7 +107,7 @@ func TestValidateMemcachedConfig(t *testing.T) {

t.Run("Should error if url has wrong format", func(t *testing.T) {
cfg := &MemcachedConfig{
Url: "wrong\\localhost:6379",
URL: "wrong\\localhost:6379",
}

err := validateMemcachedConfig(cfg)
Expand All @@ -117,7 +117,7 @@ func TestValidateMemcachedConfig(t *testing.T) {

t.Run("Shouldn't error with properly set config", func(t *testing.T) {
cfg := &MemcachedConfig{
Url: testUrl,
URL: testURL,
Get: &MemcachedGetOptions{
Key: "should_return_valid",
Expect: []byte("should_return_valid"),
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestMemcachedStatus(t *testing.T) {

_, err = checker.Status()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Unable to complete set"))
Expect(err.Error()).To(ContainSubstring("unable to complete set"))
})

t.Run("should use .Value if .Value is defined", func(t *testing.T) {
Expand Down Expand Up @@ -283,7 +283,7 @@ func TestMemcachedStatus(t *testing.T) {

_, err = checker.Status()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Unable to complete get"))
Expect(err.Error()).To(ContainSubstring("unable to complete get"))
})

t.Run("should error if .Expect is set and the value does not match", func(t *testing.T) {
Expand Down Expand Up @@ -335,7 +335,7 @@ func TestMemcachedStatus(t *testing.T) {
func setupMemcached(cfg *MemcachedConfig) (*Memcached, *MockServer, error) {
server := &MockServer{}
server.Reset()
cfg.Url = testUrl
cfg.URL = testURL
checker := &Memcached{
wrapper: &MemcachedClientWrapper{&MockMemcachedClient{}},
Config: cfg,
Expand All @@ -358,7 +358,7 @@ type MockMemcachedClient struct{}

func (m *MockMemcachedClient) Get(key string) (item *memcache.Item, err error) {
if emulateServerShutdown {
return nil, fmt.Errorf("Unable to complete get")
return nil, fmt.Errorf("unable to complete get")
}
switch key {
case "should_return_valid":
Expand All @@ -376,7 +376,7 @@ func (m *MockMemcachedClient) Get(key string) (item *memcache.Item, err error) {

func (m *MockMemcachedClient) Set(item *memcache.Item) error {
if emulateServerShutdown {
return fmt.Errorf("Unable to complete set")
return fmt.Errorf("unable to complete set")
}
return nil
}
18 changes: 9 additions & 9 deletions checkers/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type MongoConfig struct {
// Url format is localhost:27017 or mongo://localhost:27017
// Credential has format described at https://godoc.org/github.com/globalsign/mgo#Credential
type MongoAuthConfig struct {
Url string
URL string
Credentials mgo.Credential
}

Expand All @@ -50,7 +50,7 @@ func NewMongo(cfg *MongoConfig) (*Mongo, error) {
return nil, fmt.Errorf("unable to validate mongodb config: %v", err)
}

session, err := mgo.DialWithTimeout(cfg.Auth.Url, cfg.DialTimeout)
session, err := mgo.DialWithTimeout(cfg.Auth.URL, cfg.DialTimeout)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -96,23 +96,23 @@ func contains(data []string, needle string) bool {

func validateMongoConfig(cfg *MongoConfig) error {
if cfg == nil {
return fmt.Errorf("Main config cannot be nil")
return fmt.Errorf("main config cannot be nil")
}

if cfg.Auth == nil {
return fmt.Errorf("Auth config cannot be nil")
return fmt.Errorf("auth config cannot be nil")
}

if cfg.Auth.Url == "" {
return fmt.Errorf("Url string must be set in auth config")
if cfg.Auth.URL == "" {
return fmt.Errorf("url string must be set in auth config")
}

if _, err := mgo.ParseURL(cfg.Auth.Url); err != nil {
return fmt.Errorf("Unable to parse URL: %v", err)
if _, err := mgo.ParseURL(cfg.Auth.URL); err != nil {
return fmt.Errorf("unable to parse URL: %v", err)
}

if !cfg.Ping && cfg.Collection == "" {
return fmt.Errorf("At minimum, either cfg.Ping or cfg.Collection")
return fmt.Errorf("at minimum, either cfg.Ping or cfg.Collection")
}

if cfg.DialTimeout <= 0 {
Expand Down
Loading