1// Copyright 2018 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 nfs_test
15
16import (
17	"reflect"
18	"strings"
19	"testing"
20
21	"github.com/prometheus/procfs/nfs"
22)
23
24func TestNewNFSClientRPCStats(t *testing.T) {
25	tests := []struct {
26		name    string
27		content string
28		stats   *nfs.ClientRPCStats
29		invalid bool
30	}{
31		{
32			name:    "invalid file",
33			content: "invalid",
34			invalid: true,
35		}, {
36			name: "good old kernel version file",
37			content: `net 70 70 69 45
38rpc 1218785755 374636 1218815394
39proc2 18 16 57 74 52 71 73 45 86 0 52 83 61 17 53 50 23 70 82
40proc3 22 0 1061909262 48906 4077635 117661341 5 29391916 2570425 2993289 590 0 0 7815 15 1130 0 3983 92385 13332 2 1 23729
41proc4 48 98 51 54 83 85 23 24 1 28 73 68 83 12 84 39 68 59 58 88 29 74 69 96 21 84 15 53 86 54 66 56 97 36 49 32 85 81 11 58 32 67 13 28 35 90 1 26 1337
42`,
43			stats: &nfs.ClientRPCStats{
44				Network: nfs.Network{
45					NetCount:   70,
46					UDPCount:   70,
47					TCPCount:   69,
48					TCPConnect: 45,
49				},
50				ClientRPC: nfs.ClientRPC{
51					RPCCount:        1218785755,
52					Retransmissions: 374636,
53					AuthRefreshes:   1218815394,
54				},
55				V2Stats: nfs.V2Stats{
56					Null:     16,
57					GetAttr:  57,
58					SetAttr:  74,
59					Root:     52,
60					Lookup:   71,
61					ReadLink: 73,
62					Read:     45,
63					WrCache:  86,
64					Write:    0,
65					Create:   52,
66					Remove:   83,
67					Rename:   61,
68					Link:     17,
69					SymLink:  53,
70					MkDir:    50,
71					RmDir:    23,
72					ReadDir:  70,
73					FsStat:   82,
74				},
75				V3Stats: nfs.V3Stats{
76					Null:        0,
77					GetAttr:     1061909262,
78					SetAttr:     48906,
79					Lookup:      4077635,
80					Access:      117661341,
81					ReadLink:    5,
82					Read:        29391916,
83					Write:       2570425,
84					Create:      2993289,
85					MkDir:       590,
86					SymLink:     0,
87					MkNod:       0,
88					Remove:      7815,
89					RmDir:       15,
90					Rename:      1130,
91					Link:        0,
92					ReadDir:     3983,
93					ReadDirPlus: 92385,
94					FsStat:      13332,
95					FsInfo:      2,
96					PathConf:    1,
97					Commit:      23729},
98				ClientV4Stats: nfs.ClientV4Stats{
99					Null:               98,
100					Read:               51,
101					Write:              54,
102					Commit:             83,
103					Open:               85,
104					OpenConfirm:        23,
105					OpenNoattr:         24,
106					OpenDowngrade:      1,
107					Close:              28,
108					Setattr:            73,
109					FsInfo:             68,
110					Renew:              83,
111					SetClientID:        12,
112					SetClientIDConfirm: 84,
113					Lock:               39,
114					Lockt:              68,
115					Locku:              59,
116					Access:             58,
117					Getattr:            88,
118					Lookup:             29,
119					LookupRoot:         74,
120					Remove:             69,
121					Rename:             96,
122					Link:               21,
123					Symlink:            84,
124					Create:             15,
125					Pathconf:           53,
126					StatFs:             86,
127					ReadLink:           54,
128					ReadDir:            66,
129					ServerCaps:         56,
130					DelegReturn:        97,
131					GetACL:             36,
132					SetACL:             49,
133					FsLocations:        32,
134					ReleaseLockowner:   85,
135					Secinfo:            81,
136					FsidPresent:        11,
137					ExchangeID:         58,
138					CreateSession:      32,
139					DestroySession:     67,
140					Sequence:           13,
141					GetLeaseTime:       28,
142					ReclaimComplete:    35,
143					LayoutGet:          90,
144					GetDeviceInfo:      1,
145					LayoutCommit:       26,
146					LayoutReturn:       1337,
147					SecinfoNoName:      0,
148					TestStateID:        0,
149					FreeStateID:        0,
150					GetDeviceList:      0,
151					BindConnToSession:  0,
152					DestroyClientID:    0,
153					Seek:               0,
154					Allocate:           0,
155					DeAllocate:         0,
156					LayoutStats:        0,
157					Clone:              0,
158				},
159			},
160		}, {
161			name: "good file",
162			content: `net 18628 0 18628 6
163rpc 4329785 0 4338291
164proc2 18 2 69 0 0 4410 0 0 0 0 0 0 0 0 0 0 0 99 2
165proc3 22 1 4084749 29200 94754 32580 186 47747 7981 8639 0 6356 0 6962 0 7958 0 0 241 4 4 2 39
166proc4 61 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
167`,
168			stats: &nfs.ClientRPCStats{
169				Network: nfs.Network{
170					NetCount:   18628,
171					UDPCount:   0,
172					TCPCount:   18628,
173					TCPConnect: 6,
174				},
175				ClientRPC: nfs.ClientRPC{
176					RPCCount:        4329785,
177					Retransmissions: 0,
178					AuthRefreshes:   4338291,
179				},
180				V2Stats: nfs.V2Stats{
181					Null:     2,
182					GetAttr:  69,
183					SetAttr:  0,
184					Root:     0,
185					Lookup:   4410,
186					ReadLink: 0,
187					Read:     0,
188					WrCache:  0,
189					Write:    0,
190					Create:   0,
191					Remove:   0,
192					Rename:   0,
193					Link:     0,
194					SymLink:  0,
195					MkDir:    0,
196					RmDir:    0,
197					ReadDir:  99,
198					FsStat:   2,
199				},
200				V3Stats: nfs.V3Stats{
201					Null:        1,
202					GetAttr:     4084749,
203					SetAttr:     29200,
204					Lookup:      94754,
205					Access:      32580,
206					ReadLink:    186,
207					Read:        47747,
208					Write:       7981,
209					Create:      8639,
210					MkDir:       0,
211					SymLink:     6356,
212					MkNod:       0,
213					Remove:      6962,
214					RmDir:       0,
215					Rename:      7958,
216					Link:        0,
217					ReadDir:     0,
218					ReadDirPlus: 241,
219					FsStat:      4,
220					FsInfo:      4,
221					PathConf:    2,
222					Commit:      39,
223				},
224				ClientV4Stats: nfs.ClientV4Stats{
225					Null:               1,
226					Read:               0,
227					Write:              0,
228					Commit:             0,
229					Open:               0,
230					OpenConfirm:        0,
231					OpenNoattr:         0,
232					OpenDowngrade:      0,
233					Close:              0,
234					Setattr:            0,
235					FsInfo:             0,
236					Renew:              0,
237					SetClientID:        1,
238					SetClientIDConfirm: 1,
239					Lock:               0,
240					Lockt:              0,
241					Locku:              0,
242					Access:             0,
243					Getattr:            0,
244					Lookup:             0,
245					LookupRoot:         0,
246					Remove:             2,
247					Rename:             0,
248					Link:               0,
249					Symlink:            0,
250					Create:             0,
251					Pathconf:           0,
252					StatFs:             0,
253					ReadLink:           0,
254					ReadDir:            0,
255					ServerCaps:         0,
256					DelegReturn:        0,
257					GetACL:             0,
258					SetACL:             0,
259					FsLocations:        0,
260					ReleaseLockowner:   0,
261					Secinfo:            0,
262					FsidPresent:        0,
263					ExchangeID:         0,
264					CreateSession:      0,
265					DestroySession:     0,
266					Sequence:           0,
267					GetLeaseTime:       0,
268					ReclaimComplete:    0,
269					LayoutGet:          0,
270					GetDeviceInfo:      0,
271					LayoutCommit:       0,
272					LayoutReturn:       0,
273					SecinfoNoName:      0,
274					TestStateID:        0,
275					FreeStateID:        0,
276					GetDeviceList:      0,
277					BindConnToSession:  0,
278					DestroyClientID:    0,
279					Seek:               0,
280					Allocate:           0,
281					DeAllocate:         0,
282					LayoutStats:        0,
283					Clone:              0,
284				},
285			},
286		},
287	}
288
289	for _, tt := range tests {
290		t.Run(tt.name, func(t *testing.T) {
291			stats, err := nfs.ParseClientRPCStats(strings.NewReader(tt.content))
292
293			if tt.invalid && err == nil {
294				t.Fatal("expected an error, but none occurred")
295			}
296			if !tt.invalid && err != nil {
297				t.Fatalf("unexpected error: %v", err)
298			}
299
300			if want, have := tt.stats, stats; !reflect.DeepEqual(want, have) {
301				t.Fatalf("unexpected NFS stats:\nwant:\n%v\nhave:\n%v", want, have)
302			}
303		})
304	}
305}
306