github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/db/kubernetes/convert_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  	"testing"
    21  
    22  	api "github.com/openebs/node-disk-manager/api/v1alpha1"
    23  	"github.com/openebs/node-disk-manager/blockdevice"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func Test_convert_BlockDeviceAPI_To_BlockDevice(t *testing.T) {
    28  	type args struct {
    29  		in      *api.BlockDevice
    30  		wantOut *blockdevice.BlockDevice
    31  	}
    32  
    33  	fakeBDName := "my-fake-bd"
    34  	fakeHostName := "fake-hostname"
    35  	fakeNodeName := "fake-machine"
    36  	fakeDevicePath := "/dev/sdf1"
    37  	fileSystem := "ext4"
    38  	mountPoint := "/mnt/media"
    39  	deviceType := blockdevice.SparseBlockDeviceType
    40  
    41  	// building the blockdevice API object
    42  	in1 := createFakeBlockDeviceAPI(fakeBDName)
    43  	in1.Labels[KubernetesHostNameLabel] = fakeHostName
    44  	in1.Spec.NodeAttributes.NodeName = fakeNodeName
    45  	in1.Spec.Path = fakeDevicePath
    46  	in1.Spec.FileSystem.Type = fileSystem
    47  	in1.Spec.FileSystem.Mountpoint = mountPoint
    48  	in1.Spec.Details.DeviceType = deviceType
    49  	in1.Status.State = api.BlockDeviceState(blockdevice.Active)
    50  	in1.Status.ClaimState = api.DeviceClaimState(blockdevice.Claimed)
    51  
    52  	// building the core blockdevice object
    53  	out1 := createFakeBlockDevice(fakeBDName)
    54  	out1.NodeAttributes[blockdevice.HostName] = fakeHostName
    55  	out1.NodeAttributes[blockdevice.NodeName] = fakeNodeName
    56  	out1.DevPath = fakeDevicePath
    57  	out1.FSInfo.FileSystem = fileSystem
    58  	out1.FSInfo.MountPoint = append(out1.FSInfo.MountPoint, mountPoint)
    59  	out1.DeviceAttributes.DeviceType = blockdevice.SparseBlockDeviceType
    60  	out1.Status.State = blockdevice.Active
    61  	out1.Status.ClaimPhase = blockdevice.Claimed
    62  
    63  	tests := map[string]struct {
    64  		args    args
    65  		wantErr bool
    66  	}{
    67  		"converting block device k8s resource to BlockDevice": {
    68  			args: args{
    69  				in:      in1,
    70  				wantOut: out1,
    71  			},
    72  			wantErr: false,
    73  		},
    74  	}
    75  	for name, test := range tests {
    76  		t.Run(name, func(t *testing.T) {
    77  			gotOut := &blockdevice.BlockDevice{}
    78  			err := convertBlockDeviceAPIToBlockDevice(test.args.in, gotOut)
    79  			assert.Equal(t, test.args.wantOut, gotOut)
    80  			assert.Equal(t, test.wantErr, err != nil)
    81  		})
    82  	}
    83  }