Skip to content

Commit 459526d

Browse files
liornabat-sealightsrobdimsdale
authored andcommitted
Refactoring Sealights and AppDynamics Hooks
1 parent 3e70670 commit 459526d

4 files changed

Lines changed: 105 additions & 131 deletions

File tree

src/python/hooks/appdynamics_test.go

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,7 @@ import (
1616
. "github.com/onsi/gomega"
1717
)
1818

19-
func createFile(dir, filename, command string, perm os.FileMode) error {
20-
procFile := filepath.Join(dir, filename)
21-
f, err := os.OpenFile(procFile, os.O_CREATE|os.O_WRONLY, perm)
22-
if err != nil {
23-
return err
24-
}
25-
26-
defer f.Close()
27-
28-
if _, err = f.WriteString(command); err != nil {
29-
return err
30-
}
31-
return nil
32-
}
19+
const buildOrder = "9"
3320

3421
var _ = Describe("Appdynamics", func() {
3522
var (
@@ -43,15 +30,15 @@ var _ = Describe("Appdynamics", func() {
4330

4431
BeforeEach(func() {
4532
buildDir, err = os.MkdirTemp("", "python-buildpack.build.")
46-
Expect(err).To(BeNil())
33+
Expect(err).NotTo(HaveOccurred())
4734

4835
depsDir, err = os.MkdirTemp("", "python-buildpack.deps.")
49-
Expect(err).To(BeNil())
36+
Expect(err).NotTo(HaveOccurred())
5037

5138
buffer = new(bytes.Buffer)
5239
logger := libbuildpack.NewLogger(ansicleaner.New(buffer))
5340

54-
args := []string{buildDir, "", depsDir, "9"}
41+
args := []string{buildDir, "", depsDir, buildOrder}
5542
stager = libbuildpack.NewStager(args, logger, &libbuildpack.Manifest{})
5643

5744
command := &libbuildpack.Command{}
@@ -72,7 +59,7 @@ var _ = Describe("Appdynamics", func() {
7259
startCommand := "web: python flask.py"
7360
ModifiedCommand, err := appdynamics.GenerateStartUpCommand(startCommand)
7461
Expect(ModifiedCommand).To(Equal("web: pyagent run -- python flask.py"))
75-
Expect(err).To(BeNil())
62+
Expect(err).NotTo(HaveOccurred())
7663
})
7764

7865
It("Returns an error when provided the wrong format", func() {
@@ -97,13 +84,13 @@ var _ = Describe("Appdynamics", func() {
9784
})
9885

9986
It("rewrites the procfile with pyagent", func() {
100-
err = createFile(tempProcDir, "Procfile", "web: python app.py", 0666)
101-
Expect(err).To(BeNil())
87+
Expect(os.WriteFile(filepath.Join(tempProcDir, "Procfile"), []byte("web: python app.py"), 0666)).To(Succeed())
88+
Expect(err).NotTo(HaveOccurred())
10289

10390
err = appdynamics.RewriteProcFile(filepath.Join(tempProcDir, "Procfile"))
104-
Expect(err).To(BeNil())
91+
Expect(err).NotTo(HaveOccurred())
10592
startCommand, err := os.ReadFile(filepath.Join(tempProcDir, "Procfile"))
106-
Expect(err).To(BeNil())
93+
Expect(err).NotTo(HaveOccurred())
10794
Expect(string(startCommand)).To(Equal("web: pyagent run -- python app.py"))
10895
})
10996

@@ -113,8 +100,8 @@ var _ = Describe("Appdynamics", func() {
113100
})
114101

115102
It("Errors with Procfile with wrong format", func() {
116-
err = createFile(tempProcDir, "WrongFormatProcFile", "python app.py", 0666)
117-
Expect(err).To(BeNil())
103+
Expect(os.WriteFile(filepath.Join(tempProcDir, "WrongFormatProcFile"), []byte("python app.py"), 0666)).To(Succeed())
104+
Expect(err).NotTo(HaveOccurred())
118105

119106
err = appdynamics.RewriteProcFile(filepath.Join(tempProcDir, "WrongFormatProcFile"))
120107
Expect(err).To(MatchError("improper format found in Procfile"))
@@ -125,10 +112,10 @@ var _ = Describe("Appdynamics", func() {
125112
It("creates requirements.txt", func() {
126113
Expect(libbuildpack.FileExists(filepath.Join(buildDir, "requirements.txt"))).ToNot(BeTrue())
127114
err := appdynamics.RewriteRequirementsFile(stager)
128-
Expect(err).To(BeNil())
115+
Expect(err).NotTo(HaveOccurred())
129116
Expect(libbuildpack.FileExists(filepath.Join(buildDir, "requirements.txt"))).To(BeTrue())
130117
packagesList, err := os.ReadFile(filepath.Join(buildDir, "requirements.txt"))
131-
Expect(err).To(BeNil())
118+
Expect(err).NotTo(HaveOccurred())
132119
Expect(string(packagesList)).To(Equal("appdynamics"))
133120
})
134121
})
@@ -138,10 +125,10 @@ var _ = Describe("Appdynamics", func() {
138125
Expect(libbuildpack.FileExists(filepath.Join(buildDir, "requirements.txt"))).ToNot(BeTrue())
139126
procFile := filepath.Join(buildDir, "requirements.txt")
140127
f, err := os.OpenFile(procFile, os.O_CREATE|os.O_WRONLY, 0644)
141-
Expect(err).To(BeNil())
128+
Expect(err).NotTo(HaveOccurred())
142129
defer f.Close()
143130
_, err = f.WriteString("Flask")
144-
Expect(err).To(BeNil())
131+
Expect(err).NotTo(HaveOccurred())
145132
})
146133
AfterEach(func() {
147134
Expect(os.Remove(filepath.Join(buildDir, "requirements.txt"))).To(Succeed())
@@ -150,7 +137,7 @@ var _ = Describe("Appdynamics", func() {
150137
It("rewrites requirements.txt", func() {
151138
Expect(libbuildpack.FileExists(filepath.Join(buildDir, "requirements.txt"))).To(BeTrue())
152139
err := appdynamics.RewriteRequirementsFile(stager)
153-
Expect(err).To(BeNil())
140+
Expect(err).NotTo(HaveOccurred())
154141
packages, err := os.ReadFile(filepath.Join(buildDir, "requirements.txt"))
155142
Expect(string(packages)).To(Equal("Flask\nappdynamics"))
156143
})
@@ -185,16 +172,16 @@ export APPD_KEY_2=APPD_VAL_2`
185172
export APPD_KEY_1=APPD_VAL_1
186173
export APPD_KEY_2=APPD_VAL_2`
187174
script, err := os.ReadFile(appdynamicsShellScript)
188-
Expect(err).To(BeNil())
175+
Expect(err).NotTo(HaveOccurred())
189176
Expect(string(script)).To(Equal(expectedScript))
190177
})
191178
})
192179

193180
Context("BeforeCompile when VCAP_SERVICES is not present", func() {
194181
BeforeEach(func() {
195182
Expect(os.Getenv("VCAP_SERVICES")).To(Equal(""))
196-
err = createFile(buildDir, "Procfile", "web: python app.py", 0644)
197-
Expect(err).To(BeNil())
183+
Expect(os.WriteFile(filepath.Join(buildDir, "Procfile"), []byte("web: python app.py"), 0644)).To(Succeed())
184+
Expect(err).NotTo(HaveOccurred())
198185
})
199186

200187
AfterEach(func() {
@@ -203,11 +190,11 @@ export APPD_KEY_2=APPD_VAL_2`
203190

204191
It("VCAP_SERVICES is not present", func() {
205192
err := appdynamics.BeforeCompile(stager)
206-
Expect(err).To(BeNil())
193+
Expect(err).NotTo(HaveOccurred())
207194
Expect(libbuildpack.FileExists(filepath.Join(stager.DepDir(), "profile.d", "appdynamics.sh"))).To(BeFalse())
208195
Expect(libbuildpack.FileExists(filepath.Join(buildDir, "requirements.txt"))).To(BeFalse())
209196
procCommand, err := os.ReadFile(filepath.Join(buildDir, "Procfile"))
210-
Expect(err).To(BeNil())
197+
Expect(err).NotTo(HaveOccurred())
211198
Expect(string(procCommand)).To(Equal("web: python app.py"))
212199
})
213200
})
@@ -216,8 +203,8 @@ export APPD_KEY_2=APPD_VAL_2`
216203
BeforeEach(func() {
217204
Expect(os.Getenv("VCAP_SERVICES")).To(Equal(""))
218205
os.Setenv("VCAP_SERVICES", `{"service": [{"credentials": {"login": "name"}, "name": "443"}]}`)
219-
err = createFile(buildDir, "Procfile", "web: python app.py", 0644)
220-
Expect(err).To(BeNil())
206+
Expect(os.WriteFile(filepath.Join(buildDir, "Procfile"), []byte("web: python app.py"), 0644)).To(Succeed())
207+
Expect(err).NotTo(HaveOccurred())
221208
})
222209

223210
AfterEach(func() {
@@ -227,11 +214,11 @@ export APPD_KEY_2=APPD_VAL_2`
227214

228215
It("VCAP_SERVICES has no appdynamics", func() {
229216
err := appdynamics.BeforeCompile(stager)
230-
Expect(err).To(BeNil())
217+
Expect(err).NotTo(HaveOccurred())
231218
Expect(libbuildpack.FileExists(filepath.Join(stager.DepDir(), "profile.d", "appdynamics.sh"))).To(BeFalse())
232219
Expect(libbuildpack.FileExists(filepath.Join(buildDir, "requirements.txt"))).To(BeFalse())
233220
procCommand, err := os.ReadFile(filepath.Join(buildDir, "Procfile"))
234-
Expect(err).To(BeNil())
221+
Expect(err).NotTo(HaveOccurred())
235222
Expect(string(procCommand)).To(Equal("web: python app.py"))
236223
})
237224
})
@@ -246,8 +233,8 @@ export APPD_KEY_2=APPD_VAL_2`
246233
os.Setenv("APPD_TIER_NAME", "tier")
247234
os.Setenv("APPD_NODE_NAME", "node")
248235

249-
err = createFile(buildDir, "Procfile", "web: python app.py", 0644)
250-
Expect(err).To(BeNil())
236+
Expect(os.WriteFile(filepath.Join(buildDir, "Procfile"), []byte("web: python app.py"), 0644)).To(Succeed())
237+
Expect(err).NotTo(HaveOccurred())
251238
})
252239

253240
AfterEach(func() {
@@ -256,7 +243,7 @@ export APPD_KEY_2=APPD_VAL_2`
256243

257244
It(fmt.Sprintf("VCAP_SERVICES has %s", serviceName), func() {
258245
err := appdynamics.BeforeCompile(stager)
259-
Expect(err).To(BeNil())
246+
Expect(err).NotTo(HaveOccurred())
260247

261248
Expect(libbuildpack.FileExists(filepath.Join(stager.DepDir(), "profile.d", "appdynamics.sh"))).To(BeTrue())
262249
appdynamicsInfo, err := os.ReadFile(filepath.Join(stager.DepDir(), "profile.d", "appdynamics.sh"))
@@ -271,15 +258,15 @@ export APPD_NODE_NAME=node
271258
export APPD_SSL_ENABLED=off
272259
export APPD_TIER_NAME=tier`
273260
Expect(string(appdynamicsInfo)).To(Equal(expectedInfo))
274-
Expect(err).To(BeNil())
261+
Expect(err).NotTo(HaveOccurred())
275262

276263
Expect(libbuildpack.FileExists(filepath.Join(buildDir, "requirements.txt"))).To(BeTrue())
277264
packages, err := os.ReadFile(filepath.Join(buildDir, "requirements.txt"))
278-
Expect(err).To(BeNil())
265+
Expect(err).NotTo(HaveOccurred())
279266
Expect(string(packages)).To(Equal("appdynamics"))
280267

281268
procCommand, err := os.ReadFile(filepath.Join(buildDir, "Procfile"))
282-
Expect(err).To(BeNil())
269+
Expect(err).NotTo(HaveOccurred())
283270
Expect(string(procCommand)).To(Equal("web: pyagent run -- python app.py"))
284271
})
285272
})

src/python/hooks/sealights.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (sh SealightsHook) BeforeCompile(stager *libbuildpack.Stager) error {
9999
return nil
100100
}
101101

102-
sealgithsServiceName, sealightsPlan := getSealightsServiceName(services, sh.Log)
102+
sealgithsServiceName, sealightsPlan := getSealightsServiceName(services)
103103

104104
if sealgithsServiceName == "" {
105105
sh.Log.Debug("No Sealights service found, exiting")
@@ -112,31 +112,38 @@ func (sh SealightsHook) BeforeCompile(stager *libbuildpack.Stager) error {
112112
return err
113113
}
114114

115-
if err := sh.RewriteProcFileWithSealgiths(stager, sealightsConfig.GetStartFlags()); err != nil {
115+
if err := sh.RewriteProcFileWithSealights(stager, sealightsConfig.GetStartFlags()); err != nil {
116116
sh.Log.Error("Failed to rewrite Procfile with Sealights: %s", err.Error())
117117
return fmt.Errorf("Failed to rewrite Procfile with Sealights: %s", err.Error())
118118
}
119119

120120
sh.Log.Info("Successfully set up Sealights hook")
121121
return nil
122122
}
123-
func (sh SealightsHook) RewriteProcFileWithSealgiths(stager *libbuildpack.Stager, cfgFlags string) error {
123+
func (sh SealightsHook) RewriteProcFileWithSealights(stager *libbuildpack.Stager, cfgFlags string) error {
124124
sh.Log.BeginStep("Rewriting ProcFile to start with Sealights")
125125

126126
file := filepath.Join(stager.BuildDir(), "Procfile")
127127

128-
if exists, _ := libbuildpack.FileExists(file); exists {
128+
exists, err := libbuildpack.FileExists(file)
129+
if err != nil {
130+
return err
131+
}
132+
if exists {
129133
if err := sh.RewriteProcFile(file, cfgFlags); err != nil {
130134
return err
131135
}
132-
fileContents, _ := os.ReadFile(file)
136+
fileContents, err := os.ReadFile(file)
137+
if err != nil {
138+
return fmt.Errorf("Error reading file %s: %v", file, err)
139+
}
133140
sh.Log.Info(string(fileContents))
134141
} else {
135142
sh.Log.Info("Cannot find Procfile, skipping this step!")
136143
}
137144
return nil
138145
}
139-
func getSealightsServiceName(services map[string][]SealightsPlan, log *libbuildpack.Logger) (string, SealightsPlan) {
146+
func getSealightsServiceName(services map[string][]SealightsPlan) (string, SealightsPlan) {
140147
for serviceName, servicePlans := range services {
141148
if strings.Contains(serviceName, "sealights") {
142149
return serviceName, servicePlans[0]

0 commit comments

Comments
 (0)