forked from crowbar/barclamp-crowbar
-
Notifications
You must be signed in to change notification settings - Fork 5
/
node.erl
executable file
·141 lines (121 loc) · 5.87 KB
/
node.erl
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
% Copyright 2012, Dell
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
%
%
-module(node).
-export([step/3, json/3, validate/1, inspector/1, g/1]).
% Commont Routine
% Provide Feature scoped strings to DRY the code
g(Item) ->
case Item of
path -> "2.0/crowbar/2.0/node";
name -> "bdd1.example.com";
atom -> node1;
_ -> crowbar:g(Item)
end.
% Common Routine
% Makes sure that the JSON conforms to expectations (only tests deltas)
validate(JSON) ->
_Description = json:keyfind(JSON, description), % ADD CHECK!,
R =[bdd_utils:is_a(number, json:keyfind(JSON, order)),
bdd_utils:is_a(integer, json:keyfind(JSON, fingerprint)),
bdd_utils:is_a(boolean, json:keyfind(JSON, allocated)),
bdd_utils:is_a(string, json:keyfind(JSON, state)),
bdd_utils:is_a(boolean, json:keyfind(JSON,admin)),
bdd_utils:is_a(dbid, json:keyfind(JSON,os_id)),
crowbar_rest:validate(JSON)],
bdd_utils:assert(R).
validate_node_attribute(JSON) ->
R =[bdd_utils:is_a(JSON, dbid, node),
bdd_utils:is_a(JSON, dbid, attribute),
bdd_utils:is_a(JSON, string, name),
bdd_utils:is_a(JSON, str, description),
%bdd_utils:is_a(JSON, value, string),
length(JSON) =:= 4],
bdd_utils:assert(R, debug).
% Common Routine
% Returns list of nodes in the system to check for bad housekeeping
inspector(Config) ->
bdd_restrat:inspector(Config, nodes). % shared inspector works here, but may not always
% helpers
node_attribute_path(Node, Attribute) ->
NodePath = eurl:path(g(path), Node),
AttribPath = eurl:path(NodePath,"attrib"),
eurl:path(AttribPath,Attribute).
node_add_attribute(Config, Node, Attribute) ->
Path = node_attribute_path(Node, Attribute),
bdd_utils:log(Config, debug, "Node connect node+attributes ~p", [Path]),
eurl:put_post(Config, Path, [], post).
% Common Routine
% Creates JSON used for POST/PUT requests
json(Name, Description, Order) ->
json:output([{"name",Name},{"description", Description}, {"order", Order}]).
step(Config, _Given, {step_when, _N, ["REST gets the node list"]}) ->
bdd_restrat:step(Config, _Given, {step_when, _N, ["REST requests the",eurl:path(g(path),""),"page"]});
step(Config, _Given, {step_when, _N, ["REST gets the node",Name]}) ->
bdd_restrat:step(Config, _Given, {step_when, _N, ["REST requests the",eurl:path(g(path),Name),"page"]});
% Common Routine
% Validates the JSON returned by a test as part of general health tests
% Uses Feature validate, but through central routine
step(_Config, Result, {step_then, _N, ["the node is properly formatted"]}) ->
crowbar_rest:step(_Config, Result, {step_then, _N, ["the", nodes, "object is properly formatted"]});
step(Config, _Given, {step_when, _N, ["REST gets the node-attribute list for",Node]}) ->
Path = eurl:path(g(path), eurl:path(Node,"attrib")),
bdd_utils:log(Config, debug, "Nodes get node-attributes path ~p", [Path]),
eurl:get_ajax(Config, Path);
step(Config, _Given, {step_when, _N, ["REST assigns",attribute,Attribute,"to",node,Node]}) ->
R = node_add_attribute(Config, Node, Attribute),
{ajax, json:parse(R), {200, "not attribute assign step"}};
step(Config, _Global, {step_given, _N, [node,Node,"has",attribute, Attribute]}) ->
R = node_add_attribute(Config, Node, Attribute),
bdd_utils:log(Config, debug, "node: given node ~p has attribute ~p. Result ~p",[Node, Attribute, R]),
{ajax, R, {200, "node attribute assign step"}};
step(Config, Result, {step_then, _N, ["the result is a valid node-attribute json"]}) ->
bdd_utils:log(Config, x, "node: valid node-attribute? result ~p",[Result]),
JSON = bdd_restrat:get_JSON(Result),
bdd_utils:log(Config, debug, "node: valid node-attribute? json ~p",[JSON]),
validate_node_attribute(JSON);
step(Config, _Result, {step_then, _N, [node,Node,"has",attribute,Attribute]}) ->
Path = node_attribute_path(Node, Attribute),
R = eurl:get_page(Config, Path, all),
bdd_utils:log(Config, debug, "node node ~p has attribute ~p result ~p",[Node, Attribute, R]),
case R of
{200, _} -> true;
_ -> false
end;
% Common Routine
% Cleans up nodes that are created during tests
step(Config, _Given, {step_finally, _N, ["REST removes the node",Node]}) ->
bdd_restrat:destroy(Config, g(path), Node);
step(Config, _Given, {step_finally, _N, ["REST unassigns",attribute,Attribute,"from",node,Node]}) ->
NodePath = eurl:path(g(path), Node),
Path = eurl:path(NodePath,"attrib"),
bdd_utils:log(Config, debug, "Node disconnect node+attributes ~p ~p", [Path, Attribute]),
eurl:delete(Config, Path, Attribute);
step(Config, _Global, {step_setup, _N, _}) ->
% create attribute for tests
Attrib0 = attribute:json("bddtestglobal", g(description), 100),
C0 = bdd_restrat:create(Config, attribute:g(path), nodeattrib0, "bddtestglobal", Attrib0),
Attrib1 = attribute:json("bddtest1", g(description), 101),
C1 = bdd_restrat:create(C0, attribute:g(path), nodeattrib1, "bddtest1", Attrib1),
% create node(s) for tests
Node = json(g(name), g(description), 100),
C2 = bdd_restrat:create(C1, g(path), g(atom), g(name), Node),
node_add_attribute(C2, g(name), "bddtestglobal"),
C2;
step(Config, _Global, {step_teardown, _N, _}) ->
% find the node from setup and remove it
bdd_restrat:destroy(Config, g(path), g(atom)),
bdd_restrat:destroy(Config, attributes:g(path), nodeattrib0),
bdd_restrat:destroy(Config, attributes:g(path), nodeattrib1).