diff --git a/pkg/util/retry/retry_test.go b/pkg/util/retry/retry_test.go new file mode 100644 index 0000000000..fdfa3c8085 --- /dev/null +++ b/pkg/util/retry/retry_test.go @@ -0,0 +1,53 @@ +/* +Copyright 2019 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package retry + +import ( + "errors" + "testing" +) + +// Returns a function that will return n errors, then return successfully forever. +func errorGenerator(n int, retryable bool) func() error { + errorCount := 0 + return func() (err error) { + if errorCount < n { + errorCount++ + e := errors.New("Error") + if retryable { + return &RetriableError{Err: e} + } + return e + + } + + return nil + } +} + +func TestErrorGenerator(t *testing.T) { + errors := 3 + f := errorGenerator(errors, false) + for i := 0; i < errors-1; i++ { + if err := f(); err == nil { + t.Fatalf("Error should have been thrown at iteration %v", i) + } + } + if err := f(); err == nil { + t.Fatalf("Error should not have been thrown this call!") + } +} diff --git a/pkg/util/utils.go b/pkg/util/utils.go index 5f830a1262..1cd596a736 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -29,11 +29,9 @@ import ( "time" units "github.com/docker/go-units" - "github.com/golang/glog" "github.com/pkg/errors" "k8s.io/minikube/pkg/minikube/exit" "k8s.io/minikube/pkg/minikube/out" - "k8s.io/minikube/pkg/util/retry" ) // ErrPrefix notes an error @@ -100,33 +98,6 @@ func CanReadFile(path string) bool { return true } -// Retry retries a number of attempts -func Retry(attempts int, callback func() error) (err error) { - return RetryAfter(attempts, callback, 0) -} - -// RetryAfter retries a number of attempts, after a delay -func RetryAfter(attempts int, callback func() error, d time.Duration) (err error) { - m := MultiError{} - for i := 0; i < attempts; i++ { - if i > 0 { - glog.V(1).Infof("retry loop %d", i) - } - err = callback() - if err == nil { - return nil - } - m.Collect(err) - if _, ok := err.(*retry.RetriableError); !ok { - glog.Infof("non-retriable error: %v", err) - return m.ToError() - } - glog.V(2).Infof("error: %v - sleeping %s", err, d) - time.Sleep(d) - } - return m.ToError() -} - // GetBinaryDownloadURL returns a suitable URL for the platform func GetBinaryDownloadURL(version, platform string) string { switch platform { @@ -137,31 +108,6 @@ func GetBinaryDownloadURL(version, platform string) string { } } -// MultiError holds multiple errors -type MultiError struct { - Errors []error -} - -// Collect adds the error -func (m *MultiError) Collect(err error) { - if err != nil { - m.Errors = append(m.Errors, err) - } -} - -// ToError converts all errors into one -func (m MultiError) ToError() error { - if len(m.Errors) == 0 { - return nil - } - - errStrings := []string{} - for _, err := range m.Errors { - errStrings = append(errStrings, err.Error()) - } - return errors.New(strings.Join(errStrings, "\n")) -} - // IsDirectory checks if path is a directory func IsDirectory(path string) (bool, error) { fileInfo, err := os.Stat(path) diff --git a/pkg/util/utils_test.go b/pkg/util/utils_test.go index 4ce4755c7d..4fc94ce7bc 100644 --- a/pkg/util/utils_test.go +++ b/pkg/util/utils_test.go @@ -22,89 +22,8 @@ import ( "strings" "sync" "testing" - - "github.com/pkg/errors" - "k8s.io/minikube/pkg/util/retry" ) -// Returns a function that will return n errors, then return successfully forever. -func errorGenerator(n int, retryable bool) func() error { - errorCount := 0 - return func() (err error) { - if errorCount < n { - errorCount++ - e := errors.New("Error") - if retryable { - return &retry.RetriableError{Err: e} - } - return e - - } - - return nil - } -} - -func TestErrorGenerator(t *testing.T) { - errors := 3 - f := errorGenerator(errors, false) - for i := 0; i < errors-1; i++ { - if err := f(); err == nil { - t.Fatalf("Error should have been thrown at iteration %v", i) - } - } - if err := f(); err == nil { - t.Fatalf("Error should not have been thrown this call!") - } -} - -func TestRetry(t *testing.T) { - f := errorGenerator(4, true) - if err := Retry(5, f); err != nil { - t.Fatalf("Error should not have been raised by retry.") - } - - f = errorGenerator(5, true) - if err := Retry(4, f); err == nil { - t.Fatalf("Error should have been raised by retry.") - } -} - -func TestRetryNotRetriableError(t *testing.T) { - f := errorGenerator(4, false) - if err := Retry(5, f); err == nil { - t.Fatalf("Error should have been raised by retry.") - } - - f = errorGenerator(5, false) - if err := Retry(4, f); err == nil { - t.Fatalf("Error should have been raised by retry.") - } - f = errorGenerator(0, false) - if err := Retry(5, f); err != nil { - t.Fatalf("Error should not have been raised by retry.") - } -} - -func TestMultiError(t *testing.T) { - m := MultiError{} - - m.Collect(errors.New("Error 1")) - m.Collect(errors.New("Error 2")) - - err := m.ToError() - expected := `Error 1 -Error 2` - if err.Error() != expected { - t.Fatalf("%s != %s", err.Error(), expected) - } - - m = MultiError{} - if err := m.ToError(); err != nil { - t.Fatalf("Unexpected error: %v", err) - } -} - func TestGetBinaryDownloadURL(t *testing.T) { testData := []struct { version string