Skip to content

Commit 3442b6b

Browse files
authored
test(controler): add tests for handling network policy disablement (#140)
Adds behavioral test for handling disabled NetworkPolicy. Approved-by: rhdedgar
1 parent 7cca6e4 commit 3442b6b

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

controllers/llamastackdistribution_controller_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controllers_test
33
import (
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+
}

controllers/testing_support_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
corev1 "k8s.io/api/core/v1"
1616
networkingv1 "k8s.io/api/networking/v1"
1717
rbacv1 "k8s.io/api/rbac/v1"
18+
apierrors "k8s.io/apimachinery/pkg/api/errors"
1819
"k8s.io/apimachinery/pkg/api/resource"
1920
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2021
"k8s.io/apimachinery/pkg/types"
@@ -381,6 +382,16 @@ func AssertNetworkPolicyIsIngressOnly(t *testing.T, networkPolicy *networkingv1.
381382
require.Equal(t, expectedPolicyTypes, networkPolicy.Spec.PolicyTypes, "NetworkPolicy should be ingress-only")
382383
}
383384

385+
// AssertNetworkPolicyAbsent verifies that a NetworkPolicy with the given key does not exist.
386+
func AssertNetworkPolicyAbsent(t *testing.T, c client.Client, key types.NamespacedName) {
387+
t.Helper()
388+
require.Eventually(t, func() bool {
389+
var np networkingv1.NetworkPolicy
390+
err := c.Get(context.Background(), key, &np)
391+
return apierrors.IsNotFound(err)
392+
}, testTimeout, testInterval, "NetworkPolicy %s/%s should not exist", key.Namespace, key.Name)
393+
}
394+
384395
func AssertServiceAccountDeploymentAlign(t *testing.T, deployment *appsv1.Deployment, serviceAccount *corev1.ServiceAccount) {
385396
t.Helper()
386397
require.Equal(t, serviceAccount.Name, deployment.Spec.Template.Spec.ServiceAccountName,

0 commit comments

Comments
 (0)