@@ -3,6 +3,7 @@ package controllers_test
33import (
44 "context"
55 "encoding/json"
6+ "fmt"
67 "io"
78 "net/http"
89 "strings"
@@ -453,3 +454,56 @@ func TestLlamaStackProviderAndVersionInfo(t *testing.T) {
453454 updatedInstance .Status .Version .LlamaStackServerVersion ,
454455 "server version should match the mock response" )
455456}
457+
458+ func TestNetworkPolicyConfiguration (t * testing.T ) {
459+ ctrl .SetLogger (zap .New (zap .UseDevMode (true )))
460+
461+ tests := []struct {
462+ name string
463+ setup func (t * testing.T , instance * llamav1alpha1.LlamaStackDistribution )
464+ }{
465+ {
466+ name : "enabled then disabled deletes NetworkPolicy" ,
467+ setup : func (t * testing.T , instance * llamav1alpha1.LlamaStackDistribution ) {
468+ t .Helper ()
469+ // ensure NetworkPolicy exists by reconciling with feature enabled.
470+ ReconcileDistribution (t , instance , true )
471+ waitForResource (t , k8sClient , instance .Namespace , instance .Name + "-network-policy" , & networkingv1.NetworkPolicy {})
472+ },
473+ },
474+ {
475+ name : "disabled from start leaves NetworkPolicy absent" ,
476+ setup : func (t * testing.T , instance * llamav1alpha1.LlamaStackDistribution ) {
477+ // no setup needed - NetworkPolicy doesn't exist
478+ t .Helper ()
479+ },
480+ },
481+ }
482+
483+ for i , tt := range tests {
484+ t .Run (tt .name , func (t * testing.T ) {
485+ // --- arrange ---
486+ operatorNamespaceName := "test-operator-namespace"
487+ t .Setenv ("OPERATOR_NAMESPACE" , operatorNamespaceName )
488+
489+ namespace := createTestNamespace (t , "test-networkpolicy" )
490+ instance := NewDistributionBuilder ().
491+ WithName (fmt .Sprintf ("np-config-%d" , i )).
492+ WithNamespace (namespace .Name ).
493+ WithDistribution ("starter" ).
494+ Build ()
495+ require .NoError (t , k8sClient .Create (context .Background (), instance ))
496+ t .Cleanup (func () { _ = k8sClient .Delete (context .Background (), instance ) })
497+
498+ // preconditions for this scenario
499+ tt .setup (t , instance )
500+
501+ // --- act ---
502+ ReconcileDistribution (t , instance , false )
503+
504+ // --- assert ---
505+ npKey := types.NamespacedName {Name : instance .Name + "-network-policy" , Namespace : instance .Namespace }
506+ AssertNetworkPolicyAbsent (t , k8sClient , npKey )
507+ })
508+ }
509+ }
0 commit comments