Fix staticcheck in apiserver and client-go pkgs

Kubernetes-commit: 830a137d2ea70663cd94403595313b95ac40ffe8
This commit is contained in:
tiloso 2021-06-19 22:03:46 +02:00 committed by Kubernetes Publisher
parent a59bf4b9c7
commit ab3cca3647
4 changed files with 66 additions and 32 deletions

View File

@ -18,6 +18,7 @@ package cacher
import (
"context"
"errors"
"fmt"
"reflect"
goruntime "runtime"
@ -649,6 +650,7 @@ func TestCacherNoLeakWithMultipleWatchers(t *testing.T) {
// run the collision test for 3 seconds to let ~2 buckets expire
stopCh := make(chan struct{})
var watchErr error
time.AfterFunc(3*time.Second, func() { close(stopCh) })
wg := &sync.WaitGroup{}
@ -664,7 +666,8 @@ func TestCacherNoLeakWithMultipleWatchers(t *testing.T) {
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
w, err := cacher.Watch(ctx, "pods/ns", storage.ListOptions{ResourceVersion: "0", Predicate: pred})
if err != nil {
t.Fatalf("Failed to create watch: %v", err)
watchErr = fmt.Errorf("Failed to create watch: %v", err)
return
}
w.Stop()
}
@ -687,6 +690,10 @@ func TestCacherNoLeakWithMultipleWatchers(t *testing.T) {
// wait for adding/removing watchers to end
wg.Wait()
if watchErr != nil {
t.Fatal(watchErr)
}
// wait out the expiration period and pop expired watchers
time.Sleep(2 * time.Second)
cacher.bookmarkWatchers.popExpiredWatchers()
@ -742,6 +749,7 @@ func testCacherSendBookmarkEvents(t *testing.T, allowWatchBookmarks, expectedBoo
}
resourceVersion := uint64(1000)
errc := make(chan error, 1)
go func() {
deadline := time.Now().Add(time.Second)
for i := 0; time.Now().Before(deadline); i++ {
@ -752,7 +760,8 @@ func testCacherSendBookmarkEvents(t *testing.T, allowWatchBookmarks, expectedBoo
ResourceVersion: fmt.Sprintf("%v", resourceVersion+uint64(i)),
}})
if err != nil {
t.Fatalf("failed to add a pod: %v", err)
errc <- fmt.Errorf("failed to add a pod: %v", err)
return
}
time.Sleep(100 * time.Millisecond)
}
@ -762,6 +771,9 @@ func testCacherSendBookmarkEvents(t *testing.T, allowWatchBookmarks, expectedBoo
lastObservedRV := uint64(0)
for {
select {
case err := <-errc:
t.Fatal(err)
return
case event, ok := <-w.ResultChan():
if !ok {
t.Fatal("Unexpected closed")
@ -945,7 +957,6 @@ func TestDispatchingBookmarkEventsWithConcurrentStop(t *testing.T) {
select {
case <-done:
break
case <-time.After(time.Second):
t.Fatal("receive result timeout")
}
@ -994,6 +1005,8 @@ func TestBookmarksOnResourceVersionUpdates(t *testing.T) {
expectedRV := 2000
var rcErr error
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
@ -1001,7 +1014,8 @@ func TestBookmarksOnResourceVersionUpdates(t *testing.T) {
for {
event, ok := <-w.ResultChan()
if !ok {
t.Fatalf("Unexpected closed channel")
rcErr = errors.New("Unexpected closed channel")
return
}
rv, err := cacher.versioner.ObjectResourceVersion(event.Object)
if err != nil {
@ -1017,6 +1031,9 @@ func TestBookmarksOnResourceVersionUpdates(t *testing.T) {
cacher.watchCache.UpdateResourceVersion(strconv.Itoa(expectedRV))
wg.Wait()
if rcErr != nil {
t.Fatal(rcErr)
}
}
type fakeTimeBudget struct{}

View File

@ -149,7 +149,8 @@ func TestCachingObjectRaces(t *testing.T) {
}
accessor, err := meta.Accessor(object.GetObject())
if err != nil {
t.Fatalf("failed to get accessor: %v", err)
t.Errorf("failed to get accessor: %v", err)
return
}
if selfLink := accessor.GetSelfLink(); selfLink != "selfLink" {
t.Errorf("unexpected selfLink: %s", selfLink)

View File

@ -887,6 +887,7 @@ func TestWatchBookmarksWithCorrectResourceVersion(t *testing.T) {
defer watcher.Stop()
done := make(chan struct{})
errc := make(chan error, 1)
var wg sync.WaitGroup
wg.Add(1)
defer wg.Wait() // We must wait for the waitgroup to exit before we terminate the cache or the server in prior defers
@ -901,7 +902,8 @@ func TestWatchBookmarksWithCorrectResourceVersion(t *testing.T) {
pod := fmt.Sprintf("foo-%d", i)
err := createPod(etcdStorage, makeTestPod(pod))
if err != nil {
t.Fatalf("failed to create pod %v: %v", pod, err)
errc <- fmt.Errorf("failed to create pod %v: %v", pod, err)
return
}
time.Sleep(time.Second / 100)
}
@ -910,27 +912,36 @@ func TestWatchBookmarksWithCorrectResourceVersion(t *testing.T) {
bookmarkReceived := false
lastObservedResourceVersion := uint64(0)
for event := range watcher.ResultChan() {
rv, err := v.ObjectResourceVersion(event.Object)
if err != nil {
t.Fatalf("failed to parse resourceVersion from %#v", event)
}
if event.Type == watch.Bookmark {
bookmarkReceived = true
// bookmark event has a RV greater than or equal to the before one
if rv < lastObservedResourceVersion {
t.Fatalf("Unexpected bookmark resourceVersion %v less than observed %v)", rv, lastObservedResourceVersion)
for {
select {
case err := <-errc:
t.Fatal(err)
case event, ok := <-watcher.ResultChan():
if !ok {
// Make sure we have received a bookmark event
if !bookmarkReceived {
t.Fatalf("Unpexected error, we did not received a bookmark event")
}
return
}
} else {
// non-bookmark event has a RV greater than anything before
if rv <= lastObservedResourceVersion {
t.Fatalf("Unexpected event resourceVersion %v less than or equal to bookmark %v)", rv, lastObservedResourceVersion)
rv, err := v.ObjectResourceVersion(event.Object)
if err != nil {
t.Fatalf("failed to parse resourceVersion from %#v", event)
}
if event.Type == watch.Bookmark {
bookmarkReceived = true
// bookmark event has a RV greater than or equal to the before one
if rv < lastObservedResourceVersion {
t.Fatalf("Unexpected bookmark resourceVersion %v less than observed %v)", rv, lastObservedResourceVersion)
}
} else {
// non-bookmark event has a RV greater than anything before
if rv <= lastObservedResourceVersion {
t.Fatalf("Unexpected event resourceVersion %v less than or equal to bookmark %v)", rv, lastObservedResourceVersion)
}
}
lastObservedResourceVersion = rv
}
lastObservedResourceVersion = rv
}
// Make sure we have received a bookmark event
if !bookmarkReceived {
t.Fatalf("Unpexected error, we did not received a bookmark event")
}
}

View File

@ -186,12 +186,13 @@ func TestTimeouts(t *testing.T) {
func TestIntermittentConnectionLoss(t *testing.T) {
t.Parallel()
var (
wg1 sync.WaitGroup
wg2 sync.WaitGroup
timeout = 30 * time.Second
blackOut = 1 * time.Second
data = []byte("test data")
endpoint = newEndpoint()
wg1 sync.WaitGroup
wg2 sync.WaitGroup
timeout = 30 * time.Second
blackOut = 1 * time.Second
data = []byte("test data")
endpoint = newEndpoint()
encryptErr error
)
// Start KMS Plugin
f, err := mock.NewBase64Plugin(endpoint.path)
@ -229,7 +230,7 @@ func TestIntermittentConnectionLoss(t *testing.T) {
wg1.Done()
_, err := service.Encrypt(data)
if err != nil {
t.Fatalf("failed when executing encrypt, error: %v", err)
encryptErr = fmt.Errorf("failed when executing encrypt, error: %v", err)
}
}()
@ -247,6 +248,10 @@ func TestIntermittentConnectionLoss(t *testing.T) {
t.Log("Restarted KMS Plugin")
wg2.Wait()
if encryptErr != nil {
t.Error(encryptErr)
}
}
func TestUnsupportedVersion(t *testing.T) {