1 // Copyright (c) 2017 Cisco and/or its affiliates.
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:
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 . "github.com/onsi/gomega"
23 func TestDecodeCounters(t *testing.T) {
26 testCounters := VnetInterfaceCounters{
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
35 counters, err := DecodeCounters(testCounters)
37 Expect(err).ShouldNot(HaveOccurred())
38 Expect(len(counters)).To(BeEquivalentTo(2), "Incorrect size of the returned slice.")
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.")
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.")
49 func TestDecodeCombinedCounters(t *testing.T) {
52 testCounters := VnetInterfaceCounters{
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
61 counters, err := DecodeCombinedCounters(testCounters)
63 Expect(err).ShouldNot(HaveOccurred())
64 Expect(len(counters)).To(BeEquivalentTo(2), "Incorrect size of the returned slice.")
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.")
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.")
77 func TestDecodeCountersNegative1(t *testing.T) {
80 testCounters := VnetInterfaceCounters{
81 IsCombined: 1, // invalid, should be 0
83 counters, err := DecodeCounters(testCounters)
85 Expect(err).Should(HaveOccurred())
86 Expect(err.Error()).To(ContainSubstring("invalid argument"))
87 Expect(counters).To(BeNil())
90 func TestDecodeCombinedCountersNegative1(t *testing.T) {
93 testCounters := VnetInterfaceCounters{
94 IsCombined: 0, // invalid, should be 1
96 counters, err := DecodeCombinedCounters(testCounters)
98 Expect(err).Should(HaveOccurred())
99 Expect(err.Error()).To(ContainSubstring("invalid argument"))
100 Expect(counters).To(BeNil())
103 func TestDecodeCountersNegative2(t *testing.T) {
106 testCounters := VnetInterfaceCounters{
110 counters, err := DecodeCounters(testCounters)
112 Expect(err).Should(HaveOccurred())
113 Expect(err.Error()).To(ContainSubstring("unable to decode"))
114 Expect(counters).To(BeNil())
117 func TestDecodeCombinedCountersNegative2(t *testing.T) {
120 testCounters := VnetInterfaceCounters{
124 counters, err := DecodeCombinedCounters(testCounters)
126 Expect(err).Should(HaveOccurred())
127 Expect(err.Error()).To(ContainSubstring("unable to decode"))
128 Expect(counters).To(BeNil())