...

Source file src/github.com/prometheus/procfs/xfs/xfs_test.go

Documentation: github.com/prometheus/procfs/xfs

     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 xfs provides access to statistics exposed by the XFS filesystem.
    15  package xfs_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/prometheus/procfs/xfs"
    21  )
    22  
    23  func TestReadProcStat(t *testing.T) {
    24  	xfs, err := xfs.NewFS("testdata/fixtures/proc", "testdata/fixtures/sys")
    25  	if err != nil {
    26  		t.Fatalf("failed to access xfs fs: %v", err)
    27  	}
    28  	stats, err := xfs.ProcStat()
    29  	if err != nil {
    30  		t.Fatalf("failed to parse XFS stats: %v", err)
    31  	}
    32  
    33  	// Very lightweight test just to sanity check the path used
    34  	// to open XFS stats. Heavier tests in package xfs.
    35  	if want, got := uint32(92447), stats.ExtentAllocation.ExtentsAllocated; want != got {
    36  		t.Errorf("unexpected extents allocated:\nwant: %d\nhave: %d", want, got)
    37  	}
    38  }
    39  
    40  func TestReadSysStats(t *testing.T) {
    41  	xfs, err := xfs.NewFS("testdata/fixtures/proc", "testdata/fixtures/sys")
    42  	if err != nil {
    43  		t.Fatalf("failed to access xfs fs: %v", err)
    44  	}
    45  	stats, err := xfs.SysStats()
    46  	if err != nil {
    47  		t.Fatalf("failed to parse XFS stats: %v", err)
    48  	}
    49  
    50  	tests := []struct {
    51  		name      string
    52  		allocated uint32
    53  	}{
    54  		{
    55  			name:      "sda1",
    56  			allocated: 1,
    57  		},
    58  		{
    59  			name:      "sdb1",
    60  			allocated: 2,
    61  		},
    62  	}
    63  
    64  	const expect = 2
    65  
    66  	if l := len(stats); l != expect {
    67  		t.Fatalf("unexpected number of XFS stats: %d", l)
    68  	}
    69  	if l := len(tests); l != expect {
    70  		t.Fatalf("unexpected number of tests: %d", l)
    71  	}
    72  
    73  	for i, tt := range tests {
    74  		if want, got := tt.name, stats[i].Name; want != got {
    75  			t.Errorf("unexpected stats name:\nwant: %q\nhave: %q", want, got)
    76  		}
    77  
    78  		if want, got := tt.allocated, stats[i].ExtentAllocation.ExtentsAllocated; want != got {
    79  			t.Errorf("unexpected extents allocated:\nwant: %d\nhave: %d", want, got)
    80  		}
    81  	}
    82  }
    83  

View as plain text