|
| 1 | +package istioingress |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/go-logr/logr" |
| 7 | + appsv1 "k8s.io/api/apps/v1" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + |
| 11 | + "github.com/banzaicloud/koperator/api/v1beta1" |
| 12 | + "github.com/banzaicloud/koperator/pkg/resources" |
| 13 | +) |
| 14 | + |
| 15 | +func TestMeshgatewayContainerConfiguration(t *testing.T) { |
| 16 | + // Create a minimal KafkaCluster for testing |
| 17 | + kafkaCluster := &v1beta1.KafkaCluster{ |
| 18 | + ObjectMeta: metav1.ObjectMeta{ |
| 19 | + Name: "test-kafka", |
| 20 | + Namespace: "test-namespace", |
| 21 | + }, |
| 22 | + Spec: v1beta1.KafkaClusterSpec{ |
| 23 | + Brokers: []v1beta1.Broker{ |
| 24 | + {Id: 0}, |
| 25 | + }, |
| 26 | + }, |
| 27 | + } |
| 28 | + |
| 29 | + // Create reconciler |
| 30 | + reconciler := &Reconciler{ |
| 31 | + Reconciler: resources.Reconciler{ |
| 32 | + KafkaCluster: kafkaCluster, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + // Create test external listener config |
| 37 | + externalListenerConfig := v1beta1.ExternalListenerConfig{ |
| 38 | + CommonListenerSpec: v1beta1.CommonListenerSpec{ |
| 39 | + Type: "plaintext", |
| 40 | + Name: "external", |
| 41 | + ContainerPort: 9094, |
| 42 | + }, |
| 43 | + ExternalStartingPort: 19090, |
| 44 | + } |
| 45 | + |
| 46 | + // Create test ingress config |
| 47 | + ingressConfig := v1beta1.IngressConfig{ |
| 48 | + IstioIngressConfig: &v1beta1.IstioIngressConfig{}, |
| 49 | + } |
| 50 | + |
| 51 | + // Call the meshgateway function |
| 52 | + result := reconciler.meshgateway( |
| 53 | + logr.Discard(), |
| 54 | + externalListenerConfig, |
| 55 | + ingressConfig, |
| 56 | + "test-config", |
| 57 | + "default-config", |
| 58 | + "", |
| 59 | + ) |
| 60 | + |
| 61 | + // Cast to Deployment |
| 62 | + deployment, ok := result.(*appsv1.Deployment) |
| 63 | + if !ok { |
| 64 | + t.Fatalf("Expected *appsv1.Deployment, got %T", result) |
| 65 | + } |
| 66 | + |
| 67 | + // Verify deployment has one container |
| 68 | + if len(deployment.Spec.Template.Spec.Containers) != 1 { |
| 69 | + t.Fatalf("Expected 1 container, got %d", len(deployment.Spec.Template.Spec.Containers)) |
| 70 | + } |
| 71 | + |
| 72 | + container := deployment.Spec.Template.Spec.Containers[0] |
| 73 | + |
| 74 | + // Verify container name |
| 75 | + if container.Name != "istio-proxy" { |
| 76 | + t.Errorf("Expected container name 'istio-proxy', got '%s'", container.Name) |
| 77 | + } |
| 78 | + |
| 79 | + // Verify container image |
| 80 | + if container.Image != v1beta1.DefaultIstioProxyImage { |
| 81 | + t.Errorf("Expected image '%s', got '%s'", v1beta1.DefaultIstioProxyImage, container.Image) |
| 82 | + } |
| 83 | + |
| 84 | + // Verify container has command |
| 85 | + if len(container.Command) == 0 { |
| 86 | + t.Error("Expected container to have command, but it's empty") |
| 87 | + } else if container.Command[0] != "/usr/local/bin/pilot-agent" { |
| 88 | + t.Errorf("Expected command '/usr/local/bin/pilot-agent', got '%s'", container.Command[0]) |
| 89 | + } |
| 90 | + |
| 91 | + // Verify container has args |
| 92 | + if len(container.Args) == 0 { |
| 93 | + t.Error("Expected container to have args, but it's empty") |
| 94 | + } else if container.Args[0] != "proxy" { |
| 95 | + t.Errorf("Expected first arg 'proxy', got '%s'", container.Args[0]) |
| 96 | + } |
| 97 | + |
| 98 | + // Verify container has required environment variables |
| 99 | + envVarMap := make(map[string]string) |
| 100 | + for _, env := range container.Env { |
| 101 | + envVarMap[env.Name] = env.Value |
| 102 | + } |
| 103 | + |
| 104 | + requiredEnvVars := []string{ |
| 105 | + "PILOT_CERT_PROVIDER", |
| 106 | + "CA_ADDR", |
| 107 | + "PROXY_CONFIG", |
| 108 | + "ISTIO_META_CLUSTER_ID", |
| 109 | + "ISTIO_META_INTERCEPTION_MODE", |
| 110 | + "TRUST_DOMAIN", |
| 111 | + } |
| 112 | + |
| 113 | + for _, envVar := range requiredEnvVars { |
| 114 | + if _, exists := envVarMap[envVar]; !exists { |
| 115 | + t.Errorf("Expected environment variable '%s' to be set", envVar) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Verify container has required ports |
| 120 | + if len(container.Ports) != 2 { |
| 121 | + t.Errorf("Expected 2 container ports, got %d", len(container.Ports)) |
| 122 | + } |
| 123 | + |
| 124 | + portMap := make(map[string]int32) |
| 125 | + for _, port := range container.Ports { |
| 126 | + portMap[port.Name] = port.ContainerPort |
| 127 | + } |
| 128 | + |
| 129 | + if port, exists := portMap["http-envoy-prom"]; !exists || port != 15090 { |
| 130 | + t.Errorf("Expected port 'http-envoy-prom' on 15090, got %d", port) |
| 131 | + } |
| 132 | + |
| 133 | + if port, exists := portMap["status-port"]; !exists || port != 15021 { |
| 134 | + t.Errorf("Expected port 'status-port' on 15021, got %d", port) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestGetIstioProxyEnvVars(t *testing.T) { |
| 139 | + gatewayName := "test-gateway" |
| 140 | + namespace := "test-namespace" |
| 141 | + |
| 142 | + envVars := getIstioProxyEnvVars(gatewayName, namespace) |
| 143 | + |
| 144 | + // Verify we have environment variables |
| 145 | + if len(envVars) == 0 { |
| 146 | + t.Fatal("Expected environment variables, got none") |
| 147 | + } |
| 148 | + |
| 149 | + // Create a map for easier testing |
| 150 | + envVarMap := make(map[string]corev1.EnvVar) |
| 151 | + for _, env := range envVars { |
| 152 | + envVarMap[env.Name] = env |
| 153 | + } |
| 154 | + |
| 155 | + // Test specific environment variables |
| 156 | + testCases := []struct { |
| 157 | + name string |
| 158 | + expectedValue string |
| 159 | + }{ |
| 160 | + {"PILOT_CERT_PROVIDER", "istiod"}, |
| 161 | + {"CA_ADDR", "istiod.istio-system.svc:15012"}, |
| 162 | + {"PROXY_CONFIG", "{}"}, |
| 163 | + {"ISTIO_META_CLUSTER_ID", "Kubernetes"}, |
| 164 | + {"ISTIO_META_INTERCEPTION_MODE", "REDIRECT"}, |
| 165 | + {"ISTIO_META_WORKLOAD_NAME", gatewayName}, |
| 166 | + {"ISTIO_META_MESH_ID", "cluster.local"}, |
| 167 | + {"TRUST_DOMAIN", "cluster.local"}, |
| 168 | + } |
| 169 | + |
| 170 | + for _, tc := range testCases { |
| 171 | + if env, exists := envVarMap[tc.name]; !exists { |
| 172 | + t.Errorf("Expected environment variable '%s' to exist", tc.name) |
| 173 | + } else if env.Value != tc.expectedValue { |
| 174 | + t.Errorf("Expected '%s' to have value '%s', got '%s'", tc.name, tc.expectedValue, env.Value) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + // Test environment variables with field references |
| 179 | + fieldRefVars := []string{"POD_NAME", "POD_NAMESPACE", "INSTANCE_IP", "SERVICE_ACCOUNT", "HOST_IP"} |
| 180 | + for _, varName := range fieldRefVars { |
| 181 | + if env, exists := envVarMap[varName]; !exists { |
| 182 | + t.Errorf("Expected environment variable '%s' to exist", varName) |
| 183 | + } else if env.ValueFrom == nil || env.ValueFrom.FieldRef == nil { |
| 184 | + t.Errorf("Expected '%s' to have FieldRef, but it doesn't", varName) |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments