node_ipam_controller_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package nodeipam
  14. import (
  15. "net"
  16. "os"
  17. "os/exec"
  18. "testing"
  19. "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/client-go/informers"
  22. "k8s.io/client-go/kubernetes/fake"
  23. "k8s.io/kubernetes/pkg/controller"
  24. "k8s.io/kubernetes/pkg/controller/nodeipam/ipam"
  25. "k8s.io/kubernetes/pkg/controller/testutil"
  26. "k8s.io/legacy-cloud-providers/gce"
  27. )
  28. func newTestNodeIpamController(clusterCIDR, serviceCIDR *net.IPNet, nodeCIDRMaskSize int, allocatorType ipam.CIDRAllocatorType) (*Controller, error) {
  29. clientSet := fake.NewSimpleClientset()
  30. fakeNodeHandler := &testutil.FakeNodeHandler{
  31. Existing: []*v1.Node{
  32. {ObjectMeta: metav1.ObjectMeta{Name: "node0"}},
  33. },
  34. Clientset: fake.NewSimpleClientset(),
  35. }
  36. fakeClient := &fake.Clientset{}
  37. fakeInformerFactory := informers.NewSharedInformerFactory(fakeClient, controller.NoResyncPeriodFunc())
  38. fakeNodeInformer := fakeInformerFactory.Core().V1().Nodes()
  39. for _, node := range fakeNodeHandler.Existing {
  40. fakeNodeInformer.Informer().GetStore().Add(node)
  41. }
  42. fakeGCE := gce.NewFakeGCECloud(gce.DefaultTestClusterValues())
  43. return NewNodeIpamController(
  44. fakeNodeInformer, fakeGCE, clientSet,
  45. clusterCIDR, serviceCIDR, nodeCIDRMaskSize, allocatorType,
  46. )
  47. }
  48. // TestNewNodeIpamControllerWithCIDRMasks tests if the controller can be
  49. // created with combinations of network CIDRs and masks.
  50. func TestNewNodeIpamControllerWithCIDRMasks(t *testing.T) {
  51. for _, tc := range []struct {
  52. desc string
  53. clusterCIDR string
  54. serviceCIDR string
  55. maskSize int
  56. allocatorType ipam.CIDRAllocatorType
  57. wantFatal bool
  58. }{
  59. {"valid_range_allocator", "10.0.0.0/21", "10.1.0.0/21", 24, ipam.RangeAllocatorType, false},
  60. {"valid_cloud_allocator", "10.0.0.0/21", "10.1.0.0/21", 24, ipam.CloudAllocatorType, false},
  61. {"valid_ipam_from_cluster", "10.0.0.0/21", "10.1.0.0/21", 24, ipam.IPAMFromClusterAllocatorType, false},
  62. {"valid_ipam_from_cloud", "10.0.0.0/21", "10.1.0.0/21", 24, ipam.IPAMFromCloudAllocatorType, false},
  63. {"valid_skip_cluster_CIDR_validation_for_cloud_allocator", "invalid", "10.1.0.0/21", 24, ipam.CloudAllocatorType, false},
  64. {"invalid_cluster_CIDR", "invalid", "10.1.0.0/21", 24, ipam.IPAMFromClusterAllocatorType, true},
  65. {"valid_CIDR_smaller_than_mask_cloud_allocator", "10.0.0.0/26", "10.1.0.0/21", 24, ipam.CloudAllocatorType, false},
  66. {"invalid_CIDR_smaller_than_mask_other_allocators", "10.0.0.0/26", "10.1.0.0/21", 24, ipam.IPAMFromCloudAllocatorType, true},
  67. } {
  68. t.Run(tc.desc, func(t *testing.T) {
  69. _, clusterCIDRIpNet, _ := net.ParseCIDR(tc.clusterCIDR)
  70. _, serviceCIDRIpNet, _ := net.ParseCIDR(tc.serviceCIDR)
  71. if os.Getenv("EXIT_ON_FATAL") == "1" {
  72. // This is the subprocess which runs the actual code.
  73. newTestNodeIpamController(clusterCIDRIpNet, serviceCIDRIpNet, tc.maskSize, tc.allocatorType)
  74. return
  75. }
  76. // This is the host process that monitors the exit code of the subprocess.
  77. cmd := exec.Command(os.Args[0], "-test.run=TestNewNodeIpamControllerWithCIDRMasks/"+tc.desc)
  78. cmd.Env = append(os.Environ(), "EXIT_ON_FATAL=1")
  79. err := cmd.Run()
  80. var gotFatal bool
  81. if err != nil {
  82. exitErr, ok := err.(*exec.ExitError)
  83. if !ok {
  84. t.Fatalf("Failed to run subprocess: %v", err)
  85. }
  86. gotFatal = !exitErr.Success()
  87. }
  88. if gotFatal != tc.wantFatal {
  89. t.Errorf("newTestNodeIpamController(%v, %v, %v, %v) : gotFatal = %t ; wantFatal = %t", clusterCIDRIpNet, serviceCIDRIpNet, tc.maskSize, tc.allocatorType, gotFatal, tc.wantFatal)
  90. }
  91. })
  92. }
  93. }