Repository navigation
Expand file tree
/
Copy pathcontainer_test.go
More file actions
159 lines (134 loc) · 5.05 KB
/
Copy pathcontainer_test.go
File metadata and controls
159 lines (134 loc) · 5.05 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
package containers_test
import (
"os"
"path/filepath"
"github.com/cloudfoundry/java-buildpack/src/java/common"
"github.com/cloudfoundry/java-buildpack/src/java/containers"
"github.com/cloudfoundry/libbuildpack"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Container Registry", func() {
var (
ctx *common.Context
registry *containers.Registry
buildDir string
depsDir string
cacheDir string
)
BeforeEach(func() {
var err error
buildDir, err = os.MkdirTemp("", "build")
Expect(err).NotTo(HaveOccurred())
depsDir, err = os.MkdirTemp("", "deps")
Expect(err).NotTo(HaveOccurred())
cacheDir, err = os.MkdirTemp("", "cache")
Expect(err).NotTo(HaveOccurred())
// Create deps directory structure
err = os.MkdirAll(filepath.Join(depsDir, "0"), 0755)
Expect(err).NotTo(HaveOccurred())
logger := libbuildpack.NewLogger(os.Stdout)
manifest := &libbuildpack.Manifest{}
installer := &libbuildpack.Installer{}
stager := libbuildpack.NewStager([]string{buildDir, cacheDir, depsDir, "0"}, logger, manifest)
command := &libbuildpack.Command{}
ctx = &common.Context{
Stager: stager,
Manifest: manifest,
Installer: installer,
Log: logger,
Command: command,
}
registry = containers.NewRegistry(ctx)
})
AfterEach(func() {
os.RemoveAll(buildDir)
os.RemoveAll(depsDir)
os.RemoveAll(cacheDir)
})
Describe("Registry", func() {
BeforeEach(func() {
registry.Register(containers.NewSpringBootContainer(ctx))
registry.Register(containers.NewTomcatContainer(ctx))
registry.Register(containers.NewGroovyContainer(ctx))
registry.Register(containers.NewDistZipContainer(ctx))
registry.Register(containers.NewJavaMainContainer(ctx))
})
Context("with Spring Boot app", func() {
BeforeEach(func() {
os.MkdirAll(filepath.Join(buildDir, "BOOT-INF"), 0755)
// Create META-INF/MANIFEST.MF with Spring Boot markers
os.MkdirAll(filepath.Join(buildDir, "META-INF"), 0755)
manifest := "Manifest-Version: 1.0\nStart-Class: com.example.App\nSpring-Boot-Version: 2.7.0\n"
os.WriteFile(filepath.Join(buildDir, "META-INF", "MANIFEST.MF"), []byte(manifest), 0644)
})
It("detects Spring Boot container", func() {
container, name, err := registry.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(container).NotTo(BeNil())
Expect(name).To(Equal("Spring Boot"))
})
})
Context("with Spring Boot app and JBP_CONFIG_JAVA_MAIN java_main_class set", func() {
BeforeEach(func() {
// App looks like Spring Boot
os.MkdirAll(filepath.Join(buildDir, "BOOT-INF"), 0755)
os.MkdirAll(filepath.Join(buildDir, "META-INF"), 0755)
manifest := "Manifest-Version: 1.0\nStart-Class: com.example.App\nSpring-Boot-Version: 2.7.0\n"
os.WriteFile(filepath.Join(buildDir, "META-INF", "MANIFEST.MF"), []byte(manifest), 0644)
os.Setenv("JBP_CONFIG_JAVA_MAIN", `{java_main_class: "org.springframework.boot.loader.launch.PropertiesLauncher", arguments: "--loader.home=/home/vcap/data"}`)
})
AfterEach(func() {
os.Unsetenv("JBP_CONFIG_JAVA_MAIN")
})
It("selects Java Main container instead of Spring Boot", func() {
container, name, err := registry.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(container).NotTo(BeNil())
Expect(name).To(Equal("Java Main"))
})
It("uses the configured class and arguments in the start command", func() {
container, _, err := registry.Detect()
Expect(err).NotTo(HaveOccurred())
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).To(ContainSubstring("org.springframework.boot.loader.launch.PropertiesLauncher"))
Expect(cmd).To(ContainSubstring("--loader.home=/home/vcap/data"))
})
})
Context("with no detectable app", func() {
It("returns nil container", func() {
container, name, err := registry.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(container).To(BeNil())
Expect(name).To(BeEmpty())
})
})
})
Describe("DetectAll", func() {
It("returns all matching containers", func() {
// Create both Groovy and Tomcat (overlapping detection)
os.WriteFile(filepath.Join(buildDir, "app.groovy"), []byte("println 'hello'"), 0644)
os.MkdirAll(filepath.Join(buildDir, "WEB-INF"), 0755)
registry := containers.NewRegistry(ctx)
registry.Register(containers.NewGroovyContainer(ctx))
registry.Register(containers.NewTomcatContainer(ctx))
registry.Register(containers.NewJavaMainContainer(ctx))
detected, names, err := registry.DetectAll()
Expect(err).NotTo(HaveOccurred())
Expect(len(detected)).To(Equal(2))
Expect(len(names)).To(Equal(2))
Expect(names).To(ContainElement("Groovy"))
Expect(names).To(ContainElement("Tomcat"))
})
It("returns empty when no containers match", func() {
registry := containers.NewRegistry(ctx)
registry.Register(containers.NewSpringBootContainer(ctx))
registry.Register(containers.NewTomcatContainer(ctx))
detected, names, err := registry.DetectAll()
Expect(err).NotTo(HaveOccurred())
Expect(len(detected)).To(Equal(0))
Expect(len(names)).To(Equal(0))
})
})
})