github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/controller/operators/internal/alongside/alongside_test.go (about)

     1  package alongside
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  type TestAnnotatable map[string]string
    10  
    11  func (a TestAnnotatable) GetAnnotations() map[string]string {
    12  	return a
    13  }
    14  
    15  func (a *TestAnnotatable) SetAnnotations(v map[string]string) {
    16  	*a = v
    17  }
    18  
    19  func TestAnnotatorFromObject(t *testing.T) {
    20  	for _, tc := range []struct {
    21  		Name            string
    22  		Object          TestAnnotatable
    23  		NamespacedNames []NamespacedName
    24  	}{
    25  		{
    26  			Name: "annotation without AnnotationPrefix ignored",
    27  			Object: TestAnnotatable{
    28  				"foo": "namespace/name",
    29  			},
    30  		},
    31  		{
    32  			Name: "annotation with malformed value ignored",
    33  			Object: TestAnnotatable{
    34  				"operatorframework.io/installed-alongside-0": "namespace/name/color",
    35  			},
    36  		},
    37  		{
    38  			Name: "multiple valid annotations returned",
    39  			Object: TestAnnotatable{
    40  				"operatorframework.io/installed-alongside-0": "namespace-0/name-0",
    41  				"operatorframework.io/installed-alongside-1": "namespace-1/name-1",
    42  			},
    43  			NamespacedNames: []NamespacedName{
    44  				{Namespace: "namespace-0", Name: "name-0"},
    45  				{Namespace: "namespace-1", Name: "name-1"},
    46  			},
    47  		},
    48  	} {
    49  		t.Run(tc.Name, func(t *testing.T) {
    50  			assert.ElementsMatch(t, tc.NamespacedNames, Annotator{}.FromObject(&tc.Object))
    51  		})
    52  	}
    53  }
    54  
    55  func TestAnnotatorToObject(t *testing.T) {
    56  	for _, tc := range []struct {
    57  		Name            string
    58  		Object          TestAnnotatable
    59  		NamespacedNames []NamespacedName
    60  		Expected        TestAnnotatable
    61  	}{
    62  		{
    63  			Name: "existing annotation removed",
    64  			Object: TestAnnotatable{
    65  				"operatorframework.io/installed-alongside-0": "namespace/name",
    66  			},
    67  		},
    68  		{
    69  			Name: "annotation without AnnotationPrefix ignored",
    70  			Object: TestAnnotatable{
    71  				"operatorframework.io/something-else": "",
    72  			},
    73  			Expected: TestAnnotatable{
    74  				"operatorframework.io/something-else": "",
    75  			},
    76  		},
    77  		{
    78  			Name: "annotation added",
    79  			NamespacedNames: []NamespacedName{
    80  				{Namespace: "namespace-0", Name: "name-0"},
    81  			},
    82  			Expected: TestAnnotatable{
    83  				key(NamespacedName{"namespace-0", "name-0"}): "namespace-0/name-0",
    84  			},
    85  		},
    86  		{
    87  			Name: "replace multiple annotations",
    88  			Object: TestAnnotatable{
    89  				key(NamespacedName{"namespace-0", "name-0"}): "namespace-0/name-0",
    90  				key(NamespacedName{"namespace-1", "name-1"}): "namespace-1/name-1",
    91  			},
    92  			NamespacedNames: []NamespacedName{
    93  				{Namespace: "namespace-2", Name: "name-2"},
    94  				{Namespace: "namespace-3", Name: "name-3"},
    95  			},
    96  			Expected: TestAnnotatable{
    97  				key(NamespacedName{"namespace-2", "name-2"}): "namespace-2/name-2",
    98  				key(NamespacedName{"namespace-3", "name-3"}): "namespace-3/name-3",
    99  			},
   100  		},
   101  	} {
   102  		t.Run(tc.Name, func(t *testing.T) {
   103  			Annotator{}.ToObject(&tc.Object, tc.NamespacedNames)
   104  			assert.Equal(t, &tc.Expected, &tc.Object)
   105  		})
   106  	}
   107  }