...

Source file src/github.com/prometheus/procfs/net_ip_socket_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_parseNetIPSocketLine(t *testing.T) {
    23  	tests := []struct {
    24  		fields  []string
    25  		name    string
    26  		want    *netIPSocketLine
    27  		wantErr bool
    28  		isUDP   bool
    29  	}{
    30  		{
    31  			name:   "reading valid lines, no issue should happened",
    32  			fields: []string{"11:", "00000000:0000", "00000000:0000", "0A", "00000017:0000002A", "0:0", "0", "1000", "0", "39309"},
    33  			want: &netIPSocketLine{
    34  				Sl:        11,
    35  				LocalAddr: net.IP{0, 0, 0, 0},
    36  				LocalPort: 0,
    37  				RemAddr:   net.IP{0, 0, 0, 0},
    38  				RemPort:   0,
    39  				St:        10,
    40  				TxQueue:   23,
    41  				RxQueue:   42,
    42  				UID:       1000,
    43  				Inode:     39309,
    44  			},
    45  		},
    46  		{
    47  			name:    "error case - invalid line - number of fields/columns < 10",
    48  			fields:  []string{"1:", "00000000:0000", "00000000:0000", "07", "0:0", "0", "0"},
    49  			want:    nil,
    50  			wantErr: true,
    51  		},
    52  		{
    53  			name:    "error case - parse sl - not a valid uint",
    54  			fields:  []string{"a:", "00000000:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "0", "0", "39309"},
    55  			want:    nil,
    56  			wantErr: true,
    57  		},
    58  		{
    59  			name:    "error case - parse local_address - not a valid hex",
    60  			fields:  []string{"1:", "0000000O:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "0", "0", "39309"},
    61  			want:    nil,
    62  			wantErr: true,
    63  		},
    64  		{
    65  			name:    "error case - parse rem_address - not a valid hex",
    66  			fields:  []string{"1:", "00000000:0000", "0000000O:0000", "07", "00000000:00000001", "0:0", "0", "0", "0", "39309"},
    67  			want:    nil,
    68  			wantErr: true,
    69  		},
    70  		{
    71  			name:    "error case - cannot parse line - missing colon",
    72  			fields:  []string{"1:", "00000000:0000", "00000000:0000", "07", "0000000000000001", "0:0", "0", "0", "0", "39309"},
    73  			want:    nil,
    74  			wantErr: true,
    75  		},
    76  		{
    77  			name:    "error case - parse tx_queue - not a valid hex",
    78  			fields:  []string{"1:", "00000000:0000", "00000000:0000", "07", "DEADCODE:00000001", "0:0", "0", "0", "0", "39309"},
    79  			want:    nil,
    80  			wantErr: true,
    81  		},
    82  		{
    83  			name:    "error case - parse rx_queue - not a valid hex",
    84  			fields:  []string{"1:", "00000000:0000", "00000000:0000", "07", "00000000:FEEDCODE", "0:0", "0", "0", "0", "39309"},
    85  			want:    nil,
    86  			wantErr: true,
    87  		},
    88  		{
    89  			name:    "error case - parse UID - not a valid uint",
    90  			fields:  []string{"1:", "00000000:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "-10", "0", "39309"},
    91  			want:    nil,
    92  			wantErr: true,
    93  		},
    94  		{
    95  			name:    "error case - parse Inode - not a valid uint",
    96  			fields:  []string{"1:", "00000000:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "-10", "0", "-39309"},
    97  			want:    nil,
    98  			wantErr: true,
    99  		},
   100  		{
   101  			name:    "error case - parse Drops - not a valid uint",
   102  			fields:  []string{"1:", "00000000:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "10", "0", "39309", "2", "000000009bd60d72", "-5"},
   103  			want:    nil,
   104  			wantErr: true,
   105  			isUDP:   true,
   106  		},
   107  	}
   108  	for _, tt := range tests {
   109  		t.Run(tt.name, func(t *testing.T) {
   110  			got, err := parseNetIPSocketLine(tt.fields, tt.isUDP)
   111  			if (err != nil) != tt.wantErr {
   112  				t.Errorf("parseNetIPSocketLine() error = %v, wantErr %v", err, tt.wantErr)
   113  				return
   114  			}
   115  			if tt.want == nil && got != nil {
   116  				t.Errorf("parseNetIPSocketLine() = %v, want %v", got, tt.want)
   117  			}
   118  			if !reflect.DeepEqual(got, tt.want) {
   119  				t.Errorf("parseNetIPSocketLine() = %#v, want %#v", got, tt.want)
   120  			}
   121  		})
   122  	}
   123  }
   124  

View as plain text