Create download_test and add test for Binary downloads not colliding.
parent
d6f60bbd4a
commit
4e8451af95
1
go.mod
1
go.mod
|
@ -26,6 +26,7 @@ require (
|
||||||
github.com/docker/machine v0.16.2
|
github.com/docker/machine v0.16.2
|
||||||
github.com/elazarl/goproxy v0.0.0-20190421051319-9d40249d3c2f
|
github.com/elazarl/goproxy v0.0.0-20190421051319-9d40249d3c2f
|
||||||
github.com/elazarl/goproxy/ext v0.0.0-20190421051319-9d40249d3c2f // indirect
|
github.com/elazarl/goproxy/ext v0.0.0-20190421051319-9d40249d3c2f // indirect
|
||||||
|
github.com/go-logr/logr v0.4.0
|
||||||
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
|
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
|
||||||
github.com/google/go-cmp v0.5.5
|
github.com/google/go-cmp v0.5.5
|
||||||
github.com/google/go-containerregistry v0.4.1
|
github.com/google/go-containerregistry v0.4.1
|
||||||
|
|
|
@ -52,7 +52,7 @@ func Binary(binary, version, osName, archName string) (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(targetFilepath); err == nil {
|
if _, err := checkCache(targetFilepath); err == nil {
|
||||||
klog.Infof("Not caching binary, using %s", url)
|
klog.Infof("Not caching binary, using %s", url)
|
||||||
return targetFilepath, nil
|
return targetFilepath, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
mockMode = false
|
mockMode = false
|
||||||
|
checkCache = os.Stat
|
||||||
)
|
)
|
||||||
|
|
||||||
// EnableMock allows tests to selectively enable if downloads are mocked
|
// EnableMock allows tests to selectively enable if downloads are mocked
|
||||||
|
|
|
@ -0,0 +1,95 @@
|
||||||
|
/*
|
||||||
|
Copyright 2020 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 download
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-logr/logr"
|
||||||
|
"k8s.io/klog/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type mockLogger struct {
|
||||||
|
downloads int
|
||||||
|
t *testing.T
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ml *mockLogger) Enabled() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
func (ml *mockLogger) Info(msg string, keysAndValues ...interface{}) {
|
||||||
|
fmt.Println("hi - ", msg)
|
||||||
|
if strings.Contains(msg, "Mock download") {
|
||||||
|
// Make "downloads" take longer to increase lock time.
|
||||||
|
dur, err := time.ParseDuration("1s")
|
||||||
|
if err != nil {
|
||||||
|
ml.t.Errorf("Could not parse 1 second duration - should never happen")
|
||||||
|
}
|
||||||
|
time.Sleep(dur)
|
||||||
|
|
||||||
|
ml.downloads++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (ml *mockLogger) Error(err error, msg string, keysAndValues ...interface{}) {}
|
||||||
|
func (ml *mockLogger) V(level int) logr.Logger {
|
||||||
|
return ml
|
||||||
|
}
|
||||||
|
func (ml *mockLogger) WithValues(keysAndValues ...interface{}) logr.Logger {
|
||||||
|
return ml
|
||||||
|
}
|
||||||
|
func (ml *mockLogger) WithName(name string) logr.Logger {
|
||||||
|
return ml
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBinaryDownloadPreventsMultipleDownload(t *testing.T) {
|
||||||
|
EnableMock(true)
|
||||||
|
defer EnableMock(false)
|
||||||
|
tlog := &mockLogger{downloads: 0, t: t}
|
||||||
|
|
||||||
|
klog.SetLogger(tlog)
|
||||||
|
defer klog.SetLogger(nil)
|
||||||
|
|
||||||
|
checkCache = func(file string) (fs.FileInfo, error) {
|
||||||
|
if tlog.downloads == 0 {
|
||||||
|
return nil, fmt.Errorf("some error")
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var group sync.WaitGroup
|
||||||
|
group.Add(2)
|
||||||
|
dlCall := func() {
|
||||||
|
if _, err := Binary("kubectl", "v1.20.2", "linux", "amd64"); err != nil {
|
||||||
|
t.Errorf("Failed to download binary: %+v", err)
|
||||||
|
}
|
||||||
|
group.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
go dlCall()
|
||||||
|
go dlCall()
|
||||||
|
|
||||||
|
group.Wait()
|
||||||
|
|
||||||
|
if tlog.downloads != 1 {
|
||||||
|
t.Errorf("Wrong number of downloads occurred. Actual: %v, Expected: 1", tlog.downloads)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue