9
9
from ensemble.lib.mocker import MATCH
10
10
from ensemble.lib.testing import TestCase
11
from ensemble.providers.common.launch import (
12
BOOTSTRAP_PACKAGES, DEFAULT_REPOSITORIES, DEFAULT_PACKAGES)
13
11
from ensemble.providers.ec2.machine import EC2ProviderMachine
14
from ensemble.state.auth import make_identity
16
from .common import EC2TestMixin, EC2MachineLaunchMixin, MATCH_AMI
13
from .common import EC2TestMixin, EC2MachineLaunchMixin
15
DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), "data")
19
18
class EC2BootstrapTest(EC2TestMixin, EC2MachineLaunchMixin, TestCase):
34
33
MATCH(match_string))
35
34
self.mocker.result(succeed(True))
37
def _mock_launch(self, machine_id):
36
def _mock_launch(self):
38
37
"""Mock launching a bootstrap machine on ec2."""
39
credentials = "admin:%s" % self.get_config()["admin-secret"]
40
admin_identity = make_identity(credentials)
42
38
def verify_user_data(data):
44
lines = data.split("\n")
45
self.assertEqual(lines.pop(0), "#cloud-config")
46
config = load("\n".join(lines))
47
repos = [dict(source=r) for r in DEFAULT_REPOSITORIES]
48
self.assertEqual(config["apt_sources"], repos)
51
list(DEFAULT_PACKAGES) + BOOTSTRAP_PACKAGES)
52
self.failUnlessIn("admin-identity", config["runcmd"][-3])
55
'ensemble-admin initialize --instance-id=%s'
56
' --admin-identity="%s"') % (
57
"$(curl http://169.254.169.254/1.0/meta-data/instance-id)",
60
self.assertEqual(config["runcmd"][-3], script)
63
"ENSEMBLE_MACHINE_ID=0 ENSEMBLE_ZOOKEEPER=localhost:2181 "
64
"python -m ensemble.agents.machine -n "
65
"--logfile=/var/log/ensemble/machine-agent.log "
66
"--pidfile=/var/run/ensemble/machine-agent.pid")
68
self.assertEqual(config["runcmd"][-2], script)
70
provision_agent_script = (
71
"ENSEMBLE_ZOOKEEPER=localhost:2181 "
72
"python -m ensemble.agents.provision -n "
73
"--logfile=/var/log/ensemble/provision-agent.log "
74
"--pidfile=/var/run/ensemble/provision-agent.pid")
75
self.assertEqual(config["runcmd"][-1], provision_agent_script)
39
expect_path = os.path.join(DATA_DIR, "bootstrap_cloud_init")
40
with open(expect_path) as f:
41
expect_cloud_init = f.read()
42
self.assertEquals(data, expect_cloud_init)
78
45
self.ec2.run_instances(
46
image_id="ami-default",
80
47
instance_type="m1.small",
84
"%s-%s" % ("ensemble", self.env_name),
85
"%s-%s-%s" % ("ensemble", self.env_name, machine_id)],
50
security_groups=["ensemble-moon", "ensemble-moon-0"],
86
51
user_data=MATCH(verify_user_data))
88
53
def test_launch_bootstrap(self):