-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands_int_test.go
More file actions
119 lines (101 loc) · 2.66 KB
/
commands_int_test.go
File metadata and controls
119 lines (101 loc) · 2.66 KB
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
//go:build unit
package srljrpc
import (
"encoding/json"
"testing"
"github.com/azyablov/srljrpc/actions"
"github.com/azyablov/srljrpc/datastores"
"github.com/google/go-cmp/cmp"
)
func Test_rawSetCommand(t *testing.T) {
var mockSetCmd = &Command{
Path: "/interface[name={name}]/description",
Value: "This is a test.",
Action: &actions.Action{},
Datastore: &datastores.Datastore{},
}
var expected = []byte(`{
T: "path": "/interface[name={name}]/description",
T: "value": "This is a test.",
T: "path-keywords": {
T: "name": "mgmt0"
T: },
T: "recursive": false,
T: "include-field-defaults": true,
T: "action": "update",
T: "datastore": "candidate"
T:}`)
err := mockSetCmd.withDatastore(datastores.CANDIDATE)
if err != nil {
t.Fatal(err)
}
mockSetCmd.withoutRecursion()
mockSetCmd.withDefaults()
err = mockSetCmd.withPathKeywords([]byte(`{"name": "mgmt0"}`))
if err != nil {
t.Fatal(err)
}
err = mockSetCmd.SetAction(actions.UPDATE)
if err != nil {
t.Fatal(err)
}
b, err := json.MarshalIndent(mockSetCmd, "T:", " ")
if err != nil {
t.Fatal(err)
}
// uncomment for debug
// t.Log(string(b))
if out := cmp.Diff(string(b), string(expected)); out != "" {
t.Log(out)
t.Fatalf("expected: %s, got: %s", string(expected), string(b))
}
}
func Test_rawGetCommand(t *testing.T) {
var mockGetCmd = &Command{
Path: "/interface[name={name}]/description",
Action: &actions.Action{},
Datastore: &datastores.Datastore{},
}
var expected = []byte(`{
T: "path": "/interface[name={name}]/description",
T: "path-keywords": {
T: "name": "mgmt0"
T: },
T: "datastore": "running"
T:}`)
err := mockGetCmd.withDatastore(datastores.RUNNING)
if err != nil {
t.Fatal(err)
}
err = mockGetCmd.SetAction(actions.NONE)
if err != nil {
t.Fatal(err)
}
err = mockGetCmd.withPathKeywords([]byte(`{"name": "mgmt0"}`))
if err != nil {
t.Fatal(err)
}
b, err := json.MarshalIndent(mockGetCmd, "T:", " ")
if err != nil {
t.Fatal(err)
}
if out := cmp.Diff(string(b), string(expected)); out != "" {
t.Log(out)
t.Fatalf("\nexpected: %s, \ngot: %s", string(expected), string(b))
}
}
func Test_withPathKeywords(t *testing.T) {
var mockGetCmd = &Command{
Path: "/interface[name={name}]/description",
Datastore: &datastores.Datastore{},
}
err := mockGetCmd.withPathKeywords([]byte(`{"name": "mgmt0", "name1": "mgmt1", "name2" "mgmt2"}`))
//t.Logf("expected error: %s", err)
if err == nil {
t.Fatalf("expected error 'failed to unmarshal path-keywords', got nil")
}
err = mockGetCmd.withPathKeywords([]byte(`{"name": "mgmt0", "name1": "mgmt1", "name2": "mgmt2"}`))
if err != nil {
t.Fatalf("expected nil, got %s", err)
}
}