...

Source file src/github.com/openshift/custom-resource-status/objectreferences/v1/objectreferences_test.go

Documentation: github.com/openshift/custom-resource-status/objectreferences/v1

     1  package v1
     2  
     3  import (
     4  	"testing"
     5  
     6  	"k8s.io/apimachinery/pkg/api/equality"
     7  
     8  	corev1 "k8s.io/api/core/v1"
     9  )
    10  
    11  func TestSetObjectReference(t *testing.T) {
    12  	testCases := []struct {
    13  		name         string
    14  		testRef      corev1.ObjectReference
    15  		startRefs    *[]corev1.ObjectReference
    16  		expectedRefs *[]corev1.ObjectReference
    17  		shouldError  bool
    18  	}{
    19  		{
    20  			name: "add when empty",
    21  			testRef: corev1.ObjectReference{
    22  				Kind:       "FooKind",
    23  				Namespace:  "test-namespace",
    24  				Name:       "foo",
    25  				APIVersion: "test.example.io",
    26  			},
    27  			startRefs: &[]corev1.ObjectReference{},
    28  			expectedRefs: &[]corev1.ObjectReference{
    29  				{
    30  					Kind:       "FooKind",
    31  					Namespace:  "test-namespace",
    32  					Name:       "foo",
    33  					APIVersion: "test.example.io",
    34  				},
    35  			},
    36  			shouldError: false,
    37  		},
    38  		{
    39  			name: "simple add",
    40  			testRef: corev1.ObjectReference{
    41  				Kind:       "FooKind",
    42  				Namespace:  "test-namespace",
    43  				Name:       "foo",
    44  				APIVersion: "test.example.io",
    45  			},
    46  			startRefs: &[]corev1.ObjectReference{
    47  				{
    48  					Kind:       "BarKind",
    49  					Namespace:  "test-namespace",
    50  					Name:       "bar",
    51  					APIVersion: "test.example.io",
    52  				},
    53  			},
    54  			expectedRefs: &[]corev1.ObjectReference{
    55  				{
    56  					Kind:       "BarKind",
    57  					Namespace:  "test-namespace",
    58  					Name:       "bar",
    59  					APIVersion: "test.example.io",
    60  				},
    61  				{
    62  					Kind:       "FooKind",
    63  					Namespace:  "test-namespace",
    64  					Name:       "foo",
    65  					APIVersion: "test.example.io",
    66  				},
    67  			},
    68  			shouldError: false,
    69  		},
    70  		{
    71  			name: "replace reference",
    72  			testRef: corev1.ObjectReference{
    73  				Kind:       "FooKind",
    74  				Namespace:  "test-namespace",
    75  				Name:       "foo",
    76  				APIVersion: "test.example.io",
    77  				UID:        "fooid",
    78  			},
    79  			startRefs: &[]corev1.ObjectReference{
    80  				{
    81  					Kind:       "FooKind",
    82  					Namespace:  "test-namespace",
    83  					Name:       "foo",
    84  					APIVersion: "test.example.io",
    85  				},
    86  				{
    87  					Kind:       "BarKind",
    88  					Namespace:  "test-namespace",
    89  					Name:       "bar",
    90  					APIVersion: "test.example.io",
    91  				},
    92  			},
    93  			expectedRefs: &[]corev1.ObjectReference{
    94  				{
    95  					Kind:       "FooKind",
    96  					Namespace:  "test-namespace",
    97  					Name:       "foo",
    98  					APIVersion: "test.example.io",
    99  					UID:        "fooid",
   100  				},
   101  				{
   102  					Kind:       "BarKind",
   103  					Namespace:  "test-namespace",
   104  					Name:       "bar",
   105  					APIVersion: "test.example.io",
   106  				},
   107  			},
   108  			shouldError: false,
   109  		},
   110  		{
   111  			name: "error on newObject not minObjectReference",
   112  			testRef: corev1.ObjectReference{
   113  				Kind:       "FooKind",
   114  				APIVersion: "test.example.io",
   115  			},
   116  			startRefs:    &[]corev1.ObjectReference{},
   117  			expectedRefs: &[]corev1.ObjectReference{},
   118  			shouldError:  true,
   119  		},
   120  	}
   121  
   122  	for _, tc := range testCases {
   123  		t.Run(tc.name, func(t *testing.T) {
   124  			err := SetObjectReference(tc.startRefs, tc.testRef)
   125  			if err != nil && !tc.shouldError {
   126  				t.Fatalf("Error occurred unexpectedly: %v", err)
   127  			}
   128  			if err != nil && tc.shouldError {
   129  				return
   130  			}
   131  			if !equality.Semantic.DeepEqual(*tc.startRefs, *tc.expectedRefs) {
   132  				t.Errorf("Unexpected object refs '%v', expected '%v'", tc.startRefs, tc.expectedRefs)
   133  			}
   134  		})
   135  	}
   136  	return
   137  }
   138  
   139  func TestRemoveObjectReference(t *testing.T) {
   140  	testCases := []struct {
   141  		name         string
   142  		testRef      corev1.ObjectReference
   143  		startRefs    *[]corev1.ObjectReference
   144  		expectedRefs *[]corev1.ObjectReference
   145  		shouldError  bool
   146  	}{
   147  		{
   148  			name: "remove when empty",
   149  			testRef: corev1.ObjectReference{
   150  				Kind:       "FooKind",
   151  				Namespace:  "test-namespace",
   152  				Name:       "foo",
   153  				APIVersion: "test.example.io",
   154  			},
   155  			startRefs:    &[]corev1.ObjectReference{},
   156  			expectedRefs: &[]corev1.ObjectReference{},
   157  			shouldError:  false,
   158  		},
   159  		{
   160  			name: "simple remove",
   161  			testRef: corev1.ObjectReference{
   162  				Kind:       "FooKind",
   163  				Namespace:  "test-namespace",
   164  				Name:       "foo",
   165  				APIVersion: "test.example.io",
   166  			},
   167  			startRefs: &[]corev1.ObjectReference{
   168  				{
   169  					Kind:       "FooKind",
   170  					Namespace:  "test-namespace",
   171  					Name:       "foo",
   172  					APIVersion: "test.example.io",
   173  				},
   174  				{
   175  					Kind:       "BarKind",
   176  					Namespace:  "test-namespace",
   177  					Name:       "bar",
   178  					APIVersion: "test.example.io",
   179  				},
   180  			},
   181  			expectedRefs: &[]corev1.ObjectReference{
   182  				{
   183  					Kind:       "BarKind",
   184  					Namespace:  "test-namespace",
   185  					Name:       "bar",
   186  					APIVersion: "test.example.io",
   187  				},
   188  			},
   189  			shouldError: false,
   190  		},
   191  		{
   192  			name: "remove last",
   193  			testRef: corev1.ObjectReference{
   194  				Kind:       "FooKind",
   195  				Namespace:  "test-namespace",
   196  				Name:       "foo",
   197  				APIVersion: "test.example.io",
   198  			},
   199  			startRefs: &[]corev1.ObjectReference{
   200  				{
   201  					Kind:       "FooKind",
   202  					Namespace:  "test-namespace",
   203  					Name:       "foo",
   204  					APIVersion: "test.example.io",
   205  				},
   206  			},
   207  			expectedRefs: &[]corev1.ObjectReference{},
   208  			shouldError:  false,
   209  		},
   210  		{
   211  			// Not sure if this is possible by using SetObjectReference
   212  			// but testing this anyway
   213  			name: "remove matching",
   214  			testRef: corev1.ObjectReference{
   215  				Kind:       "FooKind",
   216  				Namespace:  "test-namespace",
   217  				Name:       "foo",
   218  				APIVersion: "test.example.io",
   219  			},
   220  			startRefs: &[]corev1.ObjectReference{
   221  				{
   222  					Kind:       "FooKind",
   223  					Namespace:  "test-namespace",
   224  					Name:       "foo",
   225  					APIVersion: "test.example.io",
   226  				},
   227  				{
   228  					Kind:       "BarKind",
   229  					Namespace:  "test-namespace",
   230  					Name:       "bar",
   231  					APIVersion: "test.example.io",
   232  				},
   233  				{
   234  					Kind:       "FooKind",
   235  					Namespace:  "test-namespace",
   236  					Name:       "foo",
   237  					APIVersion: "test.example.io",
   238  					UID:        "myuid",
   239  				},
   240  			},
   241  			expectedRefs: &[]corev1.ObjectReference{
   242  				{
   243  					Kind:       "BarKind",
   244  					Namespace:  "test-namespace",
   245  					Name:       "bar",
   246  					APIVersion: "test.example.io",
   247  				},
   248  			},
   249  			shouldError: false,
   250  		},
   251  		{
   252  			name: "error on rmObject not minObjectReference",
   253  			testRef: corev1.ObjectReference{
   254  				Kind:       "FooKind",
   255  				APIVersion: "test.example.io",
   256  			},
   257  			startRefs: &[]corev1.ObjectReference{
   258  				{
   259  					Kind:       "FooKind",
   260  					Namespace:  "test-namespace",
   261  					Name:       "foo",
   262  					APIVersion: "test.example.io",
   263  				},
   264  				{
   265  					Kind:       "BarKind",
   266  					Namespace:  "test-namespace",
   267  					Name:       "bar",
   268  					APIVersion: "test.example.io",
   269  				},
   270  			},
   271  			expectedRefs: &[]corev1.ObjectReference{},
   272  			shouldError:  true,
   273  		},
   274  	}
   275  
   276  	for _, tc := range testCases {
   277  		t.Run(tc.name, func(t *testing.T) {
   278  			err := RemoveObjectReference(tc.startRefs, tc.testRef)
   279  			if err != nil && !tc.shouldError {
   280  				t.Fatalf("Error occurred unexpectedly: %v", err)
   281  			}
   282  			if err != nil && tc.shouldError {
   283  				return
   284  			}
   285  			if !equality.Semantic.DeepEqual(*tc.startRefs, *tc.expectedRefs) {
   286  				t.Errorf("Unexpected object refs '%v', expected '%v'", tc.startRefs, tc.expectedRefs)
   287  			}
   288  		})
   289  	}
   290  	return
   291  }
   292  
   293  func TestFindObjectReference(t *testing.T) {
   294  	testCases := []struct {
   295  		name        string
   296  		testRef     corev1.ObjectReference
   297  		startRefs   *[]corev1.ObjectReference
   298  		expectedRef *corev1.ObjectReference
   299  		shouldError bool
   300  	}{
   301  		{
   302  			name: "simple find",
   303  			testRef: corev1.ObjectReference{
   304  				Kind:       "FooKind",
   305  				Namespace:  "test-namespace",
   306  				Name:       "foo",
   307  				APIVersion: "test.example.io",
   308  			},
   309  			startRefs: &[]corev1.ObjectReference{
   310  				{
   311  					Kind:       "FooKind",
   312  					Namespace:  "test-namespace",
   313  					Name:       "foo",
   314  					APIVersion: "test.example.io",
   315  				},
   316  			},
   317  			expectedRef: &corev1.ObjectReference{
   318  				Kind:       "FooKind",
   319  				Namespace:  "test-namespace",
   320  				Name:       "foo",
   321  				APIVersion: "test.example.io",
   322  			},
   323  			shouldError: false,
   324  		},
   325  		{
   326  			name: "find when empty",
   327  			testRef: corev1.ObjectReference{
   328  				Kind:       "FooKind",
   329  				Namespace:  "test-namespace",
   330  				Name:       "foo",
   331  				APIVersion: "test.example.io",
   332  			},
   333  			startRefs:   &[]corev1.ObjectReference{},
   334  			expectedRef: nil,
   335  			shouldError: false,
   336  		},
   337  		{
   338  			name: "err when not minimal object reference",
   339  			testRef: corev1.ObjectReference{
   340  				Kind:       "FooKind",
   341  				APIVersion: "test.example.io",
   342  			},
   343  			startRefs:   &[]corev1.ObjectReference{},
   344  			expectedRef: nil,
   345  			shouldError: true,
   346  		},
   347  	}
   348  
   349  	for _, tc := range testCases {
   350  		t.Run(tc.name, func(t *testing.T) {
   351  			foundRef, err := FindObjectReference(*tc.startRefs, tc.testRef)
   352  			if err != nil && !tc.shouldError {
   353  				t.Fatalf("Error occurred unexpectedly: %v", err)
   354  			}
   355  			if err != nil && tc.shouldError {
   356  				return
   357  			}
   358  			if !equality.Semantic.DeepEqual(foundRef, tc.expectedRef) {
   359  				t.Errorf("Unexpected object ref '%v', expected '%v'", foundRef, tc.expectedRef)
   360  			}
   361  		})
   362  	}
   363  	return
   364  }
   365  

View as plain text