Add more k8s tests

This commit is contained in:
Daniel Tomcej 2018-06-19 14:10:03 -06:00 committed by Traefiker Bot
parent e31c85aace
commit aa6fea7f21
3 changed files with 97 additions and 7 deletions

View file

@ -61,6 +61,13 @@ func eAddress(ip string) func(*corev1.EndpointAddress) {
}
}
func eAddressWithTargetRef(targetRef, ip string) func(*corev1.EndpointAddress) {
return func(address *corev1.EndpointAddress) {
address.TargetRef = &corev1.ObjectReference{Name: targetRef}
address.IP = ip
}
}
func ePorts(opts ...func(port *corev1.EndpointPort)) func(*corev1.EndpointSubset) {
return func(spec *corev1.EndpointSubset) {
for _, opt := range opts {

View file

@ -288,12 +288,11 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error)
if service.Spec.Type == "ExternalName" {
url := protocol + "://" + service.Spec.ExternalName
name := url
if port.Port != 443 && port.Port != 80 {
url = fmt.Sprintf("%s:%d", url, port.Port)
}
templateObjects.Backends[baseName].Servers[name] = types.Server{
templateObjects.Backends[baseName].Servers[url] = types.Server{
URL: url,
Weight: label.DefaultWeight,
}

View file

@ -3,6 +3,7 @@ package kubernetes
import (
"errors"
"fmt"
"os"
"reflect"
"testing"
"time"
@ -31,7 +32,18 @@ func TestLoadIngresses(t *testing.T) {
iRule(iHost("bar"),
iPaths(
onePath(iBackend("service3", intstr.FromString("https"))),
onePath(iBackend("service2", intstr.FromInt(802)))),
onePath(iBackend("service2", intstr.FromInt(802))),
),
),
iRule(iHost("service5"),
iPaths(
onePath(iBackend("service5", intstr.FromInt(8888))),
),
),
iRule(iHost("service6"),
iPaths(
onePath(iBackend("service6", intstr.FromInt(80))),
),
),
),
),
@ -75,6 +87,24 @@ func TestLoadIngresses(t *testing.T) {
sExternalName("example.com"),
sPorts(sPort(443, "https"))),
),
buildService(
sName("service5"),
sNamespace("testing"),
sUID("5"),
sSpec(
clusterIP("10.0.0.5"),
sType("ExternalName"),
sExternalName("example.com"),
sPorts(sPort(8888, "http"))),
),
buildService(
sName("service6"),
sNamespace("testing"),
sUID("6"),
sSpec(
clusterIP("10.0.0.6"),
sPorts(sPort(80, ""))),
),
}
endpoints := []*corev1.Endpoints{
@ -106,6 +136,14 @@ func TestLoadIngresses(t *testing.T) {
ePort(9443, "https")),
),
),
buildEndpoint(
eNamespace("testing"),
eName("service6"),
eUID("6"),
subset(
eAddresses(eAddressWithTargetRef("http://10.15.0.3:80", "10.15.0.3")),
ePorts(ePort(80, ""))),
),
}
watchChan := make(chan interface{})
@ -130,13 +168,28 @@ func TestLoadIngresses(t *testing.T) {
),
backend("foo/namedthing",
lbMethod("wrr"),
servers(server("https://example.com", weight(1))),
servers(
server("https://example.com", weight(1)),
),
),
backend("bar",
lbMethod("wrr"),
servers(
server("https://10.15.0.1:8443", weight(1)),
server("https://10.15.0.2:9443", weight(1))),
server("https://10.15.0.2:9443", weight(1)),
),
),
backend("service5",
lbMethod("wrr"),
servers(
server("http://example.com:8888", weight(1)),
),
),
backend("service6",
lbMethod("wrr"),
servers(
server("http://10.15.0.3:80", weight(1)),
),
),
),
frontends(
@ -156,9 +209,16 @@ func TestLoadIngresses(t *testing.T) {
passHostHeader(),
routes(route("bar", "Host:bar")),
),
frontend("service5",
passHostHeader(),
routes(route("service5", "Host:service5")),
),
frontend("service6",
passHostHeader(),
routes(route("service6", "Host:service6")),
),
),
)
assert.Equal(t, expected, actual)
}
@ -1517,7 +1577,6 @@ func TestMissingResources(t *testing.T) {
eName("missing_endpoint_subsets_service"),
eUID("4"),
eNamespace("testing"),
subset(),
),
}
@ -2163,3 +2222,28 @@ func TestProviderUpdateIngressStatus(t *testing.T) {
}
}
func TestProviderNewK8sInClusterClient(t *testing.T) {
p := Provider{}
os.Setenv("KUBERNETES_SERVICE_HOST", "localhost")
os.Setenv("KUBERNETES_SERVICE_PORT", "443")
defer os.Clearenv()
_, err := p.newK8sClient("")
assert.EqualError(t, err, "failed to create in-cluster configuration: open /var/run/secrets/kubernetes.io/serviceaccount/token: no such file or directory")
}
func TestProviderNewK8sInClusterClientFailLabelSel(t *testing.T) {
p := Provider{}
os.Setenv("KUBERNETES_SERVICE_HOST", "localhost")
os.Setenv("KUBERNETES_SERVICE_PORT", "443")
defer os.Clearenv()
_, err := p.newK8sClient("%")
assert.EqualError(t, err, "invalid ingress label selector: \"%\"")
}
func TestProviderNewK8sOutOfClusterClient(t *testing.T) {
p := Provider{}
p.Endpoint = "localhost"
_, err := p.newK8sClient("")
assert.NoError(t, err)
}