cloud_cidr_allocator_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // +build !providerless
  2. /*
  3. Copyright 2018 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package ipam
  15. import (
  16. "fmt"
  17. "testing"
  18. "time"
  19. v1 "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. )
  24. func hasNodeInProcessing(ca *cloudCIDRAllocator, name string) bool {
  25. ca.lock.Lock()
  26. defer ca.lock.Unlock()
  27. _, found := ca.nodesInProcessing[name]
  28. return found
  29. }
  30. func TestBoundedRetries(t *testing.T) {
  31. clientSet := fake.NewSimpleClientset()
  32. updateChan := make(chan string, 1) // need to buffer as we are using only on go routine
  33. stopChan := make(chan struct{})
  34. sharedInfomer := informers.NewSharedInformerFactory(clientSet, 1*time.Hour)
  35. ca := &cloudCIDRAllocator{
  36. client: clientSet,
  37. nodeUpdateChannel: updateChan,
  38. nodeLister: sharedInfomer.Core().V1().Nodes().Lister(),
  39. nodesSynced: sharedInfomer.Core().V1().Nodes().Informer().HasSynced,
  40. nodesInProcessing: map[string]*nodeProcessingInfo{},
  41. }
  42. go ca.worker(stopChan)
  43. nodeName := "testNode"
  44. ca.AllocateOrOccupyCIDR(&v1.Node{
  45. ObjectMeta: metav1.ObjectMeta{
  46. Name: nodeName,
  47. },
  48. })
  49. for hasNodeInProcessing(ca, nodeName) {
  50. // wait for node to finish processing (should terminate and not time out)
  51. }
  52. }
  53. func withinExpectedRange(got time.Duration, expected time.Duration) bool {
  54. return got >= expected/2 && got <= 3*expected/2
  55. }
  56. func TestNodeUpdateRetryTimeout(t *testing.T) {
  57. for _, tc := range []struct {
  58. count int
  59. want time.Duration
  60. }{
  61. {count: 0, want: 250 * time.Millisecond},
  62. {count: 1, want: 500 * time.Millisecond},
  63. {count: 2, want: 1000 * time.Millisecond},
  64. {count: 3, want: 2000 * time.Millisecond},
  65. {count: 50, want: 5000 * time.Millisecond},
  66. } {
  67. t.Run(fmt.Sprintf("count %d", tc.count), func(t *testing.T) {
  68. if got := nodeUpdateRetryTimeout(tc.count); !withinExpectedRange(got, tc.want) {
  69. t.Errorf("nodeUpdateRetryTimeout(tc.count) = %v; want %v", got, tc.want)
  70. }
  71. })
  72. }
  73. }