1 // Copyright (c) 2019 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.
15 // Package mock is an alternative VPP stats adapter aimed for unit/integration testing where the
16 // actual communication with VPP is not demanded.
21 "git.fd.io/govpp.git/adapter"
24 // implements StatsAPI
25 var _ adapter.StatsAPI = (*StatsAdapter)(nil)
27 // StatsAdapter simulates VPP stats socket from which stats can be read
28 type StatsAdapter struct {
29 entries []adapter.StatEntry
33 // NewStatsAdapter returns a new mock stats adapter.
34 func NewStatsAdapter() *StatsAdapter {
35 return &StatsAdapter{}
38 // Connect mocks client connection to the stats API.
39 func (a *StatsAdapter) Connect() error {
43 // Disconnect mocks client connection termination.
44 func (a *StatsAdapter) Disconnect() error {
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{
60 // DumpStats mocks all stat entries dump.
61 func (a *StatsAdapter) DumpStats(patterns ...string) ([]adapter.StatEntry, error) {
65 func (a *StatsAdapter) PrepareDir(prefixes ...string) (*adapter.StatDir, error) {
69 func (a *StatsAdapter) PrepareDirOnIndex(indexes ...uint32) (*adapter.StatDir, error) {
73 func (a *StatsAdapter) UpdateDir(dir *adapter.StatDir) error {
78 // MockStats sets mocked stat entries to be returned by DumpStats.
79 func (a *StatsAdapter) MockStats(stats []adapter.StatEntry) {
83 // MockStats sets mocked stat dir to be returned by PrepareDir.
84 func (a *StatsAdapter) MockDir(dir *adapter.StatDir) {