1 # Copyright (c) 2016 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.
19 from snaps.openstack import create_instance
20 from snaps.openstack import create_keypairs
21 from snaps.openstack import create_network
22 from snaps.openstack import create_router
23 from snaps.openstack import create_image
24 from snaps.openstack import create_flavor
25 from scp import SCPClient
27 from snaps.provisioning import ansible_utils
28 from snaps.openstack.tests import openstack_tests
29 from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase
37 class AnsibleProvisioningTests(OSIntegrationTestCase):
39 Test for the CreateInstance class with two NIC/Ports, eth0 with floating IP and eth1 w/o
44 Instantiates the CreateImage object that is responsible for downloading and creating an OS image file
47 super(self.__class__, self).__start__()
49 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
50 self.keypair_priv_filepath = 'tmp/' + guid
51 self.keypair_pub_filepath = self.keypair_priv_filepath + '.pub'
52 self.keypair_name = guid + '-kp'
53 self.vm_inst_name = guid + '-inst'
54 self.test_file_local_path = 'tmp/' + guid + '-hello.txt'
55 self.port_1_name = guid + '-port-1'
56 self.port_2_name = guid + '-port-2'
57 self.floating_ip_name = guid + 'fip1'
59 # Setup members to cleanup just in case they don't get created
60 self.inst_creator = None
61 self.keypair_creator = None
62 self.flavor_creator = None
63 self.router_creator = None
64 self.network_creator = None
65 self.image_creator = None
69 os_image_settings = openstack_tests.ubuntu_url_image(name=guid + '-' + '-image')
70 self.image_creator = create_image.OpenStackImage(self.os_creds, os_image_settings)
71 self.image_creator.create()
73 # First network is public
74 self.pub_net_config = openstack_tests.get_pub_net_config(
75 net_name=guid + '-pub-net', subnet_name=guid + '-pub-subnet',
76 router_name=guid + '-pub-router', external_net=self.ext_net_name)
78 self.network_creator = create_network.OpenStackNetwork(self.os_creds, self.pub_net_config.network_settings)
79 self.network_creator.create()
82 self.router_creator = create_router.OpenStackRouter(self.os_creds, self.pub_net_config.router_settings)
83 self.router_creator.create()
86 self.flavor_creator = create_flavor.OpenStackFlavor(
88 create_flavor.FlavorSettings(name=guid + '-flavor-name', ram=2048, disk=10, vcpus=2))
89 self.flavor_creator.create()
92 self.keypair_creator = create_keypairs.OpenStackKeypair(
93 self.os_creds, create_keypairs.KeypairSettings(
94 name=self.keypair_name, public_filepath=self.keypair_pub_filepath,
95 private_filepath=self.keypair_priv_filepath))
96 self.keypair_creator.create()
99 ports_settings = list()
100 ports_settings.append(
101 create_network.PortSettings(name=self.port_1_name,
102 network_name=self.pub_net_config.network_settings.name))
104 instance_settings = create_instance.VmInstanceSettings(
105 name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=ports_settings,
106 floating_ip_settings=[create_instance.FloatingIpSettings(
107 name=self.floating_ip_name, port_name=self.port_1_name,
108 router_name=self.pub_net_config.router_settings.name)])
110 self.inst_creator = create_instance.OpenStackVmInstance(
111 self.os_creds, instance_settings, self.image_creator.image_settings,
112 keypair_settings=self.keypair_creator.keypair_settings)
113 except Exception as e:
115 raise Exception(e.message)
119 Cleans the created objects
121 if self.inst_creator:
122 self.inst_creator.clean()
124 if self.keypair_creator:
125 self.keypair_creator.clean()
127 if self.flavor_creator:
128 self.flavor_creator.clean()
130 if os.path.isfile(self.keypair_pub_filepath):
131 os.remove(self.keypair_pub_filepath)
133 if os.path.isfile(self.keypair_priv_filepath):
134 os.remove(self.keypair_priv_filepath)
136 if self.router_creator:
137 self.router_creator.clean()
139 if self.network_creator:
140 self.network_creator.clean()
142 if self.image_creator:
143 self.image_creator.clean()
145 if os.path.isfile(self.test_file_local_path):
146 os.remove(self.test_file_local_path)
148 super(self.__class__, self).__clean__()
150 def test_apply_simple_playbook(self):
152 Tests application of an Ansible playbook that simply copies over a file:
153 1. Have a ~/.ansible.cfg (or alternate means) to set host_key_checking = False
154 2. Set the following environment variable in your executing shell: ANSIBLE_HOST_KEY_CHECKING=False
155 Should this not be performed, the creation of the host ssh key will cause your ansible calls to fail.
157 self.inst_creator.create(block=True)
159 # Block until VM's ssh port has been opened
160 self.assertTrue(self.inst_creator.vm_ssh_active(block=True))
162 ssh_client = self.inst_creator.ssh_client()
163 self.assertIsNotNone(ssh_client)
164 out = ssh_client.exec_command('pwd')[1].channel.in_buffer.read(1024)
165 self.assertIsNotNone(out)
166 self.assertGreater(len(out), 1)
168 # Need to use the first floating IP as subsequent ones are currently broken with Apex CO
169 ip = self.inst_creator.get_floating_ip().ip
170 user = self.inst_creator.get_image_user()
171 priv_key = self.inst_creator.keypair_settings.private_filepath
173 retval = ansible_utils.apply_playbook('provisioning/tests/playbooks/simple_playbook.yml', [ip], user, priv_key,
174 proxy_setting=self.os_creds.proxy_settings)
175 self.assertEquals(0, retval)
177 ssh = ansible_utils.ssh_client(ip, user, priv_key, self.os_creds.proxy_settings)
178 self.assertIsNotNone(ssh)
179 scp = SCPClient(ssh.get_transport())
180 scp.get('~/hello.txt', self.test_file_local_path)
182 self.assertTrue(os.path.isfile(self.test_file_local_path))
184 with open(self.test_file_local_path) as f:
185 file_contents = f.readline()
186 self.assertEquals('Hello World!', file_contents)
188 def test_apply_template_playbook(self):
190 Tests application of an Ansible playbook that applies a template to a file:
191 1. Have a ~/.ansible.cfg (or alternate means) to set host_key_checking = False
192 2. Set the following environment variable in your executing shell: ANSIBLE_HOST_KEY_CHECKING=False
193 Should this not be performed, the creation of the host ssh key will cause your ansible calls to fail.
195 self.inst_creator.create(block=True)
197 # Block until VM's ssh port has been opened
198 self.assertTrue(self.inst_creator.vm_ssh_active(block=True))
200 # Need to use the first floating IP as subsequent ones are currently broken with Apex CO
201 ip = self.inst_creator.get_floating_ip().ip
202 user = self.inst_creator.get_image_user()
203 priv_key = self.inst_creator.keypair_settings.private_filepath
205 ansible_utils.apply_playbook('provisioning/tests/playbooks/template_playbook.yml', [ip], user, priv_key,
206 variables={'name': 'Foo'}, proxy_setting=self.os_creds.proxy_settings)
208 ssh = ansible_utils.ssh_client(ip, user, priv_key, self.os_creds.proxy_settings)
209 self.assertIsNotNone(ssh)
210 scp = SCPClient(ssh.get_transport())
211 scp.get('/tmp/hello.txt', self.test_file_local_path)
213 self.assertTrue(os.path.isfile(self.test_file_local_path))
215 with open(self.test_file_local_path) as f:
216 file_contents = f.readline()
217 self.assertEquals('Hello Foo!', file_contents)