Skip to content

Commit 3d81f25

Browse files
committed
Merge pull request #224 from giganteous/test-freebsd-netdev
Add a unit test for the conversion
2 parents 6683a89 + 53d0a78 commit 3d81f25

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

collector/netdev_freebsd.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,23 @@ func getNetDevStats(ignore *regexp.Regexp) (map[string]map[string]string, error)
5353
devStats := map[string]string{}
5454
data := (*C.struct_if_data)(ifa.ifa_data)
5555

56-
devStats["receive_packets"] = strconv.FormatUint(uint64(data.ifi_ipackets), 10)
57-
devStats["transmit_packets"] = strconv.FormatUint(uint64(data.ifi_opackets), 10)
58-
devStats["receive_errs"] = strconv.FormatUint(uint64(data.ifi_ierrors), 10)
59-
devStats["transmit_errs"] = strconv.FormatUint(uint64(data.ifi_oerrors), 10)
60-
devStats["receive_bytes"] = strconv.FormatUint(uint64(data.ifi_ibytes), 10)
61-
devStats["transmit_bytes"] = strconv.FormatUint(uint64(data.ifi_obytes), 10)
62-
devStats["receive_multicast"] = strconv.FormatUint(uint64(data.ifi_imcasts), 10)
63-
devStats["transmit_multicast"] = strconv.FormatUint(uint64(data.ifi_omcasts), 10)
64-
devStats["receive_drop"] = strconv.FormatUint(uint64(data.ifi_iqdrops), 10)
65-
devStats["transmit_drop"] = strconv.FormatUint(uint64(data.ifi_oqdrops), 10)
56+
devStats["receive_packets"] = convertFreeBSDCPUTime(uint64(data.ifi_ipackets))
57+
devStats["transmit_packets"] = convertFreeBSDCPUTime(uint64(data.ifi_opackets))
58+
devStats["receive_errs"] = convertFreeBSDCPUTime(uint64(data.ifi_ierrors))
59+
devStats["transmit_errs"] = convertFreeBSDCPUTime(uint64(data.ifi_oerrors))
60+
devStats["receive_bytes"] = convertFreeBSDCPUTime(uint64(data.ifi_ibytes))
61+
devStats["transmit_bytes"] = convertFreeBSDCPUTime(uint64(data.ifi_obytes))
62+
devStats["receive_multicast"] = convertFreeBSDCPUTime(uint64(data.ifi_imcasts))
63+
devStats["transmit_multicast"] = convertFreeBSDCPUTime(uint64(data.ifi_omcasts))
64+
devStats["receive_drop"] = convertFreeBSDCPUTime(uint64(data.ifi_iqdrops))
65+
devStats["transmit_drop"] = convertFreeBSDCPUTime(uint64(data.ifi_oqdrops))
6666
netDev[dev] = devStats
6767
}
6868
}
6969

7070
return netDev, nil
7171
}
72+
73+
func convertFreeBSDCPUTime(counter uint64) string {
74+
return strconv.FormatUint(counter, 10)
75+
}

collector/netdev_freebsd_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2016 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 collector
15+
16+
import "testing"
17+
18+
type uintToStringTest struct {
19+
in uint64
20+
out string
21+
}
22+
23+
var uinttostringtests = []uintToStringTest{
24+
// Copied base10 values from strconv's tests:
25+
{0, "0"},
26+
{1, "1"},
27+
{12345678, "12345678"},
28+
{1<<31 - 1, "2147483647"},
29+
{1 << 31, "2147483648"},
30+
{1<<31 + 1, "2147483649"},
31+
{1<<32 - 1, "4294967295"},
32+
{1 << 32, "4294967296"},
33+
{1<<32 + 1, "4294967297"},
34+
{1 << 50, "1125899906842624"},
35+
{1<<63 - 1, "9223372036854775807"},
36+
37+
// Some values that convert correctly on amd64, but not on i386.
38+
{0x1bf0c640a, "7500227594"},
39+
{0xbee5df75, "3202735989"},
40+
}
41+
42+
func TestUintToString(t *testing.T) {
43+
for _, test := range uinttostringtests {
44+
is := convertFreeBSDCPUTime(test.in)
45+
if is != test.out {
46+
t.Errorf("convertFreeBSDCPUTime(%v) = %v want %v",
47+
test.in, is, test.out)
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)