~andreserl/pyjuju/cloud-init-orchestra-lp850260

« back to all changes in this revision

Viewing changes to ensemble/providers/ec2/tests/test_bootstrap.py

  • Committer: William Reade
  • Date: 2011-09-13 11:15:18 UTC
  • mfrom: (335.2.9 cloud-init-class-used)
  • Revision ID: fwereade@gmail.com-20110913111518-61neeujr0m73mbw0
merge lp:~fwereade/ensemble/cloud-init-class-used: machine_data is now more structured, and cloud-init data is generated from it in a more ensemble-specific way [f=820892][r=hazmat,niemeyer]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import logging
2
 
 
3
 
from yaml import dump, load
 
2
import os
 
3
from yaml import dump
4
4
 
5
5
from twisted.internet.defer import succeed
6
6
 
8
8
 
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
15
 
 
16
 
from .common import EC2TestMixin, EC2MachineLaunchMixin, MATCH_AMI
 
12
 
 
13
from .common import EC2TestMixin, EC2MachineLaunchMixin
 
14
 
 
15
DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), "data")
17
16
 
18
17
 
19
18
class EC2BootstrapTest(EC2TestMixin, EC2MachineLaunchMixin, TestCase):
34
33
            MATCH(match_string))
35
34
        self.mocker.result(succeed(True))
36
35
 
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)
41
 
 
42
38
        def verify_user_data(data):
43
 
 
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)
49
 
            self.assertEqual(
50
 
                config["packages"],
51
 
                list(DEFAULT_PACKAGES) + BOOTSTRAP_PACKAGES)
52
 
            self.failUnlessIn("admin-identity", config["runcmd"][-3])
53
 
 
54
 
            script = (
55
 
                'ensemble-admin initialize --instance-id=%s'
56
 
                ' --admin-identity="%s"') % (
57
 
                    "$(curl http://169.254.169.254/1.0/meta-data/instance-id)",
58
 
                    admin_identity)
59
 
 
60
 
            self.assertEqual(config["runcmd"][-3], script)
61
 
 
62
 
            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")
67
 
 
68
 
            self.assertEqual(config["runcmd"][-2], script)
69
 
 
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)
76
43
            return True
77
44
 
78
45
        self.ec2.run_instances(
79
 
            image_id=MATCH_AMI,
 
46
            image_id="ami-default",
80
47
            instance_type="m1.small",
81
48
            max_count=1,
82
49
            min_count=1,
83
 
            security_groups=[
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))
87
52
 
88
53
    def test_launch_bootstrap(self):
98
63
        self._mock_create_group()
99
64
        self._mock_create_machine_group(0)
100
65
        self._mock_launch_utils(region="us-east-1")
101
 
        self._mock_launch(0)
 
66
        self._mock_launch()
102
67
        self.mocker.result(succeed([]))
103
68
        self._mock_save()
104
69
        self.mocker.replay()
128
93
            SecurityGroup("ensemble-%s" % self.env_name, "")]))
129
94
        self._mock_create_machine_group(0)
130
95
        self._mock_launch_utils(region="us-east-1")
131
 
        self._mock_launch(0)
 
96
        self._mock_launch()
132
97
        self.mocker.result(succeed([]))
133
98
        self._mock_save()
134
99
        self.mocker.replay()
177
142
            SecurityGroup("ensemble-%s" % self.env_name, "")]))
178
143
        self._mock_create_machine_group(0)
179
144
        self._mock_launch_utils(region="us-east-1")
180
 
        self._mock_launch(0)
 
145
        self._mock_launch()
181
146
        self.mocker.result(succeed([self.get_instance("i-foobar")]))
182
147
        self._mock_save()
183
148
        self.mocker.replay()