Change module name to go.fd.io/govpp
[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         "go.fd.io/govpp/adapter"
22 )
23
24 // implements StatsAPI
25 var _ adapter.StatsAPI = (*StatsAdapter)(nil)
26
27 // StatsAdapter simulates VPP stats socket from which stats can be read
28 type StatsAdapter struct {
29         entries []adapter.StatEntry
30         dir     *adapter.StatDir
31 }
32
33 // NewStatsAdapter returns a new mock stats adapter.
34 func NewStatsAdapter() *StatsAdapter {
35         return &StatsAdapter{}
36 }
37
38 // Connect mocks client connection to the stats API.
39 func (a *StatsAdapter) Connect() error {
40         return nil
41 }
42
43 // Disconnect mocks client connection termination.
44 func (a *StatsAdapter) Disconnect() error {
45         return nil
46 }
47
48 // ListStats mocks name listing for all stats.
49 func (a *StatsAdapter) ListStats(patterns ...string) ([]adapter.StatIdentifier, error) {
50         var statNames []adapter.StatIdentifier
51         for _, stat := range a.entries {
52                 statNames = append(statNames, adapter.StatIdentifier{
53                         Name:  stat.Name,
54                         Index: stat.Index,
55                 })
56         }
57         return statNames, nil
58 }
59
60 // DumpStats mocks all stat entries dump.
61 func (a *StatsAdapter) DumpStats(patterns ...string) ([]adapter.StatEntry, error) {
62         return a.entries, nil
63 }
64
65 func (a *StatsAdapter) PrepareDir(prefixes ...string) (*adapter.StatDir, error) {
66         return a.dir, nil
67 }
68
69 func (a *StatsAdapter) PrepareDirOnIndex(indexes ...uint32) (*adapter.StatDir, error) {
70         return a.dir, nil
71 }
72
73 func (a *StatsAdapter) UpdateDir(dir *adapter.StatDir) error {
74         *dir = *a.dir
75         return nil
76 }
77
78 // MockStats sets mocked stat entries to be returned by DumpStats.
79 func (a *StatsAdapter) MockStats(stats []adapter.StatEntry) {
80         a.entries = stats
81 }
82
83 // MockStats sets mocked stat dir to be returned by PrepareDir.
84 func (a *StatsAdapter) MockDir(dir *adapter.StatDir) {
85         a.dir = dir
86 }