Add more k8s tests
This commit is contained in:
parent
e31c85aace
commit
aa6fea7f21
3 changed files with 97 additions and 7 deletions
|
@ -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) {
|
func ePorts(opts ...func(port *corev1.EndpointPort)) func(*corev1.EndpointSubset) {
|
||||||
return func(spec *corev1.EndpointSubset) {
|
return func(spec *corev1.EndpointSubset) {
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
|
|
|
@ -288,12 +288,11 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error)
|
||||||
|
|
||||||
if service.Spec.Type == "ExternalName" {
|
if service.Spec.Type == "ExternalName" {
|
||||||
url := protocol + "://" + service.Spec.ExternalName
|
url := protocol + "://" + service.Spec.ExternalName
|
||||||
name := url
|
|
||||||
if port.Port != 443 && port.Port != 80 {
|
if port.Port != 443 && port.Port != 80 {
|
||||||
url = fmt.Sprintf("%s:%d", url, port.Port)
|
url = fmt.Sprintf("%s:%d", url, port.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
templateObjects.Backends[baseName].Servers[name] = types.Server{
|
templateObjects.Backends[baseName].Servers[url] = types.Server{
|
||||||
URL: url,
|
URL: url,
|
||||||
Weight: label.DefaultWeight,
|
Weight: label.DefaultWeight,
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package kubernetes
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -31,7 +32,18 @@ func TestLoadIngresses(t *testing.T) {
|
||||||
iRule(iHost("bar"),
|
iRule(iHost("bar"),
|
||||||
iPaths(
|
iPaths(
|
||||||
onePath(iBackend("service3", intstr.FromString("https"))),
|
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"),
|
sExternalName("example.com"),
|
||||||
sPorts(sPort(443, "https"))),
|
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{
|
endpoints := []*corev1.Endpoints{
|
||||||
|
@ -106,6 +136,14 @@ func TestLoadIngresses(t *testing.T) {
|
||||||
ePort(9443, "https")),
|
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{})
|
watchChan := make(chan interface{})
|
||||||
|
@ -130,13 +168,28 @@ func TestLoadIngresses(t *testing.T) {
|
||||||
),
|
),
|
||||||
backend("foo/namedthing",
|
backend("foo/namedthing",
|
||||||
lbMethod("wrr"),
|
lbMethod("wrr"),
|
||||||
servers(server("https://example.com", weight(1))),
|
servers(
|
||||||
|
server("https://example.com", weight(1)),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
backend("bar",
|
backend("bar",
|
||||||
lbMethod("wrr"),
|
lbMethod("wrr"),
|
||||||
servers(
|
servers(
|
||||||
server("https://10.15.0.1:8443", weight(1)),
|
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(
|
frontends(
|
||||||
|
@ -156,9 +209,16 @@ func TestLoadIngresses(t *testing.T) {
|
||||||
passHostHeader(),
|
passHostHeader(),
|
||||||
routes(route("bar", "Host:bar")),
|
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)
|
assert.Equal(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1517,7 +1577,6 @@ func TestMissingResources(t *testing.T) {
|
||||||
eName("missing_endpoint_subsets_service"),
|
eName("missing_endpoint_subsets_service"),
|
||||||
eUID("4"),
|
eUID("4"),
|
||||||
eNamespace("testing"),
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue