...

Source file src/k8s.io/kubernetes/pkg/proxy/node_test.go

Documentation: k8s.io/kubernetes/pkg/proxy

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     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  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package proxy
    18  
    19  import (
    20  	"strconv"
    21  	"testing"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/klog/v2"
    26  )
    27  
    28  func TestNodePodCIDRHandlerAdd(t *testing.T) {
    29  	oldKlogOsExit := klog.OsExit
    30  	defer func() {
    31  		klog.OsExit = oldKlogOsExit
    32  	}()
    33  	klog.OsExit = customExit
    34  
    35  	tests := []struct {
    36  		name            string
    37  		oldNodePodCIDRs []string
    38  		newNodePodCIDRs []string
    39  		expectPanic     bool
    40  	}{
    41  		{
    42  			name: "both empty",
    43  		},
    44  		{
    45  			name:            "initialized correctly",
    46  			newNodePodCIDRs: []string{"192.168.1.0/24", "fd00:1:2:3::/64"},
    47  		},
    48  		{
    49  			name:            "already initialized and same node",
    50  			oldNodePodCIDRs: []string{"10.0.0.0/24", "fd00:3:2:1::/64"},
    51  			newNodePodCIDRs: []string{"10.0.0.0/24", "fd00:3:2:1::/64"},
    52  		},
    53  		{
    54  			name:            "already initialized and different node",
    55  			oldNodePodCIDRs: []string{"192.168.1.0/24", "fd00:1:2:3::/64"},
    56  			newNodePodCIDRs: []string{"10.0.0.0/24", "fd00:3:2:1::/64"},
    57  			expectPanic:     true,
    58  		},
    59  	}
    60  	for _, tt := range tests {
    61  		t.Run(tt.name, func(t *testing.T) {
    62  			n := &NodePodCIDRHandler{
    63  				podCIDRs: tt.oldNodePodCIDRs,
    64  			}
    65  			node := &v1.Node{
    66  				ObjectMeta: metav1.ObjectMeta{
    67  					Name:            "test-node",
    68  					ResourceVersion: "1",
    69  				},
    70  				Spec: v1.NodeSpec{
    71  					PodCIDRs: tt.newNodePodCIDRs,
    72  				},
    73  			}
    74  			defer func() {
    75  				r := recover()
    76  				if r == nil && tt.expectPanic {
    77  					t.Errorf("The code did not panic")
    78  				} else if r != nil && !tt.expectPanic {
    79  					t.Errorf("The code did panic")
    80  				}
    81  			}()
    82  
    83  			n.OnNodeAdd(node)
    84  		})
    85  	}
    86  }
    87  
    88  func TestNodePodCIDRHandlerUpdate(t *testing.T) {
    89  	oldKlogOsExit := klog.OsExit
    90  	defer func() {
    91  		klog.OsExit = oldKlogOsExit
    92  	}()
    93  	klog.OsExit = customExit
    94  
    95  	tests := []struct {
    96  		name            string
    97  		oldNodePodCIDRs []string
    98  		newNodePodCIDRs []string
    99  		expectPanic     bool
   100  	}{
   101  		{
   102  			name: "both empty",
   103  		},
   104  		{
   105  			name:            "initialize",
   106  			newNodePodCIDRs: []string{"192.168.1.0/24", "fd00:1:2:3::/64"},
   107  		},
   108  		{
   109  			name:            "same node",
   110  			oldNodePodCIDRs: []string{"192.168.1.0/24", "fd00:1:2:3::/64"},
   111  			newNodePodCIDRs: []string{"192.168.1.0/24", "fd00:1:2:3::/64"},
   112  		},
   113  		{
   114  			name:            "different nodes",
   115  			oldNodePodCIDRs: []string{"192.168.1.0/24", "fd00:1:2:3::/64"},
   116  			newNodePodCIDRs: []string{"10.0.0.0/24", "fd00:3:2:1::/64"},
   117  			expectPanic:     true,
   118  		},
   119  	}
   120  	for _, tt := range tests {
   121  		t.Run(tt.name, func(t *testing.T) {
   122  			n := &NodePodCIDRHandler{
   123  				podCIDRs: tt.oldNodePodCIDRs,
   124  			}
   125  			oldNode := &v1.Node{}
   126  			node := &v1.Node{
   127  				ObjectMeta: metav1.ObjectMeta{
   128  					Name:            "test-node",
   129  					ResourceVersion: "1",
   130  				},
   131  				Spec: v1.NodeSpec{
   132  					PodCIDRs: tt.newNodePodCIDRs,
   133  				},
   134  			}
   135  			defer func() {
   136  				r := recover()
   137  				if r == nil && tt.expectPanic {
   138  					t.Errorf("The code did not panic")
   139  				} else if r != nil && !tt.expectPanic {
   140  					t.Errorf("The code did panic")
   141  				}
   142  			}()
   143  
   144  			n.OnNodeUpdate(oldNode, node)
   145  		})
   146  	}
   147  }
   148  
   149  func customExit(exitCode int) {
   150  	panic(strconv.Itoa(exitCode))
   151  }
   152  

View as plain text