forked from go-chef/chef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_test.go
122 lines (104 loc) · 2.7 KB
/
node_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
package chef
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"reflect"
"testing"
)
var (
testNodeJSON = "test/node.json"
)
func TestNodeFromJSONDecoder(t *testing.T) {
if file, err := os.Open(testNodeJSON); err != nil {
t.Error("unexpected error", err, "during os.Open on", testNodeJSON)
} else {
dec := json.NewDecoder(file)
var n Node
if err := dec.Decode(&n); err == io.EOF {
log.Println(n)
} else if err != nil {
log.Fatal(err)
}
}
}
func TestNode_NewNode(t *testing.T) {
n := NewNode("testnode")
expect := Node{
Name: "testnode",
Environment: "_default",
ChefType: "node",
JsonClass: "Chef::Node",
}
if !reflect.DeepEqual(n, expect) {
t.Errorf("NewNode returned %+v, want %+v", n, expect)
}
}
func TestNodesService_Methods(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/nodes", func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "GET":
fmt.Fprintf(w, `{"node1":"https://chef/nodes/node1", "node2":"https://chef/nodes/node2"}`)
case r.Method == "POST":
fmt.Fprintf(w, `{ "uri": "http://localhost:4545/nodes/node1" }`)
}
})
mux.HandleFunc("/nodes/node1", func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "GET" || r.Method == "PUT":
fmt.Fprintf(w, `{
"name": "node1",
"json_class": "Chef::Node",
"chef_type": "node",
"chef_environment": "development"
}`)
case r.Method == "DELETE":
}
})
// Test list
nodes, err := client.Nodes.List()
if err != nil {
t.Errorf("Nodes.List returned error: %v", err)
}
listWant := map[string]string{"node1": "https://chef/nodes/node1", "node2": "https://chef/nodes/node2"}
if !reflect.DeepEqual(nodes, listWant) {
t.Errorf("Nodes.List returned %+v, want %+v", nodes, listWant)
}
// test Get
node, err := client.Nodes.Get("node1")
if err != nil {
t.Errorf("Nodes.Get returned error: %v", err)
}
wantNode := NewNode("node1")
wantNode.Environment = "development"
if !reflect.DeepEqual(node, wantNode) {
t.Errorf("Nodes.Get returned %+v, want %+v", node, wantNode)
}
// test Post
res, err := client.Nodes.Post(wantNode)
if err != nil {
t.Errorf("Nodes.Post returned error: %s", err.Error())
}
postResult := &NodeResult{"http://localhost:4545/nodes/node1"}
if !reflect.DeepEqual(postResult, res) {
t.Errorf("Nodes.Post returned %+v, want %+v", res, postResult)
}
// test Put
putRes, err := client.Nodes.Put(node)
if err != nil {
t.Errorf("Nodes.Put returned error", err)
}
if !reflect.DeepEqual(putRes, node) {
t.Errorf("Nodes.Post returned %+v, want %+v", putRes, node)
}
// test Delete
err = client.Nodes.Delete(node.Name)
if err != nil {
t.Errorf("Nodes.Delete returned error", err)
}
}