...

Source file src/sigs.k8s.io/cli-utils/pkg/object/dependson/strings_test.go

Documentation: sigs.k8s.io/cli-utils/pkg/object/dependson

     1  // Copyright 2021 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  //
     4  
     5  package dependson
     6  
     7  import (
     8  	"testing"
     9  
    10  	"k8s.io/apimachinery/pkg/runtime/schema"
    11  	"sigs.k8s.io/cli-utils/pkg/object"
    12  )
    13  
    14  var (
    15  	clusterScopedObj = object.ObjMetadata{
    16  		Name: "cluster-obj",
    17  		GroupKind: schema.GroupKind{
    18  			Group: "test-group",
    19  			Kind:  "test-kind",
    20  		},
    21  	}
    22  	namespacedObj = object.ObjMetadata{
    23  		Namespace: "test-namespace",
    24  		Name:      "namespaced-obj",
    25  		GroupKind: schema.GroupKind{
    26  			Group: "test-group",
    27  			Kind:  "test-kind",
    28  		},
    29  	}
    30  )
    31  
    32  func TestParseDependencySet(t *testing.T) {
    33  	testCases := map[string]struct {
    34  		annotation string
    35  		expected   DependencySet
    36  		isError    bool
    37  	}{
    38  		"empty annotation is error": {
    39  			annotation: "",
    40  			expected:   DependencySet{},
    41  			isError:    true,
    42  		},
    43  		"wrong number of namespace-scoped fields in annotation is error": {
    44  			annotation: "test-group/test-namespace/test-kind/namespaced-obj",
    45  			expected:   DependencySet{},
    46  			isError:    true,
    47  		},
    48  		"wrong number of cluster-scoped fields in annotation is error": {
    49  			annotation: "test-group/namespaces/test-kind/cluster-obj",
    50  			expected:   DependencySet{},
    51  			isError:    true,
    52  		},
    53  		"cluster-scoped object annotation": {
    54  			annotation: "test-group/test-kind/cluster-obj",
    55  			expected:   DependencySet{clusterScopedObj},
    56  			isError:    false,
    57  		},
    58  		"namespaced object annotation": {
    59  			annotation: "test-group/namespaces/test-namespace/test-kind/namespaced-obj",
    60  			expected:   DependencySet{namespacedObj},
    61  			isError:    false,
    62  		},
    63  		"namespaced object annotation with whitespace at ends is valid": {
    64  			annotation: "  test-group/namespaces/test-namespace/test-kind/namespaced-obj\n",
    65  			expected:   DependencySet{namespacedObj},
    66  			isError:    false,
    67  		},
    68  		"multiple object annotation": {
    69  			annotation: "test-group/namespaces/test-namespace/test-kind/namespaced-obj," +
    70  				"test-group/test-kind/cluster-obj",
    71  			expected: DependencySet{clusterScopedObj, namespacedObj},
    72  			isError:  false,
    73  		},
    74  	}
    75  
    76  	for tn, tc := range testCases {
    77  		t.Run(tn, func(t *testing.T) {
    78  			actual, err := ParseDependencySet(tc.annotation)
    79  			if err == nil && tc.isError {
    80  				t.Fatalf("expected error, but received none")
    81  			}
    82  			if err != nil && !tc.isError {
    83  				t.Errorf("unexpected error: %s", err)
    84  			}
    85  			if !actual.Equal(tc.expected) {
    86  				t.Errorf("expected (%s), got (%s)", tc.expected, actual)
    87  			}
    88  		})
    89  	}
    90  }
    91  
    92  func TestParseObjMetadata(t *testing.T) {
    93  	testCases := map[string]struct {
    94  		metaStr  string
    95  		expected object.ObjMetadata
    96  		isError  bool
    97  	}{
    98  		"empty annotation is error": {
    99  			metaStr:  "",
   100  			expected: object.ObjMetadata{},
   101  			isError:  true,
   102  		},
   103  		"wrong number of namespace-scoped fields in annotation is error": {
   104  			metaStr:  "test-group/test-namespace/test-kind/namespaced-obj",
   105  			expected: object.ObjMetadata{},
   106  			isError:  true,
   107  		},
   108  		"wrong number of cluster-scoped fields in annotation is error": {
   109  			metaStr:  "test-group/namespaces/test-kind/cluster-obj",
   110  			expected: object.ObjMetadata{},
   111  			isError:  true,
   112  		},
   113  		"cluster-scoped object annotation": {
   114  			metaStr:  "test-group/test-kind/cluster-obj",
   115  			expected: clusterScopedObj,
   116  			isError:  false,
   117  		},
   118  		"namespaced object annotation": {
   119  			metaStr:  "test-group/namespaces/test-namespace/test-kind/namespaced-obj",
   120  			expected: namespacedObj,
   121  			isError:  false,
   122  		},
   123  		"namespaced object annotation with whitespace at ends is valid": {
   124  			metaStr:  "  test-group/namespaces/test-namespace/test-kind/namespaced-obj\n",
   125  			expected: namespacedObj,
   126  			isError:  false,
   127  		},
   128  		"multiple is error": {
   129  			metaStr: "test-group/namespaces/test-namespace/test-kind/namespaced-obj," +
   130  				"test-group/test-kind/cluster-obj",
   131  			expected: object.ObjMetadata{},
   132  			isError:  true,
   133  		},
   134  	}
   135  
   136  	for tn, tc := range testCases {
   137  		t.Run(tn, func(t *testing.T) {
   138  			actual, err := ParseObjMetadata(tc.metaStr)
   139  			if err == nil && tc.isError {
   140  				t.Fatalf("expected error, but received none")
   141  			}
   142  			if err != nil && !tc.isError {
   143  				t.Errorf("unexpected error: %s", err)
   144  			}
   145  			if actual != tc.expected {
   146  				t.Errorf("expected (%s), got (%s)", tc.expected, actual)
   147  			}
   148  		})
   149  	}
   150  }
   151  
   152  func TestFormatDependencySet(t *testing.T) {
   153  	testCases := map[string]struct {
   154  		depSet   DependencySet
   155  		expected string
   156  		isError  bool
   157  	}{
   158  		"empty set is not error": {
   159  			depSet:   DependencySet{},
   160  			expected: "",
   161  		},
   162  		"missing kind is error": {
   163  			depSet: DependencySet{
   164  				{
   165  					Name: "cluster-obj",
   166  					GroupKind: schema.GroupKind{
   167  						Group: "test-group",
   168  					},
   169  				},
   170  			},
   171  			expected: "",
   172  			isError:  true,
   173  		},
   174  		"missing name is error": {
   175  			depSet: DependencySet{
   176  				{
   177  					GroupKind: schema.GroupKind{
   178  						Group: "test-group",
   179  						Kind:  "test-kind",
   180  					},
   181  				},
   182  			},
   183  			expected: "",
   184  			isError:  true,
   185  		},
   186  		"cluster-scoped": {
   187  			depSet:   DependencySet{clusterScopedObj},
   188  			expected: "test-group/test-kind/cluster-obj",
   189  			isError:  false,
   190  		},
   191  		"namespace-scoped": {
   192  			depSet:   DependencySet{namespacedObj},
   193  			expected: "test-group/namespaces/test-namespace/test-kind/namespaced-obj",
   194  			isError:  false,
   195  		},
   196  		"multiple dependencies": {
   197  			depSet: DependencySet{clusterScopedObj, namespacedObj},
   198  			expected: "test-group/test-kind/cluster-obj," +
   199  				"test-group/namespaces/test-namespace/test-kind/namespaced-obj",
   200  			isError: false,
   201  		},
   202  	}
   203  
   204  	for tn, tc := range testCases {
   205  		t.Run(tn, func(t *testing.T) {
   206  			actual, err := FormatDependencySet(tc.depSet)
   207  			if err == nil && tc.isError {
   208  				t.Fatalf("expected error, but received none")
   209  			}
   210  			if err != nil && !tc.isError {
   211  				t.Errorf("unexpected error: %s", err)
   212  			}
   213  			if actual != tc.expected {
   214  				t.Errorf("expected (%s), got (%s)", tc.expected, actual)
   215  			}
   216  		})
   217  	}
   218  }
   219  
   220  func TestFormatObjMetadata(t *testing.T) {
   221  	testCases := map[string]struct {
   222  		objMeta  object.ObjMetadata
   223  		expected string
   224  		isError  bool
   225  	}{
   226  		"empty is error": {
   227  			objMeta:  object.ObjMetadata{},
   228  			expected: "",
   229  			isError:  true,
   230  		},
   231  		"missing kind is error": {
   232  			objMeta: object.ObjMetadata{
   233  				Name: "cluster-obj",
   234  				GroupKind: schema.GroupKind{
   235  					Group: "test-group",
   236  				},
   237  			},
   238  			expected: "",
   239  			isError:  true,
   240  		},
   241  		"missing name is error": {
   242  			objMeta: object.ObjMetadata{
   243  				GroupKind: schema.GroupKind{
   244  					Group: "test-group",
   245  					Kind:  "test-kind",
   246  				},
   247  			},
   248  			expected: "",
   249  			isError:  true,
   250  		},
   251  		"cluster-scoped": {
   252  			objMeta:  clusterScopedObj,
   253  			expected: "test-group/test-kind/cluster-obj",
   254  			isError:  false,
   255  		},
   256  		"namespace-scoped": {
   257  			objMeta:  namespacedObj,
   258  			expected: "test-group/namespaces/test-namespace/test-kind/namespaced-obj",
   259  			isError:  false,
   260  		},
   261  	}
   262  
   263  	for tn, tc := range testCases {
   264  		t.Run(tn, func(t *testing.T) {
   265  			actual, err := FormatObjMetadata(tc.objMeta)
   266  			if err == nil && tc.isError {
   267  				t.Fatalf("expected error, but received none")
   268  			}
   269  			if err != nil && !tc.isError {
   270  				t.Errorf("unexpected error: %s", err)
   271  			}
   272  			if actual != tc.expected {
   273  				t.Errorf("expected (%s), got (%s)", tc.expected, actual)
   274  			}
   275  		})
   276  	}
   277  }
   278  

View as plain text