@@ -3,148 +3,106 @@ package frameworks_test
33import (
44 "os"
55 "path/filepath"
6- "strings"
7- "testing"
86
97 "github.com/cloudfoundry/java-buildpack/src/java/resources"
8+ . "github.com/onsi/ginkgo/v2"
9+ . "github.com/onsi/gomega"
1010)
1111
12- // TestAppDynamicsEmbeddedConfigExists tests that the app-agent-config.xml file
13- // exists in the embedded resources
14- func TestAppDynamicsEmbeddedConfigExists (t * testing.T ) {
15- embeddedPath := "app_dynamics_agent/defaults/conf/app-agent-config.xml"
16-
17- exists := resources .Exists (embeddedPath )
18- if ! exists {
19- t .Fatalf ("Expected embedded resource '%s' to exist" , embeddedPath )
20- }
21- }
22-
23- // TestAppDynamicsEmbeddedConfigContent tests that the embedded app-agent-config.xml
24- // has the expected XML structure
25- func TestAppDynamicsEmbeddedConfigContent (t * testing.T ) {
26- embeddedPath := "app_dynamics_agent/defaults/conf/app-agent-config.xml"
27-
28- configData , err := resources .GetResource (embeddedPath )
29- if err != nil {
30- t .Fatalf ("Failed to read embedded app-agent-config.xml: %v" , err )
31- }
32-
33- configStr := string (configData )
34-
35- // Verify XML root element
36- if ! strings .Contains (configStr , "<app-agent-configuration>" ) {
37- t .Error ("Expected root element '<app-agent-configuration>' in config" )
38- }
39-
40- // Verify key configuration sections
41- expectedSections := []string {
42- "<configuration-properties>" ,
43- "<sensitive-url-filters>" ,
44- "<sensitive-data-filters>" ,
45- "<agent-services>" ,
46- "<agent-service name=\" BCIEngine\" " ,
47- "<agent-service name=\" SnapshotService\" " ,
48- "<agent-service name=\" TransactionMonitoringService\" " ,
49- }
50-
51- for _ , section := range expectedSections {
52- if ! strings .Contains (configStr , section ) {
53- t .Errorf ("Expected configuration section '%s' in app-agent-config.xml" , section )
54- }
55- }
56- }
57-
58- // TestAppDynamicsConfigFileCreation tests the full workflow of reading
59- // embedded config and writing it to disk
60- func TestAppDynamicsConfigFileCreation (t * testing.T ) {
61- tmpDir , err := os .MkdirTemp ("" , "appdynamics-test-*" )
62- if err != nil {
63- t .Fatalf ("Failed to create temp dir: %v" , err )
64- }
65- defer os .RemoveAll (tmpDir )
66-
67- // Create agent directory structure (defaults/conf)
68- confDir := filepath .Join (tmpDir , "app_dynamics_agent" , "defaults" , "conf" )
69- if err := os .MkdirAll (confDir , 0755 ); err != nil {
70- t .Fatalf ("Failed to create conf directory: %v" , err )
71- }
72-
73- // Read embedded config
74- embeddedPath := "app_dynamics_agent/defaults/conf/app-agent-config.xml"
75- configData , err := resources .GetResource (embeddedPath )
76- if err != nil {
77- t .Fatalf ("Failed to read embedded config: %v" , err )
78- }
79-
80- // Write to disk (no template processing needed)
81- configPath := filepath .Join (confDir , "app-agent-config.xml" )
82- if err := os .WriteFile (configPath , configData , 0644 ); err != nil {
83- t .Fatalf ("Failed to write config file: %v" , err )
84- }
85-
86- // Verify file was created
87- if _ , err := os .Stat (configPath ); os .IsNotExist (err ) {
88- t .Error ("Config file was not created" )
89- }
90-
91- // Read back and verify content
92- writtenData , err := os .ReadFile (configPath )
93- if err != nil {
94- t .Fatalf ("Failed to read written config: %v" , err )
95- }
96-
97- writtenStr := string (writtenData )
98-
99- // Verify content integrity
100- if ! strings .Contains (writtenStr , "<app-agent-configuration>" ) {
101- t .Error ("Written config is missing root XML element" )
102- }
103-
104- if ! strings .Contains (writtenStr , "<agent-service name=\" BCIEngine\" " ) {
105- t .Error ("Written config is missing BCIEngine service" )
106- }
107- }
108-
109- // TestAppDynamicsConfigSkipIfExists tests that existing config is not overwritten
110- func TestAppDynamicsConfigSkipIfExists (t * testing.T ) {
111- tmpDir , err := os .MkdirTemp ("" , "appdynamics-test-*" )
112- if err != nil {
113- t .Fatalf ("Failed to create temp dir: %v" , err )
114- }
115- defer os .RemoveAll (tmpDir )
116-
117- confDir := filepath .Join (tmpDir , "app_dynamics_agent" , "defaults" , "conf" )
118- if err := os .MkdirAll (confDir , 0755 ); err != nil {
119- t .Fatalf ("Failed to create conf directory: %v" , err )
120- }
121-
122- // Create a user-provided config FIRST
123- configPath := filepath .Join (confDir , "app-agent-config.xml" )
124- userConfig := "<!-- User-provided configuration -->\n <app-agent-configuration><configuration-properties><property name=\" custom\" value=\" true\" /></configuration-properties></app-agent-configuration>"
125- if err := os .WriteFile (configPath , []byte (userConfig ), 0644 ); err != nil {
126- t .Fatalf ("Failed to create user config: %v" , err )
127- }
128-
129- // Simulate the framework's check: if file exists, skip installation
130- if _ , err := os .Stat (configPath ); err == nil {
131- t .Log ("Config already exists, skipping installation (as expected)" )
132- } else {
133- t .Error ("Should have detected existing config file" )
134- }
135-
136- // Verify the user config is still intact
137- existingData , err := os .ReadFile (configPath )
138- if err != nil {
139- t .Fatalf ("Failed to read existing config: %v" , err )
140- }
141-
142- existingStr := string (existingData )
143- if ! strings .Contains (existingStr , "<!-- User-provided configuration -->" ) {
144- t .Error ("User-provided config was modified" )
145- }
146-
147- if ! strings .Contains (existingStr , "custom" ) {
148- t .Error ("User-provided custom property was lost" )
149- }
150- }
12+ var _ = Describe ("AppDynamics Embedded Config" , func () {
13+ const embeddedPath = "app_dynamics_agent/defaults/conf/app-agent-config.xml"
14+
15+ Describe ("Config file existence" , func () {
16+ It ("exists in embedded resources" , func () {
17+ exists := resources .Exists (embeddedPath )
18+ Expect (exists ).To (BeTrue ())
19+ })
20+ })
21+
22+ Describe ("Config file content" , func () {
23+ It ("has expected XML structure" , func () {
24+ configData , err := resources .GetResource (embeddedPath )
25+ Expect (err ).NotTo (HaveOccurred ())
26+
27+ configStr := string (configData )
28+ Expect (configStr ).To (ContainSubstring ("<app-agent-configuration>" ))
29+
30+ expectedSections := []string {
31+ "<configuration-properties>" ,
32+ "<sensitive-url-filters>" ,
33+ "<sensitive-data-filters>" ,
34+ "<agent-services>" ,
35+ "<agent-service name=\" BCIEngine\" " ,
36+ "<agent-service name=\" SnapshotService\" " ,
37+ "<agent-service name=\" TransactionMonitoringService\" " ,
38+ }
39+
40+ for _ , section := range expectedSections {
41+ Expect (configStr ).To (ContainSubstring (section ))
42+ }
43+ })
44+ })
45+ })
46+
47+ var _ = Describe ("AppDynamics Config File Operations" , func () {
48+ var tmpDir string
49+
50+ BeforeEach (func () {
51+ var err error
52+ tmpDir , err = os .MkdirTemp ("" , "appdynamics-test-*" )
53+ Expect (err ).NotTo (HaveOccurred ())
54+ })
55+
56+ AfterEach (func () {
57+ os .RemoveAll (tmpDir )
58+ })
59+
60+ Describe ("Config file creation" , func () {
61+ It ("writes embedded config to disk successfully" , func () {
62+ confDir := filepath .Join (tmpDir , "app_dynamics_agent" , "defaults" , "conf" )
63+ err := os .MkdirAll (confDir , 0755 )
64+ Expect (err ).NotTo (HaveOccurred ())
65+
66+ embeddedPath := "app_dynamics_agent/defaults/conf/app-agent-config.xml"
67+ configData , err := resources .GetResource (embeddedPath )
68+ Expect (err ).NotTo (HaveOccurred ())
69+
70+ configPath := filepath .Join (confDir , "app-agent-config.xml" )
71+ err = os .WriteFile (configPath , configData , 0644 )
72+ Expect (err ).NotTo (HaveOccurred ())
73+
74+ _ , err = os .Stat (configPath )
75+ Expect (err ).NotTo (HaveOccurred ())
76+
77+ writtenData , err := os .ReadFile (configPath )
78+ Expect (err ).NotTo (HaveOccurred ())
79+
80+ writtenStr := string (writtenData )
81+ Expect (writtenStr ).To (ContainSubstring ("<app-agent-configuration>" ))
82+ Expect (writtenStr ).To (ContainSubstring ("<agent-service name=\" BCIEngine\" " ))
83+ })
84+ })
85+
86+ Describe ("Config file skip if exists" , func () {
87+ It ("does not overwrite existing user config" , func () {
88+ confDir := filepath .Join (tmpDir , "app_dynamics_agent" , "defaults" , "conf" )
89+ err := os .MkdirAll (confDir , 0755 )
90+ Expect (err ).NotTo (HaveOccurred ())
91+
92+ configPath := filepath .Join (confDir , "app-agent-config.xml" )
93+ userConfig := "<!-- User-provided configuration -->\n <app-agent-configuration><configuration-properties><property name=\" custom\" value=\" true\" /></configuration-properties></app-agent-configuration>"
94+ err = os .WriteFile (configPath , []byte (userConfig ), 0644 )
95+ Expect (err ).NotTo (HaveOccurred ())
96+
97+ _ , err = os .Stat (configPath )
98+ Expect (err ).NotTo (HaveOccurred ())
99+
100+ existingData , err := os .ReadFile (configPath )
101+ Expect (err ).NotTo (HaveOccurred ())
102+
103+ existingStr := string (existingData )
104+ Expect (existingStr ).To (ContainSubstring ("<!-- User-provided configuration -->" ))
105+ Expect (existingStr ).To (ContainSubstring ("custom" ))
106+ })
107+ })
108+ })
0 commit comments