From 532b6f676e5b358dbcbce700a59c0fe66ed7fc33 Mon Sep 17 00:00:00 2001 From: RainbowMango Date: Mon, 26 Aug 2019 20:55:32 +0800 Subject: [PATCH] Cleanup staticcheck issues for package in client-go. Kubernetes-commit: c8c055b3163dd2661b3f9dd1b0ffb718a61aba24 --- testing/actions.go | 4 ++-- testing/fixture_test.go | 3 ++- tools/clientcmd/client_config_test.go | 1 - tools/clientcmd/merged_client_builder_test.go | 13 ------------- tools/clientcmd/validation.go | 9 ++++++--- tools/portforward/portforward_test.go | 3 +++ tools/watch/until_test.go | 1 - util/jsonpath/parser.go | 6 ++---- 8 files changed, 15 insertions(+), 25 deletions(-) diff --git a/testing/actions.go b/testing/actions.go index e6db578e..f56b34ee 100644 --- a/testing/actions.go +++ b/testing/actions.go @@ -439,8 +439,8 @@ func (a ActionImpl) GetSubresource() string { return a.Subresource } func (a ActionImpl) Matches(verb, resource string) bool { - return strings.ToLower(verb) == strings.ToLower(a.Verb) && - strings.ToLower(resource) == strings.ToLower(a.Resource.Resource) + return strings.EqualFold(verb, a.Verb) && + strings.EqualFold(resource, a.Resource.Resource) } func (a ActionImpl) DeepCopy() Action { ret := a diff --git a/testing/fixture_test.go b/testing/fixture_test.go index 2f59e988..a0338b70 100644 --- a/testing/fixture_test.go +++ b/testing/fixture_test.go @@ -204,7 +204,8 @@ func TestWatchCallMultipleInvocation(t *testing.T) { event := <-w.ResultChan() accessor, err := meta.Accessor(event.Object) if err != nil { - t.Fatalf("unexpected error: %v", err) + t.Errorf("unexpected error: %v", err) + break } assert.Equal(t, c.op, event.Type, "watch event mismatched") assert.Equal(t, c.name, accessor.GetName(), "watched object mismatch") diff --git a/tools/clientcmd/client_config_test.go b/tools/clientcmd/client_config_test.go index 59c9c217..907dcde7 100644 --- a/tools/clientcmd/client_config_test.go +++ b/tools/clientcmd/client_config_test.go @@ -458,7 +458,6 @@ func TestCreateCleanDefaultCluster(t *testing.T) { } func TestCreateMissingContextNoDefault(t *testing.T) { - const expectedErrorContains = "Context was not found for specified context" config := createValidTestConfig() clientBuilder := NewNonInteractiveClientConfig(*config, "not-present", &ConfigOverrides{}, nil) diff --git a/tools/clientcmd/merged_client_builder_test.go b/tools/clientcmd/merged_client_builder_test.go index d1910044..cfe6cb0a 100644 --- a/tools/clientcmd/merged_client_builder_test.go +++ b/tools/clientcmd/merged_client_builder_test.go @@ -24,19 +24,6 @@ import ( clientcmdapi "k8s.io/client-go/tools/clientcmd/api" ) -type testLoader struct { - ClientConfigLoader - - called bool - config *clientcmdapi.Config - err error -} - -func (l *testLoader) Load() (*clientcmdapi.Config, error) { - l.called = true - return l.config, l.err -} - type testClientConfig struct { rawconfig *clientcmdapi.Config config *restclient.Config diff --git a/tools/clientcmd/validation.go b/tools/clientcmd/validation.go index 720d0b88..2f927072 100644 --- a/tools/clientcmd/validation.go +++ b/tools/clientcmd/validation.go @@ -185,9 +185,10 @@ func validateClusterInfo(clusterName string, clusterInfo clientcmdapi.Cluster) [ } if len(clusterInfo.CertificateAuthority) != 0 { clientCertCA, err := os.Open(clusterInfo.CertificateAuthority) - defer clientCertCA.Close() if err != nil { validationErrors = append(validationErrors, fmt.Errorf("unable to read certificate-authority %v for %v due to %v", clusterInfo.CertificateAuthority, clusterName, err)) + } else { + defer clientCertCA.Close() } } @@ -223,16 +224,18 @@ func validateAuthInfo(authInfoName string, authInfo clientcmdapi.AuthInfo) []err if len(authInfo.ClientCertificate) != 0 { clientCertFile, err := os.Open(authInfo.ClientCertificate) - defer clientCertFile.Close() if err != nil { validationErrors = append(validationErrors, fmt.Errorf("unable to read client-cert %v for %v due to %v", authInfo.ClientCertificate, authInfoName, err)) + } else { + defer clientCertFile.Close() } } if len(authInfo.ClientKey) != 0 { clientKeyFile, err := os.Open(authInfo.ClientKey) - defer clientKeyFile.Close() if err != nil { validationErrors = append(validationErrors, fmt.Errorf("unable to read client-key %v for %v due to %v", authInfo.ClientKey, authInfoName, err)) + } else { + defer clientKeyFile.Close() } } } diff --git a/tools/portforward/portforward_test.go b/tools/portforward/portforward_test.go index b39fa504..5b9afabe 100644 --- a/tools/portforward/portforward_test.go +++ b/tools/portforward/portforward_test.go @@ -376,6 +376,9 @@ func TestGetPortsReturnsDynamicallyAssignedLocalPort(t *testing.T) { <-pf.Ready ports, err := pf.GetPorts() + if err != nil { + t.Fatalf("Failed to get ports. error: %v", err) + } if len(ports) != 1 { t.Fatalf("expected 1 port, got %d", len(ports)) diff --git a/tools/watch/until_test.go b/tools/watch/until_test.go index 0243beeb..06231f26 100644 --- a/tools/watch/until_test.go +++ b/tools/watch/until_test.go @@ -35,7 +35,6 @@ import ( ) type fakePod struct { - name string } func (obj *fakePod) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind } diff --git a/util/jsonpath/parser.go b/util/jsonpath/parser.go index 1af8f269..e1aab680 100644 --- a/util/jsonpath/parser.go +++ b/util/jsonpath/parser.go @@ -37,7 +37,6 @@ type Parser struct { Name string Root *ListNode input string - cur *ListNode pos int start int width int @@ -186,8 +185,7 @@ func (p *Parser) parseInsideAction(cur *ListNode) error { func (p *Parser) parseRightDelim(cur *ListNode) error { p.pos += len(rightDelim) p.consumeText() - cur = p.Root - return p.parseText(cur) + return p.parseText(p.Root) } // parseIdentifier scans build-in keywords, like "range" "end" @@ -231,7 +229,7 @@ func (p *Parser) parseRecursive(cur *ListNode) error { func (p *Parser) parseNumber(cur *ListNode) error { r := p.peek() if r == '+' || r == '-' { - r = p.next() + p.next() } for { r = p.next()