-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcarvel_bake_test.go
More file actions
200 lines (159 loc) · 5.45 KB
/
Copy pathcarvel_bake_test.go
File metadata and controls
200 lines (159 loc) · 5.45 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
package acceptance_test
import (
"archive/zip"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
)
var pathToMain string
func TestCarvelAcceptance(t *testing.T) {
SetDefaultEventuallyTimeout(time.Minute)
RegisterFailHandler(Fail)
RunSpecs(t, "carvel acceptance")
}
var _ = BeforeSuite(func() {
if _, err := exec.LookPath("bosh"); err != nil {
Skip("bosh CLI not installed - skipping carvel acceptance tests")
}
var err error
pathToMain, err = gexec.Build("github.com/pivotal-cf/kiln")
Expect(err).NotTo(HaveOccurred())
})
var _ = AfterSuite(func() {
gexec.CleanupBuildArtifacts()
})
var _ = Describe("carvel bake command", func() {
var (
outputFile string
tmpDir string
inputPath string
commandWithArgs []string
)
const (
sampleTileFixture = "fixtures/sample-tile"
)
BeforeEach(func() {
var err error
tmpDir, err = os.MkdirTemp("", "kiln-carvel-test")
Expect(err).NotTo(HaveOccurred())
// Copy the sample-tile fixture to a temp directory
inputPath = filepath.Join(tmpDir, "tile")
err = os.CopyFS(inputPath, os.DirFS(sampleTileFixture))
Expect(err).NotTo(HaveOccurred())
// Initialize git repo (required by kiln for metadata)
gitCommands := []*exec.Cmd{
exec.Command("git", "init"),
exec.Command("git", "config", "user.email", "test@test.com"),
exec.Command("git", "config", "user.name", "Test"),
exec.Command("git", "add", "."),
exec.Command("git", "commit", "-m", "initial commit"),
}
for _, cmd := range gitCommands {
cmd.Dir = inputPath
out, err := cmd.CombinedOutput()
Expect(err).NotTo(HaveOccurred(), "error invoking git: "+string(out))
}
outputFile = filepath.Join(tmpDir, "k8s-tile-test-0.0.1.pivotal")
commandWithArgs = []string{
"carvel", "bake",
"--source-directory", inputPath,
"--output-file", outputFile,
}
})
AfterEach(func() {
_ = os.RemoveAll(tmpDir)
})
It("generates a tile with the correct structure", func() {
commandWithArgs = append(commandWithArgs, "--verbose")
command := exec.Command(pathToMain, commandWithArgs...)
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session, "60s").Should(gexec.Exit(0))
archive, err := os.Open(outputFile)
Expect(err).NotTo(HaveOccurred())
defer func() { _ = archive.Close() }()
archiveInfo, err := archive.Stat()
Expect(err).NotTo(HaveOccurred())
bakedTile, err := zip.NewReader(archive, archiveInfo.Size())
Expect(err).NotTo(HaveOccurred())
// Verify metadata exists
_, err = bakedTile.Open("metadata/metadata.yml")
Expect(err).NotTo(HaveOccurred())
// Verify releases directory contains a tgz
var foundRelease bool
for _, f := range bakedTile.File {
if filepath.Dir(f.Name) == "releases" && filepath.Ext(f.Name) == ".tgz" {
foundRelease = true
break
}
}
Expect(foundRelease).To(BeTrue(), "releases/*.tgz should be in the tile")
// Verify migrations directory exists (even if empty)
var foundMigrations bool
for _, f := range bakedTile.File {
if filepath.Dir(f.Name) == "migrations" || f.Name == "migrations/v1/" {
foundMigrations = true
break
}
}
Expect(foundMigrations).To(BeTrue(), "migrations directory should be in the tile")
Eventually(session.Out).Should(gbytes.Say("Baked"))
Eventually(session.Out).Should(gbytes.Say("k8s-tile-test"))
})
It("produces a valid zip archive", func() {
command := exec.Command(pathToMain, commandWithArgs...)
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session, "60s").Should(gexec.Exit(0))
// Verify using unzip -t
verifyCmd := exec.Command("unzip", "-t", outputFile)
verifySession, err := gexec.Start(verifyCmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(verifySession, "10s").Should(gexec.Exit(0))
Eventually(verifySession.Out).Should(gbytes.Say("No errors detected"))
})
Context("failure cases", func() {
Context("when the output-file flag is not provided", func() {
It("prints an error and exits 1", func() {
command := exec.Command(pathToMain, "carvel", "bake",
"--source-directory", inputPath,
)
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(1))
Eventually(session.Err).Should(gbytes.Say("output-file"))
})
})
Context("when the source directory does not exist", func() {
It("prints an error and exits 1", func() {
command := exec.Command(pathToMain, "carvel", "bake",
"--source-directory", "/non/existent/path",
"--output-file", outputFile,
)
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(1))
})
})
Context("when the source directory is missing base.yml", func() {
It("prints an error and exits 1", func() {
emptyDir, err := os.MkdirTemp(tmpDir, "empty-tile")
Expect(err).NotTo(HaveOccurred())
command := exec.Command(pathToMain, "carvel", "bake",
"--source-directory", emptyDir,
"--output-file", outputFile,
)
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(1))
Eventually(session.Err).Should(gbytes.Say("base.yml"))
})
})
})
})