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