github.com/nginxinc/kubernetes-ingress@v1.12.5/internal/k8s/handlers_test.go (about)

     1  package k8s
     2  
     3  import (
     4  	"testing"
     5  
     6  	v1 "k8s.io/api/core/v1"
     7  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
     8  	"k8s.io/apimachinery/pkg/util/intstr"
     9  )
    10  
    11  func TestHasServicePortChanges(t *testing.T) {
    12  	cases := []struct {
    13  		a      []v1.ServicePort
    14  		b      []v1.ServicePort
    15  		result bool
    16  		reason string
    17  	}{
    18  		{
    19  			[]v1.ServicePort{},
    20  			[]v1.ServicePort{},
    21  			false,
    22  			"Empty should report no changes",
    23  		},
    24  		{
    25  			[]v1.ServicePort{{
    26  				Port: 80,
    27  			}},
    28  			[]v1.ServicePort{{
    29  				Port: 8080,
    30  			}},
    31  			true,
    32  			"Different Ports",
    33  		},
    34  		{
    35  			[]v1.ServicePort{{
    36  				Port: 80,
    37  			}},
    38  			[]v1.ServicePort{{
    39  				Port: 80,
    40  			}},
    41  			false,
    42  			"Same Ports",
    43  		},
    44  		{
    45  			[]v1.ServicePort{{
    46  				Name: "asdf",
    47  				Port: 80,
    48  			}},
    49  			[]v1.ServicePort{{
    50  				Name: "asdf",
    51  				Port: 80,
    52  			}},
    53  			false,
    54  			"Same Port and Name",
    55  		},
    56  		{
    57  			[]v1.ServicePort{{
    58  				Name: "foo",
    59  				Port: 80,
    60  			}},
    61  			[]v1.ServicePort{{
    62  				Name: "bar",
    63  				Port: 80,
    64  			}},
    65  			true,
    66  			"Different Name same Port",
    67  		},
    68  		{
    69  			[]v1.ServicePort{{
    70  				Name: "foo",
    71  				Port: 8080,
    72  			}},
    73  			[]v1.ServicePort{{
    74  				Name: "bar",
    75  				Port: 80,
    76  			}},
    77  			true,
    78  			"Different Name different Port",
    79  		},
    80  		{
    81  			[]v1.ServicePort{{
    82  				Name: "foo",
    83  			}},
    84  			[]v1.ServicePort{{
    85  				Name: "fooo",
    86  			}},
    87  			true,
    88  			"Very similar Name",
    89  		},
    90  		{
    91  			[]v1.ServicePort{{
    92  				Name: "asdf",
    93  				Port: 80,
    94  				TargetPort: intstr.IntOrString{
    95  					IntVal: 80,
    96  				},
    97  			}},
    98  			[]v1.ServicePort{{
    99  				Name: "asdf",
   100  				Port: 80,
   101  				TargetPort: intstr.IntOrString{
   102  					IntVal: 8080,
   103  				},
   104  			}},
   105  			false,
   106  			"TargetPort should be ignored",
   107  		},
   108  		{
   109  			[]v1.ServicePort{{
   110  				Name: "foo",
   111  			}, {
   112  				Name: "bar",
   113  			}},
   114  			[]v1.ServicePort{{
   115  				Name: "foo",
   116  			}, {
   117  				Name: "bar",
   118  			}},
   119  			false,
   120  			"Multiple same names",
   121  		},
   122  		{
   123  			[]v1.ServicePort{{
   124  				Name: "foo",
   125  			}, {
   126  				Name: "bar",
   127  			}},
   128  			[]v1.ServicePort{{
   129  				Name: "foo",
   130  			}, {
   131  				Name: "bars",
   132  			}},
   133  			true,
   134  			"Multiple different names",
   135  		},
   136  		{
   137  			[]v1.ServicePort{{
   138  				Name: "foo",
   139  			}, {
   140  				Port: 80,
   141  			}},
   142  			[]v1.ServicePort{{
   143  				Port: 80,
   144  			}, {
   145  				Name: "foo",
   146  			}},
   147  			false,
   148  			"Some names some ports",
   149  		},
   150  	}
   151  
   152  	for _, c := range cases {
   153  		if c.result != hasServicePortChanges(c.a, c.b) {
   154  			t.Errorf("hasServicePortChanges returned %v, but expected %v for %q case", c.result, !c.result, c.reason)
   155  		}
   156  	}
   157  }
   158  
   159  func TestAreResourcesDifferent(t *testing.T) {
   160  	tests := []struct {
   161  		oldR, newR          *unstructured.Unstructured
   162  		expected, expectErr bool
   163  		msg                 string
   164  	}{
   165  		{
   166  			oldR: &unstructured.Unstructured{
   167  				Object: map[string]interface{}{
   168  					"spec": true, // wrong type
   169  				},
   170  			},
   171  			newR: &unstructured.Unstructured{
   172  				Object: map[string]interface{}{
   173  					"spec": map[string]interface{}{},
   174  				},
   175  			},
   176  			expected:  false,
   177  			expectErr: true,
   178  			msg:       "invalid old resource",
   179  		},
   180  		{
   181  			oldR: &unstructured.Unstructured{
   182  				Object: map[string]interface{}{
   183  					"spec": map[string]interface{}{},
   184  				},
   185  			},
   186  			newR: &unstructured.Unstructured{
   187  				Object: map[string]interface{}{
   188  					"spec": true, // wrong type
   189  				},
   190  			},
   191  			expected:  false,
   192  			expectErr: true,
   193  			msg:       "invalid new resource",
   194  		},
   195  		{
   196  			oldR: &unstructured.Unstructured{
   197  				Object: map[string]interface{}{
   198  					"spec": map[string]interface{}{},
   199  				},
   200  			},
   201  			newR: &unstructured.Unstructured{
   202  				Object: map[string]interface{}{},
   203  			},
   204  			expected:  false,
   205  			expectErr: true,
   206  			msg:       "new resource with missing spec",
   207  		},
   208  		{
   209  			oldR: &unstructured.Unstructured{
   210  				Object: map[string]interface{}{
   211  					"spec": map[string]interface{}{
   212  						"field": "a",
   213  					},
   214  				},
   215  			},
   216  			newR: &unstructured.Unstructured{
   217  				Object: map[string]interface{}{
   218  					"spec": map[string]interface{}{
   219  						"field": "a",
   220  					},
   221  				},
   222  			},
   223  			expected:  false,
   224  			expectErr: false,
   225  			msg:       "equal resources",
   226  		},
   227  		{
   228  			oldR: &unstructured.Unstructured{
   229  				Object: map[string]interface{}{
   230  					"spec": map[string]interface{}{
   231  						"field": "a",
   232  					},
   233  				},
   234  			},
   235  			newR: &unstructured.Unstructured{
   236  				Object: map[string]interface{}{
   237  					"spec": map[string]interface{}{
   238  						"field": "b",
   239  					},
   240  				},
   241  			},
   242  			expected:  true,
   243  			expectErr: false,
   244  			msg:       "not equal resources",
   245  		},
   246  		{
   247  			oldR: &unstructured.Unstructured{
   248  				Object: map[string]interface{}{},
   249  			},
   250  			newR: &unstructured.Unstructured{
   251  				Object: map[string]interface{}{
   252  					"spec": map[string]interface{}{
   253  						"field": "b",
   254  					},
   255  				},
   256  			},
   257  			expected:  true,
   258  			expectErr: false,
   259  			msg:       "not equal resources with with first resource missing spec",
   260  		},
   261  	}
   262  
   263  	for _, test := range tests {
   264  		result, err := areResourcesDifferent(test.oldR, test.newR)
   265  		if result != test.expected {
   266  			t.Errorf("areResourcesDifferent() returned %v but expected %v for the case of %s", result, test.expected, test.msg)
   267  		}
   268  		if test.expectErr && err == nil {
   269  			t.Errorf("areResourcesDifferent() returned no error for the case of %s", test.msg)
   270  		}
   271  		if !test.expectErr && err != nil {
   272  			t.Errorf("areResourcesDifferent() returned unexpected error %v for the case of %s", err, test.msg)
   273  		}
   274  	}
   275  }