diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 8e2cf2b..770d690 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -1,11 +1,11 @@ name: golangci-lint on: push: - tags: - - v* - branches: - - master + tags: [ "v*" ] + branches: [ master ] pull_request: + branches: [ "*" ] + jobs: golangci: name: lint @@ -19,4 +19,6 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v3.2.0 with: - args: --timeout=5m + version: v1.48 + skip-go-installation: true + args: --timeout=5m \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 01991f2..9c071c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,14 @@ +# v0.7.18 + +* feat: clean tags - remove blanks and duplicates, lowercase, and sort +* fix: debug messages +* fix(lint): ioutil deprecation +* fix(lint): gofmt +* fix(lint): struct alignment +* chore: update to go1.17 +* feat(deps): bump go-retryablehttp from 0.7.0 to 0.7.1 +* chore: Merge remote-tracking branch 'upstream/master' + # v0.7.17 * upd: Implements a new DisableRetries configuration setting to allow the diff --git a/account_test.go b/account_test.go index 8aafd49..c56219e 100644 --- a/account_test.go +++ b/account_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -77,7 +77,7 @@ func testAccountServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -148,16 +148,32 @@ func TestFetchAccount(t *testing.T) { apih, server := accountTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + expectedType := "*apiclient.Account" + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"valid (default,empty)", "", "*apiclient.Account", false, ""}, - {"valid (short cid)", "1234", "*apiclient.Account", false, ""}, - {"valid (long cid)", "/account/1234", "*apiclient.Account", false, ""}, + { + id: "valid (default,empty)", + cid: "", + expectedType: expectedType, + shouldFail: false, + }, + { + id: "valid (short cid)", + cid: "1234", + expectedType: expectedType, + shouldFail: false, + }, + { + id: "valid (long cid)", + cid: "/account/1234", + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { @@ -201,16 +217,33 @@ func TestUpdateAccount(t *testing.T) { apih, server := accountTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Account + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid account config (nil)"}, - {"invalid (cid)", &Account{CID: "/invalid"}, "", true, "invalid account CID (/invalid)"}, - {"valid", &testAccount, "*apiclient.Account", false, ""}, + { + id: "invalid (nil)", + cfg: nil, + expectedType: "", + shouldFail: true, + expectedErr: "invalid account config (nil)", + }, + { + id: "invalid (cid)", + cfg: &Account{CID: "/invalid"}, + expectedType: "", + shouldFail: true, + expectedErr: "invalid account CID (/invalid)", + }, + { + id: "valid", + cfg: &testAccount, + expectedType: "*apiclient.Account", + shouldFail: false, + }, } for _, test := range tests { @@ -241,15 +274,25 @@ func TestSearchAccounts(t *testing.T) { expectedType := "*[]apiclient.Account" filter := SearchFilterType(map[string][]string{"f_name_wildcard": {"*ops*"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no filter", nil, expectedType, false, ""}, - {"filter", &filter, expectedType, false, ""}, + { + id: "no filter", + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter", + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/acknowledgement_test.go b/acknowledgement_test.go index 7ae5065..d2d9f11 100644 --- a/acknowledgement_test.go +++ b/acknowledgement_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -43,7 +43,7 @@ func testAcknowledgementServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -85,7 +85,7 @@ func testAcknowledgementServer() *httptest.Server { } case "POST": defer r.Body.Close() - _, err := ioutil.ReadAll(r.Body) + _, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -138,16 +138,32 @@ func TestFetchAcknowledgement(t *testing.T) { apih, server := acknowledgementTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (empty cid)", "", "", true, "invalid acknowledgement CID (none)"}, - {"valid (short cid)", "1234", "*apiclient.Acknowledgement", false, ""}, - {"valid (long cid)", "/acknowledgement/1234", "*apiclient.Acknowledgement", false, ""}, + { + id: "invalid (empty cid)", + cid: "", + expectedType: "", + shouldFail: true, + expectedErr: "invalid acknowledgement CID (none)", + }, + { + id: "valid (short cid)", + cid: "1234", + expectedType: "*apiclient.Acknowledgement", + shouldFail: false, + }, + { + id: "valid (long cid)", + cid: "/acknowledgement/1234", + expectedType: "*apiclient.Acknowledgement", + shouldFail: false, + }, } for _, test := range tests { @@ -189,15 +205,29 @@ func TestUpdateAcknowledgement(t *testing.T) { apih, server := acknowledgementTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Acknowledgement - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid acknowledgement config (nil)"}, - {"invalid (cid)", &Acknowledgement{CID: "/invalid"}, true, "invalid acknowledgement CID (/invalid)"}, - {"valid", &testAcknowledgement, false, ""}, + { + id: "invalid (nil)", + cfg: nil, + shouldFail: true, + expectedErr: "invalid acknowledgement config (nil)", + }, + { + id: "invalid (cid)", + cfg: &Acknowledgement{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid acknowledgement CID (/invalid)", + }, + { + id: "valid", + cfg: &testAcknowledgement, + shouldFail: false, + }, } for _, test := range tests { @@ -223,16 +253,33 @@ func TestCreateAcknowledgement(t *testing.T) { apih, server := acknowledgementTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Acknowledgement + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid acknowledgement config (nil)"}, - {"invalid (cid)", &Acknowledgement{CID: "/invalid"}, "", true, "invalid acknowledgement CID (/invalid)"}, - {"valid", &testAcknowledgement, "*apiclient.Acknowledgement", false, ""}, + { + id: "invalid (nil)", + cfg: nil, + expectedType: "", + shouldFail: true, + expectedErr: "invalid acknowledgement config (nil)", + }, + { + id: "invalid (cid)", + cfg: &Acknowledgement{CID: "/invalid"}, + expectedType: "", + shouldFail: true, + expectedErr: "invalid acknowledgement CID (/invalid)", + }, + { + id: "valid", + cfg: &testAcknowledgement, + expectedType: "*apiclient.Acknowledgement", + shouldFail: false, + }, } for _, test := range tests { @@ -264,18 +311,42 @@ func TestSearchAcknowledgement(t *testing.T) { search := SearchQueryType(`(notes="something")`) filter := SearchFilterType(map[string][]string{"f__active": {"true"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + search: nil, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + search: nil, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/alert_test.go b/alert_test.go index 6e24beb..5c52448 100644 --- a/alert_test.go +++ b/alert_test.go @@ -118,16 +118,32 @@ func TestFetchAlert(t *testing.T) { apih, server := alertTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid alert CID (none)"}, - {"short cid", "1234", "*apiclient.Alert", false, ""}, - {"long cid", "/alert/1234", "*apiclient.Alert", false, ""}, + { + id: "empty cid", + cid: "", + expectedType: "", + shouldFail: true, + expectedErr: "invalid alert CID (none)", + }, + { + id: "short cid", + cid: "1234", + expectedType: "*apiclient.Alert", + shouldFail: false, + }, + { + id: "long cid", + cid: "/alert/1234", + expectedType: "*apiclient.Alert", + shouldFail: false, + }, } for _, test := range tests { @@ -173,18 +189,42 @@ func TestSearchAlerts(t *testing.T) { search := SearchQueryType(`(host="somehost.example.com")`) filter := SearchFilterType(map[string][]string{"f__cleared_on": {"null"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + search: nil, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + search: nil, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/annotation_test.go b/annotation_test.go index b872ff1..1c03fad 100644 --- a/annotation_test.go +++ b/annotation_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -43,7 +43,7 @@ func testAnnotationServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -88,7 +88,7 @@ func testAnnotationServer() *httptest.Server { } case "POST": defer r.Body.Close() - _, err := ioutil.ReadAll(r.Body) + _, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -141,16 +141,32 @@ func TestFetchAnnotation(t *testing.T) { apih, server := annotationTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid annotation CID (none)"}, - {"short cid", "1234", "*apiclient.Annotation", false, ""}, - {"long cid", "/annotation/1234", "*apiclient.Annotation", false, ""}, + { + id: "empty cid", + cid: "", + expectedType: "", + shouldFail: true, + expectedErr: "invalid annotation CID (none)", + }, + { + id: "short cid", + cid: "1234", + expectedType: "*apiclient.Annotation", + shouldFail: false, + }, + { + id: "long cid", + cid: "/annotation/1234", + expectedType: "*apiclient.Annotation", + shouldFail: false, + }, } for _, test := range tests { @@ -192,15 +208,29 @@ func TestUpdateAnnotation(t *testing.T) { apih, server := annotationTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Annotation - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid annotation config (nil)"}, - {"invalid (cid)", &Annotation{CID: "/invalid"}, true, "invalid annotation CID (/invalid)"}, - {"valid", &testAnnotation, false, ""}, + { + id: "invalid (nil)", + cfg: nil, + shouldFail: true, + expectedErr: "invalid annotation config (nil)", + }, + { + id: "invalid (cid)", + cfg: &Annotation{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid annotation CID (/invalid)", + }, + { + id: "valid", + cfg: &testAnnotation, + shouldFail: false, + }, } for _, test := range tests { @@ -226,15 +256,26 @@ func TestCreateAnnotation(t *testing.T) { apih, server := annotationTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Annotation + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid annotation config (nil)"}, - {"valid", &testAnnotation, "*apiclient.Annotation", false, ""}, + { + id: "invalid (nil)", + cfg: nil, + expectedType: "", + shouldFail: true, + expectedErr: "invalid annotation config (nil)", + }, + { + id: "valid", + cfg: &testAnnotation, + expectedType: "*apiclient.Annotation", + shouldFail: false, + }, } for _, test := range tests { @@ -262,14 +303,23 @@ func TestDeleteAnnotation(t *testing.T) { apih, server := annotationTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Annotation - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid annotation config (nil)"}, - {"valid", &testAnnotation, false, ""}, + { + id: "invalid (nil)", + cfg: nil, + shouldFail: true, + expectedErr: "invalid annotation config (nil)", + }, + { + id: "valid", + cfg: &testAnnotation, + shouldFail: false, + }, } for _, test := range tests { @@ -297,15 +347,28 @@ func TestDeleteAnnotationByCID(t *testing.T) { apih, server := annotationTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", true, "invalid annotation CID (none)"}, - {"short cid", "1234", false, ""}, - {"long cid", "/annotation/1234", false, ""}, + { + id: "empty cid", + cid: "", + shouldFail: true, + expectedErr: "invalid annotation CID (none)", + }, + { + id: "short cid", + cid: "1234", + shouldFail: false, + }, + { + id: "long cid", + cid: "/annotation/1234", + shouldFail: false, + }, } for _, test := range tests { @@ -337,18 +400,42 @@ func TestSearchAnnotations(t *testing.T) { search := SearchQueryType(`(category="updates")`) filter := SearchFilterType(map[string][]string{"f__created_gt": {"1483639916"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + search: nil, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + search: nil, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/broker_test.go b/broker_test.go index 917c8b4..31f07dc 100644 --- a/broker_test.go +++ b/broker_test.go @@ -120,16 +120,32 @@ func TestFetchBroker(t *testing.T) { apih, server := brokerTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid broker CID (none)"}, - {"short cid", "1234", "*apiclient.Broker", false, ""}, - {"long cid", "/broker/1234", "*apiclient.Broker", false, ""}, + { + id: "empty cid", + cid: "", + expectedType: "", + shouldFail: true, + expectedErr: "invalid broker CID (none)", + }, + { + id: "short cid", + cid: "1234", + expectedType: "*apiclient.Broker", + shouldFail: false, + }, + { + id: "long cid", + cid: "/broker/1234", + expectedType: "*apiclient.Broker", + shouldFail: false, + }, } for _, test := range tests { @@ -175,18 +191,42 @@ func TestSearchBrokers(t *testing.T) { search := SearchQueryType("httptrap") filter := SearchFilterType{"f__type": []string{"enterprise"}} - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + search: nil, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + search: nil, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/check_bundle.go b/check_bundle.go index 676570d..e9ce719 100644 --- a/check_bundle.go +++ b/check_bundle.go @@ -12,6 +12,7 @@ import ( "fmt" "net/url" "regexp" + "sort" "strings" "github.com/circonus-labs/go-apiclient/config" @@ -137,6 +138,10 @@ func (a *API) UpdateCheckBundle(cfg *CheckBundle) (*CheckBundle, error) { return nil, errors.Errorf("invalid check bundle CID (%s)", bundleCID) } + if len(cfg.Tags) > 0 { + cfg.Tags = fixTags(cfg.Tags) + } + jsonCfg, err := json.Marshal(cfg) if err != nil { return nil, err @@ -166,14 +171,7 @@ func (a *API) CreateCheckBundle(cfg *CheckBundle) (*CheckBundle, error) { } if len(cfg.Tags) > 0 { - // remove blanks - tags := make([]string, 0, len(cfg.Tags)) - for _, tag := range cfg.Tags { - if tag != "" { - tags = append(tags, tag) - } - } - cfg.Tags = tags + cfg.Tags = fixTags(cfg.Tags) } jsonCfg, err := json.Marshal(cfg) @@ -276,3 +274,32 @@ func (a *API) SearchCheckBundles(searchCriteria *SearchQueryType, filterCriteria return &results, nil } + +func fixTags(tags []string) []string { + if len(tags) == 0 { + return tags + } + + unique := make(map[string]bool) + var result []string + + for _, tag := range tags { + // remove blanks + if tag == "" { + continue + } + + // lowercase + tag = strings.ToLower(tag) + + // remove duplicates + if _, found := unique[tag]; !found { + unique[tag] = true + result = append(result, tag) + } + } + + sort.Strings(result) + + return result +} diff --git a/check_bundle_metrics_test.go b/check_bundle_metrics_test.go index 65f7769..152f014 100644 --- a/check_bundle_metrics_test.go +++ b/check_bundle_metrics_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -42,7 +42,7 @@ func testCheckBundleMetricsServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -84,16 +84,32 @@ func TestFetchCheckBundleMetrics(t *testing.T) { apih, server := checkBundleMetricsTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid check bundle metrics CID (none)"}, - {"short cid", "1234", "*apiclient.CheckBundleMetrics", false, ""}, - {"long cid", "/check_bundle_metrics/1234", "*apiclient.CheckBundleMetrics", false, ""}, + { + id: "empty cid", + cid: "", + expectedType: "", + shouldFail: true, + expectedErr: "invalid check bundle metrics CID (none)", + }, + { + id: "short cid", + cid: "1234", + expectedType: "*apiclient.CheckBundleMetrics", + shouldFail: false, + }, + { + id: "long cid", + cid: "/check_bundle_metrics/1234", + expectedType: "*apiclient.CheckBundleMetrics", + shouldFail: false, + }, } for _, test := range tests { @@ -121,15 +137,29 @@ func TestUpdateCheckBundleMetrics(t *testing.T) { apih, server := checkBundleMetricsTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *CheckBundleMetrics - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid check bundle metrics config (nil)"}, - {"invalid (cid)", &CheckBundleMetrics{CID: "/invalid"}, true, "invalid check bundle metrics CID (/invalid)"}, - {"valid", &testCheckBundleMetrics, false, ""}, + { + id: "invalid (nil)", + cfg: nil, + shouldFail: true, + expectedErr: "invalid check bundle metrics config (nil)", + }, + { + id: "invalid (cid)", + cfg: &CheckBundleMetrics{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid check bundle metrics CID (/invalid)", + }, + { + id: "valid", + cfg: &testCheckBundleMetrics, + shouldFail: false, + }, } for _, test := range tests { diff --git a/check_bundle_test.go b/check_bundle_test.go index 66bbabf..e5d1009 100644 --- a/check_bundle_test.go +++ b/check_bundle_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -48,7 +48,7 @@ func testCheckBundleServer() *httptest.Server { switch r.Method { case "PUT": // update defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -101,7 +101,7 @@ func testCheckBundleServer() *httptest.Server { } case "POST": // create defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -150,16 +150,32 @@ func TestFetchCheckBundle(t *testing.T) { apih, server := checkBundleTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid check bundle CID (none)"}, - {"short cid", "1234", "*apiclient.CheckBundle", false, ""}, - {"long cid", "/check_bundle/1234", "*apiclient.CheckBundle", false, ""}, + { + id: "empty cid", + cid: "", + expectedType: "", + shouldFail: true, + expectedErr: "invalid check bundle CID (none)", + }, + { + id: "short cid", + cid: "1234", + expectedType: "*apiclient.CheckBundle", + shouldFail: false, + }, + { + id: "long cid", + cid: "/check_bundle/1234", + expectedType: "*apiclient.CheckBundle", + shouldFail: false, + }, } for _, test := range tests { @@ -201,15 +217,29 @@ func TestUpdateCheckBundle(t *testing.T) { apih, server := checkBundleTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *CheckBundle - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid check bundle config (nil)"}, - {"invalid (cid)", &CheckBundle{CID: "/invalid"}, true, "invalid check bundle CID (/invalid)"}, - {"valid", &testCheckBundle, false, ""}, + { + id: "invalid (nil)", + cfg: nil, + shouldFail: true, + expectedErr: "invalid check bundle config (nil)", + }, + { + id: "invalid (cid)", + cfg: &CheckBundle{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid check bundle CID (/invalid)", + }, + { + id: "valid", + cfg: &testCheckBundle, + shouldFail: false, + }, } for _, test := range tests { @@ -235,15 +265,26 @@ func TestCreateCheckBundle(t *testing.T) { apih, server := checkBundleTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *CheckBundle + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid check bundle config (nil)"}, - {"valid", &testCheckBundle, "*apiclient.CheckBundle", false, ""}, + { + id: "invalid (nil)", + cfg: nil, + expectedType: "", + shouldFail: true, + expectedErr: "invalid check bundle config (nil)", + }, + { + id: "valid", + cfg: &testCheckBundle, + expectedType: "*apiclient.CheckBundle", + shouldFail: false, + }, } for _, test := range tests { @@ -271,14 +312,23 @@ func TestDeleteCheckBundle(t *testing.T) { apih, server := checkBundleTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *CheckBundle - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid check bundle config (nil)"}, - {"valid", &testCheckBundle, false, ""}, + { + id: "invalid (nil)", + cfg: nil, + shouldFail: true, + expectedErr: "invalid check bundle config (nil)", + }, + { + id: "valid", + cfg: &testCheckBundle, + shouldFail: false, + }, } for _, test := range tests { @@ -306,15 +356,28 @@ func TestDeleteCheckBundleByCID(t *testing.T) { apih, server := checkBundleTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", true, "invalid check bundle CID (none)"}, - {"short cid", "1234", false, ""}, - {"long cid", "/check_bundle/1234", false, ""}, + { + id: "empty cid", + cid: "", + shouldFail: true, + expectedErr: "invalid check bundle CID (none)", + }, + { + id: "short cid", + cid: "1234", + shouldFail: false, + }, + { + id: "long cid", + cid: "/check_bundle/1234", + shouldFail: false, + }, } for _, test := range tests { @@ -346,18 +409,42 @@ func TestSearchCheckBundles(t *testing.T) { search := SearchQueryType("test") filter := SearchFilterType(map[string][]string{"f__tags_has": {"cat:tag"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + search: nil, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + search: nil, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { @@ -380,3 +467,45 @@ func TestSearchCheckBundles(t *testing.T) { }) } } + +func Test_fixTags(t *testing.T) { + tests := []struct { + name string + tags []string + want []string + }{ + { + name: "blank", + tags: []string{"foo", ""}, + want: []string{"foo"}, + }, + { + name: "duplicate", + tags: []string{"foo", "foo"}, + want: []string{"foo"}, + }, + { + name: "lowercase", + tags: []string{"Foo"}, + want: []string{"foo"}, + }, + { + name: "combo1", + tags: []string{"FOO", "foo", ""}, + want: []string{"foo"}, + }, + { + name: "combo2", + tags: []string{"FOO:BAR", "foo", ""}, + want: []string{"foo", "foo:bar"}, + }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + if got := fixTags(tt.tags); !reflect.DeepEqual(got, tt.want) { + t.Errorf("fixTags() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/check_test.go b/check_test.go index 69a113f..f00b005 100644 --- a/check_test.go +++ b/check_test.go @@ -109,16 +109,32 @@ func TestFetchCheck(t *testing.T) { apih, server := checkTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid check CID (none)"}, - {"short cid", "1234", "*apiclient.Check", false, ""}, - {"long cid", "/check/1234", "*apiclient.Check", false, ""}, + { + id: "empty cid", + cid: "", + expectedType: "", + shouldFail: true, + expectedErr: "invalid check CID (none)", + }, + { + id: "short cid", + cid: "1234", + expectedType: "*apiclient.Check", + shouldFail: false, + }, + { + id: "long cid", + cid: "/check/1234", + expectedType: "*apiclient.Check", + shouldFail: false, + }, } for _, test := range tests { @@ -164,18 +180,42 @@ func TestSearchChecks(t *testing.T) { search := SearchQueryType("test") filter := SearchFilterType{"f__tags_has": []string{"cat:tag"}} - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + search: nil, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + search: nil, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/contact_group_test.go b/contact_group_test.go index 07485bf..a0241e5 100644 --- a/contact_group_test.go +++ b/contact_group_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -79,7 +79,7 @@ func testContactGroupServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -124,7 +124,7 @@ func testContactGroupServer() *httptest.Server { } case "POST": defer r.Body.Close() - _, err := ioutil.ReadAll(r.Body) + _, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -176,16 +176,32 @@ func TestFetchContactGroup(t *testing.T) { apih, server := contactGroupTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid contact group CID (none)"}, - {"short cid", "1234", "*apiclient.ContactGroup", false, ""}, - {"long cid", "/contact_group/1234", "*apiclient.ContactGroup", false, ""}, + { + id: "empty cid", + cid: "", + expectedType: "", + shouldFail: true, + expectedErr: "invalid contact group CID (none)", + }, + { + id: "short cid", + cid: "1234", + expectedType: "*apiclient.ContactGroup", + shouldFail: false, + }, + { + id: "long cid", + cid: "/contact_group/1234", + expectedType: "*apiclient.ContactGroup", + shouldFail: false, + }, } for _, test := range tests { @@ -228,15 +244,29 @@ func TestUpdateContactGroup(t *testing.T) { apih, server := contactGroupTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *ContactGroup - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid contact group config (nil)"}, - {"invalid (cid)", &ContactGroup{CID: "/invalid"}, true, "invalid contact group CID (/invalid)"}, - {"valid", &testContactGroup, false, ""}, + { + id: "invalid (nil)", + cfg: nil, + shouldFail: true, + expectedErr: "invalid contact group config (nil)", + }, + { + id: "invalid (cid)", + cfg: &ContactGroup{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid contact group CID (/invalid)", + }, + { + id: "valid", + cfg: &testContactGroup, + shouldFail: false, + }, } for _, test := range tests { @@ -262,15 +292,26 @@ func TestCreateContactGroup(t *testing.T) { apih, server := contactGroupTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *ContactGroup + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid contact group config (nil)"}, - {"valid", &testContactGroup, "*apiclient.ContactGroup", false, ""}, + { + id: "invalid (nil)", + cfg: nil, + expectedType: "", + shouldFail: true, + expectedErr: "invalid contact group config (nil)", + }, + { + id: "valid", + cfg: &testContactGroup, + expectedType: "*apiclient.ContactGroup", + shouldFail: false, + }, } for _, test := range tests { @@ -298,14 +339,23 @@ func TestDeleteContactGroup(t *testing.T) { apih, server := contactGroupTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *ContactGroup - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid contact group config (nil)"}, - {"valid", &testContactGroup, false, ""}, + { + id: "invalid (nil)", + cfg: nil, + shouldFail: true, + expectedErr: "invalid contact group config (nil)", + }, + { + id: "valid", + cfg: &testContactGroup, + shouldFail: false, + }, } for _, test := range tests { @@ -333,15 +383,28 @@ func TestDeleteContactGroupByCID(t *testing.T) { apih, server := contactGroupTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", true, "invalid contact group CID (none)"}, - {"short cid", "1234", false, ""}, - {"long cid", "/contact_group/1234", false, ""}, + { + id: "empty cid", + cid: "", + shouldFail: true, + expectedErr: "invalid contact group CID (none)", + }, + { + id: "short cid", + cid: "1234", + shouldFail: false, + }, + { + id: "long cid", + cid: "/contact_group/1234", + shouldFail: false, + }, } for _, test := range tests { @@ -373,18 +436,42 @@ func TestSearchContactGroups(t *testing.T) { search := SearchQueryType(`(name="ops")`) filter := SearchFilterType(map[string][]string{"f__last_modified_gt": {"1483639916"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + search: nil, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + search: nil, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/dashboard_test.go b/dashboard_test.go index b4c4378..b5b4eab 100644 --- a/dashboard_test.go +++ b/dashboard_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "os" @@ -459,7 +459,7 @@ func testDashboardServer() *httptest.Server { fmt.Fprintln(w, jsondash) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -504,7 +504,7 @@ func testDashboardServer() *httptest.Server { } case "POST": defer r.Body.Close() - _, err := ioutil.ReadAll(r.Body) + _, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -557,16 +557,32 @@ func TestFetchDashboard(t *testing.T) { apih, server := dashboardTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid dashboard CID (none)"}, - {"short cid", "1234", "*apiclient.Dashboard", false, ""}, - {"long cid", "/dashboard/1234", "*apiclient.Dashboard", false, ""}, + { + id: "empty cid", + cid: "", + expectedType: "", + shouldFail: true, + expectedErr: "invalid dashboard CID (none)", + }, + { + id: "short cid", + cid: "1234", + expectedType: "*apiclient.Dashboard", + shouldFail: false, + }, + { + id: "long cid", + cid: "/dashboard/1234", + expectedType: "*apiclient.Dashboard", + shouldFail: false, + }, } for _, test := range tests { @@ -614,15 +630,29 @@ func TestUpdateDashboard(t *testing.T) { apih, server := dashboardTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Dashboard - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid dashboard config (nil)"}, - {"invalid (cid)", &Dashboard{CID: "/invalid"}, true, "invalid dashboard CID (/invalid)"}, - {"valid", &testDashboard, false, ""}, + { + id: "invalid (nil)", + cfg: nil, + shouldFail: true, + expectedErr: "invalid dashboard config (nil)", + }, + { + id: "invalid (cid)", + cfg: &Dashboard{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid dashboard CID (/invalid)", + }, + { + id: "valid", + cfg: &testDashboard, + shouldFail: false, + }, } for _, test := range tests { @@ -648,15 +678,26 @@ func TestCreateDashboard(t *testing.T) { apih, server := dashboardTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Dashboard + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid dashboard config (nil)"}, - {"valid", &testDashboard, "*apiclient.Dashboard", false, ""}, + { + id: "invalid (nil)", + cfg: nil, + expectedType: "", + shouldFail: true, + expectedErr: "invalid dashboard config (nil)", + }, + { + id: "valid", + cfg: &testDashboard, + expectedType: "*apiclient.Dashboard", + shouldFail: false, + }, } for _, test := range tests { @@ -684,14 +725,23 @@ func TestDeleteDashboard(t *testing.T) { apih, server := dashboardTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Dashboard - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid dashboard config (nil)"}, - {"valid", &testDashboard, false, ""}, + { + id: "invalid (nil)", + cfg: nil, + shouldFail: true, + expectedErr: "invalid dashboard config (nil)", + }, + { + id: "valid", + cfg: &testDashboard, + shouldFail: false, + }, } for _, test := range tests { @@ -719,15 +769,28 @@ func TestDeleteDashboardByCID(t *testing.T) { apih, server := dashboardTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", true, "invalid dashboard CID (none)"}, - {"short cid", "1234", false, ""}, - {"long cid", "/dashboard/1234", false, ""}, + { + id: "empty cid", + cid: "", + shouldFail: true, + expectedErr: "invalid dashboard CID (none)", + }, + { + id: "short cid", + cid: "1234", + shouldFail: false, + }, + { + id: "long cid", + cid: "/dashboard/1234", + shouldFail: false, + }, } for _, test := range tests { @@ -759,18 +822,42 @@ func TestSearchDashboards(t *testing.T) { search := SearchQueryType("my dashboard") filter := SearchFilterType(map[string][]string{"f__created_gt": {"1483639916"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + search: nil, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + search: nil, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/doc.go b/doc.go index 2720714..d004324 100644 --- a/doc.go +++ b/doc.go @@ -8,56 +8,56 @@ Documentation at https://login.circonus.com/resources/api for more information. Raw REST methods - Get - retrieve existing item(s) - Put - update an existing item - Post - create a new item - Delete - remove an existing item + Get - retrieve existing item(s) + Put - update an existing item + Post - create a new item + Delete - remove an existing item Endpoints (supported) - Account https://login.circonus.com/resources/api/calls/account - Acknowledgement https://login.circonus.com/resources/api/calls/acknowledgement - Alert https://login.circonus.com/resources/api/calls/alert - Annotation https://login.circonus.com/resources/api/calls/annotation - Broker https://login.circonus.com/resources/api/calls/broker - Check https://login.circonus.com/resources/api/calls/check - Check Bundle https://login.circonus.com/resources/api/calls/check_bundle - Check Bundle Metrics https://login.circonus.com/resources/api/calls/check_bundle_metrics - Contact Group https://login.circonus.com/resources/api/calls/contact_group - Dashboard https://login.circonus.com/resources/api/calls/dashboard - Graph https://login.circonus.com/resources/api/calls/graph - Maintenance [window] https://login.circonus.com/resources/api/calls/maintenance - Metric https://login.circonus.com/resources/api/calls/metric - Metric Cluster https://login.circonus.com/resources/api/calls/metric_cluster - Outlier Report https://login.circonus.com/resources/api/calls/outlier_report - Provision Broker https://login.circonus.com/resources/api/calls/provision_broker - Rule Set https://login.circonus.com/resources/api/calls/rule_set - Rule Set Group https://login.circonus.com/resources/api/calls/rule_set_group - User https://login.circonus.com/resources/api/calls/user - Worksheet https://login.circonus.com/resources/api/calls/worksheet + Account https://login.circonus.com/resources/api/calls/account + Acknowledgement https://login.circonus.com/resources/api/calls/acknowledgement + Alert https://login.circonus.com/resources/api/calls/alert + Annotation https://login.circonus.com/resources/api/calls/annotation + Broker https://login.circonus.com/resources/api/calls/broker + Check https://login.circonus.com/resources/api/calls/check + Check Bundle https://login.circonus.com/resources/api/calls/check_bundle + Check Bundle Metrics https://login.circonus.com/resources/api/calls/check_bundle_metrics + Contact Group https://login.circonus.com/resources/api/calls/contact_group + Dashboard https://login.circonus.com/resources/api/calls/dashboard + Graph https://login.circonus.com/resources/api/calls/graph + Maintenance [window] https://login.circonus.com/resources/api/calls/maintenance + Metric https://login.circonus.com/resources/api/calls/metric + Metric Cluster https://login.circonus.com/resources/api/calls/metric_cluster + Outlier Report https://login.circonus.com/resources/api/calls/outlier_report + Provision Broker https://login.circonus.com/resources/api/calls/provision_broker + Rule Set https://login.circonus.com/resources/api/calls/rule_set + Rule Set Group https://login.circonus.com/resources/api/calls/rule_set_group + User https://login.circonus.com/resources/api/calls/user + Worksheet https://login.circonus.com/resources/api/calls/worksheet Endpoints (not supported) - Support may be added for these endpoints in the future. These endpoints may currently be used - directly with the Raw REST methods above. + Support may be added for these endpoints in the future. These endpoints may currently be used + directly with the Raw REST methods above. - CAQL https://login.circonus.com/resources/api/calls/caql - Check Move https://login.circonus.com/resources/api/calls/check_move - Data https://login.circonus.com/resources/api/calls/data - Snapshot https://login.circonus.com/resources/api/calls/snapshot - Tag https://login.circonus.com/resources/api/calls/tag - Template https://login.circonus.com/resources/api/calls/template + CAQL https://login.circonus.com/resources/api/calls/caql + Check Move https://login.circonus.com/resources/api/calls/check_move + Data https://login.circonus.com/resources/api/calls/data + Snapshot https://login.circonus.com/resources/api/calls/snapshot + Tag https://login.circonus.com/resources/api/calls/tag + Template https://login.circonus.com/resources/api/calls/template Verbs - Fetch singular/plural item(s) - e.g. FetchAnnotation, FetchAnnotations - Create create new item - e.g. CreateAnnotation - Update update an item - e.g. UpdateAnnotation - Delete remove an item - e.g. DeleteAnnotation, DeleteAnnotationByCID - Search search for item(s) - e.g. SearchAnnotations - New new item config - e.g. NewAnnotation (returns an empty item, - any applicable defaults defined) + Fetch singular/plural item(s) - e.g. FetchAnnotation, FetchAnnotations + Create create new item - e.g. CreateAnnotation + Update update an item - e.g. UpdateAnnotation + Delete remove an item - e.g. DeleteAnnotation, DeleteAnnotationByCID + Search search for item(s) - e.g. SearchAnnotations + New new item config - e.g. NewAnnotation (returns an empty item, + any applicable defaults defined) - Not all endpoints support all verbs. + Not all endpoints support all verbs. */ package apiclient diff --git a/go.mod b/go.mod index 4dbad10..cd3922e 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,10 @@ module github.com/circonus-labs/go-apiclient -go 1.15 +go 1.17 require ( github.com/hashicorp/go-retryablehttp v0.7.1 github.com/pkg/errors v0.9.1 ) + +require github.com/hashicorp/go-cleanhttp v0.5.1 // indirect diff --git a/go.sum b/go.sum index 13bde92..d54224c 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,3 @@ -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/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM= github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= @@ -8,7 +7,5 @@ github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1 github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -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.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= diff --git a/graph.go b/graph.go index 0841b33..41d29c9 100644 --- a/graph.go +++ b/graph.go @@ -274,7 +274,7 @@ func (a *API) CreateGraph(cfg *Graph) (*Graph, error) { } if a.Debug { - a.Log.Printf("update graph, sending JSON: %s", string(jsonCfg)) + a.Log.Printf("create graph, sending JSON: %s", string(jsonCfg)) } result, err := a.Post(config.GraphPrefix, jsonCfg) @@ -282,6 +282,10 @@ func (a *API) CreateGraph(cfg *Graph) (*Graph, error) { return nil, errors.Wrap(err, "creating graph") } + if a.Debug { + a.Log.Printf("create graph, received JSON: %s", string(result)) + } + graph := &Graph{} if err := json.Unmarshal(result, graph); err != nil { return nil, errors.Wrap(err, "parsing graph") diff --git a/graph_test.go b/graph_test.go index 7a76f09..2144893 100644 --- a/graph_test.go +++ b/graph_test.go @@ -7,9 +7,10 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" + "os" "reflect" "testing" ) @@ -79,7 +80,7 @@ func testGraphServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -124,7 +125,7 @@ func testGraphServer() *httptest.Server { } case "POST": defer r.Body.Close() - _, err := ioutil.ReadAll(r.Body) + _, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -177,16 +178,32 @@ func TestFetchGraph(t *testing.T) { apih, server := graphTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid graph CID (none)"}, - {"short cid", "01234567-89ab-cdef-0123-456789abcdef", "*apiclient.Graph", false, ""}, - {"long cid", "/graph/01234567-89ab-cdef-0123-456789abcdef", "*apiclient.Graph", false, ""}, + { + id: "empty cid", + cid: "", + expectedType: "", + shouldFail: true, + expectedErr: "invalid graph CID (none)", + }, + { + id: "short cid", + cid: "01234567-89ab-cdef-0123-456789abcdef", + expectedType: "*apiclient.Graph", + shouldFail: false, + }, + { + id: "long cid", + cid: "/graph/01234567-89ab-cdef-0123-456789abcdef", + expectedType: "*apiclient.Graph", + shouldFail: false, + }, } for _, test := range tests { @@ -228,15 +245,29 @@ func TestUpdateGraph(t *testing.T) { apih, server := graphTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Graph - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid graph config (nil)"}, - {"invalid (cid)", &Graph{CID: "/invalid"}, true, "invalid graph CID (/invalid)"}, - {"valid", &testGraph, false, ""}, + { + id: "invalid (nil)", + cfg: nil, + shouldFail: true, + expectedErr: "invalid graph config (nil)", + }, + { + id: "invalid (cid)", + cfg: &Graph{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid graph CID (/invalid)", + }, + { + id: "valid", + cfg: &testGraph, + shouldFail: false, + }, } for _, test := range tests { @@ -262,15 +293,26 @@ func TestCreateGraph(t *testing.T) { apih, server := graphTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Graph + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid graph config (nil)"}, - {"valid", &testGraph, "*apiclient.Graph", false, ""}, + { + id: "invalid (nil)", + cfg: nil, + expectedType: "", + shouldFail: true, + expectedErr: "invalid graph config (nil)", + }, + { + id: "valid", + cfg: &testGraph, + expectedType: "*apiclient.Graph", + shouldFail: false, + }, } for _, test := range tests { @@ -298,14 +340,23 @@ func TestDeleteGraph(t *testing.T) { apih, server := graphTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Graph - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid graph config (nil)"}, - {"valid", &testGraph, false, ""}, + { + id: "invalid (nil)", + cfg: nil, + shouldFail: true, + expectedErr: "invalid graph config (nil)", + }, + { + id: "valid", + cfg: &testGraph, + shouldFail: false, + }, } for _, test := range tests { @@ -333,15 +384,28 @@ func TestDeleteGraphByCID(t *testing.T) { apih, server := graphTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", true, "invalid graph CID (none)"}, - {"short cid", "01234567-89ab-cdef-0123-456789abcdef", false, ""}, - {"long cid", "/graph/01234567-89ab-cdef-0123-456789abcdef", false, ""}, + { + id: "empty cid", + cid: "", + shouldFail: true, + expectedErr: "invalid graph CID (none)", + }, + { + id: "short cid", + cid: "01234567-89ab-cdef-0123-456789abcdef", + shouldFail: false, + }, + { + id: "long cid", + cid: "/graph/01234567-89ab-cdef-0123-456789abcdef", + shouldFail: false, + }, } for _, test := range tests { @@ -373,18 +437,42 @@ func TestSearchGraphs(t *testing.T) { search := SearchQueryType("CPU Utilization") filter := SearchFilterType(map[string][]string{"f__tags_has": {"os:rhel7"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + search: nil, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + filter: nil, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + search: nil, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { @@ -411,7 +499,7 @@ func TestSearchGraphs(t *testing.T) { func TestGraphOverlaySet(t *testing.T) { t.Log("testing graph overlay set struct") - testJSON, err := ioutil.ReadFile("testdata/graph_overlayset.json") + testJSON, err := os.ReadFile("testdata/graph_overlayset.json") if err != nil { t.Fatal(err) } diff --git a/main.go b/main.go index 8ecfe4d..e2e7e26 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build go1.13 -// +build go1.13 +//go:build go1.17 +// +build go1.17 package apiclient @@ -14,7 +14,7 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" + "io" "log" "math" "math/big" @@ -184,7 +184,7 @@ func New(ac *Config) (*API, error) { a.Log = log.New(os.Stdout, "", log.LstdFlags) } if a.Log == nil { - a.Log = log.New(ioutil.Discard, "", log.LstdFlags) + a.Log = log.New(io.Discard, "", log.LstdFlags) } a.maxRetries = maxRetries @@ -340,7 +340,7 @@ func (a *API) apiCall(reqMethod string, reqPath string, data []byte) ([]byte, er if resp.StatusCode == 0 || // wtf?! resp.StatusCode >= 500 || // rutroh resp.StatusCode == 429 { // rate limit - body, readErr := ioutil.ReadAll(resp.Body) + body, readErr := io.ReadAll(resp.Body) if readErr != nil { lastHTTPError = errors.Errorf("- response: %d %s", resp.StatusCode, readErr.Error()) } else { @@ -424,7 +424,7 @@ func (a *API) apiCall(reqMethod string, reqPath string, data []byte) ([]byte, er if a.Debug { client.Logger = a.Log } else { - client.Logger = log.New(ioutil.Discard, "", log.LstdFlags) + client.Logger = log.New(io.Discard, "", log.LstdFlags) } client.CheckRetry = retryPolicy @@ -438,7 +438,7 @@ func (a *API) apiCall(reqMethod string, reqPath string, data []byte) ([]byte, er } defer resp.Body.Close() // nolint: errcheck - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, errors.Wrap(err, "reading Circonus API response") } diff --git a/main_test.go b/main_test.go index 2b36562..2a6226a 100644 --- a/main_test.go +++ b/main_test.go @@ -106,20 +106,81 @@ func retryCallServer() *httptest.Server { func TestNew(t *testing.T) { - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Config + id string shouldFail bool }{ - {"invalid config (nil)", nil, true}, - {"invalid config (blank)", &Config{}, true}, - {"token - default app/url", &Config{TokenKey: "foo"}, false}, - {"token,app - default url", &Config{TokenKey: "foo", TokenApp: "bar"}, false}, - {"token,app,acctid - default url", &Config{TokenKey: "foo", TokenApp: "bar", TokenAccountID: "0"}, false}, - {"token,app,url(host)", &Config{TokenKey: "foo", TokenApp: "bar", URL: "foo.example.com"}, false}, - {"token,app,url(trailing /)", &Config{TokenKey: "foo", TokenApp: "bar", URL: "foo.example.com/path/"}, false}, - {"token,app,url(w/o trailing /)", &Config{TokenKey: "foo", TokenApp: "bar", URL: "foo.example.com/path"}, false}, - {"invalid (url)", &Config{TokenKey: "foo", TokenApp: "bar", URL: `http://foo.example.com\path`}, true}, + { + id: "invalid config (nil)", + cfg: nil, + shouldFail: true, + }, + { + id: "invalid config (blank)", + cfg: &Config{}, + shouldFail: true, + }, + { + id: "token - default app/url", + cfg: &Config{ + TokenKey: "foo", + }, + shouldFail: false, + }, + { + id: "token,app - default url", + cfg: &Config{ + TokenKey: "foo", + TokenApp: "bar", + }, + shouldFail: false, + }, + { + id: "token,app,acctid - default url", + cfg: &Config{ + TokenKey: "foo", + TokenApp: "bar", + TokenAccountID: "0", + }, + shouldFail: false, + }, + { + id: "token,app,url(host)", + cfg: &Config{ + TokenKey: "foo", + TokenApp: "bar", + URL: "foo.example.com", + }, + shouldFail: false, + }, + { + id: "token,app,url(trailing /)", + cfg: &Config{ + TokenKey: "foo", + TokenApp: "bar", + URL: "foo.example.com/path/", + }, + shouldFail: false, + }, + { + id: "token,app,url(w/o trailing /)", + cfg: &Config{ + TokenKey: "foo", + TokenApp: "bar", + URL: "foo.example.com/path", + }, + shouldFail: false, + }, + { + id: "invalid (url)", + cfg: &Config{ + TokenKey: "foo", + TokenApp: "bar", + URL: `http://foo.example.com\path`, + }, + shouldFail: true, + }, } for _, test := range tests { diff --git a/maintenance_test.go b/maintenance_test.go index 72c5175..6823498 100644 --- a/maintenance_test.go +++ b/maintenance_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -43,7 +43,7 @@ func testMaintenanceServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -88,7 +88,7 @@ func testMaintenanceServer() *httptest.Server { } case "POST": defer r.Body.Close() - _, err := ioutil.ReadAll(r.Body) + _, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -141,16 +141,30 @@ func TestFetchMaintenanceWindow(t *testing.T) { apih, server := maintenanceTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid maintenance window CID (none)"}, - {"short cid", "1234", "*apiclient.Maintenance", false, ""}, - {"long cid", "/maintenance/1234", "*apiclient.Maintenance", false, ""}, + { + id: "empty cid", + shouldFail: true, + expectedErr: "invalid maintenance window CID (none)", + }, + { + id: "short cid", + cid: "1234", + expectedType: "*apiclient.Maintenance", + shouldFail: false, + }, + { + id: "long cid", + cid: "/maintenance/1234", + expectedType: "*apiclient.Maintenance", + shouldFail: false, + }, } for _, test := range tests { @@ -192,16 +206,30 @@ func TestUpdateMaintenanceWindow(t *testing.T) { apih, server := maintenanceTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Maintenance + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid maintenance window config (nil)"}, - {"invalid (cid)", &Maintenance{CID: "/invalid"}, "", true, "invalid maintenance window CID (/invalid)"}, - {"valid", &testMaintenance, "*apiclient.Maintenance", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid maintenance window config (nil)", + }, + { + id: "invalid (cid)", + cfg: &Maintenance{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid maintenance window CID (/invalid)", + }, + { + id: "valid", + cfg: &testMaintenance, + expectedType: "*apiclient.Maintenance", + shouldFail: false, + }, } for _, test := range tests { @@ -229,15 +257,24 @@ func TestCreateMaintenanceWindow(t *testing.T) { apih, server := maintenanceTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Maintenance + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid maintenance window config (nil)"}, - {"valid", &testMaintenance, "*apiclient.Maintenance", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid maintenance window config (nil)", + }, + { + id: "valid", + cfg: &testMaintenance, + expectedType: "*apiclient.Maintenance", + shouldFail: false, + }, } for _, test := range tests { @@ -265,14 +302,22 @@ func TestDeleteMaintenanceWindow(t *testing.T) { apih, server := maintenanceTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Maintenance - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid maintenance window config (nil)"}, - {"valid", &testMaintenance, false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid maintenance window config (nil)", + }, + { + id: "valid", + cfg: &testMaintenance, + shouldFail: false, + }, } for _, test := range tests { @@ -300,15 +345,27 @@ func TestDeleteMaintenanceWindowByCID(t *testing.T) { apih, server := maintenanceTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", true, "invalid maintenance window CID (none)"}, - {"short cid", "1234", false, ""}, - {"long cid", "/maintenance/1234", false, ""}, + { + id: "empty cid", + shouldFail: true, + expectedErr: "invalid maintenance window CID (none)", + }, + { + id: "short cid", + cid: "1234", + shouldFail: false, + }, + { + id: "long cid", + cid: "/maintenance/1234", + shouldFail: false, + }, } for _, test := range tests { @@ -340,18 +397,38 @@ func TestSearchMaintenances(t *testing.T) { search := SearchQueryType("/check_bundle/1234") filter := SearchFilterType(map[string][]string{"f_start_gt": {"1483639916"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/metric_cluster_test.go b/metric_cluster_test.go index 07bd533..a3a9b4d 100644 --- a/metric_cluster_test.go +++ b/metric_cluster_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -36,7 +36,7 @@ func testMetricClusterServer() *httptest.Server { switch r.Method { case "PUT": // update defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -93,7 +93,7 @@ func testMetricClusterServer() *httptest.Server { } case "POST": // create defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -142,19 +142,45 @@ func TestFetchMetricCluster(t *testing.T) { apih, server := metricClusterTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string extras string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", "", true, "invalid metric cluster CID (none)"}, - {"short cid", "1234", "", "*apiclient.MetricCluster", false, ""}, - {"long cid", "/metric_cluster/1234", "", "*apiclient.MetricCluster", false, ""}, - {"cid xtra/metrics", "/metric_cluster/1234", "metrics", "*apiclient.MetricCluster", false, ""}, - {"cid xtra/uuids", "/metric_cluster/1234", "uuids", "*apiclient.MetricCluster", false, ""}, + { + id: "empty cid", + shouldFail: true, + expectedErr: "invalid metric cluster CID (none)", + }, + { + id: "short cid", + cid: "1234", + expectedType: "*apiclient.MetricCluster", + shouldFail: false, + }, + { + id: "long cid", + cid: "/metric_cluster/1234", + expectedType: "*apiclient.MetricCluster", + shouldFail: false, + }, + { + id: "cid xtra/metrics", + cid: "/metric_cluster/1234", + extras: "metrics", + expectedType: "*apiclient.MetricCluster", + shouldFail: false, + }, + { + id: "cid xtra/uuids", + cid: "/metric_cluster/1234", + extras: "uuids", + expectedType: "*apiclient.MetricCluster", + shouldFail: false, + }, } for _, test := range tests { @@ -182,16 +208,30 @@ func TestFetchMetricClusters(t *testing.T) { apih, server := metricClusterTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string extras string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no extras", "", "*[]apiclient.MetricCluster", false, ""}, - {"xtra/metrics", "metrics", "*[]apiclient.MetricCluster", false, ""}, - {"xtra/uuids", "uuids", "*[]apiclient.MetricCluster", false, ""}, + { + id: "no extras", + expectedType: "*[]apiclient.MetricCluster", + shouldFail: false, + }, + { + id: "xtra/metrics", + extras: "metrics", + expectedType: "*[]apiclient.MetricCluster", + shouldFail: false, + }, + { + id: "xtra/uuids", + extras: "uuids", + expectedType: "*[]apiclient.MetricCluster", + shouldFail: false, + }, } for _, test := range tests { @@ -219,16 +259,30 @@ func TestUpdateMetricCluster(t *testing.T) { apih, server := metricClusterTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *MetricCluster + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid metric cluster config (nil)"}, - {"invalid (cid)", &MetricCluster{CID: "/invalid"}, "", true, "invalid metric cluster CID (/invalid)"}, - {"valid", &testMetricCluster, "*apiclient.MetricCluster", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid metric cluster config (nil)", + }, + { + id: "invalid (cid)", + cfg: &MetricCluster{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid metric cluster CID (/invalid)", + }, + { + id: "valid", + cfg: &testMetricCluster, + expectedType: "*apiclient.MetricCluster", + shouldFail: false, + }, } for _, test := range tests { @@ -256,15 +310,24 @@ func TestCreateMetricCluster(t *testing.T) { apih, server := metricClusterTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *MetricCluster + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid metric cluster config (nil)"}, - {"valid", &testMetricCluster, "*apiclient.MetricCluster", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid metric cluster config (nil)", + }, + { + id: "valid", + cfg: &testMetricCluster, + expectedType: "*apiclient.MetricCluster", + shouldFail: false, + }, } for _, test := range tests { @@ -292,14 +355,22 @@ func TestDeleteMetricCluster(t *testing.T) { apih, server := metricClusterTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *MetricCluster - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid metric cluster config (nil)"}, - {"valid", &testMetricCluster, false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid metric cluster config (nil)", + }, + { + id: "valid", + cfg: &testMetricCluster, + shouldFail: false, + }, } for _, test := range tests { @@ -327,15 +398,27 @@ func TestDeleteMetricClusterByCID(t *testing.T) { apih, server := metricClusterTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", true, "invalid metric cluster CID (none)"}, - {"short cid", "1234", false, ""}, - {"long cid", "/metric_cluster/1234", false, ""}, + { + id: "empty cid", + shouldFail: true, + expectedErr: "invalid metric cluster CID (none)", + }, + { + id: "short cid", + cid: "1234", + shouldFail: false, + }, + { + id: "long cid", + cid: "/metric_cluster/1234", + shouldFail: false, + }, } for _, test := range tests { @@ -367,18 +450,38 @@ func TestSearchMetricClusters(t *testing.T) { search := SearchQueryType("web servers") filter := SearchFilterType(map[string][]string{"f_tags_has": {"dc:sfo1"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/metric_test.go b/metric_test.go index 60cc754..7165d79 100644 --- a/metric_test.go +++ b/metric_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -47,7 +47,7 @@ func testMetricServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -122,16 +122,30 @@ func TestFetchMetric(t *testing.T) { apih, server := metricTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid metric CID (none)"}, - {"short cid", "1234_foo", "*apiclient.Metric", false, ""}, - {"long cid", "/metric/1234_foo", "*apiclient.Metric", false, ""}, + { + id: "empty cid", + shouldFail: true, + expectedErr: "invalid metric CID (none)", + }, + { + id: "short cid", + cid: "1234_foo", + expectedType: "*apiclient.Metric", + shouldFail: false, + }, + { + id: "long cid", + cid: "/metric/1234_foo", + expectedType: "*apiclient.Metric", + shouldFail: false, + }, } for _, test := range tests { @@ -173,16 +187,30 @@ func TestUpdateMetric(t *testing.T) { apih, server := metricTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Metric + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid metric config (nil)"}, - {"invalid (cid)", &Metric{CID: "/invalid"}, "", true, "invalid metric CID (/invalid)"}, - {"valid", &testMetric, "*apiclient.Metric", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid metric config (nil)", + }, + { + id: "invalid (cid)", + cfg: &Metric{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid metric CID (/invalid)", + }, + { + id: "valid", + cfg: &testMetric, + expectedType: "*apiclient.Metric", + shouldFail: false, + }, } for _, test := range tests { @@ -214,18 +242,38 @@ func TestSearchMetrics(t *testing.T) { search := SearchQueryType("vm`memory`used") filter := SearchFilterType(map[string][]string{"f_tags_has": {"service:cache"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/outlier_report_test.go b/outlier_report_test.go index e0ae078..0b614c6 100644 --- a/outlier_report_test.go +++ b/outlier_report_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -44,7 +44,7 @@ func testOutlierReportServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -89,7 +89,7 @@ func testOutlierReportServer() *httptest.Server { } case "POST": defer r.Body.Close() - _, err := ioutil.ReadAll(r.Body) + _, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -142,16 +142,30 @@ func TestFetchOutlierReport(t *testing.T) { apih, server := outlierReportTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid outlier report CID (none)"}, - {"short cid", "1234", "*apiclient.OutlierReport", false, ""}, - {"long cid", "/outlier_report/1234", "*apiclient.OutlierReport", false, ""}, + { + id: "empty cid", + shouldFail: true, + expectedErr: "invalid outlier report CID (none)", + }, + { + id: "short cid", + cid: "1234", + expectedType: "*apiclient.OutlierReport", + shouldFail: false, + }, + { + id: "long cid", + cid: "/outlier_report/1234", + expectedType: "*apiclient.OutlierReport", + shouldFail: false, + }, } for _, test := range tests { @@ -194,16 +208,30 @@ func TestUpdateOutlierReport(t *testing.T) { apih, server := outlierReportTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *OutlierReport + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid outlier report config (nil)"}, - {"invalid (cid)", &OutlierReport{CID: "/invalid"}, "", true, "invalid outlier report CID (/invalid)"}, - {"valid", &testOutlierReport, "*apiclient.OutlierReport", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid outlier report config (nil)", + }, + { + id: "invalid (cid)", + cfg: &OutlierReport{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid outlier report CID (/invalid)", + }, + { + id: "valid", + cfg: &testOutlierReport, + expectedType: "*apiclient.OutlierReport", + shouldFail: false, + }, } for _, test := range tests { @@ -231,15 +259,24 @@ func TestCreateOutlierReport(t *testing.T) { apih, server := outlierReportTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *OutlierReport + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid outlier report config (nil)"}, - {"valid", &testOutlierReport, "*apiclient.OutlierReport", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid outlier report config (nil)", + }, + { + id: "valid", + cfg: &testOutlierReport, + expectedType: "*apiclient.OutlierReport", + shouldFail: false, + }, } for _, test := range tests { @@ -267,14 +304,22 @@ func TestDeleteOutlierReport(t *testing.T) { apih, server := outlierReportTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *OutlierReport - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid outlier report config (nil)"}, - {"valid", &testOutlierReport, false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid outlier report config (nil)", + }, + { + id: "valid", + cfg: &testOutlierReport, + shouldFail: false, + }, } for _, test := range tests { @@ -302,15 +347,27 @@ func TestDeleteOutlierReportByCID(t *testing.T) { apih, server := outlierReportTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", true, "invalid outlier report CID (none)"}, - {"short cid", "1234", false, ""}, - {"long cid", "/outlier_report/1234", false, ""}, + { + id: "empty cid", + shouldFail: true, + expectedErr: "invalid outlier report CID (none)", + }, + { + id: "short cid", + cid: "1234", + shouldFail: false, + }, + { + id: "long cid", + cid: "/outlier_report/1234", + shouldFail: false, + }, } for _, test := range tests { @@ -342,18 +399,38 @@ func TestSearchOutlierReports(t *testing.T) { search := SearchQueryType("requests per second") filter := SearchFilterType(map[string][]string{"f_tags_has": {"service:web"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/provision_broker_test.go b/provision_broker_test.go index c8deb33..89ecfb8 100644 --- a/provision_broker_test.go +++ b/provision_broker_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -50,7 +50,7 @@ func testProvisionBrokerServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -65,7 +65,7 @@ func testProvisionBrokerServer() *httptest.Server { switch r.Method { case "POST": defer r.Body.Close() - _, err := ioutil.ReadAll(r.Body) + _, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -118,16 +118,30 @@ func TestFetchProvisionBroker(t *testing.T) { apih, server := provisionBrokerTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid provision broker CID (none)"}, - {"short cid", "abc-1234", "*apiclient.ProvisionBroker", false, ""}, - {"long cid", "/provision_broker/abc-1234", "*apiclient.ProvisionBroker", false, ""}, + { + id: "empty cid", + shouldFail: true, + expectedErr: "invalid provision broker CID (none)", + }, + { + id: "short cid", + cid: "abc-1234", + expectedType: "*apiclient.ProvisionBroker", + shouldFail: false, + }, + { + id: "long cid", + cid: "/provision_broker/abc-1234", + expectedType: "*apiclient.ProvisionBroker", + shouldFail: false, + }, } for _, test := range tests { @@ -155,18 +169,39 @@ func TestUpdateProvisionBroker(t *testing.T) { apih, server := provisionBrokerTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { + cfg *ProvisionBroker id string cid string - cfg *ProvisionBroker expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (cid)", "", nil, "", true, "invalid provision broker CID (none)"}, - {"invalid (cfg)", "abc", nil, "", true, "invalid provision broker config (nil)"}, - {"invalid (cid)", "/invalid", &ProvisionBroker{}, "", true, "invalid provision broker CID (/invalid)"}, - {"valid", "/provision_broker/abc-1234", &testProvisionBroker, "*apiclient.ProvisionBroker", false, ""}, + { + id: "invalid (cid)", + shouldFail: true, + expectedErr: "invalid provision broker CID (none)", + }, + { + id: "invalid (cfg)", + cid: "abc", + shouldFail: true, + expectedErr: "invalid provision broker config (nil)", + }, + { + id: "invalid (cid)", + cid: "/invalid", + cfg: &ProvisionBroker{}, + shouldFail: true, + expectedErr: "invalid provision broker CID (/invalid)", + }, + { + id: "valid", + cid: "/provision_broker/abc-1234", + cfg: &testProvisionBroker, + expectedType: "*apiclient.ProvisionBroker", + shouldFail: false, + }, } for _, test := range tests { @@ -194,15 +229,24 @@ func TestCreateProvisionBroker(t *testing.T) { apih, server := provisionBrokerTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *ProvisionBroker + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid provision broker config (nil)"}, - {"valid", &testProvisionBroker, "*apiclient.ProvisionBroker", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid provision broker config (nil)", + }, + { + id: "valid", + cfg: &testProvisionBroker, + expectedType: "*apiclient.ProvisionBroker", + shouldFail: false, + }, } for _, test := range tests { diff --git a/rule_set_group_test.go b/rule_set_group_test.go index 69b66d1..ad37640 100644 --- a/rule_set_group_test.go +++ b/rule_set_group_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -70,7 +70,7 @@ func testRuleSetGroupServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -115,7 +115,7 @@ func testRuleSetGroupServer() *httptest.Server { } case "POST": defer r.Body.Close() - _, err := ioutil.ReadAll(r.Body) + _, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -168,16 +168,30 @@ func TestFetchRuleSetGroup(t *testing.T) { apih, server := ruleSetGroupTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid rule set group CID (none)"}, - {"short cid", "1234", "*apiclient.RuleSetGroup", false, ""}, - {"long cid", "/rule_set_group/1234", "*apiclient.RuleSetGroup", false, ""}, + { + id: "empty cid", + shouldFail: true, + expectedErr: "invalid rule set group CID (none)", + }, + { + id: "short cid", + cid: "1234", + expectedType: "*apiclient.RuleSetGroup", + shouldFail: false, + }, + { + id: "long cid", + cid: "/rule_set_group/1234", + expectedType: "*apiclient.RuleSetGroup", + shouldFail: false, + }, } for _, test := range tests { @@ -219,16 +233,30 @@ func TestUpdateRuleSetGroup(t *testing.T) { apih, server := ruleSetGroupTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *RuleSetGroup + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid rule set group config (nil)"}, - {"invalid (cid)", &RuleSetGroup{CID: "/invalid"}, "", true, "invalid rule set group CID (/invalid)"}, - {"valid", &testRuleSetGroup, "*apiclient.RuleSetGroup", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid rule set group config (nil)", + }, + { + id: "invalid (cid)", + cfg: &RuleSetGroup{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid rule set group CID (/invalid)", + }, + { + id: "valid", + cfg: &testRuleSetGroup, + expectedType: "*apiclient.RuleSetGroup", + shouldFail: false, + }, } for _, test := range tests { @@ -256,15 +284,24 @@ func TestCreateRuleSetGroup(t *testing.T) { apih, server := ruleSetGroupTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *RuleSetGroup + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid rule set group config (nil)"}, - {"valid", &testRuleSetGroup, "*apiclient.RuleSetGroup", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid rule set group config (nil)", + }, + { + id: "valid", + cfg: &testRuleSetGroup, + expectedType: "*apiclient.RuleSetGroup", + shouldFail: false, + }, } for _, test := range tests { @@ -292,14 +329,22 @@ func TestDeleteRuleSetGroup(t *testing.T) { apih, server := ruleSetGroupTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *RuleSetGroup - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid rule set group config (nil)"}, - {"valid", &testRuleSetGroup, false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid rule set group config (nil)", + }, + { + id: "valid", + cfg: &testRuleSetGroup, + shouldFail: false, + }, } for _, test := range tests { @@ -327,15 +372,27 @@ func TestDeleteRuleSetGroupByCID(t *testing.T) { apih, server := ruleSetGroupTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", true, "invalid rule set group CID (none)"}, - {"short cid", "1234", false, ""}, - {"long cid", "/rule_set_group/1234", false, ""}, + { + id: "empty cid", + shouldFail: true, + expectedErr: "invalid rule set group CID (none)", + }, + { + id: "short cid", + cid: "1234", + shouldFail: false, + }, + { + id: "long cid", + cid: "/rule_set_group/1234", + shouldFail: false, + }, } for _, test := range tests { @@ -367,18 +424,38 @@ func TestSearchRuleSetGroups(t *testing.T) { search := SearchQueryType("web requests") filter := SearchFilterType(map[string][]string{"f_tags_has": {"location:conus"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/rule_set_test.go b/rule_set_test.go index a877ab3..2c793ea 100644 --- a/rule_set_test.go +++ b/rule_set_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -104,7 +104,7 @@ func testRuleSetServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -130,7 +130,7 @@ func testRuleSetServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -175,7 +175,7 @@ func testRuleSetServer() *httptest.Server { } case "POST": defer r.Body.Close() - _, err := ioutil.ReadAll(r.Body) + _, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -228,18 +228,42 @@ func TestFetchRuleSet(t *testing.T) { apih, server := ruleSetTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid rule set CID (none)"}, - {"short (old) cid", "1234_tt_firstbyte", "*apiclient.RuleSet", false, ""}, - {"long (old) cid", "/rule_set/1234_tt_firstbyte", "*apiclient.RuleSet", false, ""}, - {"short (new) cid", "1234", "*apiclient.RuleSet", false, ""}, - {"long (new) cid", "/rule_set/1234", "*apiclient.RuleSet", false, ""}, + { + id: "empty cid", + shouldFail: true, + expectedErr: "invalid rule set CID (none)", + }, + { + id: "short (old) cid", + cid: "1234_tt_firstbyte", + expectedType: "*apiclient.RuleSet", + shouldFail: false, + }, + { + id: "long (old) cid", + cid: "/rule_set/1234_tt_firstbyte", + expectedType: "*apiclient.RuleSet", + shouldFail: false, + }, + { + id: "short (new) cid", + cid: "1234", + expectedType: "*apiclient.RuleSet", + shouldFail: false, + }, + { + id: "long (new) cid", + cid: "/rule_set/1234", + expectedType: "*apiclient.RuleSet", + shouldFail: false, + }, } for _, test := range tests { @@ -281,17 +305,36 @@ func TestUpdateRuleSet(t *testing.T) { apih, server := ruleSetTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *RuleSet + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid rule set config (nil)"}, - {"invalid (cid)", &RuleSet{CID: "/invalid"}, "", true, "invalid rule set CID (/invalid)"}, - {"valid", &testRuleSet, "*apiclient.RuleSet", false, ""}, - {"valid", &testRuleSetNewCID, "*apiclient.RuleSet", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid rule set config (nil)", + }, + { + id: "invalid (cid)", + cfg: &RuleSet{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid rule set CID (/invalid)", + }, + { + id: "valid", + cfg: &testRuleSet, + expectedType: "*apiclient.RuleSet", + shouldFail: false, + }, + { + id: "valid", + cfg: &testRuleSetNewCID, + expectedType: "*apiclient.RuleSet", + shouldFail: false, + }, } for _, test := range tests { @@ -319,16 +362,30 @@ func TestCreateRuleSet(t *testing.T) { apih, server := ruleSetTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *RuleSet + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid rule set config (nil)"}, - {"valid", &testRuleSet, "*apiclient.RuleSet", false, ""}, - {"valid", &testRuleSetNewCID, "*apiclient.RuleSet", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid rule set config (nil)", + }, + { + id: "valid", + cfg: &testRuleSet, + expectedType: "*apiclient.RuleSet", + shouldFail: false, + }, + { + id: "valid", + cfg: &testRuleSetNewCID, + expectedType: "*apiclient.RuleSet", + shouldFail: false, + }, } for _, test := range tests { @@ -356,15 +413,27 @@ func TestDeleteRuleSet(t *testing.T) { apih, server := ruleSetTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *RuleSet - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid rule set config (nil)"}, - {"valid", &testRuleSet, false, ""}, - {"valid", &testRuleSetNewCID, false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid rule set config (nil)", + }, + { + id: "valid", + cfg: &testRuleSet, + shouldFail: false, + }, + { + id: "valid", + cfg: &testRuleSetNewCID, + shouldFail: false, + }, } for _, test := range tests { @@ -392,17 +461,37 @@ func TestDeleteRuleSetByCID(t *testing.T) { apih, server := ruleSetTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", true, "invalid rule set CID (none)"}, - {"short (old) cid", "1234_tt_firstbyte", false, ""}, - {"long (old) cid", "/rule_set/1234_tt_firstbyte", false, ""}, - {"short (new) cid", "1234", false, ""}, - {"long (new) cid", "/rule_set/1234", false, ""}, + { + id: "empty cid", + shouldFail: true, + expectedErr: "invalid rule set CID (none)", + }, + { + id: "short (old) cid", + cid: "1234_tt_firstbyte", + shouldFail: false, + }, + { + id: "long (old) cid", + cid: "/rule_set/1234_tt_firstbyte", + shouldFail: false, + }, + { + id: "short (new) cid", + cid: "1234", + shouldFail: false, + }, + { + id: "long (new) cid", + cid: "/rule_set/1234", + shouldFail: false, + }, } for _, test := range tests { @@ -434,18 +523,38 @@ func TestSearchRuleSets(t *testing.T) { search := SearchQueryType("request`latency_ms") filter := SearchFilterType(map[string][]string{"f_tags_has": {"service:web"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/user_test.go b/user_test.go index 90d3729..a0fd14d 100644 --- a/user_test.go +++ b/user_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -45,7 +45,7 @@ func testUserServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -116,16 +116,30 @@ func TestFetchUser(t *testing.T) { apih, server := userTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"valid (default,empty)", "", "*apiclient.User", false, ""}, - {"valid (short cid)", "1234", "*apiclient.User", false, ""}, - {"valid (long cid)", "/user/1234", "*apiclient.User", false, ""}, + { + id: "valid (default,empty)", + expectedType: "*apiclient.User", + shouldFail: false, + }, + { + id: "valid (short cid)", + cid: "1234", + expectedType: "*apiclient.User", + shouldFail: false, + }, + { + id: "valid (long cid)", + cid: "/user/1234", + expectedType: "*apiclient.User", + shouldFail: false, + }, } for _, test := range tests { @@ -169,16 +183,30 @@ func TestUpdateUser(t *testing.T) { apih, server := userTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *User + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid user config (nil)"}, - {"invalid (cid)", &User{CID: "/invalid"}, "", true, "invalid user CID (/invalid)"}, - {"valid", &testUser, "*apiclient.User", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid user config (nil)", + }, + { + id: "invalid (cid)", + cfg: &User{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid user CID (/invalid)", + }, + { + id: "valid", + cfg: &testUser, + expectedType: "*apiclient.User", + shouldFail: false, + }, } for _, test := range tests { @@ -209,15 +237,24 @@ func TestSearchUsers(t *testing.T) { expectedType := "*[]apiclient.User" filter := SearchFilterType(map[string][]string{"f_firstname": {"john"}, "f_lastname": {"doe"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no filter", nil, expectedType, false, ""}, - {"filter", &filter, expectedType, false, ""}, + { + id: "no filter", + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter", + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests { diff --git a/worksheet.go b/worksheet.go index 0494fa9..22a098f 100644 --- a/worksheet.go +++ b/worksheet.go @@ -152,7 +152,7 @@ func (a *API) CreateWorksheet(cfg *Worksheet) (*Worksheet, error) { } if a.Debug { - a.Log.Printf("create annotation, sending JSON: %s", string(jsonCfg)) + a.Log.Printf("create worksheet, sending JSON: %s", string(jsonCfg)) } result, err := a.Post(config.WorksheetPrefix, jsonCfg) diff --git a/worksheet_test.go b/worksheet_test.go index d1f1816..cc4e0a2 100644 --- a/worksheet_test.go +++ b/worksheet_test.go @@ -7,7 +7,7 @@ package apiclient import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -53,7 +53,7 @@ func testWorksheetServer() *httptest.Server { fmt.Fprintln(w, string(ret)) case "PUT": defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -98,7 +98,7 @@ func testWorksheetServer() *httptest.Server { } case "POST": defer r.Body.Close() - _, err := ioutil.ReadAll(r.Body) + _, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -151,16 +151,30 @@ func TestFetchWorksheet(t *testing.T) { apih, server := worksheetTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", "", true, "invalid worksheet CID (none)"}, - {"short cid", "01234567-89ab-cdef-0123-456789abcdef", "*apiclient.Worksheet", false, ""}, - {"long cid", "/worksheet/01234567-89ab-cdef-0123-456789abcdef", "*apiclient.Worksheet", false, ""}, + { + id: "empty cid", + shouldFail: true, + expectedErr: "invalid worksheet CID (none)", + }, + { + id: "short cid", + cid: "01234567-89ab-cdef-0123-456789abcdef", + expectedType: "*apiclient.Worksheet", + shouldFail: false, + }, + { + id: "long cid", + cid: "/worksheet/01234567-89ab-cdef-0123-456789abcdef", + expectedType: "*apiclient.Worksheet", + shouldFail: false, + }, } for _, test := range tests { @@ -203,16 +217,30 @@ func TestUpdateWorksheet(t *testing.T) { apih, server := worksheetTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Worksheet + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid worksheet config (nil)"}, - {"invalid (cid)", &Worksheet{CID: "/invalid"}, "", true, "invalid worksheet CID (/invalid)"}, - {"valid", &testWorksheet, "*apiclient.Worksheet", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid worksheet config (nil)", + }, + { + id: "invalid (cid)", + cfg: &Worksheet{CID: "/invalid"}, + shouldFail: true, + expectedErr: "invalid worksheet CID (/invalid)", + }, + { + id: "valid", + cfg: &testWorksheet, + expectedType: "*apiclient.Worksheet", + shouldFail: false, + }, } for _, test := range tests { @@ -240,15 +268,24 @@ func TestCreateWorksheet(t *testing.T) { apih, server := worksheetTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Worksheet + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, "", true, "invalid worksheet config (nil)"}, - {"valid", &testWorksheet, "*apiclient.Worksheet", false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid worksheet config (nil)", + }, + { + id: "valid", + cfg: &testWorksheet, + expectedType: "*apiclient.Worksheet", + shouldFail: false, + }, } for _, test := range tests { @@ -276,14 +313,22 @@ func TestDeleteWorksheet(t *testing.T) { apih, server := worksheetTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet - id string + tests := []struct { cfg *Worksheet - shouldFail bool + id string expectedErr string + shouldFail bool }{ - {"invalid (nil)", nil, true, "invalid worksheet config (nil)"}, - {"valid", &testWorksheet, false, ""}, + { + id: "invalid (nil)", + shouldFail: true, + expectedErr: "invalid worksheet config (nil)", + }, + { + id: "valid", + cfg: &testWorksheet, + shouldFail: false, + }, } for _, test := range tests { @@ -311,15 +356,27 @@ func TestDeleteWorksheetByCID(t *testing.T) { apih, server := worksheetTestBootstrap(t) defer server.Close() - tests := []struct { //nolint:govet + tests := []struct { id string cid string - shouldFail bool expectedErr string + shouldFail bool }{ - {"empty cid", "", true, "invalid worksheet CID (none)"}, - {"short cid", "01234567-89ab-cdef-0123-456789abcdef", false, ""}, - {"long cid", "/worksheet/01234567-89ab-cdef-0123-456789abcdef", false, ""}, + { + id: "empty cid", + shouldFail: true, + expectedErr: "invalid worksheet CID (none)", + }, + { + id: "short cid", + cid: "01234567-89ab-cdef-0123-456789abcdef", + shouldFail: false, + }, + { + id: "long cid", + cid: "/worksheet/01234567-89ab-cdef-0123-456789abcdef", + shouldFail: false, + }, } for _, test := range tests { @@ -351,18 +408,38 @@ func TestSearchWorksheets(t *testing.T) { search := SearchQueryType("web servers") filter := SearchFilterType(map[string][]string{"f_favorite": {"true"}}) - tests := []struct { //nolint:govet - id string + tests := []struct { search *SearchQueryType filter *SearchFilterType + id string expectedType string - shouldFail bool expectedErr string + shouldFail bool }{ - {"no search, no filter", nil, nil, expectedType, false, ""}, - {"search no filter", &search, nil, expectedType, false, ""}, - {"filter no search", nil, &filter, expectedType, false, ""}, - {"both filter and search", &search, &filter, expectedType, false, ""}, + { + id: "no search, no filter", + expectedType: expectedType, + shouldFail: false, + }, + { + id: "search no filter", + search: &search, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "filter no search", + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, + { + id: "both filter and search", + search: &search, + filter: &filter, + expectedType: expectedType, + shouldFail: false, + }, } for _, test := range tests {