From 6959aa0ce2c484b995a339ad02f0e1c88bc13e49 Mon Sep 17 00:00:00 2001 From: Aaron Prindle Date: Sun, 19 Feb 2017 19:22:29 -0800 Subject: [PATCH] Added unit tests to downloader.go --- pkg/util/downloader.go | 14 ++--- pkg/util/downloader_test.go | 113 ++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 7 deletions(-) create mode 100644 pkg/util/downloader_test.go diff --git a/pkg/util/downloader.go b/pkg/util/downloader.go index b18e5a505..8a72c6eb5 100644 --- a/pkg/util/downloader.go +++ b/pkg/util/downloader.go @@ -52,7 +52,7 @@ func (f DefaultDownloader) GetISOFileURI(isoURL string) string { } func (f DefaultDownloader) CacheMinikubeISOFromURL(isoURL string) error { - if !f.shouldCacheMinikubeISO(isoURL) { + if !f.ShouldCacheMinikubeISO(isoURL) { glog.Infof("Not caching ISO, using %s", isoURL) return nil } @@ -73,14 +73,14 @@ func (f DefaultDownloader) CacheMinikubeISOFromURL(isoURL string) error { } fmt.Println("Downloading Minikube ISO") - if err := download.ToFile(isoURL, f.getISOCacheFilepath(isoURL), options); err != nil { + if err := download.ToFile(isoURL, f.GetISOCacheFilepath(isoURL), options); err != nil { return errors.Wrap(err, "Error downloading Minikube ISO") } return nil } -func (f DefaultDownloader) shouldCacheMinikubeISO(isoURL string) bool { +func (f DefaultDownloader) ShouldCacheMinikubeISO(isoURL string) bool { // store the miniube-iso inside the .minikube dir urlObj, err := url.Parse(isoURL) @@ -90,18 +90,18 @@ func (f DefaultDownloader) shouldCacheMinikubeISO(isoURL string) bool { if urlObj.Scheme == fileScheme { return false } - if f.isMinikubeISOCached(isoURL) { + if f.IsMinikubeISOCached(isoURL) { return false } return true } -func (f DefaultDownloader) getISOCacheFilepath(isoURL string) string { +func (f DefaultDownloader) GetISOCacheFilepath(isoURL string) string { return filepath.Join(constants.GetMinipath(), "cache", "iso", filepath.Base(isoURL)) } -func (f DefaultDownloader) isMinikubeISOCached(isoURL string) bool { - if _, err := os.Stat(f.getISOCacheFilepath(isoURL)); os.IsNotExist(err) { +func (f DefaultDownloader) IsMinikubeISOCached(isoURL string) bool { + if _, err := os.Stat(f.GetISOCacheFilepath(isoURL)); os.IsNotExist(err) { return false } return true diff --git a/pkg/util/downloader_test.go b/pkg/util/downloader_test.go new file mode 100644 index 000000000..dad6ad997 --- /dev/null +++ b/pkg/util/downloader_test.go @@ -0,0 +1,113 @@ +/* +Copyright 2016 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 util + +import ( + "bytes" + "io" + "io/ioutil" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" + + "k8s.io/minikube/pkg/minikube/constants" + "k8s.io/minikube/pkg/minikube/tests" +) + +func TestGetISOFileURI(t *testing.T) { + dler := DefaultDownloader{} + + tests := map[string]string{ + "file:///test/path/minikube-test.iso": "file:///test/path/minikube-test.iso", + "https://storage.googleapis.com/minikube/iso/minikube-test.iso": "file://" + filepath.ToSlash(filepath.Join(constants.GetMinipath(), "cache", "iso", "minikube-test.iso")), + } + + for input, expected := range tests { + if isoFileURI := dler.GetISOFileURI(input); isoFileURI != expected { + t.Fatalf("Expected GetISOFileURI with input %s to return %s but instead got: %s", input, expected, isoFileURI) + } + } + +} + +var testISOString = "hello" + +func TestCacheMinikubeISOFromURL(t *testing.T) { + tempDir := tests.MakeTempDir() + defer os.RemoveAll(tempDir) + dler := DefaultDownloader{} + isoPath := filepath.Join(constants.GetMinipath(), "cache", "iso", "minikube-test.iso") + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, testISOString) + })) + isoURL := server.URL + "/minikube-test.iso" + if err := dler.CacheMinikubeISOFromURL(isoURL); err != nil { + t.Fatalf("Unexpected error from CacheMinikubeISOFromURL: %s", err) + } + + transferred, err := ioutil.ReadFile(filepath.Join(isoPath)) + if err != nil { + t.Fatalf("File not copied. Could not open file at path: %s", isoPath) + } + + //test that the ISO is transferred properly + contents := []byte(testISOString) + if !bytes.Contains(transferred, contents) { + t.Fatalf("Expected transfers to contain: %s. It was: %s", contents, transferred) + } + +} + +func TestShouldCacheMinikubeISO(t *testing.T) { + dler := DefaultDownloader{} + + tests := map[string]bool{ + "file:///test/path/minikube-test.iso": false, + "https://storage.googleapis.com/minikube/iso/minikube-test.iso": true, + } + + for input, expected := range tests { + if out := dler.ShouldCacheMinikubeISO(input); out != expected { + t.Fatalf("Expected ShouldCacheMinikubeISO with input %s to return %d but instead got: %t", input, expected, out) + } + } +} + +func TestIsMinikubeISOCached(t *testing.T) { + tempDir := tests.MakeTempDir() + defer os.RemoveAll(tempDir) + + dler := DefaultDownloader{} + + testFileURI := "file:///test/path/minikube-test.iso" + expected := false + + if out := dler.IsMinikubeISOCached(testFileURI); out != expected { + t.Fatalf("Expected IsMinikubeISOCached with input to return %s but instead got: %s", testFileURI, expected, out) + } + + ioutil.WriteFile(filepath.Join(constants.GetMinipath(), "cache", "iso", "minikube-test.iso"), []byte(testISOString), os.FileMode(int(0644))) + + expected = true + if out := dler.IsMinikubeISOCached(testFileURI); out != expected { + t.Fatalf("Expected IsMinikubeISOCached with input to return %s but instead got: %s", testFileURI, expected, out) + } + +} -- GitLab