...

Source file src/github.com/prometheus/procfs/sysfs/class_sas_port_test.go

Documentation: github.com/prometheus/procfs/sysfs

     1  // Copyright 2021 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  //go:build linux
    15  // +build linux
    16  
    17  package sysfs
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  )
    24  
    25  func TestSASPortClass(t *testing.T) {
    26  	fs, err := NewFS(sysTestFixtures)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	got, err := fs.SASPortClass()
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	want := SASPortClass{
    37  		"port-11:0": {
    38  			Name:      "port-11:0",
    39  			SASPhys:   []string{"phy-11:10", "phy-11:11", "phy-11:8", "phy-11:9"},
    40  			Expanders: []string{"expander-11:0"},
    41  		},
    42  		"port-11:0:0": {
    43  			Name:       "port-11:0:0",
    44  			SASPhys:    []string{"phy-11:0:2"},
    45  			EndDevices: []string{"end_device-11:0:0"},
    46  		},
    47  		"port-11:0:1": {
    48  			Name:       "port-11:0:1",
    49  			SASPhys:    []string{"phy-11:0:4"},
    50  			EndDevices: []string{"end_device-11:0:1"},
    51  		},
    52  		"port-11:0:2": {
    53  			Name:       "port-11:0:2",
    54  			SASPhys:    []string{"phy-11:0:6"},
    55  			EndDevices: []string{"end_device-11:0:2"},
    56  		},
    57  		"port-11:1": {
    58  			Name:      "port-11:1",
    59  			SASPhys:   []string{"phy-11:12", "phy-11:13", "phy-11:14", "phy-11:15"},
    60  			Expanders: []string{"expander-11:1"},
    61  		},
    62  	}
    63  
    64  	if diff := cmp.Diff(want, got); diff != "" {
    65  		t.Fatalf("unexpected SASDevice class (-want +got):\n%s", diff)
    66  	}
    67  }
    68  
    69  func TestSASPortGetByName(t *testing.T) {
    70  	fs, err := NewFS(sysTestFixtures)
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	dc, err := fs.SASPortClass()
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	want := "port-11:0:0"
    81  	got := dc.GetByName("port-11:0:0").Name
    82  	if diff := cmp.Diff(want, got); diff != "" {
    83  		t.Fatalf("unexpected SASPort name (-want +got):\n%s", diff)
    84  	}
    85  
    86  	// Doesn't exist.
    87  	got2 := dc.GetByName("port-15")
    88  	if got2 != nil {
    89  		t.Fatalf("unexpected GetByName response: got %v want nil", got2)
    90  	}
    91  }
    92  
    93  func TestSASPortGetByPhy(t *testing.T) {
    94  	fs, err := NewFS(sysTestFixtures)
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  	dc, err := fs.SASPortClass()
   100  	if err != nil {
   101  		t.Fatal(err)
   102  	}
   103  
   104  	want := "port-11:0:2"
   105  	got := dc.GetByPhy("phy-11:0:6").Name
   106  	if diff := cmp.Diff(want, got); diff != "" {
   107  		t.Fatalf("unexpected SASPort class (-want +got):\n%s", diff)
   108  	}
   109  
   110  	// Doesn't exist.
   111  	got2 := dc.GetByPhy("phy-12:0")
   112  	if got2 != nil {
   113  		t.Fatalf("unexpected GetByPhy response: got %v want nil", got2)
   114  	}
   115  }
   116  
   117  func TestSASPortGetByExpander(t *testing.T) {
   118  	fs, err := NewFS(sysTestFixtures)
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  
   123  	dc, err := fs.SASPortClass()
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  
   128  	want := "port-11:0"
   129  	got := dc.GetByExpander("expander-11:0").Name
   130  	if diff := cmp.Diff(want, got); diff != "" {
   131  		t.Fatalf("unexpected SASPort class (-want +got):\n%s", diff)
   132  	}
   133  
   134  	// Doesn't exist.
   135  	got2 := dc.GetByExpander("expander-12:0")
   136  	if got2 != nil {
   137  		t.Fatalf("unexpected GetByPhy response: got %v want nil", got2)
   138  	}
   139  }
   140  
   141  func TestSASPortGetByEndDevice(t *testing.T) {
   142  	fs, err := NewFS(sysTestFixtures)
   143  	if err != nil {
   144  		t.Fatal(err)
   145  	}
   146  
   147  	dc, err := fs.SASPortClass()
   148  	if err != nil {
   149  		t.Fatal(err)
   150  	}
   151  
   152  	want := "port-11:0:2"
   153  	got := dc.GetByEndDevice("end_device-11:0:2").Name
   154  	if diff := cmp.Diff(want, got); diff != "" {
   155  		t.Fatalf("unexpected SASPort class (-want +got):\n%s", diff)
   156  	}
   157  
   158  	// Doesn't exist.
   159  	got2 := dc.GetByEndDevice("end_device-12:0")
   160  	if got2 != nil {
   161  		t.Fatalf("unexpected GetByPhy response: got %v want nil", got2)
   162  	}
   163  }
   164  

View as plain text