@@ -18,6 +18,7 @@ package tests
1818import (
1919 "context"
2020 "fmt"
21+ "log"
2122 "os/exec"
2223 "path/filepath"
2324 "runtime"
@@ -27,6 +28,7 @@ import (
2728 "github.com/devfile/devworkspace-operator/test/e2e/pkg/config"
2829 "github.com/onsi/ginkgo/v2"
2930 "github.com/onsi/gomega"
31+ corev1 "k8s.io/api/core/v1"
3032 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3133)
3234
@@ -63,21 +65,30 @@ var _ = ginkgo.Describe("[Custom Init Container Tests]", func() {
6365 if err != nil {
6466 ginkgo .Fail (fmt .Sprintf ("Failed to apply DWOC: %s. Output: %s" , err , string (output )))
6567 }
68+ // Give the controller a moment to sync the DWOC config
69+ // The config is synced via predicates, but we want to ensure it's processed
70+ ginkgo .GinkgoWriter .Printf ("Applied DWOC config, waiting for sync...\n " )
6671 })
6772
6873 ginkgo .It ("Create workspace and verify custom init-persistent-home executed" , func () {
74+ log .Printf ("[TEST] Starting: Create workspace and verify custom init-persistent-home executed" )
75+ ginkgo .GinkgoWriter .Printf ("Starting test: Create workspace and verify custom init-persistent-home executed\n " )
6976 workspaceFile := filepath .Join (getProjectRoot (), "test" , "resources" , "custom-init-test-workspace.yaml" )
7077 commandResult , err := config .DevK8sClient .OcApplyWorkspace (config .DevWorkspaceNamespace , workspaceFile )
7178 if err != nil {
7279 ginkgo .Fail (fmt .Sprintf ("Failed to create workspace with custom init: %s %s" , err .Error (), commandResult ))
7380 return
7481 }
82+ log .Printf ("[TEST] Workspace %s created, waiting for Running status..." , workspaceName )
83+ ginkgo .GinkgoWriter .Printf ("Workspace %s created, waiting for Running status...\n " , workspaceName )
7584
7685 // Wait for workspace to be running
7786 deploy , err := config .DevK8sClient .WaitDevWsStatus (workspaceName , config .DevWorkspaceNamespace , dw .DevWorkspaceStatusRunning )
7887 if ! deploy {
7988 ginkgo .Fail (fmt .Sprintf ("Workspace didn't start properly. Error: %s" , err ))
8089 }
90+ log .Printf ("[TEST] Workspace %s is Running, waiting for pod..." , workspaceName )
91+ ginkgo .GinkgoWriter .Printf ("Workspace %s is Running, waiting for pod...\n " , workspaceName )
8192
8293 // Wait for pod to be running (may take a while for large image pulls)
8394 podSelector := fmt .Sprintf ("controller.devfile.io/devworkspace_name=%s" , workspaceName )
@@ -86,17 +97,62 @@ var _ = ginkgo.Describe("[Custom Init Container Tests]", func() {
8697 podName , err = config .AdminK8sClient .GetPodNameBySelector (podSelector , config .DevWorkspaceNamespace )
8798 return err
8899 }, "10m" , "5s" ).Should (gomega .Succeed ())
100+ log .Printf ("[TEST] Pod %s found, checking init container..." , podName )
101+ ginkgo .GinkgoWriter .Printf ("Pod %s found, checking init container...\n " , podName )
89102
90- // Wait for init container to complete and verify it succeeded
103+ // Wait for init container to be present in pod spec and complete successfully
104+ // Generated by Cursor AI - Enhanced diagnostics for init container status checking
91105 gomega .Eventually (func () error {
92106 pod , err := config .AdminK8sClient .Kube ().CoreV1 ().Pods (config .DevWorkspaceNamespace ).Get (context .TODO (), podName , metav1.GetOptions {})
93107 if err != nil {
94108 return err
95109 }
110+
111+ // First, check if init container is defined in pod spec
112+ initContainerInSpec := false
113+ var foundInitContainer * corev1.Container
114+ for i := range pod .Spec .InitContainers {
115+ if pod .Spec .InitContainers [i ].Name == constants .HomeInitComponentName {
116+ initContainerInSpec = true
117+ foundInitContainer = & pod .Spec .InitContainers [i ]
118+ break
119+ }
120+ }
121+
122+ if ! initContainerInSpec {
123+ // List all init containers in spec for debugging
124+ initContainerNames := make ([]string , 0 , len (pod .Spec .InitContainers ))
125+ for _ , initContainer := range pod .Spec .InitContainers {
126+ initContainerNames = append (initContainerNames , initContainer .Name )
127+ }
128+ // Also check pod phase and conditions for more context
129+ podPhase := pod .Status .Phase
130+ return fmt .Errorf ("init container %s not found in pod spec (pod phase: %s). Found init containers: %v. Pod conditions: %+v" ,
131+ constants .HomeInitComponentName , podPhase , initContainerNames , pod .Status .Conditions )
132+ }
133+
134+ // Verify the init container has the expected command
135+ if foundInitContainer != nil {
136+ if len (foundInitContainer .Command ) == 0 || foundInitContainer .Command [0 ] != "/bin/sh" {
137+ return fmt .Errorf ("init container %s has unexpected command: %v. Expected: [/bin/sh, -c]" ,
138+ constants .HomeInitComponentName , foundInitContainer .Command )
139+ }
140+ }
141+
142+ // Check init container status - it may not be populated immediately
96143 for _ , status := range pod .Status .InitContainerStatuses {
97144 if status .Name == constants .HomeInitComponentName {
98145 if status .State .Terminated == nil {
99- return fmt .Errorf ("init container %s still running" , constants .HomeInitComponentName )
146+ if status .State .Waiting != nil {
147+ return fmt .Errorf ("init container %s is waiting: %s (reason: %s)" ,
148+ constants .HomeInitComponentName , status .State .Waiting .Message , status .State .Waiting .Reason )
149+ }
150+ if status .State .Running != nil {
151+ // Still running, which is fine - we'll check again
152+ return fmt .Errorf ("init container %s still running" , constants .HomeInitComponentName )
153+ }
154+ // Status exists but state is nil - might be initializing
155+ return fmt .Errorf ("init container %s status exists but state is nil (may be initializing)" , constants .HomeInitComponentName )
100156 }
101157 if status .State .Terminated .ExitCode != 0 {
102158 // Get logs for debugging
@@ -111,18 +167,35 @@ var _ = ginkgo.Describe("[Custom Init Container Tests]", func() {
111167 status .State .Terminated .Message ,
112168 logMsg )
113169 }
170+ // Success!
171+ log .Printf ("[TEST] Init container %s completed successfully with exit code 0" , constants .HomeInitComponentName )
172+ ginkgo .GinkgoWriter .Printf ("Init container %s completed successfully with exit code 0\n " , constants .HomeInitComponentName )
114173 return nil
115174 }
116175 }
117- return fmt .Errorf ("init container %s status not found" , constants .HomeInitComponentName )
118- }, "5m" , "5s" ).Should (gomega .Succeed (), "Init container should complete successfully" )
119176
177+ // Init container is in spec but status not found yet
178+ // This can happen if the pod was just created and init containers haven't started
179+ statusNames := make ([]string , 0 , len (pod .Status .InitContainerStatuses ))
180+ for _ , status := range pod .Status .InitContainerStatuses {
181+ statusNames = append (statusNames , status .Name )
182+ }
183+ return fmt .Errorf ("init container %s is in pod spec but status not found yet (pod phase: %s). Found statuses: %v. This may be normal if the pod was just created" ,
184+ constants .HomeInitComponentName , pod .Status .Phase , statusNames )
185+ }, "5m" , "2s" ).Should (gomega .Succeed (), "Init container should complete successfully" )
186+
187+ log .Printf ("[TEST] Init container verification passed, checking marker file..." )
188+ ginkgo .GinkgoWriter .Printf ("Init container verification passed, checking marker file...\n " )
120189 // Check that the custom init script ran by verifying the marker file exists
121190 resultOfExecCommand , err := config .DevK8sClient .ExecCommandInContainer (podName , config .DevWorkspaceNamespace , "tooling" , "test -f /home/user/.custom_init_complete && echo 'SUCCESS' || echo 'FAILED'" )
122191 if err != nil {
123192 ginkgo .Fail (fmt .Sprintf ("Cannot execute command in container. Error: `%s`. Exec output: `%s`" , err , resultOfExecCommand ))
124193 }
194+ log .Printf ("[TEST] Marker file check result: %s" , resultOfExecCommand )
195+ ginkgo .GinkgoWriter .Printf ("Marker file check result: %s\n " , resultOfExecCommand )
125196 gomega .Expect (resultOfExecCommand ).To (gomega .ContainSubstring ("SUCCESS" ))
197+ log .Printf ("[TEST] ✓ Test PASSED: custom init-persistent-home executed and marker file created" )
198+ ginkgo .GinkgoWriter .Printf ("Test completed successfully: custom init-persistent-home executed and marker file created\n " )
126199 })
127200
128201 ginkgo .AfterEach (func () {
0 commit comments