Skip to content

Commit 786a5f9

Browse files
committed
chore(integration_runner): Refactors code
The MakeRun() function serves as a common starting point for tests and it was brought up that the manipulatiuon of the settings which occurrd in PhpTx() and CgiTx() could be unified here to clean up the code. This code also asserts if a C test case is run as that is not a supported configuration.
1 parent 6fe3be5 commit 786a5f9

File tree

2 files changed

+62
-88
lines changed

2 files changed

+62
-88
lines changed

daemon/internal/newrelic/integration/test.go

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ func merge(a, b map[string]string) map[string]string {
202202
}
203203

204204
func (t *Test) MakeRun(ctx *Context) (Tx, error) {
205+
206+
// we don't support running C tests - assert this so we can
207+
// troubleshoot if we try to run a C test
208+
if t.IsC() {
209+
fmt.Printf("ERROR - UNEXPECTED - Running C test: %s\n", t.Path)
210+
os.Exit(1)
211+
}
212+
205213
t.Env = merge(ctx.Env, t.Env)
206214
settings := merge(ctx.Settings, t.Settings)
207215
settings["newrelic.appname"] = t.Name
@@ -214,6 +222,47 @@ func (t *Test) MakeRun(ctx *Context) (Tx, error) {
214222
}
215223
}
216224

