bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/scollector/collectors/disk_test.go (about)

     1  package collectors
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestGetResync(t *testing.T) {
     9  	progress, ok := getResync("Rebuild Status : 0%")
    10  	if progress >= 0.0001 || !ok {
    11  		t.Fatalf("Rebuild Status should be 0 percent, got %f", progress)
    12  	}
    13  	progress, ok = getResync("Rebuild Status : 20% complete")
    14  	if progress != 20.0 || !ok {
    15  		t.Fatalf("progress should be at 20 percent got: %f", progress)
    16  	}
    17  	if _, ok = getResync("blam 20% dd"); ok {
    18  		t.Fatalf("we should not have detected a resync")
    19  	}
    20  }
    21  
    22  type spindle struct {
    23  	dev string
    24  	ok  bool
    25  }
    26  
    27  func TestGetSpindle(t *testing.T) {
    28  	tests := map[string]spindle{
    29  		"2       8       19        0      active sync   /dev/sdb3": {"/dev/sdb3", true},
    30  		"2       0        0        2      removed":                 {"", false},
    31  		"3       8       17        -      faulty   /dev/sdb1":      {"/dev/sdb1", true},
    32  		"fdaf /dev ": {"", false},
    33  		"fdsa":       {"", false},
    34  		"":           {"", false},
    35  	}
    36  	for s, expected := range tests {
    37  		if dev, ok := getSpindle(s); ok != expected.ok || dev != expected.dev {
    38  			t.Fatalf("Wrong spindle result: got: %v %v, expected: %v %v", dev, ok, expected.dev, expected.ok)
    39  		}
    40  	}
    41  }
    42  
    43  type state struct {
    44  	s  mdadmState
    45  	ok bool
    46  }
    47  
    48  func TestGetState(t *testing.T) {
    49  	tests := map[string]state{
    50  		"State : clean, degraded": {mdadmDegraded, true},
    51  		"State : active, FAILED":  {mdadmFailed, true},
    52  		"State : clean":           {mdadmNormal, true},
    53  		" fdaf /dev ":             {mdadmUnknown, false},
    54  	}
    55  	for s, expected := range tests {
    56  		if state, ok := getState(s); ok != expected.ok || state != expected.s {
    57  			t.Fatalf("Wrong state: got: %v %v, expected: %v %v", state, ok, expected.s, expected.ok)
    58  		}
    59  	}
    60  }
    61  
    62  func compTab(a []string, b []string) bool {
    63  	if len(a) != len(b) {
    64  		return false
    65  	}
    66  	if len(a) == 0 {
    67  		return true
    68  	}
    69  	for i := range a {
    70  		if a[i] != b[i] {
    71  			return false
    72  		}
    73  	}
    74  	return true
    75  }
    76  
    77  func gotexp(got volumeDetail, exp volumeDetail, t *testing.T) {
    78  	if got.syncProgress != exp.syncProgress {
    79  		t.Fatalf("parseExamineMdadm:syncProgress got: %v exp: %v", got.syncProgress, exp.syncProgress)
    80  	}
    81  	if got.state != exp.state {
    82  		t.Fatalf("parseExamineMdadm:syncProgress got: %v exp: %v", got.state, exp.state)
    83  	}
    84  	if !compTab(got.failedSpindle, exp.failedSpindle) {
    85  		t.Fatalf("parseExamineMdadm:syncProgress got: %v exp: %v", got.failedSpindle, exp.failedSpindle)
    86  	}
    87  	if !compTab(got.activeSpindle, exp.activeSpindle) {
    88  		t.Fatalf("parseExamineMdadm:syncProgress got: %v exp: %v", got.activeSpindle, exp.activeSpindle)
    89  	}
    90  	if !compTab(got.spareSpindle, exp.spareSpindle) {
    91  		t.Fatalf("parseExamineMdadm:syncProgress got: %v exp: %v", got.spareSpindle, exp.spareSpindle)
    92  	}
    93  }
    94  
    95  func TestParseExamineMdadm(t *testing.T) {
    96  	test := ` /dev/md0:
    97          Version : 1.2
    98    Creation Time : Tue Jun 28 20:11:03 2016
    99       Raid Level : raid1
   100       Array Size : 204608 (199.85 MiB 209.52 MB)
   101    Used Dev Size : 204608 (199.85 MiB 209.52 MB)
   102     Raid Devices : 2
   103    Total Devices : 2
   104      Persistence : Superblock is persistent
   105  
   106      Update Time : Wed Jun 29 20:40:09 2016
   107            State : clean, degraded
   108   Active Devices : 1
   109  Working Devices : 1
   110   Failed Devices : 1
   111    Spare Devices : 0
   112  
   113             Name : ny-xav01.ds.stackexchange.com:0  (local to host ny-xav01.ds.stackexchange.com)
   114             UUID : 92a9102d:e9e62da2:ed2ed365:07251d1b
   115           Events : 63
   116  
   117      Number   Major   Minor   RaidDevice State
   118         2       8       19        0      active sync   /dev/sdb3
   119         2       0        0        2      removed
   120  
   121         3       8       17        -      faulty   /dev/sdb1
   122  `
   123  	exp := volumeDetail{
   124  		100.0,
   125  		mdadmDegraded,
   126  		[]string{"/dev/sdb1"},
   127  		[]string{"/dev/sdb3"},
   128  		[]string{},
   129  	}
   130  
   131  	got := parseExamineMdadm(strings.NewReader(test))
   132  	gotexp(got, exp, t)
   133  
   134  	t2 := `
   135  /dev/md0:
   136          Version : 1.2
   137    Creation Time : Tue Jun 28 20:11:03 2016
   138       Raid Level : raid1
   139       Array Size : 204608 (199.85 MiB 209.52 MB)
   140    Used Dev Size : 204608 (199.85 MiB 209.52 MB)
   141     Raid Devices : 2
   142    Total Devices : 2
   143      Persistence : Superblock is persistent
   144  
   145      Update Time : Thu Jun 30 18:52:45 2016
   146            State : clean, degraded, recovering
   147   Active Devices : 1
   148  Working Devices : 2
   149   Failed Devices : 0
   150    Spare Devices : 1
   151  
   152   Rebuild Status : 29% complete
   153  
   154             Name : ny-xav01.ds.stackexchange.com:0  (local to host ny-xav01.ds.stackexchange.com)
   155             UUID : 92a9102d:e9e62da2:ed2ed365:07251d1b
   156           Events : 92
   157  
   158      Number   Major   Minor   RaidDevice State
   159         2       8       19        0      active sync   /dev/sdb3
   160         3       8       17        1      spare rebuilding   /dev/sdb1
   161  `
   162  	exp = volumeDetail{
   163  		29.0,
   164  		mdadmDegraded,
   165  		[]string{},
   166  		[]string{"/dev/sdb3"},
   167  		[]string{"/dev/sdb1"},
   168  	}
   169  
   170  	got = parseExamineMdadm(strings.NewReader(t2))
   171  	gotexp(got, exp, t)
   172  
   173  	t3 := `/dev/md0:
   174          Version : 1.2
   175    Creation Time : Tue Jun 28 20:11:03 2016
   176       Raid Level : raid1
   177       Array Size : 204608 (199.85 MiB 209.52 MB)
   178    Used Dev Size : 204608 (199.85 MiB 209.52 MB)
   179     Raid Devices : 2
   180    Total Devices : 2
   181      Persistence : Superblock is persistent
   182  
   183      Update Time : Thu Jun 30 18:55:13 2016
   184            State : clean
   185   Active Devices : 2
   186  Working Devices : 2
   187   Failed Devices : 0
   188    Spare Devices : 0
   189  
   190             Name : ny-xav01.ds.stackexchange.com:0  (local to host ny-xav01.ds.stackexchange.com)
   191             UUID : 92a9102d:e9e62da2:ed2ed365:07251d1b
   192           Events : 105
   193  
   194      Number   Major   Minor   RaidDevice State
   195         2       8       19        0      active sync   /dev/sdb3
   196         3       8       17        1      active sync   /dev/sdb1
   197  `
   198  
   199  	exp = volumeDetail{
   200  		100.0,
   201  		mdadmNormal,
   202  		[]string{},
   203  		[]string{"/dev/sdb3", "/dev/sdb1"},
   204  		[]string{},
   205  	}
   206  	got = parseExamineMdadm(strings.NewReader(t3))
   207  	gotexp(got, exp, t)
   208  }