github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/partition/partition_test.go (about)

     1  /*
     2  Copyright 2020 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 partition
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/diskfs/go-diskfs/partition/gpt"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestCreatePartitionTable(t *testing.T) {
    27  	tests := map[string]struct {
    28  		actualDisk             Disk
    29  		expectedPartitionTable *gpt.Table
    30  		wantErr                bool
    31  	}{
    32  		"disk size is zero": {
    33  			actualDisk: Disk{
    34  				DevPath:          "/dev/sda",
    35  				DiskSize:         0,
    36  				LogicalBlockSize: 0,
    37  				table:            nil,
    38  			},
    39  			expectedPartitionTable: nil,
    40  			wantErr:                true,
    41  		},
    42  		"disk with zero block size": {
    43  			actualDisk: Disk{
    44  				DevPath:          "/dev/sda",
    45  				DiskSize:         500107862016,
    46  				LogicalBlockSize: 0,
    47  				table:            nil,
    48  			},
    49  			expectedPartitionTable: &gpt.Table{
    50  				LogicalSectorSize: 512,
    51  				ProtectiveMBR:     true,
    52  			},
    53  			wantErr: false,
    54  		},
    55  		"disk with 4k block size": {
    56  			actualDisk: Disk{
    57  				DevPath:          "/dev/sda",
    58  				DiskSize:         500107862016,
    59  				LogicalBlockSize: 4096,
    60  				table:            nil,
    61  			},
    62  			expectedPartitionTable: &gpt.Table{
    63  				LogicalSectorSize: 4096,
    64  				ProtectiveMBR:     true,
    65  			},
    66  			wantErr: false,
    67  		},
    68  	}
    69  	for name, test := range tests {
    70  		t.Run(name, func(t *testing.T) {
    71  			if err := test.actualDisk.createPartitionTable(); (err != nil) != test.wantErr {
    72  				t.Errorf("CreatePartitionTable() error = %v, wantErr %v", err, test.wantErr)
    73  			}
    74  			assert.Equal(t, test.actualDisk.table, test.expectedPartitionTable)
    75  		})
    76  	}
    77  }
    78  
    79  func TestAddPartition(t *testing.T) {
    80  	tests := map[string]struct {
    81  		actualDisk             Disk
    82  		expectedPartitionTable *gpt.Table
    83  		wantErr                bool
    84  	}{
    85  		"465GiB HDD with 512 block size": {
    86  			actualDisk: Disk{
    87  				DevPath:          "/dev/sda",
    88  				DiskSize:         500107862016,
    89  				LogicalBlockSize: 512,
    90  				table:            &gpt.Table{},
    91  			},
    92  			expectedPartitionTable: &gpt.Table{
    93  				Partitions: []*gpt.Partition{
    94  					{
    95  						Start: 2048,
    96  						End:   976773134,
    97  						Type:  gpt.LinuxFilesystem,
    98  						Name:  OpenEBSNDMPartitionName,
    99  					},
   100  				},
   101  			},
   102  			wantErr: false,
   103  		},
   104  		"375 GiB SSD with 4k block size": {
   105  			actualDisk: Disk{
   106  				DevPath:          "/dev/sda",
   107  				DiskSize:         402653184000,
   108  				LogicalBlockSize: 4096,
   109  				table:            &gpt.Table{},
   110  			},
   111  			expectedPartitionTable: &gpt.Table{
   112  				Partitions: []*gpt.Partition{
   113  					{
   114  						Start: 256,
   115  						End:   98303994,
   116  						Type:  gpt.LinuxFilesystem,
   117  						Name:  OpenEBSNDMPartitionName,
   118  					},
   119  				},
   120  			},
   121  		},
   122  	}
   123  	for name, test := range tests {
   124  		t.Run(name, func(t *testing.T) {
   125  			if err := test.actualDisk.addPartition(); (err != nil) != test.wantErr {
   126  				t.Errorf("AddPartition() error = %v, wantErr %v", err, test.wantErr)
   127  			}
   128  			assert.Equal(t, test.actualDisk.table, test.expectedPartitionTable)
   129  		})
   130  	}
   131  }