added mock adapter for new VPP stats
[govpp.git] / adapter / mock / mock_stats_adapter.go
1 // Copyright (c) 2019 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 mock is an alternative VPP stats adapter aimed for unit/integration testing where the
16 // actual communication with VPP is not demanded.
17
18 package mock
19
20 import (
21         "git.fd.io/govpp.git/adapter"
22 )
23
24 // StatsAdapter simulates VPP stats socket from which stats can be read
25 type StatsAdapter struct {
26         entries []*adapter.StatEntry
27 }
28
29 // NewStatsAdapter returns a new mock stats adapter.
30 func NewStatsAdapter() *StatsAdapter {
31         return &StatsAdapter{}
32 }
33
34 // Connect mocks client connection to the stats API.
35 func (a *StatsAdapter) Connect() error {
36         return nil
37 }
38
39 // Disconnect mocks client connection termination.
40 func (a *StatsAdapter) Disconnect() error {
41         return nil
42 }
43
44 // ListStats mocks name listing for all stats.
45 func (a *StatsAdapter) ListStats(patterns ...string) ([]string, error) {
46         var statNames []string
47         for _, stat := range a.entries {
48                 statNames = append(statNames, stat.Name)
49         }
50         return statNames, nil
51 }
52
53 // DumpStats mocks all stat entries dump.
54 func (a *StatsAdapter)  DumpStats(patterns ...string) ([]*adapter.StatEntry, error) {
55         return a.entries, nil
56 }
57
58 // MockStats replaces current values of all supported stats by provided value
59 func (a *StatsAdapter) MockStats(stats []*adapter.StatEntry) {
60         a.entries = stats
61 }