github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/db/kubernetes/filter_test.go (about)

     1  /*
     2  Copyright 2019 The OpenEBS Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package kubernetes
    18  
    19  import (
    20  	"errors"
    21  	"sigs.k8s.io/controller-runtime/pkg/client"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestGenerateLabelFilter(t *testing.T) {
    28  	type args struct {
    29  		key   string
    30  		value string
    31  	}
    32  	tests := map[string]struct {
    33  		args args
    34  		want interface{}
    35  		err  error
    36  	}{
    37  		"when key is empty": {
    38  			args: args{
    39  				key:   "",
    40  				value: "machine1",
    41  			},
    42  			want: nil,
    43  			err:  errors.New("key/value is empty for label filter"),
    44  		},
    45  		"when value is empty": {
    46  			args: args{
    47  				key:   "hostname",
    48  				value: "",
    49  			},
    50  			want: nil,
    51  			err:  errors.New("key/value is empty for label filter"),
    52  		},
    53  		"when both key and value are empty": {
    54  			args: args{
    55  				key:   "",
    56  				value: "",
    57  			},
    58  			want: nil,
    59  			err:  errors.New("key/value is empty for label filter"),
    60  		},
    61  		"when valid key and value is given": {
    62  			args: args{
    63  				key:   "ndm.io/managed",
    64  				value: "false",
    65  			},
    66  			want: client.MatchingLabels{"ndm.io/managed": "false"},
    67  			err:  nil,
    68  		},
    69  		"when a valid hostname key is present": {
    70  			args: args{
    71  				key:   "hostname",
    72  				value: "machine1",
    73  			},
    74  			want: client.MatchingLabels{"kubernetes.io/hostname": "machine1"},
    75  			err:  nil,
    76  		},
    77  	}
    78  	for name, test := range tests {
    79  		t.Run(name, func(t *testing.T) {
    80  			got, err := GenerateLabelFilter(test.args.key, test.args.value)
    81  			assert.Equal(t, test.want, got)
    82  			assert.Equal(t, test.err, err)
    83  		})
    84  	}
    85  }