Skip to content

Commit

Permalink
Merge pull request #12 from maier/CIRC-8780
Browse files Browse the repository at this point in the history
v0.0.9 [CIRC-8780]
  • Loading branch information
maier committed Aug 22, 2022
2 parents 701b74c + 8c7383b commit 5db07a1
Show file tree
Hide file tree
Showing 15 changed files with 530 additions and 38 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
- uses: actions/setup-go@v2
with:
stable: true
go-version: 1.16.x
go-version: 1.17.x
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.42
version: v1.48
skip-go-installation: true
args: --timeout=5m
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# v0.0.9

* chore: add RefreshCheckBundle tests
* feet: add `RefreshCheckBundle` method
* chore: add UpdateCheckTags tests
* feat: add `UpdateCheckTags` method
* fix: ensure the trap check has been initialized
* feat(deps): bump go-apiclient from 0.7.15 to 0.7.18
* feat(deps): bump go-retryablehttp from 0.7.0 to 0.7.1
* fix(lint): ioutil deprecation
* chore: update to go1.17

# v0.0.8

* add: reset flag and handle rest in setBrokerTLSConfig
Expand Down
1 change: 1 addition & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ type API interface {
FetchCheckBundle(cid apiclient.CIDType) (*apiclient.CheckBundle, error)
CreateCheckBundle(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error)
SearchCheckBundles(searchCriteria *apiclient.SearchQueryType, filterCriteria *apiclient.SearchFilterType) (*[]apiclient.CheckBundle, error)
UpdateCheckBundle(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error)
}
43 changes: 43 additions & 0 deletions api_moq_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package trapcheck

