...

Source file src/sigs.k8s.io/cli-utils/pkg/object/graph/error_test.go

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

     1  // Copyright 2022 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package graph
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"k8s.io/apimachinery/pkg/runtime/schema"
    11  	"sigs.k8s.io/cli-utils/pkg/object"
    12  )
    13  
    14  var (
    15  	on1 = object.ObjMetadata{Name: "obj1", Namespace: "ns1", GroupKind: schema.GroupKind{Group: "test", Kind: "foo"}}
    16  	on2 = object.ObjMetadata{Name: "obj2", Namespace: "ns1", GroupKind: schema.GroupKind{Group: "test", Kind: "foo"}}
    17  )
    18  
    19  func TestExternalDependencyErrorString(t *testing.T) {
    20  	testCases := map[string]struct {
    21  		err            ExternalDependencyError
    22  		expectedString string
    23  	}{
    24  		"cluster-scoped": {
    25  			err: ExternalDependencyError{
    26  				Edge: Edge{
    27  					From: o1,
    28  					To:   o2,
    29  				},
    30  			},
    31  			expectedString: `external dependency: test/foo/obj1 -> test/foo/obj2`,
    32  		},
    33  		"namespace-scoped": {
    34  			err: ExternalDependencyError{
    35  				Edge: Edge{
    36  					From: on1,
    37  					To:   on2,
    38  				},
    39  			},
    40  			expectedString: `external dependency: test/namespaces/ns1/foo/obj1 -> test/namespaces/ns1/foo/obj2`,
    41  		},
    42  	}
    43  
    44  	for tn, tc := range testCases {
    45  		t.Run(tn, func(t *testing.T) {
    46  			assert.Equal(t, tc.expectedString, tc.err.Error())
    47  		})
    48  	}
    49  }
    50  
    51  func TestCyclicDependencyErrorString(t *testing.T) {
    52  	testCases := map[string]struct {
    53  		err            CyclicDependencyError
    54  		expectedString string
    55  	}{
    56  		"two object cycle": {
    57  			err: CyclicDependencyError{
    58  				Edges: []Edge{
    59  					{
    60  						From: o1,
    61  						To:   o2,
    62  					},
    63  					{
    64  						From: o2,
    65  						To:   o1,
    66  					},
    67  				},
    68  			},
    69  			expectedString: `cyclic dependency:
    70  - test/foo/obj1 -> test/foo/obj2
    71  - test/foo/obj2 -> test/foo/obj1`,
    72  		},
    73  		"three object cycle": {
    74  			err: CyclicDependencyError{
    75  				Edges: []Edge{
    76  					{
    77  						From: o1,
    78  						To:   o2,
    79  					},
    80  					{
    81  						From: o2,
    82  						To:   o3,
    83  					},
    84  					{
    85  						From: o3,
    86  						To:   o1,
    87  					},
    88  				},
    89  			},
    90  			expectedString: `cyclic dependency:
    91  - test/foo/obj1 -> test/foo/obj2
    92  - test/foo/obj2 -> test/foo/obj3
    93  - test/foo/obj3 -> test/foo/obj1`,
    94  		},
    95  	}
    96  
    97  	for tn, tc := range testCases {
    98  		t.Run(tn, func(t *testing.T) {
    99  			assert.Equal(t, tc.expectedString, tc.err.Error())
   100  		})
   101  	}
   102  }
   103  
   104  func TestDuplicateDependencyErrorString(t *testing.T) {
   105  	testCases := map[string]struct {
   106  		err            DuplicateDependencyError
   107  		expectedString string
   108  	}{
   109  		"cluster-scoped": {
   110  			err: DuplicateDependencyError{
   111  				Edge: Edge{
   112  					From: o1,
   113  					To:   o2,
   114  				},
   115  			},
   116  			expectedString: `duplicate dependency: test/foo/obj1 -> test/foo/obj2`,
   117  		},
   118  		"namespace-scoped": {
   119  			err: DuplicateDependencyError{
   120  				Edge: Edge{
   121  					From: on1,
   122  					To:   on2,
   123  				},
   124  			},
   125  			expectedString: `duplicate dependency: test/namespaces/ns1/foo/obj1 -> test/namespaces/ns1/foo/obj2`,
   126  		},
   127  	}
   128  
   129  	for tn, tc := range testCases {
   130  		t.Run(tn, func(t *testing.T) {
   131  			assert.Equal(t, tc.expectedString, tc.err.Error())
   132  		})
   133  	}
   134  }
   135  

View as plain text