89 lines
3.2 KiB
Go
89 lines
3.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"testing"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
mikrotikv1alpha1 "github.com/feedc0de/mikrotik-operator/api/v1alpha1"
|
|
"github.com/feedc0de/mikrotik-operator/internal/routeros"
|
|
)
|
|
|
|
func TestDesiredRules(t *testing.T) {
|
|
router := &mikrotikv1alpha1.MikroTikRouter{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "edge"},
|
|
Spec: mikrotikv1alpha1.MikroTikRouterSpec{AllowedNamespaces: []string{"games"}},
|
|
}
|
|
service := corev1.Service{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: "games", Name: "factorio",
|
|
Annotations: map[string]string{
|
|
AnnotationRouter: "edge", AnnotationServicePort: "game",
|
|
AnnotationDestinationPort: "40000", AnnotationInInterfaceList: "WAN",
|
|
},
|
|
},
|
|
Spec: corev1.ServiceSpec{
|
|
Type: corev1.ServiceTypeLoadBalancer,
|
|
Ports: []corev1.ServicePort{
|
|
{Name: "game", Port: 34197, Protocol: corev1.ProtocolUDP},
|
|
{Name: "rcon", Port: 27015, Protocol: corev1.ProtocolTCP},
|
|
},
|
|
},
|
|
Status: corev1.ServiceStatus{LoadBalancer: corev1.LoadBalancerStatus{Ingress: []corev1.LoadBalancerIngress{
|
|
{IP: "2001:db8::1"}, {IP: "192.168.0.17"},
|
|
}}},
|
|
}
|
|
|
|
rules, preserve, issues := DesiredRules(router, []corev1.Service{service})
|
|
if len(issues) != 0 {
|
|
t.Fatalf("unexpected issues: %#v", issues)
|
|
}
|
|
if len(preserve) != 0 {
|
|
t.Fatalf("unexpected preserve set: %#v", preserve)
|
|
}
|
|
if len(rules) != 1 {
|
|
t.Fatalf("got %d rules, want 1", len(rules))
|
|
}
|
|
want := routeros.Rule{
|
|
Comment: routeros.ManagedComment("games", "factorio"), Protocol: "udp",
|
|
DestinationPort: "40000", InInterfaceList: "WAN",
|
|
ToAddress: "192.168.0.17", ToPort: "34197",
|
|
}
|
|
if rules[0] != want {
|
|
t.Fatalf("rule = %#v, want %#v", rules[0], want)
|
|
}
|
|
}
|
|
|
|
func TestDesiredRulesPreservesRuleWhileLoadBalancerIsPending(t *testing.T) {
|
|
router := &mikrotikv1alpha1.MikroTikRouter{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "edge"},
|
|
Spec: mikrotikv1alpha1.MikroTikRouterSpec{AllowedNamespaces: []string{"games"}},
|
|
}
|
|
service := corev1.Service{
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: "games", Name: "factorio", Annotations: map[string]string{AnnotationRouter: "edge"}},
|
|
Spec: corev1.ServiceSpec{Type: corev1.ServiceTypeLoadBalancer, Ports: []corev1.ServicePort{{Port: 34197, Protocol: corev1.ProtocolUDP}}},
|
|
}
|
|
rules, preserve, issues := DesiredRules(router, []corev1.Service{service})
|
|
if len(rules) != 0 || len(issues) != 1 {
|
|
t.Fatalf("rules=%#v issues=%#v", rules, issues)
|
|
}
|
|
if _, ok := preserve[routeros.ManagedComment("games", "factorio")]; !ok {
|
|
t.Fatal("pending rule was not preserved")
|
|
}
|
|
}
|
|
|
|
func TestDesiredRulesDoesNotHonorUnlistedNamespace(t *testing.T) {
|
|
router := &mikrotikv1alpha1.MikroTikRouter{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "edge"},
|
|
Spec: mikrotikv1alpha1.MikroTikRouterSpec{AllowedNamespaces: []string{"trusted"}},
|
|
}
|
|
service := corev1.Service{ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: "untrusted", Name: "bad", Annotations: map[string]string{AnnotationRouter: "edge"},
|
|
}}
|
|
rules, preserve, issues := DesiredRules(router, []corev1.Service{service})
|
|
if len(rules) != 0 || len(preserve) != 0 || len(issues) != 0 {
|
|
t.Fatalf("unlisted namespace affected reconciliation: rules=%#v preserve=%#v issues=%#v", rules, preserve, issues)
|
|
}
|
|
}
|