github.com/jaypipes/ghw@v0.21.1/pkg/block/block_test.go (about)

     1  //
     2  // Use and distribution licensed under the Apache license version 2.
     3  //
     4  // See the COPYING file in the root project directory for full text.
     5  //
     6  
     7  package block_test
     8  
     9  import (
    10  	"encoding/json"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  	"testing"
    15  
    16  	"github.com/jaypipes/ghw/pkg/block"
    17  
    18  	"github.com/jaypipes/ghw/testdata"
    19  )
    20  
    21  // nolint: gocyclo
    22  func TestBlock(t *testing.T) {
    23  	if _, ok := os.LookupEnv("GHW_TESTING_SKIP_BLOCK"); ok {
    24  		t.Skip("Skipping block tests.")
    25  	}
    26  
    27  	info, err := block.New()
    28  
    29  	if err != nil {
    30  		t.Fatalf("Expected no error creating block.Info, but got %v", err)
    31  	}
    32  	tpb := info.TotalPhysicalBytes
    33  
    34  	if tpb < 1 {
    35  		t.Fatalf("Expected >0 total physical bytes, got %d", tpb)
    36  	}
    37  
    38  	disks := info.Disks
    39  	if len(disks) == 0 {
    40  		t.Fatalf("Expected >0 disks. Got %d", len(disks))
    41  	}
    42  
    43  	var d0 *block.Disk
    44  	// Skip loop devices on generic tests as we don't know what the underlying system is going to have
    45  	// And loop devices don't have Serial Numbers for example.
    46  	for _, d := range disks {
    47  		if d.StorageController != block.STORAGE_CONTROLLER_LOOP {
    48  			d0 = d
    49  			break
    50  		}
    51  	}
    52  	if d0.Name == "" {
    53  		t.Fatalf("Expected disk name, but got \"\"")
    54  	}
    55  	if d0.SizeBytes <= 0 {
    56  		t.Fatalf("Expected >0 disk size, but got %d", d0.SizeBytes)
    57  	}
    58  	if d0.Partitions == nil {
    59  		t.Fatalf("Expected non-nil partitions, but got nil.")
    60  	}
    61  	if d0.PhysicalBlockSizeBytes <= 0 {
    62  		t.Fatalf("Expected >0 sector size, but got %d", d0.PhysicalBlockSizeBytes)
    63  	}
    64  
    65  	if len(d0.Partitions) > 0 {
    66  		p0 := d0.Partitions[0]
    67  		if p0 == nil {
    68  			t.Fatalf("Expected non-nil partition, but got nil.")
    69  		}
    70  		if !strings.HasPrefix(p0.Name, d0.Name) {
    71  			t.Fatalf("Expected partition name to begin with disk name but "+
    72  				"got %s does not begin with %s", p0.Name, d0.Name)
    73  		}
    74  	}
    75  
    76  	for _, p := range d0.Partitions {
    77  		if p.SizeBytes <= 0 {
    78  			t.Fatalf("Expected >0 partition size, but got %d", p.SizeBytes)
    79  		}
    80  		if p.Disk != d0 {
    81  			t.Fatalf("Expected disk to be the same as d0 but got %v", p.Disk)
    82  		}
    83  	}
    84  }
    85  
    86  func TestBlockMarshalUnmarshal(t *testing.T) {
    87  	blocks, err := block.New()
    88  	if err != nil {
    89  		t.Fatalf("Expected no error creating block.Info, but got %v", err)
    90  	}
    91  
    92  	data, err := json.Marshal(blocks)
    93  	if err != nil {
    94  		t.Fatalf("Expected no error marshaling block.Info, but got %v", err)
    95  	}
    96  
    97  	var bi *block.Info
    98  	err = json.Unmarshal(data, &bi)
    99  	if err != nil {
   100  		t.Fatalf("Expected no error unmarshaling block.Info, but got %v", err)
   101  	}
   102  }
   103  
   104  type blockData struct {
   105  	Block block.Info `json:"block"`
   106  }
   107  
   108  func TestBlockUnmarshal(t *testing.T) {
   109  	testdataPath, err := testdata.SamplesDirectory()
   110  	if err != nil {
   111  		t.Fatalf("Expected nil err when detecting the samples directory, but got %v", err)
   112  	}
   113  
   114  	data, err := os.ReadFile(filepath.Join(testdataPath, "dell-r610-block.json"))
   115  	if err != nil {
   116  		t.Fatalf("Expected nil err when reading the sample data, but got %v", err)
   117  	}
   118  
   119  	var bd blockData
   120  	err = json.Unmarshal(data, &bd)
   121  	if err != nil {
   122  		t.Fatalf("Expected no error unmarshaling block.Info, but got %v", err)
   123  	}
   124  
   125  	// to learn why we check these values, please review the "dell-r610-block.json" sample
   126  	sda := findDiskByName(bd.Block.Disks, "sda")
   127  	if sda == nil {
   128  		t.Fatalf("unexpected error: can't find 'sda' in the test data")
   129  	}
   130  	if sda.DriveType != block.DRIVE_TYPE_HDD || sda.StorageController != block.STORAGE_CONTROLLER_SCSI {
   131  		t.Fatalf("inconsistent data for sda: %s", sda)
   132  	}
   133  
   134  	zram0 := findDiskByName(bd.Block.Disks, "zram0")
   135  	if zram0 == nil {
   136  		t.Fatalf("unexpected error: can't find 'zram0' in the test data")
   137  	}
   138  	if zram0.DriveType != block.DRIVE_TYPE_SSD {
   139  		t.Fatalf("inconsistent data for zram0: %s", zram0)
   140  	}
   141  }
   142  
   143  func findDiskByName(disks []*block.Disk, name string) *block.Disk {
   144  	for _, disk := range disks {
   145  		if disk.Name == name {
   146  			return disk
   147  		}
   148  	}
   149  	return nil
   150  }