Skip to content

Commit d010c3e

Browse files
committed
add unit test.
1 parent 76cf9f2 commit d010c3e

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

controllers/resource_helper_test.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ import (
2828
corev1 "k8s.io/api/core/v1"
2929
"k8s.io/apimachinery/pkg/api/resource"
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31+
"k8s.io/apimachinery/pkg/runtime"
3132
"k8s.io/apimachinery/pkg/util/intstr"
33+
"sigs.k8s.io/controller-runtime/pkg/client"
34+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3235
)
3336

3437
func TestBuildContainerSpec(t *testing.T) {
@@ -664,3 +667,146 @@ func newDefaultStartupProbe(port int32) *corev1.Probe {
664667
SuccessThreshold: startupProbeSuccessThreshold,
665668
}
666669
}
670+
671+
func TestAddExternalConfigMapEnvVars(t *testing.T) {
672+
testCases := []struct {
673+
name string
674+
instance *llamav1alpha1.LlamaStackDistribution
675+
existingConfigMaps []corev1.ConfigMap
676+
expectedEnvVars []corev1.EnvVar
677+
expectError bool
678+
}{
679+
{
680+
name: "no external configmaps configured",
681+
instance: &llamav1alpha1.LlamaStackDistribution{
682+
ObjectMeta: metav1.ObjectMeta{
683+
Name: "test",
684+
Namespace: "test-ns",
685+
},
686+
Spec: llamav1alpha1.LlamaStackDistributionSpec{
687+
Server: llamav1alpha1.ServerSpec{
688+
EnvFromExternalConfigMaps: nil,
689+
},
690+
},
691+
},
692+
existingConfigMaps: []corev1.ConfigMap{},
693+
expectedEnvVars: []corev1.EnvVar{},
694+
expectError: false,
695+
},
696+
{
697+
name: "single external configmap with mapping",
698+
instance: &llamav1alpha1.LlamaStackDistribution{
699+
ObjectMeta: metav1.ObjectMeta{
700+
Name: "test",
701+
Namespace: "test-ns",
702+
},
703+
Spec: llamav1alpha1.LlamaStackDistributionSpec{
704+
Server: llamav1alpha1.ServerSpec{
705+
EnvFromExternalConfigMaps: []llamav1alpha1.ExternalConfigMapSpec{
706+
{
707+
Name: "trustyai-config",
708+
Namespace: "redhat-ods-applications",
709+
Mapping: map[string]string{
710+
"ragas-provider-image": "RAGAS_PROVIDER_IMAGE",
711+
"garak-provider-image": "GARAK_PROVIDER_IMAGE",
712+
},
713+
},
714+
},
715+
},
716+
},
717+
},
718+
existingConfigMaps: []corev1.ConfigMap{
719+
{
720+
ObjectMeta: metav1.ObjectMeta{
721+
Name: "trustyai-config",
722+
Namespace: "redhat-ods-applications",
723+
},
724+
Data: map[string]string{
725+
"ragas-provider-image": "quay.io/trustyai/ragas:v1.2.3-konflux-abc123",
726+
"garak-provider-image": "quay.io/trustyai/garak:v1.2.3-konflux-def456",
727+
"other-config": "should-be-ignored",
728+
},
729+
},
730+
},
731+
expectedEnvVars: []corev1.EnvVar{
732+
{Name: "RAGAS_PROVIDER_IMAGE", Value: "quay.io/trustyai/ragas:v1.2.3-konflux-abc123"},
733+
{Name: "GARAK_PROVIDER_IMAGE", Value: "quay.io/trustyai/garak:v1.2.3-konflux-def456"},
734+
},
735+
expectError: false,
736+
},
737+
{
738+
name: "configmap not found - should not error but skip",
739+
instance: &llamav1alpha1.LlamaStackDistribution{
740+
ObjectMeta: metav1.ObjectMeta{
741+
Name: "test",
742+
Namespace: "test-ns",
743+
},
744+
Spec: llamav1alpha1.LlamaStackDistributionSpec{
745+
Server: llamav1alpha1.ServerSpec{
746+
EnvFromExternalConfigMaps: []llamav1alpha1.ExternalConfigMapSpec{
747+
{
748+
Name: "missing-config",
749+
Namespace: "redhat-ods-applications",
750+
Mapping: map[string]string{
751+
"some-key": "SOME_ENV",
752+
},
753+
},
754+
},
755+
},
756+
},
757+
},
758+
existingConfigMaps: []corev1.ConfigMap{},
759+
expectedEnvVars: []corev1.EnvVar{},
760+
expectError: false,
761+
},
762+
}
763+
764+
for _, tc := range testCases {
765+
t.Run(tc.name, func(t *testing.T) {
766+
scheme := runtime.NewScheme()
767+
require.NoError(t, corev1.AddToScheme(scheme))
768+
require.NoError(t, llamav1alpha1.AddToScheme(scheme))
769+
770+
objs := make([]client.Object, len(tc.existingConfigMaps))
771+
for i := range tc.existingConfigMaps {
772+
objs[i] = &tc.existingConfigMaps[i]
773+
}
774+
775+
fakeClient := fake.NewClientBuilder().
776+
WithScheme(scheme).
777+
WithObjects(objs...).
778+
Build()
779+
780+
reconciler := &LlamaStackDistributionReconciler{
781+
Client: fakeClient,
782+
}
783+
784+
container := &corev1.Container{
785+
Name: "test-container",
786+
Image: "test-image",
787+
Env: []corev1.EnvVar{}, // Start with empty env vars
788+
}
789+
790+
err := addExternalConfigMapEnvVars(t.Context(), reconciler, tc.instance, container)
791+
792+
if tc.expectError {
793+
assert.Error(t, err)
794+
} else {
795+
require.NoError(t, err)
796+
797+
for _, expectedEnv := range tc.expectedEnvVars {
798+
found := false
799+
for _, actualEnv := range container.Env {
800+
if actualEnv.Name == expectedEnv.Name && actualEnv.Value == expectedEnv.Value {
801+
found = true
802+
break
803+
}
804+
}
805+
assert.True(t, found, "Expected env var %s=%s not found in container env vars", expectedEnv.Name, expectedEnv.Value)
806+
}
807+
808+
assert.Len(t, container.Env, len(tc.expectedEnvVars), "Unexpected number of env vars added")
809+
}
810+
})
811+
}
812+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ require (
2727
github.com/cespare/xxhash/v2 v2.2.0 // indirect
2828
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2929
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
30+
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
3031
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
3132
github.com/fsnotify/fsnotify v1.7.0 // indirect
3233
github.com/go-errors/errors v1.4.2 // indirect

0 commit comments

Comments
 (0)