This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
profile_d_writer_test.go
91 lines (70 loc) · 2.53 KB
/
profile_d_writer_test.go
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
package staticfile_test
import (
"os"
"path/filepath"
"testing"
"github.com/paketo-community/staticfile"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testProfileDWriter(t *testing.T, when spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
profileDWriter staticfile.ProfileDWriter
layersDir string
)
it.Before(func() {
var err error
layersDir, err = os.MkdirTemp("", "layers")
Expect(err).NotTo(HaveOccurred())
profileDWriter = staticfile.NewProfileDWriter()
})
it.After(func() {
err := os.RemoveAll(layersDir)
Expect(err).NotTo(HaveOccurred())
})
when("WriteInitScript", func() {
it("writes the init script to the profile.d directory", func() {
profileDDest := filepath.Join(layersDir, "profile.d")
err := profileDWriter.WriteInitScript(profileDDest)
Expect(err).NotTo(HaveOccurred())
contents, err := os.ReadFile(filepath.Join(profileDDest, "00_staticfile.sh"))
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal(staticfile.InitScriptContents))
})
})
when("WriteStartoggingScript", func() {
it("writes the start logging script to the profile.d directory", func() {
profileDDest := filepath.Join(layersDir, "profile.d")
err := profileDWriter.WriteStartLoggingScript(profileDDest)
Expect(err).NotTo(HaveOccurred())
contents, err := os.ReadFile(filepath.Join(profileDDest, "01_start_logging.sh"))
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal(staticfile.StartLoggingContents))
})
})
when("error cases", func() {
when("it can not create the profile.d directory", func() {
it("errors", func() {
profileDDest := filepath.Join(layersDir, "profile.d")
err := os.WriteFile(profileDDest, []byte(``), 0000)
Expect(err).NotTo(HaveOccurred())
err = profileDWriter.WriteStartLoggingScript(profileDDest)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("could not create the profile.d layer directory"))
})
})
when("it can not create the start_logging.sh script", func() {
it("errors", func() {
profileDDest := filepath.Join(layersDir, "profile.d")
err := os.MkdirAll(profileDDest, 0744)
Expect(err).NotTo(HaveOccurred())
err = os.WriteFile(filepath.Join(profileDDest, "01_start_logging.sh"), []byte(``), 0000)
Expect(err).NotTo(HaveOccurred())
err = profileDWriter.WriteStartLoggingScript(profileDDest)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("could not write the 01_start_logging.sh script"))
})
})
})
}