Skip to content

Commit c65225d

Browse files
authored
Merge pull request #7896 from tstromberg/download-tests-suck
download: unify handling, refuse while under test
2 parents 539d5aa + 3bcfebf commit c65225d

File tree

9 files changed

+121
-197
lines changed

9 files changed

+121
-197
lines changed

pkg/minikube/download/binary.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
"github.com/blang/semver"
2626
"github.com/golang/glog"
27-
"github.com/hashicorp/go-getter"
2827
"github.com/pkg/errors"
2928
"k8s.io/minikube/pkg/minikube/localpath"
3029
)
@@ -58,28 +57,14 @@ func Binary(binary, version, osName, archName string) (string, error) {
5857
return targetFilepath, nil
5958
}
6059

61-
if err = os.MkdirAll(targetDir, 0777); err != nil {
62-
return "", errors.Wrapf(err, "mkdir %s", targetDir)
63-
}
64-
65-
tmpDst := targetFilepath + ".download"
66-
67-
client := &getter.Client{
68-
Src: url,
69-
Dst: tmpDst,
70-
Mode: getter.ClientModeFile,
71-
Options: []getter.ClientOption{getter.WithProgress(DefaultProgressBar)},
72-
}
73-
74-
glog.Infof("Downloading: %+v", client)
75-
if err := client.Get(); err != nil {
60+
if err := download(url, targetFilepath); err != nil {
7661
return "", errors.Wrapf(err, "download failed: %s", url)
7762
}
7863

7964
if osName == runtime.GOOS && archName == runtime.GOARCH {
80-
if err = os.Chmod(tmpDst, 0755); err != nil {
65+
if err = os.Chmod(targetFilepath, 0755); err != nil {
8166
return "", errors.Wrapf(err, "chmod +x %s", targetFilepath)
8267
}
8368
}
84-
return targetFilepath, os.Rename(tmpDst, targetFilepath)
69+
return targetFilepath, nil
8570
}

pkg/minikube/download/binary_test.go

Lines changed: 0 additions & 129 deletions
This file was deleted.

pkg/minikube/download/download.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package download
18+
19+
import (
20+
"flag"
21+
"fmt"
22+
"os"
23+
"path/filepath"
24+
"strings"
25+
26+
"github.com/golang/glog"
27+
"github.com/hashicorp/go-getter"
28+
"github.com/pkg/errors"
29+
)
30+
31+
var (
32+
mockMode = false
33+
)
34+
35+
// EnableMock allows tests to selectively enable if downloads are mocked
36+
func EnableMock(b bool) {
37+
mockMode = b
38+
}
39+
40+
// download is a well-configured atomic download function
41+
func download(src string, dst string) error {
42+
tmpDst := dst + ".download"
43+
client := &getter.Client{
44+
Src: src,
45+
Dst: tmpDst,
46+
Dir: false,
47+
Mode: getter.ClientModeFile,
48+
Options: []getter.ClientOption{getter.WithProgress(DefaultProgressBar)},
49+
Getters: map[string]getter.Getter{
50+
"file": &getter.FileGetter{Copy: false},
51+
"http": &getter.HttpGetter{Netrc: false},
52+
"https": &getter.HttpGetter{Netrc: false},
53+
},
54+
}
55+
56+
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
57+
return errors.Wrap(err, "mkdir")
58+
}
59+
60+
// Don't bother with getter.MockGetter, as we don't provide a way to inspect the outcome
61+
if mockMode {
62+
glog.Infof("Mock download: %s -> %s", src, dst)
63+
// Callers expect the file to exist
64+
_, err := os.Create(dst)
65+
return err
66+
}
67+
68+
// Politely prevent tests from shooting themselves in the foot
69+
if withinUnitTest() {
70+
return fmt.Errorf("unmocked download under test")
71+
}
72+
73+
glog.Infof("Downloading: %s -> %s", src, dst)
74+
if err := client.Get(); err != nil {
75+
return errors.Wrapf(err, "getter: %+v", client)
76+
}
77+
return os.Rename(tmpDst, dst)
78+
}
79+
80+
// withinUnitTset detects if we are in running within a unit-test
81+
func withinUnitTest() bool {
82+
// Nope, it's the integration test
83+
if flag.Lookup("minikube-start-args") != nil || strings.HasPrefix(filepath.Base(os.Args[0]), "e2e-") {
84+
return false
85+
}
86+
87+
return flag.Lookup("test.v") != nil || strings.HasSuffix(os.Args[0], "test")
88+
}

pkg/minikube/download/driver.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
"os"
2222

