@@ -385,3 +385,101 @@ func TestBuildProxySidecarContainer_SpireEnabled(t *testing.T) {
385385 t .Error ("svid-output volume mount should be present when SPIRE is enabled" )
386386 }
387387}
388+
389+ // TestBuildEnvoyProxyContainer_SpireEnabled_HasSocketMount asserts that
390+ // the SPIRE workload-API socket volume is mounted into the envoy-proxy
391+ // container when SPIRE is on. The bundled spiffe-helper inside the
392+ // combined image dials this socket; without the mount it sits in a
393+ // silent dial-loop and never writes /opt/svid*.pem.
394+ func TestBuildEnvoyProxyContainer_SpireEnabled_HasSocketMount (t * testing.T ) {
395+ cfg := config .CompiledDefaults ()
396+ builder := NewContainerBuilder (cfg )
397+ container := builder .BuildEnvoyProxyContainerWithSpireOption (true )
398+
399+ // Derive the expected mount path from SpiffeConfig.SocketPath the
400+ // same way the production code does, so a future change to the
401+ // canonical SocketPath in defaults.go can't leave this test
402+ // asserting against a stale literal.
403+ wantPath := spireSocketDir (cfg .Spiffe .SocketPath )
404+ if wantPath == "" {
405+ t .Fatalf ("spireSocketDir(%q) returned empty — defaults must declare a valid socket path" , cfg .Spiffe .SocketPath )
406+ }
407+
408+ found := false
409+ for _ , vm := range container .VolumeMounts {
410+ if vm .Name == "spire-agent-socket" {
411+ found = true
412+ if vm .MountPath != wantPath {
413+ t .Errorf ("spire-agent-socket mount path = %q, want %q (derived from SpiffeConfig.SocketPath %q)" ,
414+ vm .MountPath , wantPath , cfg .Spiffe .SocketPath )
415+ }
416+ if ! vm .ReadOnly {
417+ t .Error ("spire-agent-socket mount should be read-only (CSI volume itself is read-only)" )
418+ }
419+ break
420+ }
421+ }
422+ if ! found {
423+ t .Error ("envoy-proxy container missing spire-agent-socket mount when SPIRE is enabled — bundled spiffe-helper can't reach the workload API" )
424+ }
425+ }
426+
427+ // TestBuildEnvoyProxyContainer_SpireDisabled_NoSocketMount: with SPIRE
428+ // off the socket mount must be absent — there's no spiffe-helper to
429+ // dial the socket, and mounting it would still try to schedule the
430+ // CSI volume.
431+ func TestBuildEnvoyProxyContainer_SpireDisabled_NoSocketMount (t * testing.T ) {
432+ builder := NewContainerBuilder (config .CompiledDefaults ())
433+ container := builder .BuildEnvoyProxyContainerWithSpireOption (false )
434+
435+ for _ , vm := range container .VolumeMounts {
436+ if vm .Name == "spire-agent-socket" {
437+ t .Error ("envoy-proxy container should NOT have spire-agent-socket mount when SPIRE is disabled" )
438+ }
439+ }
440+ }
441+
442+ // TestBuildProxySidecarContainer_SpireEnabled_HasSocketMount: same as
443+ // the envoy-proxy variant but for the proxy-sidecar combined image.
444+ // The bundled spiffe-helper has the same workload-API requirement.
445+ func TestBuildProxySidecarContainer_SpireEnabled_HasSocketMount (t * testing.T ) {
446+ cfg := config .CompiledDefaults ()
447+ builder := NewContainerBuilder (cfg )
448+ container := builder .BuildProxySidecarContainer (true )
449+
450+ wantPath := spireSocketDir (cfg .Spiffe .SocketPath )
451+ if wantPath == "" {
452+ t .Fatalf ("spireSocketDir(%q) returned empty — defaults must declare a valid socket path" , cfg .Spiffe .SocketPath )
453+ }
454+
455+ found := false
456+ for _ , vm := range container .VolumeMounts {
457+ if vm .Name == "spire-agent-socket" {
458+ found = true
459+ if vm .MountPath != wantPath {
460+ t .Errorf ("spire-agent-socket mount path = %q, want %q (derived from SpiffeConfig.SocketPath %q)" ,
461+ vm .MountPath , wantPath , cfg .Spiffe .SocketPath )
462+ }
463+ if ! vm .ReadOnly {
464+ t .Error ("spire-agent-socket mount should be read-only (CSI volume itself is read-only)" )
465+ }
466+ break
467+ }
468+ }
469+ if ! found {
470+ t .Error ("proxy-sidecar container missing spire-agent-socket mount when SPIRE is enabled — bundled spiffe-helper can't reach the workload API" )
471+ }
472+ }
473+
474+ // TestBuildProxySidecarContainer_SpireDisabled_NoSocketMount mirrors
475+ // the envoy-proxy negative test for the proxy-sidecar variant.
476+ func TestBuildProxySidecarContainer_SpireDisabled_NoSocketMount (t * testing.T ) {
477+ builder := NewContainerBuilder (config .CompiledDefaults ())
478+ container := builder .BuildProxySidecarContainer (false )
479+
480+ for _ , vm := range container .VolumeMounts {
481+ if vm .Name == "spire-agent-socket" {
482+ t .Error ("proxy-sidecar container should NOT have spire-agent-socket mount when SPIRE is disabled" )
483+ }
484+ }
485+ }
0 commit comments