forked from paketo-buildpacks/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpip_test.go
More file actions
187 lines (154 loc) · 5.91 KB
/
pip_test.go
File metadata and controls
187 lines (154 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package integration_test
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/occam"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
. "github.com/paketo-buildpacks/occam/matchers"
)
func testPip(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
Eventually = NewWithT(t).Eventually
pack occam.Pack
docker occam.Docker
)
it.Before(func() {
pack = occam.NewPack()
docker = occam.NewDocker()
})
context("when building a pip app", func() {
var (
image occam.Image
container occam.Container
name string
source string
)
it.Before(func() {
var err error
name, err = occam.RandomName()
Expect(err).NotTo(HaveOccurred())
})
it.After(func() {
Expect(docker.Container.Remove.Execute(container.ID)).To(Succeed())
Expect(docker.Image.Remove.Execute(image.ID)).To(Succeed())
Expect(docker.Volume.Remove.Execute(occam.CacheVolumeNames(name))).To(Succeed())
Expect(os.RemoveAll(source)).To(Succeed())
})
it("creates a working OCI image with a start command", func() {
var err error
source, err = occam.Source(filepath.Join("testdata", "pip"))
Expect(err).NotTo(HaveOccurred())
var logs fmt.Stringer
image, logs, err = pack.WithNoColor().Build.
WithBuildpacks(pythonBuildpack).
WithPullPolicy("never").
WithEnv(map[string]string{
"BPE_SOME_VARIABLE": "some-value",
"BP_IMAGE_LABELS": "some-label=some-value",
"BP_LIVE_RELOAD_ENABLED": "true",
}).
Execute(name, source)
Expect(err).NotTo(HaveOccurred(), logs.String())
container, err = docker.Container.Run.
WithEnv(map[string]string{"PORT": "8080"}).
WithPublish("8080").
WithPublishAll().
Execute(image.ID)
Expect(err).NotTo(HaveOccurred())
Eventually(container).Should(BeAvailable())
response, err := http.Get(fmt.Sprintf("http://localhost:%s", container.HostPort("8080")))
Expect(err).NotTo(HaveOccurred())
defer response.Body.Close()
Expect(response.StatusCode).To(Equal(http.StatusOK))
content, err := io.ReadAll(response.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("Hello, World with pip!"))
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for CA Certificates")))
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for CPython")))
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for Pip")))
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for Pip Install")))
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for Python Start")))
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for Procfile")))
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for Environment Variables")))
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for Image Labels")))
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for Watchexec")))
Expect(image.Buildpacks[7].Key).To(Equal("paketo-buildpacks/environment-variables"))
Expect(image.Buildpacks[7].Layers["environment-variables"].Metadata["variables"]).To(Equal(map[string]interface{}{"SOME_VARIABLE": "some-value"}))
Expect(image.Labels["some-label"]).To(Equal("some-value"))
})
context("when using CA certificates", func() {
var client *http.Client
it.Before(func() {
var err error
source, err = occam.Source(filepath.Join("testdata", "ca_cert_apps"))
Expect(err).NotTo(HaveOccurred())
caCert, err := os.ReadFile(filepath.Join(source, "client_certs", "ca.pem"))
Expect(err).NotTo(HaveOccurred())
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
cert, err := tls.LoadX509KeyPair(
filepath.Join(source, "client_certs", "cert.pem"),
filepath.Join(source, "client_certs", "key.pem"))
Expect(err).NotTo(HaveOccurred())
client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
},
},
}
})
it("builds a working OCI image with a start command and uses a client-side CA cert for requests", func() {
var err error
var logs fmt.Stringer
image, logs, err = pack.WithNoColor().Build.
WithBuildpacks(pythonBuildpack).
WithPullPolicy("never").
Execute(name, filepath.Join(source, "pip"))
Expect(err).NotTo(HaveOccurred())
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for CA Certificates")))
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for CPython")))
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for Pip")))
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for Pip Install")))
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for Python Start")))
Expect(logs).To(ContainLines(ContainSubstring("Buildpack for Procfile")))
container, err = docker.Container.Run.
WithPublish("8080").
WithEnv(map[string]string{
"PORT": "8080",
"SERVICE_BINDING_ROOT": "/bindings",
}).
WithVolumes(fmt.Sprintf("%s:/bindings/ca-certificates", filepath.Join(source, "bindings"))).
Execute(image.ID)
Expect(err).NotTo(HaveOccurred())
Eventually(func() string {
cLogs, err := docker.Container.Logs.Execute(container.ID)
Expect(err).NotTo(HaveOccurred())
return cLogs.String()
}).Should(
ContainSubstring("Added 1 additional CA certificate(s) to system truststore"),
)
request, err := http.NewRequest("GET", fmt.Sprintf("https://localhost:%s", container.HostPort("8080")), nil)
Expect(err).NotTo(HaveOccurred())
var response *http.Response
Eventually(func() error {
var err error
response, err = client.Do(request)
return err
}).Should(BeNil())
defer response.Body.Close()
Expect(response.StatusCode).To(Equal(http.StatusOK))
})
})
})
}