95 lines
3.0 KiB
Go
95 lines
3.0 KiB
Go
package kubernetes
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
"gitea.home.hrajfrisbee.cz/kacerr/egress-proxies-operator/internal/provider"
|
|
)
|
|
|
|
func TestBuildPod(t *testing.T) {
|
|
t.Parallel()
|
|
req := provider.CreateRequest{
|
|
Name: "proxy-abc123",
|
|
UID: "uid-1",
|
|
Namespace: "crawl",
|
|
ProxyName: "proxy-eu-1",
|
|
Port: 3128,
|
|
}
|
|
pod := buildPod("ubuntu/squid:6.6-24.04_edge", req)
|
|
|
|
if pod.Name != req.Name {
|
|
t.Errorf("pod.Name = %q, want %q", pod.Name, req.Name)
|
|
}
|
|
if pod.Namespace != req.Namespace {
|
|
t.Errorf("pod.Namespace = %q, want %q", pod.Namespace, req.Namespace)
|
|
}
|
|
if pod.Labels[provider.LabelManaged] != provider.LabelManagedYes {
|
|
t.Errorf("labels[%s] = %q, want %q", provider.LabelManaged, pod.Labels[provider.LabelManaged], provider.LabelManagedYes)
|
|
}
|
|
if pod.Labels[provider.LabelUID] != req.UID {
|
|
t.Errorf("labels[%s] = %q, want %q", provider.LabelUID, pod.Labels[provider.LabelUID], req.UID)
|
|
}
|
|
if pod.Spec.RestartPolicy != corev1.RestartPolicyAlways {
|
|
t.Errorf("RestartPolicy = %v, want Always", pod.Spec.RestartPolicy)
|
|
}
|
|
if len(pod.Spec.Containers) != 1 {
|
|
t.Fatalf("len(Containers) = %d, want 1", len(pod.Spec.Containers))
|
|
}
|
|
c := pod.Spec.Containers[0]
|
|
if c.Image != "ubuntu/squid:6.6-24.04_edge" {
|
|
t.Errorf("Image = %q, want ubuntu/squid:6.6-24.04_edge", c.Image)
|
|
}
|
|
if len(c.Ports) != 1 || c.Ports[0].ContainerPort != req.Port {
|
|
t.Errorf("Ports = %+v, want a single entry on port %d", c.Ports, req.Port)
|
|
}
|
|
var confEnv string
|
|
for _, e := range c.Env {
|
|
if e.Name == "SQUID_CONF" {
|
|
confEnv = e.Value
|
|
}
|
|
}
|
|
if !strings.Contains(confEnv, "http_port 3128") {
|
|
t.Errorf("SQUID_CONF env = %q, want it to contain %q", confEnv, "http_port 3128")
|
|
}
|
|
}
|
|
|
|
func TestBuildPod_usesRequestPort(t *testing.T) {
|
|
t.Parallel()
|
|
req := provider.CreateRequest{Name: "proxy-x", UID: "uid-2", Namespace: "ns", Port: 8080}
|
|
pod := buildPod("img", req)
|
|
conf := envValue(t, pod, "SQUID_CONF")
|
|
if !strings.Contains(conf, "http_port 8080") {
|
|
t.Errorf("SQUID_CONF = %q, want it to contain %q", conf, "http_port 8080")
|
|
}
|
|
if pod.Spec.Containers[0].Ports[0].ContainerPort != 8080 {
|
|
t.Errorf("ContainerPort = %d, want 8080", pod.Spec.Containers[0].Ports[0].ContainerPort)
|
|
}
|
|
}
|
|
|
|
func TestSquidConf_permissive(t *testing.T) {
|
|
t.Parallel()
|
|
conf := squidConf(3128)
|
|
// max_filedescriptors guards against squid sizing its FD tables from a
|
|
// container's effectively-unlimited RLIMIT_NOFILE and getting OOM-killed
|
|
// at startup — found by the kind verification run, must not regress.
|
|
for _, want := range []string{"http_port 3128", "http_access allow all", "via off", "forwarded_for off", "max_filedescriptors 1024"} {
|
|
if !strings.Contains(conf, want) {
|
|
t.Errorf("squidConf() = %q, want it to contain %q", conf, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func envValue(t *testing.T, pod *corev1.Pod, name string) string {
|
|
t.Helper()
|
|
for _, e := range pod.Spec.Containers[0].Env {
|
|
if e.Name == name {
|
|
return e.Value
|
|
}
|
|
}
|
|
t.Fatalf("env var %q not found on container", name)
|
|
return ""
|
|
}
|