github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/db/kubernetes/client_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  	"os"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/openebs/node-disk-manager/blockdevice"
    25  	"github.com/stretchr/testify/assert"
    26  	"k8s.io/client-go/rest"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  )
    29  
    30  func TestClientListBlockDevice(t *testing.T) {
    31  	type fields struct {
    32  		cfg       *rest.Config
    33  		client    client.Client
    34  		namespace string
    35  	}
    36  	type args struct {
    37  		filters []interface{}
    38  	}
    39  	tests := map[string]struct {
    40  		fields  fields
    41  		args    args
    42  		want    []blockdevice.BlockDevice
    43  		wantErr bool
    44  	}{
    45  		// TODO: Add test cases.
    46  	}
    47  	for name, test := range tests {
    48  		t.Run(name, func(t *testing.T) {
    49  			cl := &Client{
    50  				cfg:       test.fields.cfg,
    51  				client:    test.fields.client,
    52  				namespace: test.fields.namespace,
    53  			}
    54  			got, err := cl.ListBlockDevice(test.args.filters...)
    55  			if (err != nil) != test.wantErr {
    56  				t.Errorf("ListBlockDevice() error = %v, wantErr %v", err, test.wantErr)
    57  				return
    58  			}
    59  			if !reflect.DeepEqual(got, test.want) {
    60  				t.Errorf("ListBlockDevice() got = %v, want %v", got, test.want)
    61  			}
    62  		})
    63  	}
    64  }
    65  
    66  func TestClientsetNamespace(t *testing.T) {
    67  
    68  	fakeNamespace := "openebs"
    69  	cl := &Client{}
    70  
    71  	// setting namespace in client when env is not available
    72  	err1 := cl.setNamespace()
    73  	ns1 := cl.namespace
    74  
    75  	//setting namespace environment variable
    76  	_ = os.Setenv(NamespaceENV, fakeNamespace)
    77  
    78  	// setting namespace in client when env is available
    79  	err2 := cl.setNamespace()
    80  	ns2 := cl.namespace
    81  
    82  	tests := map[string]struct {
    83  		wantNamespace string
    84  		gotNamespace  string
    85  		wantErr       bool
    86  		gotErr        error
    87  	}{
    88  		"when NAMESPACE env is not available": {
    89  			ns1,
    90  			"",
    91  			true,
    92  			err1,
    93  		},
    94  		"when NAMESPACE env is available": {
    95  			ns2,
    96  			fakeNamespace,
    97  			false,
    98  			err2,
    99  		},
   100  	}
   101  	for name, test := range tests {
   102  		t.Run(name, func(t *testing.T) {
   103  			assert.Equal(t, test.wantNamespace, test.gotNamespace)
   104  			assert.Equal(t, test.wantErr, test.gotErr != nil)
   105  		})
   106  	}
   107  }