-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathconda_test.go
More file actions
170 lines (142 loc) · 5.91 KB
/
Copy pathconda_test.go
File metadata and controls
170 lines (142 loc) · 5.91 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
package conda_test
import (
"bytes"
"io"
"os"
"path/filepath"
"github.com/cloudfoundry/python-buildpack/src/python/conda"
"github.com/cloudfoundry/libbuildpack"
"github.com/cloudfoundry/libbuildpack/ansicleaner"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "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 = os.MkdirTemp("", "python-buildpack.build.")
Expect(err).To(BeNil())
DeferCleanup(os.RemoveAll, buildDir)
cacheDir, err = os.MkdirTemp("", "python-buildpack.cache.")
Expect(err).To(BeNil())
DeferCleanup(os.RemoveAll, cacheDir)
depsDir, err = os.MkdirTemp("", "python-buildpack.deps.")
Expect(err).To(BeNil())
DeferCleanup(os.RemoveAll, depsDir)
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)
})
Describe("Version", func() {
Context("runtime.txt specifies python 3", func() {
BeforeEach(func() {
Expect(os.WriteFile(filepath.Join(buildDir, "runtime.txt"), []byte("python-3.2.3"), 0644)).To(Succeed())
})
It("returns 'miniforge'", func() {
Expect(subject.Version()).To(Equal("miniforge"))
})
})
Context("runtime.txt does not exist", func() {
It("returns 'miniforge'", func() {
Expect(subject.Version()).To(Equal("miniforge"))
})
})
})
Describe("Install", func() {
It("downloads and installs miniconda version", func() {
mockInstaller.EXPECT().InstallOnlyVersion("Miniconda7", gomock.Any()).Do(func(_, path string) {
Expect(os.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(os.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, _ ...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(os.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, _ ...string) {
Expect(path).To(Equal(installerPath))
})
Expect(subject.Install("Miniconda7")).To(Succeed())
Expect(installerPath).ToNot(BeARegularFile())
})
})
Describe("UpdateAndClean", func() {
var condaPkgs string
BeforeEach(func() {
condaPkgs = os.Getenv("CONDA_PKGS_DIRS")
DeferCleanup(func() {
if condaPkgs != "" {
os.Setenv("CONDA_PKGS_DIRS", condaPkgs)
}
os.Unsetenv("BP_DEBUG")
})
})
It("uses staging cache for conda cache", 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())
Expect(os.Getenv("CONDA_PKGS_DIRS")).To(Equal(filepath.Join(cacheDir, "conda")))
})
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
`))
})
})