Skip to content

Commit 7f6aacf

Browse files
committed
tests: Fixes integration_runner to not load opcache if already
1 parent 61d7343 commit 7f6aacf

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

daemon/cmd/integration_runner/main.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,8 @@ func main() {
362362
ctx.Settings["newrelic.loglevel"] = *flagLoglevel
363363
}
364364

365-
if false == *flagOpcacheOff {
366-
// PHP Modules common to all tests
367-
ctx.Settings["zend_extension"] = "opcache.so"
368-
369-
// PHP INI values common to all tests
370-
// These settings can be overwritten by adding new values to the INI block
371-
ctx.Settings["opcache.enable"] = "1"
372-
ctx.Settings["opcache.enable_cli"] = "1"
373-
}
365+
ctx.OPCacheModuleLoaded = integration.GetOPCacheModuleLoaded(*flagPHP, *flagCGI)
366+
ctx.UseOPCache = !*flagOpcacheOff
374367

375368
// If the user provided a custom agent extension, use it.
376369
if len(*flagAgent) > 0 {

daemon/internal/newrelic/integration/context.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type Context struct {
1818
Env map[string]string // environment variables to pass to each test
1919
Settings map[string]string // settings to pass to each test
2020
Timeout time.Duration // maximum test duration
21+
OPCacheModuleLoaded map[string]bool // map of PHP and CGI to OPcache default loaded status
22+
UseOPCache bool // whether to use OPcache in tests
2123
}
2224

2325
func NewContext(php, cgi string) *Context {

daemon/internal/newrelic/integration/transaction.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,22 @@ 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-
args := phpArgs(nil, filepath.Base(src.Name()), false, settings)
49+
// Make a copy of settings to avoid mutating the original map
50+
phpSettings := make(map[string]string, len(settings))
51+
for k, v := range settings {
52+
phpSettings[k] = v
53+
}
54+
if ctx.UseOPCache {
55+
if !ctx.OPCacheModuleLoaded[ctx.PHP] {
56+
phpSettings["zend_extension"] = "opcache.so"
57+
}
58+
phpSettings["opcache.enable"] = "1"
59+
phpSettings["opcache.enable_cli"] = "1"
60+
}
61+
62+
args := phpArgs(nil, filepath.Base(src.Name()), false, phpSettings)
5063

51-
if ctx.Valgrind != "" && settings["newrelic.appname"] != "skipif" {
64+
if ctx.Valgrind != "" && phpSettings["newrelic.appname"] != "skipif" {
5265
txn = &ValgrindCLI{
5366
CLI: CLI{
5467
Path: ctx.PHP,
@@ -106,14 +119,27 @@ func CgiTx(src Script, env, settings map[string]string, headers http.Header, ctx
106119
return nil, fmt.Errorf("unable to create cgi request: %v", err)
107120
}
108121

122+
// Make a copy of settings to avoid mutating the original map
123+
cgiSettings := make(map[string]string, len(settings))
124+
for k, v := range settings {
125+
cgiSettings[k] = v
126+
}
127+
if ctx.UseOPCache {
128+
if !ctx.OPCacheModuleLoaded[ctx.CGI] {
129+
cgiSettings["zend_extension"] = "opcache.so"
130+
}
131+
cgiSettings["opcache.enable"] = "1"
132+
cgiSettings["opcache.enable_cli"] = "1"
133+
}
134+
109135
if ctx.Valgrind != "" {
110136
tx := &ValgrindCGI{
111137
CGI: CGI{
112138
request: req,
113139
handler: &cgi.Handler{
114140
Path: ctx.CGI,
115141
Dir: src.Dir(),
116-
Args: phpArgs(nil, "", false, settings),
142+
Args: phpArgs(nil, "", false, cgiSettings),
117143
},
118144
},
119145
Valgrind: ctx.Valgrind,
@@ -144,7 +170,7 @@ func CgiTx(src Script, env, settings map[string]string, headers http.Header, ctx
144170
handler: &cgi.Handler{
145171
Path: ctx.CGI,
146172
Dir: src.Dir(),
147-
Args: phpArgs(nil, "", false, settings),
173+
Args: phpArgs(nil, "", false, cgiSettings),
148174
},
149175
}
150176
tx.handler.Env = append(tx.handler.Env,

daemon/internal/newrelic/integration/util.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
package integration
77

88
import (
9+
"bytes"
910
"fmt"
11+
"os"
1012
"os/exec"
1113
)
1214

@@ -31,3 +33,29 @@ func GetAgentVersion(agent_extension string) string {
3133
}
3234
return string(output)
3335
}
36+
37+
func IsOPcacheLoaded(php_executable string) bool {
38+
fmt.Printf("Checking if OPcache is loaded using %s\n", php_executable)
39+
cmd := exec.Command(php_executable, "-m")
40+
41+
output, err := cmd.Output()
42+
43+
if err != nil {
44+
fmt.Printf("Failed to check if OPcache is loaded: %v\n", err)
45+
os.Exit(1)
46+
}
47+
48+
// Check if "Zend OPcache" is in the output
49+
return bytes.Contains(output, []byte("Zend OPcache"))
50+
}
51+
52+
func GetOPCacheModuleLoaded(php, cgi string) map[string]bool {
53+
result := make(map[string]bool)
54+
55+
result[php] = IsOPcacheLoaded(php)
56+
result[cgi] = IsOPcacheLoaded(cgi)
57+
58+
fmt.Printf("OPcache loaded status: %+v\n", result)
59+
60+
return result
61+
}

0 commit comments

Comments
 (0)