Last active
March 22, 2018 08:43
-
-
Save apahim/89385db0f0fc6eecb2287b61e407b13e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from avocado import main | |
from avocado.utils import vmimage | |
from avocado.utils import git | |
from avocado.utils import process | |
from avocado.utils import path as utils_path | |
from avocado_qemu import QemuTest | |
from avocado_qemu import GetConsole | |
class TestMinishift(QemuTest): | |
""" | |
:avocado: enable | |
""" | |
def setUp(self): | |
# Deploy minishift from Host to Guest so Ansible doesn't have | |
# to download it later. If you have minishift.iso cached, this will | |
# speed up the test. | |
self.deploy_minishift = self.params.get('deploy_minishift', | |
default=False) | |
if self.deploy_minishift: | |
minishift_iso_url = ('http://artifacts.ci.centos.org/' | |
'fedora-atomic/minishift/iso/minishift.iso') | |
self.minishift_iso = self.fetch_asset(minishift_iso_url, expire='7d') | |
# Get the Repository/branch containing the Ansible Playbooks | |
repo = self.params.get('repo', default='https://github.com/' | |
'CentOS-PaaS-SIG/contra-env-setup.git') | |
branch = self.params.get('branch', default='master') | |
git.get_repo(repo, | |
branch=branch, | |
destination_dir=self.workdir) | |
# Get/Set VM Image | |
distro = self.params.get('distro', default='Fedora') | |
image = vmimage.get(distro) | |
self.vm.add_image(image.path, cloudinit=True) | |
# Grown up the VM Image disk size | |
qemu_img_bin = utils_path.find_command('qemu-img') | |
process.run('%s resize %s 50G' % (qemu_img_bin, image.path)) | |
# VM Network | |
self.vm.args.extend(['-device', 'virtio-net-pci,id=avocado_nic,netdev=device_avocado_nic']) | |
self.vm.args.extend(['-netdev', 'user,id=device_avocado_nic,hostfwd=tcp::12345-:22']) | |
# VM CPU/Memmory | |
self.vm.args.extend(['-m', '8000']) | |
self.vm.args.extend(['-cpu', 'host']) | |
self.vm.args.extend(['-accel', 'kvm']) | |
# Headless VM | |
self.vm.args.extend(['-display', 'none', '-vga', 'none']) | |
# Run VM | |
self.vm.launch() | |
# Wait VM to be UP | |
with GetConsole(self.vm): | |
pass | |
def test(self): | |
# Gerenal Ansible Configuration | |
env = {'ANSIBLE_CONFIG': 'ansible.cfg'} | |
ssh_args = ['-o UserKnownHostsFile=/dev/null', | |
'-o StrictHostKeyChecking=no'] | |
# VM Preparation Using Ansible AdHoc command | |
prep_cmd = ['resize2fs /dev/sda1', | |
'dnf install -y python libselinux-python', | |
'dnf update -y nettle'] | |
# Add minishift.iso preparation mommands if we are going to | |
# deploy minishift | |
if self.deploy_minishift: | |
prep_cmd.extend(['mkdir /home/avocado/minishift', | |
'chown avocado:avocado /home/avocado/minishift']) | |
# Execute Ansible AdHoc preparation commands | |
cmd = ('ansible localhost -i "localhost," --become -m raw ' | |
'-a "{prep_cmd}" ' | |
'--user {username} ' | |
'--ssh-common-args="{ssh_args}"') | |
cmd = cmd.format(prep_cmd='; '.join(prep_cmd), | |
username=self.vm.username, | |
ssh_args=' '.join(ssh_args)) | |
process.run(cmd, env=env) | |
# Another Ansible AdHoc command to deploy minishift.iso | |
if self.deploy_minishift: | |
cmd = ('ansible localhost -i "localhost," ' | |
'-m copy -a "src={minishift_iso} ' | |
'dest=~/minishift/minishift.iso" ' | |
'--user {username} ' | |
'--ssh-common-args="{ssh_args}" -vvv') | |
cmd = cmd.format(minishift_iso=self.minishift_iso, | |
username=self.vm.username, | |
ssh_args=' '.join(ssh_args)) | |
process.run(cmd, env=env) | |
# Main Ansible Playbook execution | |
cmd = ('ansible-playbook -i "localhost," playbooks/setup.yml ' | |
'-e remote_user=%s --ssh-common-args="%s"' % | |
(self.vm.username, ' '.join(ssh_args))) | |
process.run(cmd, env=env) | |
def tearDown(self): | |
self.vm.shutdown() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment