-
Notifications
You must be signed in to change notification settings - Fork 31
/
export_test.go
202 lines (178 loc) · 5.64 KB
/
export_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// Copyright (c) 2015-2016, Arista Networks, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of Arista Networks nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARISTA NETWORKS
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
package goeapi
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path"
"testing"
"time"
)
const checkMark = "\u2713"
const xMark = "\u2717"
var fixturesPath = ""
func init() {
workingDir, err := os.Getwd()
if err != nil {
panic(err)
}
fixturesPath = path.Join(workingDir, "/testdata/fixtures")
rand.Seed(time.Now().UTC().UnixNano())
}
/*
****************************************************************************
*
* DummyEapiConnection is a Dummy connection object that adheres to the
* EapiConnection Inteface. The Execute() method below (currently) returns
* a non-error with allocated JSONRPCResponse so the upper layer API can
* be tested. Commands received by this DummyConnection are cached and retreived
* to compare to what would be sent.
*
* Note:
* Execute() clears the the previous cached list of commands and replaces
* with current command list.
*
****************************************************************************
*/
type DummyEapiConnection struct {
EapiConnection
Commands []interface{}
retError bool
}
func NewDummyEapiConnection(transport string, host string, username string,
password string, port int) *DummyEapiConnection {
conn := EapiConnection{}
return &DummyEapiConnection{EapiConnection: conn, retError: false}
}
func (conn *DummyEapiConnection) Execute(commands []interface{},
encoding string) (*JSONRPCResponse, error) {
if conn.retError {
conn.retError = false
err := fmt.Errorf("Mock Error")
conn.SetError(err)
return &JSONRPCResponse{}, err
}
conn.ClearError()
conn.Commands = nil
conn.Commands = append(conn.Commands, commands...)
resp := &JSONRPCResponse{
Result: make([]map[string]interface{}, len(commands)),
}
if encoding == "json" {
return resp, nil
}
for idx := range resp.Result {
resp.Result[idx] = make(map[string]interface{})
resp.Result[idx]["output"] = ""
}
if encoding == "text" && len(commands) >= 2 &&
(commands[1] == "show running-config all" ||
commands[1] == "show startup-config") {
resp.Result[1]["output"] = LoadFixtureFile("running_config.text")
}
return resp, nil
}
func (conn *DummyEapiConnection) setReturnError(enable bool) {
conn.retError = enable
}
func (conn *DummyEapiConnection) decodeJSONFile(r io.Reader) *JSONRPCResponse {
dec := json.NewDecoder(r)
var v JSONRPCResponse
if err := dec.Decode(&v); err != nil {
panic(err)
}
return &v
}
// Retreive the cached list of commands from the connection.
func (conn *DummyEapiConnection) GetCommands() []interface{} {
return conn.Commands
}
var runConf string
var dummyNode *Node
var dummyConnection *DummyEapiConnection
var duts []*Node
// Setup/Teardown
func TestMain(m *testing.M) {
runConf = GetFixture("running_config.text")
LoadConfig(GetFixture("dut.conf"))
conns := Connections()
fmt.Println("Connections: ", conns)
for _, name := range conns {
if name != "localhost" {
node, _ := ConnectTo(name)
duts = append(duts, node)
}
}
// Create a Node with a DummyConnection to be used in
// UnitTests.
dummyConnection = NewDummyEapiConnection("", "", "", "", 0)
dummyNode = &Node{}
dummyNode.SetAutoRefresh(false)
dummyNode.SetConnection(dummyConnection)
os.Exit(m.Run())
}
// GetFixturesPath aquires the global fixtures path
func GetFixturesPath() string {
return fixturesPath
}
// GetFixture returns the full path to filenmae within
// fixtures
func GetFixture(filename string) string {
return path.Join(fixturesPath, filename)
}
// LoadFixtureFile reads the fixtures file into a string.
func LoadFixtureFile(file string) string {
if data, err := ioutil.ReadFile(GetFixture(file)); err == nil {
return string(data)
}
return ""
}
// RandomInt randomly creates a int between
// min and max
func RandomInt(min int, max int) int {
return min + rand.Intn(max-min)
}
const charBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// RandomString randomly creates a string within the
// range of minchar to maxchar
func RandomString(minchar int, maxchar int) string {
size := RandomInt(minchar, maxchar)
bytes := make([]byte, size)
for i := 0; i < size; i++ {
bytes[i] = charBytes[rand.Intn(len(charBytes)-1)]
}
return string(bytes)
}