forked from cloudfoundry/python-buildpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconda_test.go
More file actions
216 lines (185 loc) · 8.16 KB
/
Copy pathconda_test.go
File metadata and controls
216 lines (185 loc) · 8.16 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package conda_test
import (
"bytes"
io "io"
"io/ioutil"
"os"
"path/filepath"
"github.com/cloudfoundry/python-buildpack/src/python/conda"
"github.com/cloudfoundry/libbuildpack"
"github.com/cloudfoundry/libbuildpack/ansicleaner"
gomock "github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
//go:generate mockgen -source=conda.go --destination=mocks_test.go --package=conda_test
var _ = Describe("Conda", func() {
var (
err error
buildDir string
cacheDir string
depsDir string
depsIdx string
depDir string
subject *conda.Conda
logger *libbuildpack.Logger
buffer *bytes.Buffer
mockCtrl *gomock.Controller
mockInstaller *MockInstaller
mockStager *MockStager
mockCommand *MockCommand
)
BeforeEach(func() {
buildDir, err = ioutil.TempDir("", "python-buildpack.build.")
Expect(err).To(BeNil())
cacheDir, err = ioutil.TempDir("", "python-buildpack.cache.")
Expect(err).To(BeNil())
depsDir, err = ioutil.TempDir("", "python-buildpack.deps.")
Expect(err).To(BeNil())
depsIdx = "13"
depDir = filepath.Join(depsDir, depsIdx)
mockCtrl = gomock.NewController(GinkgoT())
mockInstaller = NewMockInstaller(mockCtrl)
mockStager = NewMockStager(mockCtrl)
mockStager.EXPECT().BuildDir().AnyTimes().Return(buildDir)
mockStager.EXPECT().CacheDir().AnyTimes().Return(cacheDir)
mockStager.EXPECT().DepDir().AnyTimes().Return(depDir)
mockStager.EXPECT().DepsIdx().AnyTimes().Return(depsIdx)
mockCommand = NewMockCommand(mockCtrl)
buffer = new(bytes.Buffer)
logger = libbuildpack.NewLogger(ansicleaner.New(buffer))
subject = conda.New(mockInstaller, mockStager, mockCommand, logger)
})
AfterEach(func() {
mockCtrl.Finish()
Expect(os.RemoveAll(buildDir)).To(Succeed())
Expect(os.RemoveAll(cacheDir)).To(Succeed())
Expect(os.RemoveAll(depsDir)).To(Succeed())
})
Describe("Version", func() {
Context("runtime.txt specifies python 2", func() {
BeforeEach(func() {
Expect(ioutil.WriteFile(filepath.Join(buildDir, "runtime.txt"), []byte("python-2.6.3"), 0644)).To(Succeed())
})
It("returns 'miniconda2'", func() {
Expect(subject.Version()).To(Equal("miniconda2"))
})
})
Context("runtime.txt specifies python 3", func() {
BeforeEach(func() {
Expect(ioutil.WriteFile(filepath.Join(buildDir, "runtime.txt"), []byte("python-3.2.3"), 0644)).To(Succeed())
})
It("returns 'miniconda3'", func() {
Expect(subject.Version()).To(Equal("miniconda3"))
})
})
Context("runtime.txt does not exist", func() {
It("returns 'miniconda2'", func() {
Expect(subject.Version()).To(Equal("miniconda2"))
})
})
})
Describe("Install", func() {
It("downloads and installs miniconda version", func() {
mockInstaller.EXPECT().InstallOnlyVersion("Miniconda7", gomock.Any()).Do(func(_, path string) {
Expect(ioutil.WriteFile(path, []byte{}, 0644)).To(Succeed())
})
mockCommand.EXPECT().Execute("/", gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any())
Expect(subject.Install("Miniconda7")).To(Succeed())
})
It("make downloaded file executable", func() {
mockInstaller.EXPECT().InstallOnlyVersion("Miniconda7", gomock.Any()).Do(func(_, path string) {
Expect(ioutil.WriteFile(path, []byte{}, 0644)).To(Succeed())
})
mockCommand.EXPECT().Execute("/", gomock.Any(), gomock.Any(), gomock.Any(), "-b", "-p", filepath.Join(depDir, "conda")).Do(func(_ string, _, _ io.Writer, path, _, _, _ string) {
fi, err := os.Lstat(path)
Expect(err).NotTo(HaveOccurred())
Expect(fi.Mode()).To(Equal(os.FileMode(0755)))
})
Expect(subject.Install("Miniconda7")).To(Succeed())
})
It("deletes installer", func() {
var installerPath string
mockInstaller.EXPECT().InstallOnlyVersion("Miniconda7", gomock.Any()).Do(func(_, path string) {
Expect(ioutil.WriteFile(path, []byte{}, 0644)).To(Succeed())
installerPath = path
})
mockCommand.EXPECT().Execute("/", gomock.Any(), gomock.Any(), gomock.Any(), "-b", "-p", filepath.Join(depDir, "conda")).Do(func(_ string, _, _ io.Writer, path, _, _, _ string) {
Expect(path).To(Equal(installerPath))
})
Expect(subject.Install("Miniconda7")).To(Succeed())
Expect(installerPath).ToNot(BeARegularFile())
})
})
Describe("UpdateAndClean", func() {
AfterEach(func() {
os.Unsetenv("BP_DEBUG")
})
Context("BP_DEBUG == false", func() {
It("calls update and clean on conda (with quiet flag)", func() {
mockCommand.EXPECT().Execute("/", gomock.Any(), gomock.Any(), filepath.Join(depDir, "conda", "bin", "conda"), "env", "update", "--quiet", "-n", "dep_env", "-f", filepath.Join(buildDir, "environment.yml"))
mockCommand.EXPECT().Execute("/", gomock.Any(), gomock.Any(), filepath.Join(depDir, "conda", "bin", "conda"), "clean", "-pt")
Expect(subject.UpdateAndClean()).To(Succeed())
})
})
Context("BP_DEBUG == true", func() {
BeforeEach(func() {
os.Setenv("BP_DEBUG", "1")
})
It("calls update and clean on conda (with debug and verbose flags)", func() {
mockCommand.EXPECT().Execute("/", gomock.Any(), gomock.Any(), filepath.Join(depDir, "conda", "bin", "conda"), "env", "update", "--debug", "--verbose", "-n", "dep_env", "-f", filepath.Join(buildDir, "environment.yml"))
mockCommand.EXPECT().Execute("/", gomock.Any(), gomock.Any(), filepath.Join(depDir, "conda", "bin", "conda"), "clean", "-pt")
Expect(subject.UpdateAndClean()).To(Succeed())
})
})
})
It("ProfileD", func() {
Expect(subject.ProfileD()).To(Equal(`grep -rlI ` + depDir + ` $DEPS_DIR/13/conda | xargs sed -i -e "s|` + depDir + `|$DEPS_DIR/13|g"
source activate dep_env
`))
})
Describe("SaveCache", func() {
It("copies the conda envs dir to cache", func() {
mockCommand.EXPECT().Output("/", "cp", "-Rl", filepath.Join(depDir, "conda", "envs"), filepath.Join(cacheDir, "envs"))
Expect(subject.SaveCache()).To(Succeed())
})
It("stores dep dir in cache as conda_prefix", func() {
mockCommand.EXPECT().Output(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
Expect(subject.SaveCache()).To(Succeed())
Expect(filepath.Join(cacheDir, "conda_prefix")).To(BeARegularFile())
Expect(ioutil.ReadFile(filepath.Join(cacheDir, "conda_prefix"))).To(Equal([]byte(depDir)))
})
})
Describe("RestoreCache", func() {
Context("no cache", func() {
It("does nothing", func() {
Expect(subject.RestoreCache()).To(Succeed())
})
})
Context("envs cache exists", func() {
BeforeEach(func() {
Expect(ioutil.WriteFile(filepath.Join(cacheDir, "conda_prefix"), []byte("/old/dep/dir\n"), 0644)).To(Succeed())
Expect(os.MkdirAll(filepath.Join(cacheDir, "envs", "dir1", "dir2"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(cacheDir, "envs", "dir1", "dir2", "file"), []byte("contents"), 0644)).To(Succeed())
Expect(os.MkdirAll(filepath.Join(depDir, "conda", "envs", "existing"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(depDir, "conda", "envs", "existing", "file"), []byte("contents"), 0644)).To(Succeed())
})
It("moves copies cache envs directories to conda directory", func() {
Expect(subject.RestoreCache()).To(Succeed())
Expect(filepath.Join(depDir, "conda", "envs", "dir1", "dir2", "file")).To(BeARegularFile())
Expect(ioutil.ReadFile(filepath.Join(depDir, "conda", "envs", "dir1", "dir2", "file"))).To(Equal([]byte("contents")))
})
It("does not alter existing files in conda envs", func() {
Expect(subject.RestoreCache()).To(Succeed())
Expect(filepath.Join(depDir, "conda", "envs", "existing", "file")).To(BeARegularFile())
Expect(ioutil.ReadFile(filepath.Join(depDir, "conda", "envs", "existing", "file"))).To(Equal([]byte("contents")))
})
It("converts old depDir to new depDir", func() {
Expect(ioutil.WriteFile(filepath.Join(cacheDir, "envs", "dir1", "dir2", "file"), []byte("run /old/dep/dir/conda"), 0644)).To(Succeed())
Expect(subject.RestoreCache()).To(Succeed())
Expect(filepath.Join(depDir, "conda", "envs", "dir1", "dir2", "file")).To(BeARegularFile())
Expect(ioutil.ReadFile(filepath.Join(depDir, "conda", "envs", "dir1", "dir2", "file"))).To(Equal([]byte("run " + depDir + "/conda")))
})
})
})
})