...

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

Documentation: github.com/prometheus/procfs/iscsi

     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 iscsi_test
    15  
    16  import (
    17  	"reflect"
    18  	"testing"
    19  
    20  	"github.com/prometheus/procfs/iscsi"
    21  )
    22  
    23  func TestGetStats(t *testing.T) {
    24  	tests := []struct {
    25  		stat *iscsi.Stats
    26  	}{
    27  		{
    28  			stat: &iscsi.Stats{
    29  				Name: "iqn.2003-01.org.linux-iscsi.osd1.x8664:sn.8888bbbbddd0",
    30  				Tpgt: []iscsi.TPGT{
    31  					{
    32  						Name:     "tpgt_1",
    33  						TpgtPath: "testdata/fixtures/sys/kernel/config/target/iscsi/iqn.2003-01.org.linux-iscsi.osd1.x8664:sn.8888bbbbddd0/tpgt_1",
    34  						IsEnable: true,
    35  						Luns: []iscsi.LUN{
    36  							{
    37  								Name:       "lun_0",
    38  								LunPath:    "testdata/fixtures/sys/kernel/config/target/iscsi/iqn.2003-01.org.linux-iscsi.osd1.x8664:sn.8888bbbbddd0/tpgt_1/lun/lun_0",
    39  								Backstore:  "rd_mcp",
    40  								ObjectName: "ramdisk_lio_1G",
    41  								TypeNumber: "119",
    42  							},
    43  						},
    44  					},
    45  				},
    46  				RootPath: "testdata/fixtures/sys/kernel/config/target/iscsi",
    47  			},
    48  		},
    49  		{
    50  			stat: &iscsi.Stats{
    51  				Name: "iqn.2003-01.org.linux-iscsi.osd1.x8664:sn.abcd1abcd2ab",
    52  				Tpgt: []iscsi.TPGT{
    53  					{
    54  						Name:     "tpgt_1",
    55  						TpgtPath: "testdata/fixtures/sys/kernel/config/target/iscsi/iqn.2003-01.org.linux-iscsi.osd1.x8664:sn.abcd1abcd2ab/tpgt_1",
    56  						IsEnable: true,
    57  						Luns: []iscsi.LUN{
    58  							{
    59  								Name:       "lun_0",
    60  								LunPath:    "testdata/fixtures/sys/kernel/config/target/iscsi/iqn.2003-01.org.linux-iscsi.osd1.x8664:sn.abcd1abcd2ab/tpgt_1/lun/lun_0",
    61  								Backstore:  "iblock",
    62  								ObjectName: "block_lio_rbd1",
    63  								TypeNumber: "0",
    64  							},
    65  						},
    66  					},
    67  				},
    68  				RootPath: "testdata/fixtures/sys/kernel/config/target/iscsi",
    69  			},
    70  		},
    71  		{
    72  			stat: &iscsi.Stats{
    73  				Name: "iqn.2016-11.org.linux-iscsi.igw.x86:dev.rbd0",
    74  				Tpgt: []iscsi.TPGT{
    75  					{
    76  						Name:     "tpgt_1",
    77  						TpgtPath: "testdata/fixtures/sys/kernel/config/target/iscsi/iqn.2016-11.org.linux-iscsi.igw.x86:dev.rbd0/tpgt_1",
    78  						IsEnable: true,
    79  						Luns: []iscsi.LUN{
    80  							{
    81  								Name:       "lun_0",
    82  								LunPath:    "testdata/fixtures/sys/kernel/config/target/iscsi/iqn.2016-11.org.linux-iscsi.igw.x86:dev.rbd0/tpgt_1/lun/lun_0",
    83  								Backstore:  "fileio",
    84  								ObjectName: "file_lio_1G",
    85  								TypeNumber: "1",
    86  							},
    87  						},
    88  					},
    89  				},
    90  				RootPath: "testdata/fixtures/sys/kernel/config/target/iscsi",
    91  			},
    92  		},
    93  		{
    94  			stat: &iscsi.Stats{
    95  				Name: "iqn.2016-11.org.linux-iscsi.igw.x86:sn.ramdemo",
    96  				Tpgt: []iscsi.TPGT{
    97  					{
    98  						Name:     "tpgt_1",
    99  						TpgtPath: "testdata/fixtures/sys/kernel/config/target/iscsi/iqn.2016-11.org.linux-iscsi.igw.x86:sn.ramdemo/tpgt_1",
   100  						IsEnable: true,
   101  						Luns: []iscsi.LUN{
   102  							{
   103  								Name:       "lun_0",
   104  								LunPath:    "testdata/fixtures/sys/kernel/config/target/iscsi/iqn.2016-11.org.linux-iscsi.igw.x86:sn.ramdemo/tpgt_1/lun/lun_0",
   105  								Backstore:  "rbd",
   106  								ObjectName: "iscsi-images-demo",
   107  								TypeNumber: "0",
   108  							},
   109  						},
   110  					},
   111  				},
   112  				RootPath: "testdata/fixtures/sys/kernel/config/target/iscsi",
   113  			},
   114  		},
   115  	}
   116  
   117  	readTests := []struct {
   118  		read  uint64
   119  		write uint64
   120  		iops  uint64
   121  	}{
   122  		{10325, 40325, 204950},
   123  		{20095, 71235, 104950},
   124  		{10195, 30195, 301950},
   125  		{1504, 4733, 1234},
   126  	}
   127  
   128  	sysconfigfs, err := iscsi.NewFS("testdata/fixtures/sys", "testdata/fixtures/sys/kernel/config")
   129  	if err != nil {
   130  		t.Fatalf("failed to access xfs fs: %v", err)
   131  	}
   132  	sysfsStat, err := sysconfigfs.ISCSIStats()
   133  	statSize := len(sysfsStat)
   134  	if statSize != 4 {
   135  		t.Errorf("fixtures size does not match %d", statSize)
   136  	}
   137  	if err != nil {
   138  		t.Errorf("unexpected test fixtures")
   139  	}
   140  
   141  	for i, stat := range sysfsStat {
   142  		want, have := tests[i].stat, stat
   143  		if !reflect.DeepEqual(want, have) {
   144  			t.Errorf("unexpected iSCSI stats:\nwant:\n%v\nhave:\n%v", want, have)
   145  		} else {
   146  			readMB, writeMB, iops, err := iscsi.ReadWriteOPS(stat.RootPath+"/"+stat.Name,
   147  				stat.Tpgt[0].Name, stat.Tpgt[0].Luns[0].Name)
   148  			if err != nil {
   149  				t.Errorf("unexpected iSCSI ReadWriteOPS path %s %s %s",
   150  					stat.Name, stat.Tpgt[0].Name, stat.Tpgt[0].Luns[0].Name)
   151  				t.Errorf("%v", err)
   152  			}
   153  			if !reflect.DeepEqual(readTests[i].read, readMB) {
   154  				t.Errorf("unexpected iSCSI read data :\nwant:\n%v\nhave:\n%v", readTests[i].read, readMB)
   155  			}
   156  			if !reflect.DeepEqual(readTests[i].write, writeMB) {
   157  				t.Errorf("unexpected iSCSI write data :\nwant:\n%v\nhave:\n%v", readTests[i].write, writeMB)
   158  			}
   159  			if !reflect.DeepEqual(readTests[i].iops, iops) {
   160  				t.Errorf("unexpected iSCSI iops data :\nwant:\n%v\nhave:\n%v", readTests[i].iops, iops)
   161  			}
   162  			if stat.Tpgt[0].Luns[0].Backstore == "rd_mcp" {
   163  				haveRdmcp, err := sysconfigfs.GetRDMCPPath("119", "ramdisk_lio_1G")
   164  				if err != nil {
   165  					t.Errorf("fail rdmcp error %v", err)
   166  				}
   167  				// Name ObjectName
   168  				wantRdmcp := &iscsi.RDMCP{"rd_mcp_" + stat.Tpgt[0].Luns[0].TypeNumber, stat.Tpgt[0].Luns[0].ObjectName}
   169  
   170  				if !reflect.DeepEqual(wantRdmcp, haveRdmcp) {
   171  					t.Errorf("unexpected rdmcp data :\nwant:\n%v\nhave:\n%v", wantRdmcp, haveRdmcp)
   172  				}
   173  			} else if stat.Tpgt[0].Luns[0].Backstore == "iblock" {
   174  				haveIblock, err := sysconfigfs.GetIblockUdev("0", "block_lio_rbd1")
   175  				if err != nil {
   176  					t.Errorf("fail iblock error %v", err)
   177  				}
   178  				// Name Bnumber ObjectName Iblock
   179  				wantIblock := &iscsi.IBLOCK{"iblock_" + stat.Tpgt[0].Luns[0].TypeNumber, stat.Tpgt[0].Luns[0].TypeNumber, stat.Tpgt[0].Luns[0].ObjectName, "/dev/rbd1"}
   180  				if !reflect.DeepEqual(wantIblock, haveIblock) {
   181  					t.Errorf("unexpected iblock data :\nwant:\n%v\nhave:\n%v", wantIblock, haveIblock)
   182  				}
   183  			} else if stat.Tpgt[0].Luns[0].Backstore == "fileio" {
   184  				haveFileIO, err := sysconfigfs.GetFileioUdev("1", "file_lio_1G")
   185  				if err != nil {
   186  					t.Errorf("fail fileio error %v", err)
   187  				}
   188  				// Name, Fnumber, ObjectName, Filename
   189  				wantFileIO := &iscsi.FILEIO{"fileio_" + stat.Tpgt[0].Luns[0].TypeNumber, stat.Tpgt[0].Luns[0].TypeNumber, "file_lio_1G", "/home/iscsi/file_back_1G"}
   190  				if !reflect.DeepEqual(wantFileIO, haveFileIO) {
   191  					t.Errorf("unexpected fileio data :\nwant:\n%v\nhave:\n%v", wantFileIO, haveFileIO)
   192  				}
   193  			} else if stat.Tpgt[0].Luns[0].Backstore == "rbd" {
   194  				haveRBD, err := sysconfigfs.GetRBDMatch("0", "iscsi-images-demo")
   195  				if err != nil {
   196  					t.Errorf("fail rbd error %v", err)
   197  				}
   198  				// Name, Rnumber, Pool, Image
   199  				wantRBD := &iscsi.RBD{"rbd_" + stat.Tpgt[0].Luns[0].TypeNumber, stat.Tpgt[0].Luns[0].TypeNumber, "iscsi-images", "demo"}
   200  				if !reflect.DeepEqual(wantRBD, haveRBD) {
   201  					t.Errorf("unexpected fileio data :\nwant:\n%v\nhave:\n%v", wantRBD, haveRBD)
   202  				}
   203  			}
   204  		}
   205  	}
   206  }
   207  

View as plain text