From f198f9654b72faae24a05addd2af0f08ad862dc5 Mon Sep 17 00:00:00 2001 From: Jay Pipes Date: Tue, 25 Jun 2024 12:20:42 -0400 Subject: [PATCH] fix up linter failures --- action.go | 2 +- assertions.go | 30 ----------------------------- compare.go | 44 +++++++++++++++++++++---------------------- fixtures/kind/kind.go | 2 +- parse.go | 18 ------------------ parse_test.go | 6 ------ placement.go | 6 ++++++ 7 files changed, 29 insertions(+), 79 deletions(-) diff --git a/action.go b/action.go index 3d1d97f..adb49f5 100644 --- a/action.go +++ b/action.go @@ -378,7 +378,7 @@ func (a *Action) delete( if ons == "" { ons = ns } - if err = a.doDelete(ctx, c, res, name, ns); err != nil { + if err = a.doDelete(ctx, c, res, name, ons); err != nil { return err } } diff --git a/assertions.go b/assertions.go index 37daeb9..ed5b059 100644 --- a/assertions.go +++ b/assertions.go @@ -328,36 +328,6 @@ func (a *assertions) expectsNotFound() bool { return (exp.Len != nil && *exp.Len == 0) || exp.NotFound } -// notFoundOK returns true if the supplied error and response matches the -// NotFound condition and the Len==0 condition, false otherwise -func (a *assertions) notFoundOK() bool { - if a.expectsNotFound() { - // First check if the error is like one returned from Get or Delete - // that has a 404 ErrStatus.Code in it - apierr, ok := a.err.(*apierrors.StatusError) - if ok { - if http.StatusNotFound != int(apierr.ErrStatus.Code) { - msg := fmt.Sprintf("got status code %d", apierr.ErrStatus.Code) - a.Fail(ExpectedNotFound(msg)) - return false - } - return true - } - // Next check to see if the supplied resp is a list of objects returned - // by the dynamic client and if so, is that an empty list. - list, ok := a.r.(*unstructured.UnstructuredList) - if ok { - if len(list.Items) != 0 { - msg := fmt.Sprintf("got %d items", len(list.Items)) - a.Fail(ExpectedNotFound(msg)) - return false - } - return true - } - } - return true -} - // lenOK returns true if the subject matches the Len condition, false otherwise func (a *assertions) lenOK() bool { exp := a.exp diff --git a/compare.go b/compare.go index 2bde545..7e081dd 100644 --- a/compare.go +++ b/compare.go @@ -6,7 +6,7 @@ package kube import ( "fmt" - "io/ioutil" + "os" "reflect" "strconv" "strings" @@ -42,7 +42,7 @@ func compareConditions( panic(msg) } if (!found || len(conds) == 0) && len(expected) != 0 { - for condType, _ := range expected { + for condType := range expected { d.Add(fmt.Sprintf("no condition with type %q found", condType)) } return d @@ -130,13 +130,13 @@ func compareConditions( // map[string]interface{} is the collection of resource fields that we will // match against. func matchObjectFromAny(m interface{}) map[string]interface{} { - switch m.(type) { + switch m := m.(type) { case string: var err error var b []byte - v := m.(string) + v := m if probablyFilePath(v) { - b, err = ioutil.ReadFile(v) + b, err = os.ReadFile(v) if err != nil { // NOTE(jaypipes): We already validated that the file exists at // parse time. If we get an error here, just panic cuz there's @@ -155,7 +155,7 @@ func matchObjectFromAny(m interface{}) map[string]interface{} { } return obj case map[string]interface{}: - return m.(map[string]interface{}) + return m } return map[string]interface{}{} } @@ -238,7 +238,7 @@ func collectFieldDifferences( } return case int, int8, int16, int32, int64: - switch subject.(type) { + switch subject := subject.(type) { case int, int8, int16, int32, int64: mv := toInt64(match) sv := toInt64(subject) @@ -261,8 +261,7 @@ func collectFieldDifferences( } case string: mv := toInt64(match) - ss := subject.(string) - sv, err := strconv.Atoi(ss) + sv, err := strconv.Atoi(subject) if err != nil { diff := fmt.Sprintf( "%s had different values. expected %v but found %v", @@ -296,8 +295,7 @@ func collectFieldDifferences( } case string: mv, _ := match.(string) - sv, _ := subject.(string) - if mv != sv { + if mv != subject { diff := fmt.Sprintf( "%s had different values. expected %v but found %v", fp, match, subject, @@ -362,34 +360,34 @@ func typesComparable(a, b interface{}) bool { // toUint64 takes an interface and returns a uint64 func toUint64(v interface{}) uint64 { - switch v.(type) { + switch v := v.(type) { case uint64: - return v.(uint64) + return v case uint8: - return uint64(v.(uint8)) + return uint64(v) case uint16: - return uint64(v.(uint16)) + return uint64(v) case uint32: - return uint64(v.(uint32)) + return uint64(v) case uint: - return uint64(v.(uint)) + return uint64(v) } return 0 } // toInt64 takes an interface and returns an int64 func toInt64(v interface{}) int64 { - switch v.(type) { + switch v := v.(type) { case int64: - return v.(int64) + return v case int8: - return int64(v.(int8)) + return int64(v) case int16: - return int64(v.(int16)) + return int64(v) case int32: - return int64(v.(int32)) + return int64(v) case int: - return int64(v.(int)) + return int64(v) } return 0 } diff --git a/fixtures/kind/kind.go b/fixtures/kind/kind.go index 382c918..10cabb5 100644 --- a/fixtures/kind/kind.go +++ b/fixtures/kind/kind.go @@ -144,7 +144,7 @@ func (f *KindFixture) waitForDefaultServiceAccount(ctx context.Context) error { ) ticker := backoff.NewTicker(bo) attempts := 1 - for _ = range ticker.C { + for range ticker.C { found := true _, err = clientset.CoreV1().ServiceAccounts("default").Get(context.TODO(), "default", metav1.GetOptions{}) if err != nil { diff --git a/parse.go b/parse.go index eaa42e2..75fc6bc 100644 --- a/parse.go +++ b/parse.go @@ -356,24 +356,6 @@ func (e *Expect) UnmarshalYAML(node *yaml.Node) error { return nil } -// expandShortcut looks at the shortcut fields (e.g. `kube.create`) and expands -// the shortcut into a full KubeSpec. -func expandShortcut(s *Spec) { - if s.Kube != nil { - return - } - ks := &KubeSpec{ - Action: Action{}, - } - if s.KubeCreate != "" { - ks.Create = s.KubeCreate - } - if s.KubeApply != "" { - ks.Apply = s.KubeApply - } - s.Kube = ks -} - // moreThanOneAction returns true if the test author has specified more than a // single action in the KubeSpec. func moreThanOneAction(a *Action) bool { diff --git a/parse_test.go b/parse_test.go index 042158e..867f2e1 100644 --- a/parse_test.go +++ b/parse_test.go @@ -6,7 +6,6 @@ package kube_test import ( "path/filepath" - "runtime" "testing" "github.com/gdt-dev/gdt" @@ -16,11 +15,6 @@ import ( "github.com/stretchr/testify/require" ) -func currentDir() string { - _, filename, _, _ := runtime.Caller(0) - return filepath.Dir(filename) -} - func TestFailureBadDefaults(t *testing.T) { assert := assert.New(t) require := require.New(t) diff --git a/placement.go b/placement.go index 64deb53..8caf747 100644 --- a/placement.go +++ b/placement.go @@ -35,6 +35,9 @@ func getNodes( Kind: "Node", } res, err := c.gvrFromGVK(gvk) + if err != nil { + panic(err) + } opts := metav1.ListOptions{} list, err := c.client.Resource(res).Namespace("").List( ctx, opts, @@ -94,6 +97,9 @@ func getPods( Kind: "Pod", } res, err := c.gvrFromGVK(gvk) + if err != nil { + panic(err) + } opts := client.ListOptions{ LabelSelector: ls, Namespace: ns,