-
Notifications
You must be signed in to change notification settings - Fork 76
/
bundle_downloader_test.go
130 lines (119 loc) · 3.33 KB
/
bundle_downloader_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2021 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package installer
import (
"errors"
"log"
"os"
"path/filepath"
"github.com/go-logr/logr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type mockImgpkg struct {
callCount int
err error
}
func (mi *mockImgpkg) Get(_, _ string) error {
mi.callCount++
return mi.err
}
var _ = Describe("Byohost Installer Tests", func() {
var (
bd *bundleDownloader
mi *mockImgpkg
repoAddr string
downloadPath string
normalizedOsVersion string
k8sVersion string
)
const testTag = "test-tag"
BeforeEach(func() {
normalizedOsVersion = "Ubuntu_20.04.3_x64"
k8sVersion = "1.22"
repoAddr = ""
var err error
downloadPath, err = os.MkdirTemp("", "downloaderTest")
if err != nil {
log.Fatal(err)
}
bd = &bundleDownloader{repoAddr, downloadPath, logr.Discard()}
mi = &mockImgpkg{}
})
AfterEach(func() {
err := os.RemoveAll(downloadPath)
if err != nil {
log.Fatal(err)
}
})
Context("When given correct arguments", func() {
It("Should download bundle", func() {
// Test download on cache missing
err := bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
testTag,
mi.Get)
Expect(err).ShouldNot((HaveOccurred()))
// Test no download on cache hit
err = bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
testTag,
mi.Get)
Expect(err).ShouldNot((HaveOccurred()))
Expect(mi.callCount).Should(Equal(1))
})
It("Should create dir if missing and download bundle", func() {
bd.downloadPath = filepath.Join(bd.downloadPath, "a", "b", "c")
err := bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
testTag,
mi.Get)
Expect(err).ShouldNot((HaveOccurred()))
})
})
Context("When there is error during download", func() {
It("Should return error if given bad repo", func() {
mi.err = errors.New("Fetching image: Get \"a.a.com/\": dial tcp: lookup a.a.com: no such host")
err := bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
testTag,
mi.Get)
Expect(err).Should((HaveOccurred()))
Expect(err.Error()).Should(Equal(ErrBundleDownload.Error()))
})
It("Should return error if connection timed out", func() {
mi.err = errors.New("Extracting image into directory: read tcp 192.168.0.1:1->1.1.1.1:1: read: connection timed out")
err := bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
testTag,
mi.Get)
Expect(err).Should((HaveOccurred()))
Expect(err.Error()).Should(Equal(ErrBundleDownload.Error()))
})
It("Should return error if failure in name resolution", func() {
mi.err = errors.New("Fetching image: Get \"a.a/\": dial tcp: lookup a.a: Temporary failure in name resolution")
err := bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
testTag,
mi.Get)
Expect(err).Should((HaveOccurred()))
Expect(err.Error()).Should(Equal(ErrBundleDownload.Error()))
})
It("Should return error if out of space", func() {
mi.err = errors.New("Extracting image into directory: write /tmp/asd: no space left on device")
err := bd.DownloadFromRepo(
normalizedOsVersion,
k8sVersion,
testTag,
mi.Get)
Expect(err).Should((HaveOccurred()))
Expect(err.Error()).Should(Equal(ErrBundleExtract.Error()))
})
})
})