metallb_test.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright Broadcom, Inc. All Rights Reserved.
  2. // SPDX-License-Identifier: APACHE-2.0
  3. package integration
  4. import (
  5. "context"
  6. "fmt"
  7. "time"
  8. v1 "k8s.io/api/core/v1"
  9. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  10. "k8s.io/client-go/dynamic"
  11. corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
  12. . "github.com/onsi/ginkgo/v2"
  13. . "github.com/onsi/gomega"
  14. )
  15. var _ = Describe("MetalLB", func() {
  16. var typedClient corev1.CoreV1Interface
  17. var dynamicClient dynamic.Interface
  18. var ctx context.Context
  19. BeforeEach(func() {
  20. typedClient = corev1.NewForConfigOrDie(clusterConfigOrDie())
  21. dynamicClient = dynamic.NewForConfigOrDie(clusterConfigOrDie())
  22. ctx = context.Background()
  23. })
  24. Context("configured via IPAddressPool & L2Advertisement resources (CRDs)", func() {
  25. var ipPoolConfig = IpAddressPoolConfig{
  26. name: "ip-address-pool-vib-tests",
  27. addressFrom: "192.168.100.0",
  28. addressTo: "192.168.100.255",
  29. }
  30. var advertConfig = L2AdvertisementConfig{
  31. name: "l2-advertisement-vib-tests",
  32. ipAddressPoolName: ipPoolConfig.name,
  33. }
  34. BeforeEach(func() {
  35. createIpAddressPoolResourceOrDie(ctx, dynamicClient, &ipPoolConfig)
  36. createL2AdvertisementResourceOrDie(ctx, dynamicClient, &advertConfig)
  37. })
  38. AfterEach(func() {
  39. // Not need to panic here if failed, the cluster is expected to clean up with the undeployment
  40. dynamicClient.Resource(l2AdvertisementType).Namespace(*namespace).Delete(ctx, advertConfig.name, metav1.DeleteOptions{})
  41. dynamicClient.Resource(ipAddressPoolType).Namespace(*namespace).Delete(ctx, ipPoolConfig.name, metav1.DeleteOptions{})
  42. })
  43. When("a Service resource is created", func() {
  44. var service *v1.Service
  45. var serviceName string = "lb-vib-tests"
  46. var hasIP bool
  47. BeforeEach(func() {
  48. var err error
  49. createServiceOrDie(ctx, typedClient, serviceName)
  50. // Once created, the controller has to assign an IP to the managed ingress
  51. hasIP, err = retry("hasIPAssigned", 8, 15*time.Second, func() (bool, error) {
  52. return hasIPAssigned(ctx, typedClient, serviceName)
  53. })
  54. if err != nil {
  55. panic(fmt.Sprintf("There was an error checking whether the testing service had an IP assigned: %q", err))
  56. }
  57. Expect(hasIP).To(BeTrue())
  58. service, err = typedClient.Services(*namespace).Get(ctx, serviceName, metav1.GetOptions{})
  59. if err != nil {
  60. panic(fmt.Sprintf("There was an error retrieving the created Service resource: %s", err))
  61. }
  62. })
  63. AfterEach(func() {
  64. // Not need to panic here if failed, the cluster is expected to clean up with the undeployment
  65. typedClient.Services(*namespace).Delete(ctx, serviceName, metav1.DeleteOptions{})
  66. })
  67. It("should be assigned a valid IP", func() {
  68. Expect(len(service.Status.LoadBalancer.Ingress)).To(BeNumerically(">", 0))
  69. Expect(service.Status.LoadBalancer.Ingress[0].IP).NotTo(BeNil())
  70. Expect(checkValidIP(service.Status.LoadBalancer.Ingress[0].IP, ipPoolConfig.addressFrom, ipPoolConfig.addressTo)).To(BeTrue())
  71. })
  72. })
  73. })
  74. })