import (
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/http/httptest"
Expand All @@ -21,7 +21,7 @@ import (
func TestTrapCheck_brokerSupportsCheckType(t *testing.T) {
tc := &TrapCheck{}
tc.Log = &LogWrapper{
Log: log.New(ioutil.Discard, "", log.LstdFlags),
Log: log.New(io.Discard, "", log.LstdFlags),
Debug: false,
}

Expand Down Expand Up @@ -83,7 +83,7 @@ func TestTrapCheck_brokerSupportsCheckType(t *testing.T) {
func TestTrapCheck_getBrokerCNList(t *testing.T) {
tc := &TrapCheck{}
tc.Log = &LogWrapper{
Log: log.New(ioutil.Discard, "", log.LstdFlags),
Log: log.New(io.Discard, "", log.LstdFlags),
Debug: false,
}

Expand Down Expand Up @@ -208,7 +208,7 @@ func TestTrapCheck_getBrokerCNList(t *testing.T) {
func TestTrapCheck_isValidBroker(t *testing.T) {
tc := &TrapCheck{}
tc.Log = &LogWrapper{
Log: log.New(ioutil.Discard, "", log.LstdFlags),
Log: log.New(io.Discard, "", log.LstdFlags),
Debug: false,
}

Expand Down Expand Up @@ -334,7 +334,7 @@ func TestTrapCheck_isValidBroker(t *testing.T) {
func TestTrapCheck_getBroker(t *testing.T) {
tc := &TrapCheck{}
tc.Log = &LogWrapper{
Log: log.New(ioutil.Discard, "", log.LstdFlags),
Log: log.New(io.Discard, "", log.LstdFlags),
Debug: false,
}

Expand Down
5 changes: 5 additions & 0 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func (tc *TrapCheck) createCheckBundle(cfg *apiclient.CheckBundle) error {
if cfg == nil {
return fmt.Errorf("invalid check bundle config (nil)")
}
if cfg.Type == "" {
return fmt.Errorf("invalid check bundle config (no check type)")
}

// add broker here, no reason to do it in applying defaults as that's
// done every time, even when a check could be found (so no point "selecting"
// a broker to create a check, when a check already exists)
Expand All @@ -139,6 +143,7 @@ func (tc *TrapCheck) createCheckBundle(cfg *apiclient.CheckBundle) error {
}
cfg.Brokers = []string{tc.broker.CID}
}

bundle, err := tc.client.CreateCheckBundle(cfg)
if err != nil {
return fmt.Errorf("create check bundle: %w", err)
Expand Down
62 changes: 62 additions & 0 deletions check_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package trapcheck

import (
"context"
"fmt"
"strings"

"github.com/circonus-labs/go-apiclient"
)

func (tc *TrapCheck) UpdateCheckTags(ctx context.Context, tags []string) (*apiclient.CheckBundle, error) {
if tc.checkBundle == nil {
return nil, fmt.Errorf("invalid state, check bundle is nil")
}
if len(tags) == 0 {
return nil, nil
}

update := false
for _, tag := range tags {
if tag == "" {
continue
}
found := false
tagParts := strings.SplitN(tag, ":", 2)
for j, ctag := range tc.checkBundle.Tags {
if tag == ctag {
found = true
break
}

ctagParts := strings.SplitN(ctag, ":", 2)
if len(tagParts) == len(ctagParts) {
if tagParts[0] == ctagParts[0] {
if tagParts[1] != ctagParts[1] {
tc.Log.Warnf("modifying tag: new: %v old: %v", tagParts, ctagParts)
tc.checkBundle.Tags[j] = tag
update = true // but force update since we're modifying a tag
found = true
break
}
}

}
}
if !found {
tc.Log.Warnf("adding missing tag: %s curr: %v", tag, tc.checkBundle.Tags)
tc.checkBundle.Tags = append(tc.checkBundle.Tags, tag)
update = true
}
}

if update {
b, err := tc.client.UpdateCheckBundle(tc.checkBundle)
if err != nil {
return nil, fmt.Errorf("api updating check bundle tags: %w", err)
}
return b, nil
}

return nil, nil
}
132 changes: 132 additions & 0 deletions check_tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package trapcheck

import (
"context"
"fmt"
"io"
"log"
"reflect"
"testing"

"github.com/circonus-labs/go-apiclient"
)

func TestTrapCheck_UpdateCheckTags(t *testing.T) {
tc := &TrapCheck{}
tc.Log = &LogWrapper{
Log: log.New(io.Discard, "", log.LstdFlags),
Debug: false,
}

tests := []struct {
client API
bundle *apiclient.CheckBundle
want *apiclient.CheckBundle
name string
newTags []string
wantErr bool
}{
{
name: "new tag",
bundle: &apiclient.CheckBundle{
Tags: []string{"foo"},
},
newTags: []string{"bar"},
want: &apiclient.CheckBundle{
Tags: []string{"foo", "bar"},
},
wantErr: false,
client: &APIMock{
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) {
return cfg, nil
},
},
},
{
name: "new tag, ignore blank",
bundle: &apiclient.CheckBundle{
Tags: []string{"foo"},
},
newTags: []string{"bar", ""},
want: &apiclient.CheckBundle{
Tags: []string{"foo", "bar"},
},
wantErr: false,
client: &APIMock{
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) {
return cfg, nil
},
},
},
{
name: "modify tag",
bundle: &apiclient.CheckBundle{
Tags: []string{"foo:bar"},
},
newTags: []string{"foo:baz"},
want: &apiclient.CheckBundle{
Tags: []string{"foo:baz"},
},
wantErr: false,
client: &APIMock{
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) {
return cfg, nil
},
},
},
{
name: "no change",
bundle: &apiclient.CheckBundle{
Tags: []string{"foo"},
},
newTags: []string{"foo"},
want: nil,
wantErr: false,
client: &APIMock{
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) {
return cfg, nil
},
},
},
{
name: "invalid (nil check bundle)",
bundle: nil,
wantErr: true,
},
{
name: "no tags",
bundle: &apiclient.CheckBundle{},
want: nil,
wantErr: false,
},
{
name: "api error",
bundle: &apiclient.CheckBundle{Tags: []string{"foo"}},
newTags: []string{"bar"},
want: nil,
wantErr: true,
client: &APIMock{
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) {
return nil, fmt.Errorf("api error 500")
},
},
},
}
for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
tc.client = tt.client
tc.checkBundle = tt.bundle

got, err := tc.UpdateCheckTags(context.Background(), tt.newTags)
if (err != nil) != tt.wantErr {
t.Errorf("TrapCheck.UpdateCheckTags() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("TrapCheck.UpdateCheckTags() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 5db07a1

Please sign in to comment.