...

Source file src/github.com/prometheus/procfs/btrfs/get_test.go

Documentation: github.com/prometheus/procfs/btrfs

     1  // Copyright 2019 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package btrfs
    15  
    16  import "testing"
    17  
    18  type testVector struct {
    19  	uuid, label        string
    20  	devices, features  int
    21  	data, meta, system alloc
    22  	commitstats        commit
    23  }
    24  
    25  type alloc struct {
    26  	layout string
    27  	size   uint64
    28  	ratio  float64
    29  }
    30  
    31  type commit struct {
    32  	commits       uint64
    33  	lastCommitMs  uint64
    34  	maxCommitMs   uint64
    35  	totalCommitMs uint64
    36  }
    37  
    38  func TestFSBtrfsStats(t *testing.T) {
    39  	btrfs, err := NewFS("testdata/fixtures/sys")
    40  	if err != nil {
    41  		t.Fatalf("failed to access Btrfs filesystem: %v", err)
    42  	}
    43  	stats, err := btrfs.Stats()
    44  	if err != nil {
    45  		t.Fatalf("failed to parse Btrfs stats: %v", err)
    46  	}
    47  
    48  	tests := []testVector{
    49  		{
    50  			uuid:        "0abb23a9-579b-43e6-ad30-227ef47fcb9d",
    51  			label:       "fixture",
    52  			devices:     2,
    53  			features:    4,
    54  			data:        alloc{"raid0", 2147483648, 1},
    55  			meta:        alloc{"raid1", 1073741824, 2},
    56  			system:      alloc{"raid1", 8388608, 2},
    57  			commitstats: commit{258051, 1000, 51462, 47836090},
    58  		},
    59  		{
    60  			uuid:        "7f07c59f-6136-449c-ab87-e1cf2328731b",
    61  			label:       "",
    62  			devices:     4,
    63  			features:    5,
    64  			data:        alloc{"raid5", 644087808, 4. / 3.},
    65  			meta:        alloc{"raid6", 429391872, 4. / 2.},
    66  			system:      alloc{"raid6", 16777216, 4. / 2.},
    67  			commitstats: commit{0, 0, 0, 0},
    68  		},
    69  	}
    70  
    71  	if l := len(stats); l != len(tests) {
    72  		t.Fatalf("unexpected number of btrfs stats: %d", l)
    73  	}
    74  
    75  	for i, tt := range tests {
    76  		if want, got := tt.uuid, stats[i].UUID; want != got {
    77  			t.Errorf("fs %q unexpected stats name:\nwant: %q\nhave: %q", tt.uuid, want, got)
    78  		}
    79  
    80  		if want, got := tt.devices, len(stats[i].Devices); want != got {
    81  			t.Errorf("fs %q unexpected number of devices:\nwant: %d\nhave: %d", tt.uuid, want, got)
    82  		}
    83  
    84  		if want, got := tt.features, len(stats[i].Features); want != got {
    85  			t.Errorf("fs %q unexpected number of features:\nwant: %d\nhave: %d", tt.uuid, want, got)
    86  		}
    87  
    88  		if want, got := tt.data.size, stats[i].Allocation.Data.TotalBytes; want != got {
    89  			t.Errorf("fs %q unexpected data size:\nwant: %d\nhave: %d", tt.uuid, want, got)
    90  		}
    91  
    92  		if want, got := tt.meta.size, stats[i].Allocation.Metadata.TotalBytes; want != got {
    93  			t.Errorf("fs %q unexpected metadata size:\nwant: %d\nhave: %d", tt.uuid, want, got)
    94  		}
    95  
    96  		if want, got := tt.system.size, stats[i].Allocation.System.TotalBytes; want != got {
    97  			t.Errorf("fs %q unexpected system size:\nwant: %d\nhave: %d", tt.uuid, want, got)
    98  		}
    99  
   100  		if want, got := tt.data.ratio, stats[i].Allocation.Data.Layouts[tt.data.layout].Ratio; want != got {
   101  			t.Errorf("fs %q unexpected data ratio:\nwant: %f\nhave: %f", tt.uuid, want, got)
   102  		}
   103  
   104  		if want, got := tt.meta.ratio, stats[i].Allocation.Metadata.Layouts[tt.meta.layout].Ratio; want != got {
   105  			t.Errorf("fs %q unexpected metadata ratio:\nwant: %f\nhave: %f", tt.uuid, want, got)
   106  		}
   107  
   108  		if want, got := tt.system.ratio, stats[i].Allocation.System.Layouts[tt.system.layout].Ratio; want != got {
   109  			t.Errorf("fs %q unexpected system ratio:\nwant: %f\nhave: %f", tt.uuid, want, got)
   110  		}
   111  
   112  		if want, got := tt.commitstats.commits, stats[i].CommitStats.Commits; want != got {
   113  			t.Errorf("fs %q unexpected commit stats commits:\nwant: %d\nhave: %d", tt.uuid, want, got)
   114  		}
   115  
   116  		if want, got := tt.commitstats.lastCommitMs, stats[i].CommitStats.LastCommitMs; want != got {
   117  			t.Errorf("fs %q unexpected commit stats last_commit_ms:\nwant: %d\nhave: %d", tt.uuid, want, got)
   118  		}
   119  
   120  		if want, got := tt.commitstats.maxCommitMs, stats[i].CommitStats.MaxCommitMs; want != got {
   121  			t.Errorf("fs %q unexpected commit stats max_commit_ms:\nwant: %d\nhave: %d", tt.uuid, want, got)
   122  		}
   123  
   124  		if want, got := tt.commitstats.totalCommitMs, stats[i].CommitStats.TotalCommitMs; want != got {
   125  			t.Errorf("fs %q unexpected commit stats total_commit_ms:\nwant: %d\nhave: %d", tt.uuid, want, got)
   126  		}
   127  	}
   128  }
   129  

View as plain text