initial commit
[govpp.git] / api / ifcounters / ifcounters_test.go
1 // Copyright (c) 2017 Cisco and/or its affiliates.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package ifcounters
16
17 import (
18         "testing"
19
20         . "github.com/onsi/gomega"
21 )
22
23 func TestDecodeCounters(t *testing.T) {
24         RegisterTestingT(t)
25
26         testCounters := VnetInterfaceCounters{
27                 VnetCounterType: 1,
28                 IsCombined:      0,
29                 FirstSwIfIndex:  5,
30                 Count:           2,
31                 Data: []byte{0, 0, 0, 2, // Count
32                         0, 0, 0, 0, 0, 0, 0, 10, // first counter
33                         0, 0, 0, 0, 0, 0, 0, 11}, // second counter
34         }
35         counters, err := DecodeCounters(testCounters)
36
37         Expect(err).ShouldNot(HaveOccurred())
38         Expect(len(counters)).To(BeEquivalentTo(2), "Incorrect size of the returned slice.")
39
40         Expect(counters[0].Type).To(BeEquivalentTo(1), "Incorrect counter type.")
41         Expect(counters[0].SwIfIndex).To(BeEquivalentTo(5), "Incorrect SwIfIndex.")
42         Expect(counters[0].Packets).To(BeEquivalentTo(10), "Incorrect Packets count.")
43
44         Expect(counters[1].Type).To(BeEquivalentTo(1), "Incorrect counter type.")
45         Expect(counters[1].SwIfIndex).To(BeEquivalentTo(6), "Incorrect SwIfIndex.")
46         Expect(counters[1].Packets).To(BeEquivalentTo(11), "Incorrect Packets count.")
47 }
48
49 func TestDecodeCombinedCounters(t *testing.T) {
50         RegisterTestingT(t)
51
52         testCounters := VnetInterfaceCounters{
53                 VnetCounterType: 1,
54                 IsCombined:      1,
55                 FirstSwIfIndex:  20,
56                 Count:           2,
57                 Data: []byte{0, 0, 0, 2, // Count
58                         0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 11, // first counter
59                         0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 13}, // second counter
60         }
61         counters, err := DecodeCombinedCounters(testCounters)
62
63         Expect(err).ShouldNot(HaveOccurred())
64         Expect(len(counters)).To(BeEquivalentTo(2), "Incorrect size of the returned slice.")
65
66         Expect(counters[0].Type).To(BeEquivalentTo(1), "Incorrect counter type.")
67         Expect(counters[0].SwIfIndex).To(BeEquivalentTo(20), "Incorrect SwIfIndex.")
68         Expect(counters[0].Packets).To(BeEquivalentTo(10), "Incorrect Packets count.")
69         Expect(counters[0].Bytes).To(BeEquivalentTo(11), "Incorrect Bytes count.")
70
71         Expect(counters[1].Type).To(BeEquivalentTo(1), "Incorrect counter type.")
72         Expect(counters[1].SwIfIndex).To(BeEquivalentTo(21), "Incorrect SwIfIndex.")
73         Expect(counters[1].Packets).To(BeEquivalentTo(12), "Incorrect Packets count.")
74         Expect(counters[1].Bytes).To(BeEquivalentTo(13), "Incorrect Bytes count.")
75 }
76
77 func TestDecodeCountersNegative1(t *testing.T) {
78         RegisterTestingT(t)
79
80         testCounters := VnetInterfaceCounters{
81                 IsCombined: 1, // invalid, should be 0
82         }
83         counters, err := DecodeCounters(testCounters)
84
85         Expect(err).Should(HaveOccurred())
86         Expect(err.Error()).To(ContainSubstring("invalid argument"))
87         Expect(counters).To(BeNil())
88 }
89
90 func TestDecodeCombinedCountersNegative1(t *testing.T) {
91         RegisterTestingT(t)
92
93         testCounters := VnetInterfaceCounters{
94                 IsCombined: 0, // invalid, should be 1
95         }
96         counters, err := DecodeCombinedCounters(testCounters)
97
98         Expect(err).Should(HaveOccurred())
99         Expect(err.Error()).To(ContainSubstring("invalid argument"))
100         Expect(counters).To(BeNil())
101 }
102
103 func TestDecodeCountersNegative2(t *testing.T) {
104         RegisterTestingT(t)
105
106         testCounters := VnetInterfaceCounters{
107                 IsCombined: 0,
108                 // no data
109         }
110         counters, err := DecodeCounters(testCounters)
111
112         Expect(err).Should(HaveOccurred())
113         Expect(err.Error()).To(ContainSubstring("unable to decode"))
114         Expect(counters).To(BeNil())
115 }
116
117 func TestDecodeCombinedCountersNegative2(t *testing.T) {
118         RegisterTestingT(t)
119
120         testCounters := VnetInterfaceCounters{
121                 IsCombined: 1,
122                 // no data
123         }
124         counters, err := DecodeCombinedCounters(testCounters)
125
126         Expect(err).Should(HaveOccurred())
127         Expect(err.Error()).To(ContainSubstring("unable to decode"))
128         Expect(counters).To(BeNil())
129 }