/
test_cmd_basic.py
155 lines (135 loc) · 5.34 KB
/
test_cmd_basic.py
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
import json
from poni import tool
from helper import *
single_file_plugin_text = """
from poni import config
class PlugIn(config.PlugIn):
def add_actions(self):
self.add_file("%(source)s", dest_path="%(dest)s")
"""
class TestCommands(Helper):
def init_repo(self):
repo = self.temp_file()
poni = tool.Tool(default_repo_path=repo)
assert not poni.run(["init"])
config = json.load(file(repo / "repo.json"))
assert isinstance(config, dict)
return poni, repo
def test_add_node(self):
poni, repo = self.init_repo()
nodes = ["foo", "bar/foo/baz"]
for node in nodes:
poni.run(["add-node", node])
node_config = repo / "system" / node / "node.json"
config = json.load(file(node_config))
print node, config
assert isinstance(config, dict)
assert config["host"] == ""
def test_create(self):
poni, repo = self.init_repo()
nodes = ["foo", "bar/foo/baz"]
for node in nodes:
poni.run(["add-system", node])
node_config = repo / "system" / node / "system.json"
config = json.load(file(node_config))
print node, config
assert isinstance(config, dict)
assert config == {}
def test_set(self):
poni, repo = self.init_repo()
node = "test"
assert not poni.run(["add-node", node])
vals = {
"foo": "bar",
"a": "b=c",
"int": ("int:1", 1),
"float": ("float:2", 2.0),
"str": ("str:123", "str:123"),
"b0": ("bool:0", False),
"b1": ("bool:1", True),
"bt": ("bool:true", True),
"bf": ("bool:false", False),
"bon": ("bool:on", True),
"boff": ("bool:off", False),
}
node_config = repo / "system" / node / "node.json"
for key, val in vals.iteritems():
if isinstance(val, (str, unicode)):
inval = val
outval = val
else:
inval, outval = val
set_str = "%s=%s" % (key, inval)
assert not poni.run(["set", node, set_str])
config = json.load(file(node_config))
assert config[key] == outval
assert not poni.run(["set", node, "one.two.three.four=five", "-v"])
config = json.load(file(node_config))
assert config["one"]["two"]["three"]["four"] == "five"
def test_list(self):
poni, repo = self.init_repo()
node = "test"
conf = "conf"
assert not poni.run(["add-node", node])
assert not poni.run(["add-config", node, conf])
assert not poni.run(["set", node, "cloud.blah=foo"])
flags = ["--systems", "--config", "--controls", "--tree",
"--node-prop", "--cloud", "--query-status", "--config-prop",
"--inherits"]
for combo in combos(flags):
cmd = ["list"]
cmd.extend(combo)
print cmd
assert not poni.run(cmd)
def test_script(self):
for combo in combos(["--verbose"]):
poni, repo = self.init_repo()
node = "test"
script_file = self.temp_file()
file(script_file, "w").write(
"add-node %s\nset %s foo=bar" % (node, node))
assert not poni.run(["script", script_file])
node_config = repo / "system" / node / "node.json"
config = json.load(file(node_config))
assert config["foo"] == "bar"
# TODO: stdin version of "script" command
def test_verify_no_content(self):
poni, repo = self.init_repo()
node = "test"
conf = "conf"
assert not poni.run(["add-node", node])
assert not poni.run(["add-config", node, conf])
assert not poni.run(["verify"])
assert not poni.run(["deploy"])
assert not poni.run(["audit"])
def test_config_inherit(self):
poni, repo = self.init_repo()
# add template config
template_node = "tnode"
template_conf = "tconf"
assert not poni.run(["add-node", template_node])
assert not poni.run(["set", template_node, "verify=bool:off"])
assert not poni.run(["add-config", template_node, template_conf])
# write template config plugin.py
tconf_path = "%s/%s" % (template_node, template_conf)
conf_dir = repo / "system" / template_node / "config" / template_conf
tplugin_path = conf_dir / "plugin.py"
output_file = self.temp_file()
tfile = "test.txt"
args = dict(source=tfile, dest=output_file)
tplugin_path.open("w").write(single_file_plugin_text % args)
# write template config template file
tfile_path = conf_dir / tfile
template_text = "hello"
tfile_path.open("w").write(template_text)
# add inherited config
instance_node = "inode"
instance_conf = "iconf"
assert not poni.run(["add-node", instance_node])
assert not poni.run(["set", instance_node, "host=@local"])
assert not poni.run(["add-config", instance_node, instance_conf,
"--inherit", tconf_path])
# deploy and verify
assert not poni.run(["deploy"])
output = output_file.bytes()
assert output == template_text