Recognize stat_dir_type_empty
[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 // 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) ([]string, error) {
50         var statNames []string
51         for _, stat := range a.entries {
52                 statNames = append(statNames, string(stat.Name))
53         }
54         return statNames, nil
55 }
56
57 // DumpStats mocks all stat entries dump.
58 func (a *StatsAdapter) DumpStats(patterns ...string) ([]adapter.StatEntry, error) {
59         return a.entries, nil
60 }
61
62 func (a *StatsAdapter) PrepareDir(prefixes ...string) (*adapter.StatDir, error) {
63         return a.dir, nil
64 }
65
66 func (a *StatsAdapter) UpdateDir(dir *adapter.StatDir) error {
67         *dir = *a.dir
68         return nil
69 }
70
71 // MockStats sets mocked stat entries to be returned by DumpStats.
72 func (a *StatsAdapter) MockStats(stats []adapter.StatEntry) {
73         a.entries = stats
74 }
75
76 // MockStats sets mocked stat dir to be returned by PrepareDir.
77 func (a *StatsAdapter) MockDir(dir *adapter.StatDir) {
78         a.dir = dir
79 }