225+
// Make a copy of settings to avoid mutating the original map
226+
// Need to adjust settings to opcache
227+
phpSettings := make(map[string]string, len(settings))
228+
setOPCacheEnable := true
229+
setOPCacheEnableCLI := true
230+
var php_executable string
231+
if t.IsWeb() {
232+
php_executable = ctx.CGI
233+
} else {
234+
php_executable = ctx.PHP
235+
}
236+
for k, v := range settings {
237+
phpSettings[k] = v
238+
239+
// see if settings affect opcache config
240+
// if so then we will not set config below
241+
if k == "opcache.enable" {
242+
setOPCacheEnable = false
243+
} else if k == "opcache.enable_cli" {
244+
setOPCacheEnableCLI = false
245+
}
246+
}
247+
if ctx.UseOPCache {
248+
if !ctx.OPCacheModuleLoaded[php_executable] {
249+
phpSettings["zend_extension"] = "opcache.so"
250+
}
251+
if setOPCacheEnable {
252+
phpSettings["opcache.enable"] = "1"
253+
}
254+
if setOPCacheEnableCLI {
255+
phpSettings["opcache.enable_cli"] = "1"
256+
}
257+
} else {
258+
if setOPCacheEnable {
259+
phpSettings["opcache.enable"] = "0"
260+
}
261+
if setOPCacheEnableCLI {
262+
phpSettings["opcache.enable_cli"] = "0"
263+
}
264+
}
265+
217266
// Make a copy of t.PhpModules and remove any entries containing "opcache.so"
218267
// if opcache.so is loaded by default
219268
//
@@ -222,28 +271,21 @@ func (t *Test) MakeRun(ctx *Context) (Tx, error) {
222271
// 2. Web test and php-cgi has opcache.so loaded by default - remove any PHPMODULE spec for opcache.so
223272
// 3. PHP test and php has opcache.so loaded by default - remove any PHPMODULE spec for opcache.so
224273
phpModulesCopy := make(map[string]string)
225-
if !t.IsC() {
226-
if (t.IsWeb() && ctx.OPCacheModuleLoaded[ctx.CGI]) ||
227-
(ctx.OPCacheModuleLoaded[ctx.PHP]) {
228-
for k, v := range t.PhpModules {
229-
if !strings.Contains(v, "opcache.so") {
230-
phpModulesCopy[k] = v
231-
}
274+
if (t.IsWeb() && ctx.OPCacheModuleLoaded[ctx.CGI]) ||
275+
(ctx.OPCacheModuleLoaded[ctx.PHP]) {
276+
for k, v := range t.PhpModules {
277+
if !strings.Contains(v, "opcache.so") {
278+
phpModulesCopy[k] = v
232279
}
233280
}
234-
} else {
235-
fmt.Printf("ERROR - UNEXPECTED - Running C test: %s\n", t.Path)
236-
os.Exit(1)
237281
}
238-
settings = merge(settings, phpModulesCopy)
239282

240-
if t.IsC() {
241-
return CTx(ScriptFile(t.Path), t.Env, settings, headers, ctx)
242-
}
283+
phpSettings = merge(phpSettings, phpModulesCopy)
284+
243285
if t.IsWeb() {
244-
return CgiTx(ScriptFile(t.Path), t.Env, settings, headers, ctx)
286+
return CgiTx(ScriptFile(t.Path), t.Env, phpSettings, headers, ctx)
245287
}
246-
return PhpTx(ScriptFile(t.Path), t.Env, settings, ctx)
288+
return PhpTx(ScriptFile(t.Path), t.Env, phpSettings, ctx)
247289
}
248290

249291
func (t *Test) MakeSkipIf(ctx *Context) (Tx, error) {

daemon/internal/newrelic/integration/transaction.go

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -46,43 +46,9 @@ func PhpTx(src Script, env, settings map[string]string, ctx *Context) (Tx, error
4646
// Note: file path must be relative to the working directory.
4747
var txn Tx
4848

49-
// Make a copy of settings to avoid mutating the original map
50-
phpSettings := make(map[string]string, len(settings))
51-
setOPCacheEnable := true
52-
setOPCacheEnableCLI := true
53-
for k, v := range settings {
54-
phpSettings[k] = v
55-
56-
// see if settings affect opcache config
57-
// if so then we will not set config below
58-
if k == "opcache.enable" {
59-
setOPCacheEnable = false
60-
} else if k == "opcache.enable_cli" {
61-
setOPCacheEnableCLI = false
62-
}
63-
}
64-
if ctx.UseOPCache {
65-
if !ctx.OPCacheModuleLoaded[ctx.CGI] {
66-
phpSettings["zend_extension"] = "opcache.so"
67-
}
68-
if setOPCacheEnable {
69-
phpSettings["opcache.enable"] = "1"
70-
}
71-
if setOPCacheEnableCLI {
72-
phpSettings["opcache.enable_cli"] = "1"
73-
}
74-
} else {
75-
if setOPCacheEnable {
76-
phpSettings["opcache.enable"] = "0"
77-
}
78-
if setOPCacheEnableCLI {
79-
phpSettings["opcache.enable_cli"] = "0"
80-
}
81-
}
82-
83-
args := phpArgs(nil, filepath.Base(src.Name()), false, phpSettings)
49+
args := phpArgs(nil, filepath.Base(src.Name()), false, settings)
8450

85-
if ctx.Valgrind != "" && phpSettings["newrelic.appname"] != "skipif" {
51+
if ctx.Valgrind != "" && settings["newrelic.appname"] != "skipif" {
8652
txn = &ValgrindCLI{
8753
CLI: CLI{
8854
Path: ctx.PHP,
@@ -140,48 +106,14 @@ func CgiTx(src Script, env, settings map[string]string, headers http.Header, ctx
140106
return nil, fmt.Errorf("unable to create cgi request: %v", err)
141107
}
142108

143-
// Make a copy of settings to avoid mutating the original map
144-
cgiSettings := make(map[string]string, len(settings))
145-
setOPCacheEnable := true
146-
setOPCacheEnableCLI := true
147-
for k, v := range settings {
148-
cgiSettings[k] = v
149-
150-
// see if settings affect opcache config
151-
// if so then we will not set config below
152-
if k == "opcache.enable" {
153-
setOPCacheEnable = false
154-
} else if k == "opcache.enable_cli" {
155-
setOPCacheEnableCLI = false
156-
}
157-
}
158-
if ctx.UseOPCache {
159-
if !ctx.OPCacheModuleLoaded[ctx.CGI] {
160-
cgiSettings["zend_extension"] = "opcache.so"
161-
}
162-
if setOPCacheEnable {
163-
cgiSettings["opcache.enable"] = "1"
164-
}
165-
if setOPCacheEnableCLI {
166-
cgiSettings["opcache.enable_cli"] = "1"
167-
}
168-
} else {
169-
if setOPCacheEnable {
170-
cgiSettings["opcache.enable"] = "0"
171-
}
172-
if setOPCacheEnableCLI {
173-
cgiSettings["opcache.enable_cli"] = "0"
174-
}
175-
}
176-
177109
if ctx.Valgrind != "" {
178110
tx := &ValgrindCGI{
179111
CGI: CGI{
180112
request: req,
181113
handler: &cgi.Handler{
182114
Path: ctx.CGI,
183115
Dir: src.Dir(),
184-
Args: phpArgs(nil, "", false, cgiSettings),
116+
Args: phpArgs(nil, "", false, settings),
185117
},
186118
},
187119
Valgrind: ctx.Valgrind,
@@ -212,7 +144,7 @@ func CgiTx(src Script, env, settings map[string]string, headers http.Header, ctx
212144
handler: &cgi.Handler{
213145
Path: ctx.CGI,
214146
Dir: src.Dir(),
215-
Args: phpArgs(nil, "", false, cgiSettings),
147+
Args: phpArgs(nil, "", false, settings),
216148
},
217149
}
218150
tx.handler.Env = append(tx.handler.Env,

0 commit comments

Comments
 (0)