github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/controller/operators/olm/labeler_test.go (about)

     1  package olm
     2  
     3  import (
     4  	"testing"
     5  
     6  	opregistry "github.com/operator-framework/operator-registry/pkg/registry"
     7  	"github.com/stretchr/testify/require"
     8  	"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/apimachinery/pkg/labels"
    11  )
    12  
    13  func TestLabelSetsFor(t *testing.T) {
    14  	tests := []struct {
    15  		name     string
    16  		obj      interface{}
    17  		expected []labels.Set
    18  	}{
    19  		{
    20  			name:     "Nil/Nil",
    21  			obj:      nil,
    22  			expected: nil,
    23  		},
    24  		{
    25  			name:     "NotOperatorSurfaceOrCRD/Nil",
    26  			obj:      struct{ data string }{"some-data"},
    27  			expected: nil,
    28  		},
    29  		{
    30  			name: "CRD/ProvidedAndRequired",
    31  			obj: &v1beta1.CustomResourceDefinition{
    32  				TypeMeta: metav1.TypeMeta{
    33  					Kind:       "CustomResourceDefinition",
    34  					APIVersion: v1beta1.SchemeGroupVersion.String(),
    35  				},
    36  				ObjectMeta: metav1.ObjectMeta{
    37  					Name: "Ghosts.ghouls",
    38  				},
    39  				Spec: v1beta1.CustomResourceDefinitionSpec{
    40  					Group: "ghouls",
    41  					Versions: []v1beta1.CustomResourceDefinitionVersion{
    42  						{
    43  							Name:    "v1alpha1",
    44  							Storage: true,
    45  							Served:  true,
    46  						},
    47  					},
    48  					Names: v1beta1.CustomResourceDefinitionNames{
    49  						Kind:   "Ghost",
    50  						Plural: "Ghosts",
    51  					},
    52  				},
    53  			},
    54  			expected: []labels.Set{
    55  				{
    56  					APILabelKeyPrefix + "6435ab0d7c6bda64": "provided",
    57  				},
    58  				{
    59  					APILabelKeyPrefix + "6435ab0d7c6bda64": "required",
    60  				},
    61  			},
    62  		},
    63  		{
    64  			name: "OperatorSurface/Provided",
    65  			obj: operatorSurface{
    66  				ProvidedAPIs: map[opregistry.APIKey]struct{}{
    67  					{Group: "ghouls", Version: "v1alpha1", Kind: "Ghost", Plural: "Ghosts"}: {},
    68  				},
    69  			},
    70  			expected: []labels.Set{
    71  				{
    72  					APILabelKeyPrefix + "6435ab0d7c6bda64": "provided",
    73  				},
    74  			},
    75  		},
    76  		{
    77  			name: "OperatorSurface/ProvidedAndRequired",
    78  			obj: operatorSurface{
    79  				ProvidedAPIs: map[opregistry.APIKey]struct{}{
    80  					{Group: "ghouls", Version: "v1alpha1", Kind: "Ghost", Plural: "Ghosts"}: {},
    81  				},
    82  				RequiredAPIs: map[opregistry.APIKey]struct{}{
    83  					{Group: "ghouls", Version: "v1alpha1", Kind: "Goblin", Plural: "Goblins"}: {},
    84  				},
    85  			},
    86  			expected: []labels.Set{
    87  				{
    88  					APILabelKeyPrefix + "6435ab0d7c6bda64": "provided",
    89  					APILabelKeyPrefix + "557c9f42470aa352": "required",
    90  				},
    91  			},
    92  		},
    93  	}
    94  	for _, tt := range tests {
    95  		t.Run(tt.name, func(t *testing.T) {
    96  			labelSets, err := LabelSetsFor(tt.obj)
    97  			require.NoError(t, err)
    98  			require.ElementsMatch(t, tt.expected, labelSets)
    99  		})
   100  	}
   101  }