2323
"github.com/blang/semver"
24-
"github.com/golang/glog"
25-
"github.com/hashicorp/go-getter"
2624
"github.com/pkg/errors"
2725
"k8s.io/minikube/pkg/minikube/out"
2826
)
@@ -35,25 +33,10 @@ func driverWithChecksumURL(name string, v semver.Version) string {
3533
// Driver downloads an arbitrary driver
3634
func Driver(name string, destination string, v semver.Version) error {
3735
out.T(out.FileDownload, "Downloading driver {{.driver}}:", out.V{"driver": name})
38-
39-
tmpDst := destination + ".download"
40-
41-
url := driverWithChecksumURL(name, v)
42-
client := &getter.Client{
43-
Src: url,
44-
Dst: tmpDst,
45-
Mode: getter.ClientModeFile,
46-
Options: []getter.ClientOption{getter.WithProgress(DefaultProgressBar)},
36+
if err := download(driverWithChecksumURL(name, v), destination); err != nil {
37+
return errors.Wrap(err, "download")
4738
}
4839

49-
glog.Infof("Downloading: %+v", client)
50-
if err := client.Get(); err != nil {
51-
return errors.Wrapf(err, "download failed: %s", url)
52-
}
5340
// Give downloaded drivers a baseline decent file permission
54-
err := os.Chmod(tmpDst, 0755)
55-
if err != nil {
56-
return err
57-
}
58-
return os.Rename(tmpDst, destination)
41+
return os.Chmod(destination, 0755)
5942
}

pkg/minikube/download/iso.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"time"
2727

2828
"github.com/golang/glog"
29-
"github.com/hashicorp/go-getter"
3029
"github.com/juju/mutex"
3130
"github.com/pkg/errors"
3231
"k8s.io/minikube/pkg/minikube/localpath"
@@ -134,19 +133,5 @@ func downloadISO(isoURL string, skipChecksum bool) error {
134133
urlWithChecksum = isoURL
135134
}
136135

137-
tmpDst := dst + ".download"
138-
139-
opts := []getter.ClientOption{getter.WithProgress(DefaultProgressBar)}
140-
client := &getter.Client{
141-
Src: urlWithChecksum,
142-
Dst: tmpDst,
143-
Mode: getter.ClientModeFile,
144-
Options: opts,
145-
}
146-
147-
glog.Infof("full url: %s", urlWithChecksum)
148-
if err := client.Get(); err != nil {
149-
return errors.Wrap(err, isoURL)
150-
}
151-
return os.Rename(tmpDst, dst)
136+
return download(urlWithChecksum, dst)
152137
}

pkg/minikube/download/preload.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"google.golang.org/api/option"
3131

3232
"github.com/golang/glog"
33-
"github.com/hashicorp/go-getter"
3433
"github.com/pkg/errors"
3534
"github.com/spf13/viper"
3635
"k8s.io/minikube/pkg/minikube/localpath"
@@ -133,27 +132,19 @@ func Preload(k8sVersion, containerRuntime string) error {
133132
out.T(out.FileDownload, "Downloading Kubernetes {{.version}} preload ...", out.V{"version": k8sVersion})
134133
url := remoteTarballURL(k8sVersion, containerRuntime)
135134

136-
tmpDst := targetPath + ".download"
137-
client := &getter.Client{
138-
Src: url,
139-
Dst: tmpDst,
140-
Mode: getter.ClientModeFile,
141-
Options: []getter.ClientOption{getter.WithProgress(DefaultProgressBar)},
142-
}
143-
144-
glog.Infof("Downloading: %+v", client)
145-
if err := client.Get(); err != nil {
135+
if err := download(url, targetPath); err != nil {
146136
return errors.Wrapf(err, "download failed: %s", url)
147137
}
148138

149139
if err := saveChecksumFile(k8sVersion, containerRuntime); err != nil {
150140
return errors.Wrap(err, "saving checksum file")
151141
}
152142

153-
if err := verifyChecksum(k8sVersion, containerRuntime, tmpDst); err != nil {
143+
if err := verifyChecksum(k8sVersion, containerRuntime, targetPath); err != nil {
154144
return errors.Wrap(err, "verify")
155145
}
156-
return os.Rename(tmpDst, targetPath)
146+
147+
return nil
157148
}
158149

159150
func saveChecksumFile(k8sVersion, containerRuntime string) error {

pkg/minikube/machine/cache_binaries_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"k8s.io/minikube/pkg/minikube/assets"
2626
"k8s.io/minikube/pkg/minikube/bootstrapper"
2727
"k8s.io/minikube/pkg/minikube/command"
28+
"k8s.io/minikube/pkg/minikube/download"
2829
)
2930

3031
type copyFailRunner struct {
@@ -82,6 +83,8 @@ func TestCopyBinary(t *testing.T) {
8283
}
8384

8485
func TestCacheBinariesForBootstrapper(t *testing.T) {
86+
download.EnableMock(true)
87+
8588
oldMinikubeHome := os.Getenv("MINIKUBE_HOME")
8689
defer os.Setenv("MINIKUBE_HOME", oldMinikubeHome)
8790

0 commit comments

Comments
 (0)