1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
20 from scp import SCPClient
22 from snaps.config.keypair import KeypairConfig
23 from snaps.config.network import PortConfig
24 from snaps.config.security_group import (
25 Direction, Protocol, SecurityGroupConfig, SecurityGroupRuleConfig)
26 from snaps.config.vm_inst import VmInstanceConfig, FloatingIpConfig
28 from snaps.openstack import create_flavor
29 from snaps.openstack import create_image
30 from snaps.openstack import create_instance
31 from snaps.openstack import create_keypairs
32 from snaps.openstack import create_network
33 from snaps.openstack import create_router
34 from snaps.openstack.create_security_group import OpenStackSecurityGroup
35 from snaps.openstack.tests import openstack_tests
36 from snaps.openstack.tests.create_instance_tests import check_dhcp_lease
37 from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase
38 from snaps.openstack.utils import nova_utils
39 from snaps.provisioning import ansible_utils
47 class AnsibleProvisioningTests(OSIntegrationTestCase):
49 Test for the CreateInstance class with two NIC/Ports, eth0 with floating IP
55 Instantiates the CreateImage object that is responsible for downloading
56 and creating an OS image file within OpenStack
58 super(self.__class__, self).__start__()
60 self.nova = nova_utils.nova_client(self.os_creds, self.os_session)
62 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
63 self.keypair_priv_filepath = 'tmp/' + guid
64 self.keypair_pub_filepath = self.keypair_priv_filepath + '.pub'
65 self.keypair_name = guid + '-kp'
66 self.vm_inst_name = guid + '-inst'
67 self.test_file_local_path = 'tmp/' + guid + '-hello.txt'
68 self.port_1_name = guid + '-port-1'
69 self.port_2_name = guid + '-port-2'
70 self.floating_ip_name = guid + 'fip1'
72 # Setup members to cleanup just in case they don't get created
73 self.inst_creator = None
74 self.keypair_creator = None
75 self.sec_grp_creator = None
76 self.flavor_creator = None
77 self.router_creator = None
78 self.network_creator = None
79 self.image_creator = None
83 os_image_settings = openstack_tests.ubuntu_image_settings(
84 name=guid + '-' + '-image',
85 image_metadata=self.image_metadata)
86 self.image_creator = create_image.OpenStackImage(
87 self.os_creds, os_image_settings)
88 self.image_creator.create()
90 # First network is public
91 self.pub_net_config = openstack_tests.get_pub_net_config(
92 project_name=self.os_creds.project_name,
93 net_name=guid + '-pub-net',
94 mtu=1442, subnet_name=guid + '-pub-subnet',
95 router_name=guid + '-pub-router',
96 external_net=self.ext_net_name)
98 self.network_creator = create_network.OpenStackNetwork(
99 self.os_creds, self.pub_net_config.network_settings)
100 self.network_creator.create()
103 self.router_creator = create_router.OpenStackRouter(
104 self.os_creds, self.pub_net_config.router_settings)
105 self.router_creator.create()
108 flavor_config = openstack_tests.get_flavor_config(
109 name=guid + '-flavor-name', ram=2048, disk=10,
110 vcpus=2, metadata=self.flavor_metadata)
112 self.flavor_creator = create_flavor.OpenStackFlavor(
113 self.admin_os_creds, flavor_config)
114 self.flavor_creator.create()
117 self.keypair_creator = create_keypairs.OpenStackKeypair(
118 self.os_creds, KeypairConfig(
119 name=self.keypair_name,
120 public_filepath=self.keypair_pub_filepath,
121 private_filepath=self.keypair_priv_filepath))
122 self.keypair_creator.create()
124 # Create Security Group
125 sec_grp_name = guid + '-sec-grp'
126 rule1 = SecurityGroupRuleConfig(
127 sec_grp_name=sec_grp_name, direction=Direction.ingress,
128 protocol=Protocol.icmp)
129 rule2 = SecurityGroupRuleConfig(
130 sec_grp_name=sec_grp_name, direction=Direction.ingress,
131 protocol=Protocol.tcp, port_range_min=22, port_range_max=22)
132 self.sec_grp_creator = OpenStackSecurityGroup(
135 name=sec_grp_name, rule_settings=[rule1, rule2]))
136 self.sec_grp_creator.create()
139 ports_settings = list()
140 ports_settings.append(
142 name=self.port_1_name,
143 network_name=self.pub_net_config.network_settings.name))
145 instance_settings = VmInstanceConfig(
146 name=self.vm_inst_name,
147 flavor=self.flavor_creator.flavor_settings.name,
148 port_settings=ports_settings,
149 floating_ip_settings=[FloatingIpConfig(
150 name=self.floating_ip_name, port_name=self.port_1_name,
151 router_name=self.pub_net_config.router_settings.name)])
153 self.inst_creator = create_instance.OpenStackVmInstance(
154 self.os_creds, instance_settings,
155 self.image_creator.image_settings,
156 keypair_settings=self.keypair_creator.keypair_settings)
163 Cleans the created objects
165 if self.inst_creator:
167 self.inst_creator.clean()
171 if self.sec_grp_creator:
173 self.sec_grp_creator.clean()
177 if self.keypair_creator:
179 self.keypair_creator.clean()
183 if self.flavor_creator:
185 self.flavor_creator.clean()
189 if os.path.isfile(self.keypair_pub_filepath):
191 os.remove(self.keypair_pub_filepath)
195 if os.path.isfile(self.keypair_priv_filepath):
197 os.remove(self.keypair_priv_filepath)
201 if self.router_creator:
203 self.router_creator.clean()
207 if self.network_creator:
209 self.network_creator.clean()
213 if self.image_creator and not self.image_creator.image_settings.exists:
215 self.image_creator.clean()
219 if os.path.isfile(self.test_file_local_path):
220 os.remove(self.test_file_local_path)
222 super(self.__class__, self).__clean__()
224 def test_apply_simple_playbook(self):
226 Tests application of an Ansible playbook that simply copies over a file
227 1. Have a ~/.ansible.cfg (or alternate means) to
228 set host_key_checking = False
229 2. Set the following environment variable in your executing shell:
230 ANSIBLE_HOST_KEY_CHECKING=False
231 Should this not be performed, the creation of the host ssh key will
232 cause your ansible calls to fail.
234 self.inst_creator.create(block=True)
236 priv_ip = self.inst_creator.get_port_ip(self.port_1_name)
237 self.assertTrue(check_dhcp_lease(self.inst_creator, priv_ip))
239 # Apply Security Group
240 self.inst_creator.add_security_group(
241 self.sec_grp_creator.get_security_group())
243 # Block until VM's ssh port has been opened
244 self.assertTrue(self.inst_creator.vm_ssh_active(block=True))
246 # Block until cloud-init has completed
247 self.assertTrue(self.inst_creator.cloud_init_complete(block=True))
249 ssh_client = self.inst_creator.ssh_client()
250 self.assertIsNotNone(ssh_client)
253 out = ssh_client.exec_command('pwd')[1].channel.in_buffer.read(
255 self.assertIsNotNone(out)
256 self.assertGreater(len(out), 1)
260 # Need to use the first floating IP as subsequent ones are currently
261 # broken with Apex CO
262 ip = self.inst_creator.get_floating_ip().ip
263 user = self.inst_creator.get_image_user()
264 priv_key = self.inst_creator.keypair_settings.private_filepath
266 relative_pb_path = pkg_resources.resource_filename(
267 'snaps.provisioning.tests.playbooks', 'simple_playbook.yml')
268 self.inst_creator.apply_ansible_playbook(relative_pb_path)
270 ssh = ansible_utils.ssh_client(
271 ip, user, private_key_filepath=priv_key,
272 proxy_settings=self.os_creds.proxy_settings)
273 self.assertIsNotNone(ssh)
276 scp = SCPClient(ssh.get_transport())
277 scp.get('~/hello.txt', self.test_file_local_path)
283 self.assertTrue(os.path.isfile(self.test_file_local_path))
288 with open(self.test_file_local_path) as test_file:
289 file_contents = test_file.readline()
290 self.assertEqual('Hello World!', file_contents)
295 def test_apply_template_playbook(self):
297 Tests application of an Ansible playbook that applies a template to a
299 1. Have a ~/.ansible.cfg (or alternate means) to set
300 host_key_checking = False
301 2. Set the following environment variable in your executing shell:
302 ANSIBLE_HOST_KEY_CHECKING=False
303 Should this not be performed, the creation of the host ssh key will
304 cause your ansible calls to fail.
306 self.inst_creator.create(block=True)
308 priv_ip = self.inst_creator.get_port_ip(self.port_1_name)
309 self.assertTrue(check_dhcp_lease(self.inst_creator, priv_ip))
311 # Apply Security Group
312 self.inst_creator.add_security_group(
313 self.sec_grp_creator.get_security_group())
315 # Block until VM's ssh port has been opened
316 self.assertTrue(self.inst_creator.vm_ssh_active(block=True))
318 # Block until cloud-init has completed
319 self.assertTrue(self.inst_creator.cloud_init_complete(block=True))
321 # Apply Security Group
322 self.inst_creator.add_security_group(
323 self.sec_grp_creator.get_security_group())
325 # Need to use the first floating IP as subsequent ones are currently
326 # broken with Apex CO
327 ip = self.inst_creator.get_floating_ip().ip
328 user = self.inst_creator.get_image_user()
329 priv_key = self.inst_creator.keypair_settings.private_filepath
331 relative_pb_path = pkg_resources.resource_filename(
332 'snaps.provisioning.tests.playbooks',
333 'template_playbook.yml')
334 self.inst_creator.apply_ansible_playbook(
335 relative_pb_path, variables={'name': 'Foo'})
337 ssh = ansible_utils.ssh_client(
338 ip, user, private_key_filepath=priv_key,
339 proxy_settings=self.os_creds.proxy_settings)
340 self.assertIsNotNone(ssh)
344 scp = SCPClient(ssh.get_transport())
345 scp.get('/tmp/hello.txt', self.test_file_local_path)
351 self.assertTrue(os.path.isfile(self.test_file_local_path))
355 with open(self.test_file_local_path) as test_file:
356 file_contents = test_file.readline()
357 self.assertEqual('Hello Foo!', file_contents)