Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 82 additions & 81 deletions tests/e2e/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
Expand All @@ -25,17 +26,12 @@ import (
func TestNodeSetCorrectNodeLabelsAndIPAddresses(t *testing.T) {
t.Parallel()

ctx := context.Background()
node, err := testCluster.k8sClient.CoreV1().Nodes().Get(t.Context(), testCluster.ControlNodeName(), metav1.GetOptions{})
require.NoError(t, err)

node, err := testCluster.k8sClient.CoreV1().Nodes().Get(ctx, testCluster.ControlNodeName(), metav1.GetOptions{})
assert.NoError(t, err)

server, _, err := testCluster.hcloud.Server.Get(ctx, testCluster.ControlNodeName())
if err != nil {
return
}
server, _, err := testCluster.hcloud.Server.Get(t.Context(), testCluster.ControlNodeName())
require.NoError(t, err)

labels := node.Labels
expectedLabels := map[string]string{
"node.kubernetes.io/instance-type": server.ServerType.Name,
"topology.kubernetes.io/region": server.Location.Name,
Expand All @@ -46,26 +42,15 @@ func TestNodeSetCorrectNodeLabelsAndIPAddresses(t *testing.T) {
"instance.hetzner.cloud/provided-by": "cloud",
}
for expectedLabel, expectedValue := range expectedLabels {
if labelValue, ok := labels[expectedLabel]; !ok || labelValue != expectedValue {
t.Errorf("node have a not expected label %s, ok: %v, given value %s, expected value %s", expectedLabel, ok, labelValue, expectedValue)
}
}

for _, address := range node.Status.Addresses {
if address.Type == corev1.NodeExternalIP {
expectedIP := server.PublicNet.IPv4.IP.String()
if expectedIP != address.Address {
t.Errorf("Got %s as NodeExternalIP but expected %s", address.Address, expectedIP)
}
}
assert.Equal(t, expectedValue, node.Labels[expectedLabel], "unexpected value for label %s", expectedLabel)
}

for _, address := range node.Status.Addresses {
if address.Type == corev1.NodeInternalIP {
expectedIP := server.PrivateNet[0].IP.String()
if expectedIP != address.Address {
t.Errorf("Got %s as NodeInternalIP but expected %s", address.Address, expectedIP)
}
switch address.Type {
case corev1.NodeExternalIP:
assert.Equal(t, server.PublicNet.IPv4.IP.String(), address.Address, "unexpected NodeExternalIP")
case corev1.NodeInternalIP:
assert.Equal(t, server.PrivateNet[0].IP.String(), address.Address, "unexpected NodeInternalIP")
}
}
}
Expand All @@ -77,52 +62,58 @@ func TestServiceLoadBalancersMinimalSetup(t *testing.T) {
t: t,
podName: "loadbalancer-minimal",
}
t.Cleanup(func() {
lbTest.TearDown()
})

pod := lbTest.DeployTestPod()
pod, err := lbTest.DeployTestPod()
require.NoError(t, err)

lbSvc := lbTest.ServiceDefinition(pod, map[string]string{
string(annotation.LBLocation): "nbg1",
})

lbSvc, err := lbTest.CreateService(lbSvc)
if assert.NoError(t, err, "deploying test svc") {
WaitForHTTPAvailable(t, lbSvc.Status.LoadBalancer.Ingress[0].IP, false)
}
lbSvc, err = lbTest.CreateService(lbSvc)
require.NoError(t, err)

lbTest.TearDown()
err = lbTest.WaitForHTTPAvailable(lbSvc.Status.LoadBalancer.Ingress[0].IP, false)
require.NoError(t, err)
}

func TestServiceLoadBalancersHTTPS(t *testing.T) {
t.Parallel()

cert := testCluster.CreateTLSCertificate(t, "loadbalancer-https")
lbTest := lbTestHelper{
t: t,
podName: "loadbalancer-https",
port: 443,
}
t.Cleanup(func() {
lbTest.TearDown()
})

cert, err := testCluster.CreateTLSCertificate(t, "loadbalancer-https")
require.NoError(t, err)

pod := lbTest.DeployTestPod()
pod, err := lbTest.DeployTestPod()
require.NoError(t, err)

lbSvc := lbTest.ServiceDefinition(pod, map[string]string{
string(annotation.LBLocation): "nbg1",
string(annotation.LBSvcHTTPCertificates): cert.Name,
string(annotation.LBSvcProtocol): "https",
})

lbSvc, err := lbTest.CreateService(lbSvc)
if assert.NoError(t, err, "deploying test svc") {
WaitForHTTPAvailable(t, lbSvc.Status.LoadBalancer.Ingress[0].IP, true)
}
lbSvc, err = lbTest.CreateService(lbSvc)
require.NoError(t, err)

lbTest.TearDown()
err = lbTest.WaitForHTTPAvailable(lbSvc.Status.LoadBalancer.Ingress[0].IP, true)
require.NoError(t, err)
}

func TestServiceLoadBalancersHTTPSWithManagedCertificate(t *testing.T) {
t.Parallel()

ctx := context.Background()

if testCluster.certDomain == "" {
t.Skip("Skipping because CERT_DOMAIN is not set")
}
Expand All @@ -133,8 +124,12 @@ func TestServiceLoadBalancersHTTPSWithManagedCertificate(t *testing.T) {
podName: "loadbalancer-https",
port: 443,
}
t.Cleanup(func() {
lbTest.TearDown()
})

pod := lbTest.DeployTestPod()
pod, err := lbTest.DeployTestPod()
require.NoError(t, err)

lbSvc := lbTest.ServiceDefinition(pod, map[string]string{
string(annotation.LBLocation): "nbg1",
Expand All @@ -144,62 +139,66 @@ func TestServiceLoadBalancersHTTPSWithManagedCertificate(t *testing.T) {
string(annotation.LBSvcHTTPManagedCertificateUseACMEStaging): "true",
})

lbSvc, err := lbTest.CreateService(lbSvc)
if assert.NoError(t, err, "deploying test svc") {
certs, err := testCluster.hcloud.Certificate.AllWithOpts(ctx, hcloud.CertificateListOpts{
ListOpts: hcloud.ListOpts{
LabelSelector: fmt.Sprintf("%s=%s", hcops.LabelServiceUID, lbSvc.ObjectMeta.UID),
},
})
assert.NoError(t, err)
if assert.Len(t, certs, 1) {
testCluster.certificates.Add(certs[0].ID)
}
}
lbSvc, err = lbTest.CreateService(lbSvc)
require.NoError(t, err)

lbTest.TearDown()
certs, err := testCluster.hcloud.Certificate.AllWithOpts(t.Context(), hcloud.CertificateListOpts{
ListOpts: hcloud.ListOpts{
LabelSelector: fmt.Sprintf("%s=%s", hcops.LabelServiceUID, lbSvc.ObjectMeta.UID),
},
})
assert.NoError(t, err)
if assert.Len(t, certs, 1) {
testCluster.certificates.Add(certs[0].ID)
}
}

func TestServiceLoadBalancersWithPrivateNetwork(t *testing.T) {
t.Parallel()

lbTest := lbTestHelper{t: t, podName: "loadbalancer-private-network"}
t.Cleanup(func() {
lbTest.TearDown()
})

pod := lbTest.DeployTestPod()
pod, err := lbTest.DeployTestPod()
require.NoError(t, err)

ipRange := &net.IPNet{
IP: net.IPv4(10, 0, 0, 0),
Mask: net.CIDRMask(24, 32),
}

lbSvcDefinition := lbTest.ServiceDefinition(pod, map[string]string{
lbSvc := lbTest.ServiceDefinition(pod, map[string]string{
string(annotation.LBLocation): "nbg1",
string(annotation.LBUsePrivateIP): "true",
string(annotation.PrivateSubnetIPRange): ipRange.String(),
})

lbSvc, err := lbTest.CreateService(lbSvcDefinition)
if assert.NoError(t, err, "deploying test svc") {
WaitForHTTPAvailable(t, lbSvc.Status.LoadBalancer.Ingress[0].IP, false)
lbSvc, err = lbTest.CreateService(lbSvc)
require.NoError(t, err)

anyInIPRange := slices.ContainsFunc(lbSvc.Status.LoadBalancer.Ingress, func(ingress corev1.LoadBalancerIngress) bool {
ip := net.ParseIP(ingress.IP)
if ip == nil {
return false
}
return ipRange.Contains(ip)
})

assert.True(t, anyInIPRange)
}
err = lbTest.WaitForHTTPAvailable(lbSvc.Status.LoadBalancer.Ingress[0].IP, false)
require.NoError(t, err)

lbTest.TearDown()
anyInIPRange := slices.ContainsFunc(lbSvc.Status.LoadBalancer.Ingress, func(ingress corev1.LoadBalancerIngress) bool {
ip := net.ParseIP(ingress.IP)
if ip == nil {
return false
}
return ipRange.Contains(ip)
})
assert.True(t, anyInIPRange)
}

func TestRouteNetworksPodIPsAreAccessible(t *testing.T) {
t.Parallel()

err := wait.PollUntilContextTimeout(context.Background(), 1*time.Second, 2*time.Minute, true, func(ctx context.Context) (bool, error) {
var (
nodeInternalIP string
routeGateway string
)
err := wait.PollUntilContextTimeout(t.Context(), 1*time.Second, 2*time.Minute, true, func(ctx context.Context) (bool, error) {
node, err := testCluster.k8sClient.CoreV1().Nodes().Get(ctx, testCluster.ControlNodeName(), metav1.GetOptions{})
if err != nil {
return false, err
Expand All @@ -210,18 +209,20 @@ func TestRouteNetworksPodIPsAreAccessible(t *testing.T) {
return false, err
}
for _, route := range network.Routes {
if route.Destination.String() == node.Spec.PodCIDR {
for _, a := range node.Status.Addresses {
if a.Type == corev1.NodeInternalIP {
assert.Equal(t, a.Address, route.Gateway.String())
}
if route.Destination.String() != node.Spec.PodCIDR {
continue
}
routeGateway = route.Gateway.String()
for _, a := range node.Status.Addresses {
if a.Type == corev1.NodeInternalIP {
nodeInternalIP = a.Address
break
}
return true, nil
}
return true, nil
}
return false, nil
})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "error waiting for pod IPs being accessible")
assert.Equal(t, nodeInternalIP, routeGateway, "route gateway should match node internal IP")
}
18 changes: 7 additions & 11 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
package e2e

import (
"context"
"fmt"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -34,8 +34,8 @@ func TestPodIsPresent(t *testing.T) {
t.Parallel()

t.Run("hcloud-cloud-controller-manager pod is present in kube-system", func(t *testing.T) {
pods, err := testCluster.k8sClient.CoreV1().Pods("kube-system").List(context.Background(), metav1.ListOptions{})
assert.NoError(t, err)
pods, err := testCluster.k8sClient.CoreV1().Pods("kube-system").List(t.Context(), metav1.ListOptions{})
require.NoError(t, err)

found := false
for _, pod := range pods.Items {
Expand All @@ -44,20 +44,16 @@ func TestPodIsPresent(t *testing.T) {
break
}
}
if !found {
t.Error("kube-system does not contain a pod named hcloud-cloud-controller-manager")
}
assert.True(t, found, "kube-system does not contain a pod named hcloud-cloud-controller-manager")
})

t.Run("pod with app=hcloud-cloud-controller-manager is present in kube-system", func(t *testing.T) {
pods, err := testCluster.k8sClient.CoreV1().Pods("kube-system").
List(context.Background(), metav1.ListOptions{
List(t.Context(), metav1.ListOptions{
LabelSelector: "app.kubernetes.io/name=hcloud-cloud-controller-manager",
})
assert.NoError(t, err)
require.NoError(t, err)

if len(pods.Items) == 0 {
t.Fatal("kube-system does not contain a pod with label app=hcloud-cloud-controller-manager")
}
require.NotEmpty(t, pods.Items, "kube-system does not contain a pod with label app=hcloud-cloud-controller-manager")
})
}
Loading
Loading