...

Source file src/github.com/prometheus/procfs/net_tcp_test.go

Documentation: github.com/prometheus/procfs

     1  // Copyright 2020 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 procfs
    15  
    16  import (
    17  	"net"
    18  	"reflect"
    19  	"testing"
    20  )
    21  
    22  func Test_newNetTCP(t *testing.T) {
    23  	tests := []struct {
    24  		name    string
    25  		file    string
    26  		want    NetTCP
    27  		wantErr bool
    28  	}{
    29  		{
    30  			name: "tcp file found, no error should come up",
    31  			file: "testdata/fixtures/proc/net/tcp",
    32  			want: []*netIPSocketLine{
    33  				{
    34  					Sl:        0,
    35  					LocalAddr: net.IP{10, 0, 0, 5},
    36  					LocalPort: 22,
    37  					RemAddr:   net.IP{0, 0, 0, 0},
    38  					RemPort:   0,
    39  					St:        10,
    40  					TxQueue:   0,
    41  					RxQueue:   1,
    42  					UID:       0,
    43  					Inode:     2740,
    44  				},
    45  				{
    46  					Sl:        1,
    47  					LocalAddr: net.IP{0, 0, 0, 0},
    48  					LocalPort: 22,
    49  					RemAddr:   net.IP{0, 0, 0, 0},
    50  					RemPort:   0,
    51  					St:        10,
    52  					TxQueue:   1,
    53  					RxQueue:   0,
    54  					UID:       0,
    55  					Inode:     2740,
    56  				},
    57  				{
    58  					Sl:        2,
    59  					LocalAddr: net.IP{0, 0, 0, 0},
    60  					LocalPort: 22,
    61  					RemAddr:   net.IP{0, 0, 0, 0},
    62  					RemPort:   0,
    63  					St:        10,
    64  					TxQueue:   1,
    65  					RxQueue:   1,
    66  					UID:       0,
    67  					Inode:     2740,
    68  				},
    69  			},
    70  			wantErr: false,
    71  		},
    72  		{
    73  			name: "tcp6 file found, no error should come up",
    74  			file: "testdata/fixtures/proc/net/tcp6",
    75  			want: []*netIPSocketLine{
    76  				{
    77  					Sl:        1315,
    78  					LocalAddr: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    79  					LocalPort: 5355,
    80  					RemAddr:   net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    81  					RemPort:   0,
    82  					St:        7,
    83  					TxQueue:   0,
    84  					RxQueue:   0,
    85  					UID:       981,
    86  					Inode:     21040,
    87  				},
    88  				{
    89  					Sl:        6073,
    90  					LocalAddr: net.IP{254, 128, 0, 0, 0, 0, 0, 0, 86, 225, 173, 255, 254, 124, 102, 9},
    91  					LocalPort: 51073,
    92  					RemAddr:   net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    93  					RemPort:   0,
    94  					St:        7,
    95  					TxQueue:   0,
    96  					RxQueue:   0,
    97  					UID:       1000,
    98  					Inode:     11337031,
    99  				},
   100  			},
   101  			wantErr: false,
   102  		},
   103  		{
   104  			name:    "error case - file not found",
   105  			file:    "somewhere over the rainbow",
   106  			want:    nil,
   107  			wantErr: true,
   108  		},
   109  		{
   110  			name:    "error case - parse error",
   111  			file:    "testdata/fixtures/proc/net/tcp_broken",
   112  			want:    nil,
   113  			wantErr: true,
   114  		},
   115  	}
   116  	for _, tt := range tests {
   117  		t.Run(tt.name, func(t *testing.T) {
   118  			got, err := newNetTCP(tt.file)
   119  			if (err != nil) != tt.wantErr {
   120  				t.Errorf("newNetTCP() error = %v, wantErr %v", err, tt.wantErr)
   121  				return
   122  			}
   123  			if !reflect.DeepEqual(got, tt.want) {
   124  				t.Errorf("newNetTCP() = %v, want %v", got, tt.want)
   125  			}
   126  		})
   127  	}
   128  }
   129  
   130  func Test_newNetTCPSummary(t *testing.T) {
   131  	tests := []struct {
   132  		name    string
   133  		file    string
   134  		want    *NetTCPSummary
   135  		wantErr bool
   136  	}{
   137  		{
   138  			name:    "tcp file found, no error should come up",
   139  			file:    "testdata/fixtures/proc/net/tcp",
   140  			want:    &NetTCPSummary{TxQueueLength: 2, RxQueueLength: 2, UsedSockets: 3},
   141  			wantErr: false,
   142  		},
   143  		{
   144  			name:    "tcp6 file found, no error should come up",
   145  			file:    "testdata/fixtures/proc/net/tcp6",
   146  			want:    &NetTCPSummary{TxQueueLength: 0, RxQueueLength: 0, UsedSockets: 2},
   147  			wantErr: false,
   148  		},
   149  		{
   150  			name:    "error case - file not found",
   151  			file:    "somewhere over the rainbow",
   152  			want:    nil,
   153  			wantErr: true,
   154  		},
   155  		{
   156  			name:    "error case - parse error",
   157  			file:    "testdata/fixtures/proc/net/tcp_broken",
   158  			want:    nil,
   159  			wantErr: true,
   160  		},
   161  	}
   162  	for _, tt := range tests {
   163  		t.Run(tt.name, func(t *testing.T) {
   164  			got, err := newNetTCPSummary(tt.file)
   165  			if (err != nil) != tt.wantErr {
   166  				t.Errorf("newNetTCPSummary() error = %v, wantErr %v", err, tt.wantErr)
   167  				return
   168  			}
   169  			if !reflect.DeepEqual(got, tt.want) {
   170  				t.Errorf("newNetTCPSummary() = %v, want %v", got, tt.want)
   171  			}
   172  		})
   173  	}
   174  }
   175  

View as plain text