github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/disk/stat_test.go (about)

     1  //go:build linux && !s390x && !arm && !386
     2  // +build linux,!s390x,!arm,!386
     3  
     4  // Copyright (c) 2015-2024 MinIO, Inc.
     5  //
     6  // This file is part of MinIO Object Storage stack
     7  //
     8  // This program is free software: you can redistribute it and/or modify
     9  // it under the terms of the GNU Affero General Public License as published by
    10  // the Free Software Foundation, either version 3 of the License, or
    11  // (at your option) any later version.
    12  //
    13  // This program is distributed in the hope that it will be useful
    14  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  // GNU Affero General Public License for more details.
    17  //
    18  // You should have received a copy of the GNU Affero General Public License
    19  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    20  
    21  package disk
    22  
    23  import (
    24  	"os"
    25  	"reflect"
    26  	"runtime"
    27  	"testing"
    28  )
    29  
    30  func TestReadDriveStats(t *testing.T) {
    31  	if runtime.GOOS == "windows" {
    32  		t.Skip("skipping this test in windows")
    33  	}
    34  	testCases := []struct {
    35  		stat            string
    36  		expectedIOStats IOStats
    37  		expectErr       bool
    38  	}{
    39  		{
    40  			stat: "1432553   420084 66247626  2398227  7077314  8720147 157049224  7469810        0  7580552  9869354    46037        0 41695120     1315        0        0",
    41  			expectedIOStats: IOStats{
    42  				ReadIOs:        1432553,
    43  				ReadMerges:     420084,
    44  				ReadSectors:    66247626,
    45  				ReadTicks:      2398227,
    46  				WriteIOs:       7077314,
    47  				WriteMerges:    8720147,
    48  				WriteSectors:   157049224,
    49  				WriteTicks:     7469810,
    50  				CurrentIOs:     0,
    51  				TotalTicks:     7580552,
    52  				ReqTicks:       9869354,
    53  				DiscardIOs:     46037,
    54  				DiscardMerges:  0,
    55  				DiscardSectors: 41695120,
    56  				DiscardTicks:   1315,
    57  				FlushIOs:       0,
    58  				FlushTicks:     0,
    59  			},
    60  			expectErr: false,
    61  		},
    62  		{
    63  			stat: "1432553   420084 66247626  2398227  7077314  8720147 157049224  7469810        0  7580552  9869354    46037        0 41695120     1315",
    64  			expectedIOStats: IOStats{
    65  				ReadIOs:        1432553,
    66  				ReadMerges:     420084,
    67  				ReadSectors:    66247626,
    68  				ReadTicks:      2398227,
    69  				WriteIOs:       7077314,
    70  				WriteMerges:    8720147,
    71  				WriteSectors:   157049224,
    72  				WriteTicks:     7469810,
    73  				CurrentIOs:     0,
    74  				TotalTicks:     7580552,
    75  				ReqTicks:       9869354,
    76  				DiscardIOs:     46037,
    77  				DiscardMerges:  0,
    78  				DiscardSectors: 41695120,
    79  				DiscardTicks:   1315,
    80  			},
    81  			expectErr: false,
    82  		},
    83  		{
    84  			stat: "1432553   420084 66247626  2398227  7077314  8720147 157049224  7469810        0  7580552  9869354",
    85  			expectedIOStats: IOStats{
    86  				ReadIOs:      1432553,
    87  				ReadMerges:   420084,
    88  				ReadSectors:  66247626,
    89  				ReadTicks:    2398227,
    90  				WriteIOs:     7077314,
    91  				WriteMerges:  8720147,
    92  				WriteSectors: 157049224,
    93  				WriteTicks:   7469810,
    94  				CurrentIOs:   0,
    95  				TotalTicks:   7580552,
    96  				ReqTicks:     9869354,
    97  			},
    98  			expectErr: false,
    99  		},
   100  		{
   101  			stat:            "1432553   420084 66247626  2398227",
   102  			expectedIOStats: IOStats{},
   103  			expectErr:       true,
   104  		},
   105  	}
   106  
   107  	for _, testCase := range testCases {
   108  		testCase := testCase
   109  		t.Run("", func(t *testing.T) {
   110  			tmpfile, err := os.CreateTemp("", "testfile")
   111  			if err != nil {
   112  				t.Error(err)
   113  			}
   114  			tmpfile.WriteString(testCase.stat)
   115  			tmpfile.Sync()
   116  			tmpfile.Close()
   117  
   118  			iostats, err := readDriveStats(tmpfile.Name())
   119  			if err != nil && !testCase.expectErr {
   120  				t.Fatalf("unexpected err; %v", err)
   121  			}
   122  			if testCase.expectErr && err == nil {
   123  				t.Fatal("expected to fail but err is nil")
   124  			}
   125  			if !reflect.DeepEqual(iostats, testCase.expectedIOStats) {
   126  				t.Fatalf("expected iostats: %v but got %v", testCase.expectedIOStats, iostats)
   127  			}
   128  		})
   129  	}
   130  }