def setUp(self): """ setUp function for Ruby Utils test, this setsup the ruby version as defined in RUBY_VERSION_FILE and GEMS list as defined in Root and CCS directories. """ ctx = Context() self.public_cert = os.path.join(ctx.reporoot_path(), TestEncUtils.PUBLIC_CERT) self.private_key = os.path.join(ctx.reporoot_path(), TestEncUtils.PRIVATE_KEY) self.data = "alpha" if not os.path.isfile(self.public_cert): self.fail("Setup FAILS as the test public cert is unavilable") if not os.path.isfile(self.private_key): self.fail("Setup FAILS as the test private key is unavilable")
def setUp(self): """setUp function for context attribute <> clone latest ccsbuildtools """ self.ctx = Context() returncode, username = helper_utils.get_gitusername(self.ctx.path) if returncode > 0: helper_utils.get_loginusername() service_utils.sync_service(self.ctx.path, "master", username, "ccs-build-tools")
def __init__(self, *args, **kwargs): # set up the environment python style :-) os.environ["OS_AUTH_URL"] = "https://us-rdu-3.cisco.com:5000/v2.0" os.environ["OS_TENANT_NAME"] = "jenkins-slab" os.environ["OS_TENANT_ID"] = "dc4b64c3ddcc4ce5abbddd43a24b1b0a" os.environ["OS_USERNAME"] = "******" os.environ["OS_PASSWORD"] = "******" os.environ["OS_REGION_NAME"] = "us-rdu-3" self.ctx = Context() super(TestSLABNetworking, self).__init__(*args, **kwargs)
def setUp(self): """ setUp function for gerrit functions """ self.sortTestMethodsUsing = None self.ctx = Context() self.ctx.debug = True self.user = "******" self.prjname = "testproject" self.prjdir = "/tmp/" + self.prjname self.hostname = self.ctx.get_gerrit_server()['hostname'] self.port = self.ctx.get_gerrit_server()['port'] self.testrepo = "testproject" service_utils.run_this("cd /tmp;" "git clone ssh://{}@{}:{}/{}".format(self.user, self.hostname, self.port, self.prjname)) service_utils.run_this("cd {};git checkout develop;".format(self.prjdir)) with open("{}/test.py".format(self.prjdir), "a") as tfile: tfile.write("print 'Hello world'") cmdrt, cmdrtstr = service_utils.run_this("cd {};".format(self.prjdir) + "git add test.py;" "git commit -m 'one additional line';") cmd = "cd {};git review develop".format(self.prjdir) cmdrt, cmdrtstr = service_utils.run_this(cmd) if cmdrt: click.echo("unable to perform setup for the test") click.echo("test failed") self.fail(cmdrtstr) mtch = re.search("(https://.*/)([0-9]+)", cmdrtstr) if not (mtch and mtch.group(2)): click.echo("unable to find match in the string\n{}".format(cmdrtstr)) self.fail("test failed: unable to determine changeset") self.review = mtch.group(2)
def tearDownClass(cls): """ Perform cleanup """ click.echo('Cleaning up all the VMs :') ctx = Context() vm_connection = vagrant_utils.Connect_to_vagrant(vm_name="infra-001", path=ctx.path) try: statuses = vm_connection.v.status() for status in statuses: vm_connection.v.destroy(status[0]) click.echo("Destroyed VM name : {} ".format(status[0])) except CalledProcessError: click.echo("Could not perform VM cleanup. Exiting")
def setUp(self): """ setUp function for context attribute <> clone latest ccsbuildtools """ self.sortTestMethodsUsing = None self.name = "puppettest" self.roles = ["role1", "role2"] self.chk_script = "ansible.chk" self.gsrvr = Context().get_gerrit_staging_server() blder = create_repo.Repo.builder("Puppet", self.gsrvr, self.name) blder.chk_script = self.chk_script blder.play_roles = self.roles self.user = blder.get_usr() self.reponame = blder.get_reponame() # make sure nothing exist if os.path.exists(self.reponame): shutil.rmtree(self.reponame) blder.construct()
def setUp(self): """ setUp function for Ruby Utils test, this setsup the ruby version as defined in RUBY_VERSION_FILE and GEMS list as defined in Root and CCS directories. """ ctx = Context() path_to_reporoot = os.path.split(ctx.path) path_to_reporoot = os.path.split(path_to_reporoot[0]) path_to_reporoot = path_to_reporoot[0] # setup the gem's ruby_utils.setup_gems(ctx.path) ruby_utils.setup_gems(ctx.path, 0) # setup the gem's ruby_utils.setup_gems(ctx.path, 1) ruby_utils.setup_gems(ctx.path, 0) try: self.ruby_version = None with open( os.path.join(path_to_reporoot, TestRubyUtils.RUBY_VERSION_FILE)) as f: self.ruby_version = f.readlines()[0].strip()[5:10] self.gems = [] with open( os.path.join(path_to_reporoot, TestRubyUtils.ROOT_GEMFILE)) as f: self.gems = self._list_of_gems(f) with open(os.path.join(path_to_reporoot, TestRubyUtils.CCS_GEMFILE)) as f: self.gems = self.gems + self._list_of_gems(f) except IOError as e: self.Fail( 1, 0, "Setup FAILS b/c can't access ruby-version, ccs-data/Gemfile.")
def setUp(self): """ Setup variables required to test the os_provider functions """ self.ctx = Context()
class TestCCsDataUtils(unittest.TestCase): """ TestCCsDataUtils class is a unittest class for ccsdata_utils. It gets all the data avialable in the .stack directory. It uses ccsdata_utils to gets all the sites information in the data structure and verifies if the data associated with the given site svl-pod-1 is available or not. Also checks if the environment associated with the site exist or not. Attributes: ctx: Context object of servicelab module. tempdir: Temporary directory generated by tempfile to use for simulated ccs-data """ ctx = Context() tempdir = tempfile.mkdtemp() def setUp(self): """ setUp function of the TestCCsDataUtils class, it sets up the simulated ccs-data directories and host.yaml files. """ self.site_env_path = os.path.join(self.tempdir, 'environments') self.site_to_check = "svl-svc-1" self.hostsd_path = os.path.join(self.site_env_path, 'test_env', 'hosts.d') if not os.path.exists(self.hostsd_path): os.makedirs(self.hostsd_path) self.flavors_list = [] for i in range(1, 11): flavor = 'fake-flavor-' + str(i).zfill(3) self.flavors_list.append(flavor) hostname = 'fake-host-name-' + str(i).zfill(3) + '.yaml' host_file = os.path.join(self.hostsd_path, hostname) host_data = { 'deploy_args': { 'flavor': flavor, } } with open(host_file, 'w') as output_file: output_file.write(yaml.dump(host_data, default_flow_style=False)) self.flavors_list.sort() def test_ccs_site_exist(self): """ The test_ccs_site_exist is a test case to check if site_to_check is available amongs the sites which we get by using ccsdata_utilsr:.list_envs_or_sites function. If the assertion fails then test fails. """ return_code, sites = ccsdata_utils.list_envs_or_sites(self.ctx.path) self.assertIsNotNone(sites[self.site_to_check]) def test_ccs_site_env_exist(self): """ The test_ccs_site_env_exist is a test case to check if environment for site_to_check is available amongs the sites which we get by using ccsdata_utilsr:list_envs_or_sites function. If the assertion fails then test fails. The assertion can fail for number of reasons : 1. The site_to_check site is not in the list returned by ccsdata_utilsr:list_envs_or_sites. 2. If we do not have an environment for the the given site_to_check site. 3. If the data exist but is not in the directory as supplied by (path, site, environemt). """ return_code, sites = ccsdata_utils.list_envs_or_sites(self.ctx.path) self.assertIsNotNone(sites[self.site_to_check]) envs = sites[self.site_to_check] self.assertIsNotNone(envs) # we expect atleast one environment to exist in svl-pod-1 env = envs.keys()[0] self.assertIsNotNone(env) print env path = ccsdata_utils.get_env_for_site_path(self.ctx.path, self.site_to_check, env) self.assertTrue(os.path.isdir(path)) def test_get_flavors_from_site(self): """ The test_get_flavors_from_site is a test case to check if deploy_args['flavors'] are properly extracted from ccs-data for a specified site. """ site_flavors = ccsdata_utils.get_flavors_from_site(self.site_env_path) self.assertEqual(self.flavors_list, site_flavors)
1. Creating the project on the gerrit server. 2. Downloading the template. 3. Instantiating the template to the correct value. 4. Creating the nimbus """ slab_logger.log(15, 'Constructing the empty project') try: if self.check(): return self.create_project() self.download_template() self.instantiate_template() self.create_nimbus() except Exception: raise # # This is the driver stub # if __name__ == '__main__': from servicelab.stack import Context Repo.builder("Puppet", Context().get_gerrit_staging_server(), "gamma").construct() Repo.builder("Ansible", Context().get_gerrit_staging_server(), "gamma").construct() Repo.builder("Project", Context().get_gerrit_staging_server(), "gamma").construct() Repo.builder("EmptyProject", Context().get_gerrit_staging_server(), "gamma").construct()
class TestVagrantFileUtils(unittest.TestCase): """ TestVagrantFileUtils class is a unittest class for vagrantfile_utils. Attributes: """ ctx = Context() host_var_tempdir = tempfile.mkdtemp() def setUp(self): """ Setup variables required to test the os_provider functions """ self.box = 'http://cis-kickstart.cisco.com/ccs-rhel-7.box' self.float_net = 'public-floating-602' self.networks = [{'name': 'SLAB_test_mgmt'}, {'name': 'SLAB_test_lab'}] self.parsed_nets = "[{name: 'SLAB_test_mgmt'}," + \ "{name: 'SLAB_test_lab', address: '192.168.100.6'}]" self.noaddr_nets = "[{name: 'SLAB_test_mgmt'}," + \ "{name: 'SLAB_test_lab', address: " self.yaml_nested_path = 'services/ccs-data/sites/ccs-dev-1/environments/' + \ 'dev-tenant/hosts.d/' self.test_yaml_dir = os.path.join(self.host_var_tempdir, self.yaml_nested_path) self.vagrant_file = os.path.join(self.host_var_tempdir, 'Vagrantfile') self.vf_utils = vagrantfile_utils.SlabVagrantfile( self.host_var_tempdir) os.makedirs(self.test_yaml_dir) self.vagrant_data = """\ # -*- mode: ruby -*- # vi: set ft=ruby : VAGRANTFILE_API_VERSION = "2" required_plugins = %w( vagrant-hostmanager vagrant-openstack-provider ) required_plugins.each do |plugin| system "vagrant plugin install #{plugin}" unless Vagrant.has_plugin? plugin end #---------------- Setup to pass the envirionment variable ------------------- def get_rcmd(rcmd, variable) if ENV[variable] if rcmd rcmd << " " end value = ENV[variable] rcmd = rcmd + "export #{variable}=#{value}" end return rcmd end def get_rcmdlst(rcmd) env_var_cmd = "" if rcmd env_var_cmd = <<CMD echo "#{rcmd}" | tee -a /home/vagrant/.bash_profile CMD end return env_var_cmd end remote_cmd = "" remote_cmd = get_rcmd(remote_cmd, 'HEIGHLINER_DEPLOY_TARGET_HOSTS') remote_cmd = get_rcmd(remote_cmd, 'HEIGHLINER_DEPLOY_TAGS') remote_cmd = get_rcmd(remote_cmd, 'CCS_ENVIRONMENT') env_var_cmd = get_rcmdlst(remote_cmd) heighliner_script = <<SCRIPT #{env_var_cmd} SCRIPT #---------------- END ------------------------------------------------------- Vagrant.configure(VAGRANTFILE_API_VERSION) do |cluster| """ self.vm_yaml_data = ''' deploy_args: flavor: 4cpu.8ram.20-96sas image: RHEL-7 groups: - virtual hostname: test_neutron_api.service.cloud.com role: tenant_neutron_api server: 10.10.10.10 type: virtual ''' self.test_host = 'test_host' self.test_yaml_file = os.path.join(self.test_yaml_dir, self.test_host + '.yaml') with open(self.test_yaml_file, 'w') as yaml: yaml.write(self.vm_yaml_data) self.host_vars = {'flavor': '4cpu.8ram.20-96sas', 'image': 'RHEL-7'} self.host_dict = { self.test_host: { 'box': self.box, 'domain': '1', 'ip': '192.168.100.6', 'mac': '020027000006', 'memory': '1024', 'profile': 'null', 'role': 'none', } } os.environ['OS_USERNAME'] = '******' os.environ['OS_PASSWORD'] = '******' os.environ['OS_AUTH_URL'] = 'http://slab.cisco.com:5000/v2.0/' os.environ['OS_TENANT_NAME'] = 'dev-tenant' def tearDown(self): """ Tear down variables and files created to test the os_provider functions """ shutil.rmtree(self.host_var_tempdir) os.environ['OS_USERNAME'] = '' os.environ['OS_PASSWORD'] = '' os.environ['OS_AUTH_URL'] = '' os.environ['OS_TENANT_NAME'] = '' def test_init_vagrantfile(self): """ Tests the init_vagrantfile method """ self.vf_utils.init_vagrantfile() with open(self.vagrant_file, 'r') as f: my_data = f.read() self.assertEqual(my_data, self.vagrant_data + '\n') def test_write_it(self): """ Tests the write_it method """ data_1 = 'first line\n' data_2 = 'second line\n' data_3 = 'another line\n' data_4 = 'I forgot where I was going with this\n' all_data = data_1 + data_2 + data_3 + data_4 + '\n' self.vf_utils.write_it(data_1, data_2, data_3, data_4) with open(self.vagrant_file, 'r') as f: my_data = f.read() self.assertEqual(my_data, all_data) def test_append_it(self): """ Test the append_it method """ data_1 = 'first new line\n' data_2 = 'second new line\n' data_3 = 'Do you see what I did there?\n' data_4 = 'Because I do not\n' all_data = self.vagrant_data + data_1 + data_2 + data_3 + data_4 + 'end\nend\n' with open(self.vagrant_file, 'w') as f: f.write(self.vagrant_data) f.write('\n') self.vf_utils.append_it(data_1, data_2, data_3, data_4) with open(self.vagrant_file, 'r') as f: my_data = f.read() self.assertEqual(my_data, all_data) def test_add_virtualbox_vm(self): """ Test the add_virtualbox_vm method """ compare_data = str( self.vagrant_data + 'cluster.vm.define \"' + self.test_host + '\" do |config|\n' ' config.hostmanager.enabled = true\n' ' config.hostmanager.include_offline = true\n' ' config.vm.box = "' + self.box + '"\n' ' config.vm.provider :virtualbox do |vb, override|\n' ' vb.customize ["modifyvm", :id, "--memory", "1024"]\n' ' end\n config.vm.hostname = "test_host"\n' ' config.vm.network :private_network, ip: "192.168.100.6", ' 'mac: "020027000006"\n' ' config.vm.provision "shell", path: "provision/infra.sh"\n' ' config.vm.provision:shell, :inline => heighliner_script\n' ' config.vm.provision "shell", path: "provision/node.sh"\n' ' config.vm.provision "file", source: "provision/ssh-config",' 'destination:"/home/vagrant/.ssh/config"\n' ' config.vm.provision "file", source: "hosts", destination: ' '"/etc/hosts"\n' ' config.vm.synced_folder "services", "/opt/ccs/services"\n' 'end\nend\n') with open(self.vagrant_file, 'w') as f: f.write(self.vagrant_data) f.write('\n') self.vf_utils.add_virtualbox_vm(self.host_dict) with open(self.vagrant_file, 'r') as f: file_data = f.read() self.assertEqual(file_data, compare_data) def test_add_openstack_vm(self): """ Test the add_openstack_vm method """ compare_data = ( 'cluster.vm.define "' + self.test_host + '" do |config|\n' ' cluster.ssh.username = \'cloud-user\' \n' ' config.hostmanager.enabled = true\n' ' config.hostmanager.include_offline = true\n' ' config.vm.provider :openstack do |os, override|\n' ' os.openstack_auth_url = "http://slab.cisco.com:5000/v2.0/"\n' ' os.username = "******"\n' ' os.password = "******"\n' ' os.tenant_name = "dev-tenant"\n' ' os.flavor = "4cpu.8ram.20-96sas"\n' ' os.image = "RHEL-7"\n' ' os.floating_ip_pool = "public-floating-602"\n' ' os.openstack_network_url= "http://slab.cisco.com:9696/v2.0"\n' ' os.openstack_image_url = "http://slab.cisco.com:9292/v2/"\n' ' os.networks = ' + self.parsed_nets + '\n' ' os.security_groups = [{name: \'default\'}]\n' ' override.vm.box = "openstack"\n' ' end\n' ' config.vm.provision "shell", path: "provision/infra-OS.sh"\n' ' config.vm.provision "shell", path: "provision/node-OS.sh"\n' ' config.vm.provision "file", source: "provision/ssh-config", ' 'destination:"/home/cloud-user/.ssh/config"\n' ' config.vm.provision "file", source: "hosts", destination: ' '"/etc/hosts"\n' ' config.vm.synced_folder "services", "/opt/ccs/services/"\n') compare_data = self.vagrant_data + compare_data + 'end\nend\n' with open(self.vagrant_file, 'w') as f: f.write(self.vagrant_data) f.write('end\n') sec_groups = [{'name': "default"}] self.vf_utils.set_env_vars(self.float_net, self.networks, sec_groups) self.vf_utils.add_openstack_vm(self.host_dict) with open(self.vagrant_file, 'r') as f: file_data = f.read() self.assertEqual(file_data, compare_data) def test_env_vars_settings(self): """ Test the set_env_vars method """ compare_data = { 'username': '******', 'openstack_auth_url': 'http://slab.cisco.com:5000/v2.0/', 'tenant_name': 'dev-tenant', 'floating_ip_pool': self.float_net, 'openstack_image_url': 'http://slab.cisco.com:9292/v2/', 'openstack_network_url': 'http://slab.cisco.com:9696/v2.0', 'password': '******', 'networks': self.noaddr_nets, 'security_groups': "[{name: 'default'}]" } sec_groups = [{'name': "default"}] self.vf_utils.set_env_vars(self.float_net, self.networks, sec_groups) self.assertEqual(self.vf_utils.env_vars, compare_data) def test_multiple_networks_data(self): """ Test the get_multiple_networks method """ net = self.vf_utils.get_multiple_networks(self.networks) self.assertEqual(net, self.noaddr_nets) def test_securitygroups_namelst_fetch(self): """ Test the get_securitygroups_namelst method """ compare_data = "[{name: 'default'},{name: 'something'},{name: 'myfancysecgroup'}]" sec_groups = [{ 'name': 'default', 'something': 'stuff' }, { 'name': 'something' }, { 'name': 'myfancysecgroup' }] parsed_groups = self.vf_utils.get_securitygroups_namelst(sec_groups) self.assertEqual(parsed_groups, compare_data) def test_host_image_flavors_setting(self): """ Test the set_host_image_flavors method """ compare_data = {'image': 'slab-RHEL7.1v9', 'flavor': '2cpu.4ram.20sas'} self.vf_utils.hostname = self.test_host self.vf_utils.set_host_image_flavors(self.ctx.path) self.assertEqual(self.vf_utils.host_vars, compare_data)
import os import re import sys import yaml import socket import ipaddress import service_utils import logger_utils from servicelab import settings from servicelab.stack import Context slab_logger = logger_utils.setup_logger(settings.verbosity, 'stack.utils.tc_vm_yaml_create') ctx = Context() def open_yaml(filename): """Opens yaml file into dictionary Args: filename {str}: File path and name to open Returns: {dict}: PyYaml extracted dict of data from the opened file Example Usage: file_data = open_yaml('/some/path/to/my/environment.yaml') """ try:
class TestGerrtFunctions(unittest.TestCase): """ TestGerritFunctions class is a unittest class testing the various gerrit api's including the 1. Check the incoming queue 2. Check the outgoing queue 3. Check if the particualr review exist in the repository 4. Check if -1 can be given for the changes. 5. Check if +1 can be given for the changes. 6. Check if the changes can be merged (+2 +1) The staging server is used for testing the changes. A sample project testproject has been created on teh staging server. This project is cloned on the tmp directory. A line is added to the test.py. After the locally committing the file the test is run to check the input, output queues and finally various review numbers are performed to finally abandon or approve the merge. """ def setUp(self): """ setUp function for gerrit functions """ self.sortTestMethodsUsing = None self.ctx = Context() self.ctx.debug = True self.user = "******" self.prjname = "testproject" self.prjdir = "/tmp/" + self.prjname self.hostname = self.ctx.get_gerrit_server()['hostname'] self.port = self.ctx.get_gerrit_server()['port'] self.testrepo = "testproject" service_utils.run_this("cd /tmp;" "git clone ssh://{}@{}:{}/{}".format(self.user, self.hostname, self.port, self.prjname)) service_utils.run_this("cd {};git checkout develop;".format(self.prjdir)) with open("{}/test.py".format(self.prjdir), "a") as tfile: tfile.write("print 'Hello world'") cmdrt, cmdrtstr = service_utils.run_this("cd {};".format(self.prjdir) + "git add test.py;" "git commit -m 'one additional line';") cmd = "cd {};git review develop".format(self.prjdir) cmdrt, cmdrtstr = service_utils.run_this(cmd) if cmdrt: click.echo("unable to perform setup for the test") click.echo("test failed") self.fail(cmdrtstr) mtch = re.search("(https://.*/)([0-9]+)", cmdrtstr) if not (mtch and mtch.group(2)): click.echo("unable to find match in the string\n{}".format(cmdrtstr)) self.fail("test failed: unable to determine changeset") self.review = mtch.group(2) def tearDown(self): """ teardown function removes the directory /tmp/prjdir """ if self.review: gfn = gerrit_functions.GerritFns(self.user, self.prjname, self.ctx) gfn.print_gerrit("", self.review, self.user, "", "abandoned") shutil.rmtree(self.prjdir) def test_review(self): """ check if we get the review """ gfn = gerrit_functions.GerritFns(self.user, self.prjname, self.ctx) gerrit_functions.GerritFns.instrument_code = True rev = gfn.print_gerrit("", self.review, self.user, "", "") self.assertIsNotNone(rev, "Unable to find the review item {}".format(self.review)) def test_review_number(self): """ test the review number """ gfn = gerrit_functions.GerritFns(self.user, self.prjname, self.ctx) gerrit_functions.GerritFns.instrument_code = True gfn.change_review(self.review, 1, 0, "reviewed") rev = gfn.print_gerrit("", self.review, self.user, "", "") self.assertEqual(rev["currentPatchSet"]["approvals"][0]["value"], "1", "unable to change the code review value") def test_abandon(self): """ test if able to abandon """ gfn = gerrit_functions.GerritFns(self.user, self.prjname, self.ctx) gerrit_functions.GerritFns.instrument_code = True gfn.code_state(self.review, "abandon", "abandon by unit test") rev = gfn.print_gerrit("", self.review, self.user, "", "abandoned") if rev: self.review = None self.assertIsNotNone(rev, "Unable to find the review item {}".format(self.review))
class TestServiceUtils(unittest.TestCase): """ Test all of the functions in servicelab/utils/service_utils.py Attributes: ctx: Click module context object from servicelab/stack.py """ ctx = Context() username = ctx.username if re.search('sdlc-\d+', socket.gethostname()): username = '******' def setUp(self): """ Setup a separate temporary directory to use for each test """ with temporary_dir() as temp_dir: self.temp_dir = temp_dir def tearDown(self): shutil.rmtree(self.temp_dir) def test_sync_service(self): """ Test the sync_service function Ensure that a git repo is successfully synced Ensure that a nonexistant git repo is not successfully synced """ sync_service_out = service_utils.sync_service( path=self.temp_dir, branch='master', username=self.username, service_name='service-horizon') self.assertEqual(sync_service_out, True) sync_service_out = service_utils.sync_service( path=self.temp_dir, branch='master', username=self.username, service_name='service-fakeservice') self.assertEqual(sync_service_out, False) def test_build_data(self): """ Test the build_data function Ensure that ccs-data is not installed Ensure that lightfuse.rb is able to build the ccs-dev-1 site """ build_data_return, build_data_out = service_utils.build_data( self.temp_dir) self.assertEqual(build_data_return, 1) self.assertTrue('Could not find ccs-data' in build_data_out) shutil.copytree(os.path.join(self.ctx.path, 'provision'), os.path.join(self.temp_dir, 'provision')) service_utils._git_clone(path=self.temp_dir, branch='master', username=self.username, service_name='ccs-data') build_data_return, build_data_out = service_utils.build_data( self.temp_dir) self.assertEqual(build_data_return, 0) self.assertFalse(build_data_out) def test_copy_certs(self): """ Test the copy_certs function Ensure that trying to copy to an invalid directory fails Ensure that the cert files copy to the specified directory """ src_path = os.path.join(self.ctx.path, 'provision') dst_path = os.path.join(self.temp_dir, 'fake_dir') copy_certs_return = service_utils.copy_certs(src_path, dst_path) self.assertEqual(copy_certs_return, 1) check_path = os.path.join(self.temp_dir, 'modules', 'ccs', 'files', 'certs', 'dev-csi-a') os.makedirs(check_path) copy_certs_return = service_utils.copy_certs(src_path, self.temp_dir) self.assertEqual(copy_certs_return, 0) for pem_file in [ 'ccsapi.dev-csi-a.cis.local.pem', 'meter.dev-csi-a.cis.local.pem', 'ha_dev-csi-a.cis.local.pem', 'ha_storage.dev-csi-a.cis.local.pem' ]: self.assertTrue(os.path.isfile(os.path.join(check_path, pem_file))) def test_git_clone(self): """ Test the _git_clone function Ensure failure with bad repo/branch names Ensure success with valide repo/branch names """ git_clone_return, git_clone_data = service_utils._git_clone( path=self.temp_dir, branch='fake-branch', username=self.username, service_name='fake-repo') self.assertEqual(git_clone_return, 1) repo_dir = os.path.join(self.temp_dir, 'services', 'fake-repo') self.assertFalse(os.path.isdir(repo_dir)) git_clone_return, git_clone_data = service_utils._git_clone( path=self.temp_dir, branch='master', username=self.username, service_name='service-horizon') self.assertEqual(git_clone_return, 0) repo_dir = os.path.join(self.temp_dir, 'services', 'service-horizon') self.assertTrue(os.path.isdir(repo_dir)) def test_git_pull_ff(self): """ Test the _git_pull_ff function Clone a repo Ensure failure of fast forward pull with bad branch name Ensure success of fast forward pull with valid repo/branch names """ git_clone_return, git_clone_data = service_utils._git_clone( path=self.temp_dir, branch='master', username=self.username, service_name='service-horizon') git_pull_return, git_pull_output = service_utils._git_pull_ff( path=self.temp_dir, branch='service-horizon', service_name='fake-repo') self.assertEqual(git_pull_return, 1) git_pull_return, git_pull_output = service_utils._git_pull_ff( path=self.temp_dir, branch='master', service_name='service-horizon') self.assertEqual(git_pull_return, 0) def test_check_for_cmd(self): """ Test the _check_for_cmd function, which checks if the specified command is installed Ensure fake-command is not installed Ensure git is installed """ check_cmd_return, check_cmd_output = service_utils._check_for_cmd( 'fake-command') self.assertEqual(check_cmd_return, 1) check_cmd_return, check_cmd_output = service_utils._check_for_cmd( 'git') self.assertEqual(check_cmd_return, 0) def test_setup_vagrant_sshkeys(self): """ Test the setup_vagrant_sshkeys function, which gens vagrant keys if they are not already installed """ service_utils.setup_vagrant_sshkeys(self.temp_dir) key_path = os.path.join(self.temp_dir, 'id_rsa') self.assertEqual(os.path.isfile(key_path), True) self.assertEqual(os.path.isfile(key_path + '.pub'), True) first_key_hash = hashlib.md5(open(key_path, 'rb').read()).hexdigest() service_utils.setup_vagrant_sshkeys(self.temp_dir) second_key_hash = hashlib.md5(open(key_path, 'rb').read()).hexdigest() self.assertEqual(first_key_hash, second_key_hash) def test_link(self): """ Test the link function, which links the current-service dir to services/repo_name Ensure failure of invalid data Ensure successful symlink of valid data """ link_return = service_utils.link(path=self.temp_dir, service_name='fake-repo', branch='fake-branch', username=self.username) self.assertEqual(link_return, 1) link_return = service_utils.link(path=self.temp_dir, service_name='service-horizon', branch='master', username=self.username) self.assertEqual(link_return, 0) def test_clean(self): """ Test the clean function, which deletes the current file and unlinks current_service Clone and symlink a repo to current-service, and set as current Ensure current file and current-service symlink exist Ensure current file and current-service symlink are removed """ sync_service_return = service_utils.sync_service( path=self.temp_dir, branch='master', username=self.username, service_name='service-horizon') link_return = service_utils.link(path=self.temp_dir, service_name='service-horizon', branch='master', username=self.username) self.assertEqual(link_return, 0) self.assertTrue( os.path.islink(os.path.join(self.temp_dir, 'current_service'))) self.assertTrue(os.path.isfile(os.path.join(self.temp_dir, 'current'))) service_utils.clean(self.temp_dir) self.assertFalse( os.path.islink(os.path.join(self.temp_dir, 'current_service'))) self.assertFalse(os.path.isfile(os.path.join(self.temp_dir, 'current'))) def test_check_service(self): """ Test the check_service function, which checks gerrit for the specified repo Ensure failure of invalid repo Emsure success of valid repo """ check_service_return = service_utils.check_service( path=self.temp_dir, service_name='fake_repo') self.assertEqual(check_service_return, 1) check_service_return = service_utils.check_service( path=self.temp_dir, service_name='service-horizon') self.assertEqual(check_service_return, 0) def test_run_this(self): """ Test the run_this function, which runs a shell command Ensure failure of invalid command Ensure success of valid command """ run_this_return, run_this_output = service_utils.run_this( 'fake-command') self.assertTrue(run_this_return != 0) run_this_return, run_this_output = service_utils.run_this('type bash') self.assertEqual(run_this_return, 0) def test_installed(self): """ Test the installed function, which checks if the specified repo is cloned locally Ensure failure with fake repo Ensure success with valid repo """ sync_service_return = service_utils.sync_service( path=self.temp_dir, branch='master', username=self.username, service_name='service-horizon') sync_service_return = service_utils.link( path=self.temp_dir, service_name='service-horizon', branch='master', username=self.username) installed_return = service_utils.installed('fake-service', self.temp_dir) self.assertFalse(installed_return) installed_return = service_utils.installed('service-horizon', self.temp_dir) self.assertTrue(installed_return)
class TestStackUp(unittest.TestCase): """ TestStackUp class is a unittest class for testing the 'stack up' commands """ ctx = Context() def setUp(self): """ Build site data needed to test both vagrant.yaml and vm host.yaml creation """ self.site = 'ccs-dev-1' self.tenant = 'dev-tenant' self.hosts_path = os.path.join(self.ctx.path, 'services', 'ccs-data', 'sites', self.site, 'environments', self.tenant, 'hosts.d') self.subnet = '192.168.100.0/24' self.addr_subnet = ipaddress.IPv4Network(unicode(self.subnet)) os.environ['OS_USERNAME'] = '******' os.environ['OS_PASSWORD'] = '******' os.environ['OS_REGION_NAME'] = 'us-rdu-3' os.environ['OS_AUTH_URL'] = 'https://us-rdu-3.cisco.com:5000/v2.0/' os.environ['OS_TENANT_NAME'] = 'jenkins-slab' os.environ['OS_TENANT_ID'] = 'dc4b64c3ddcc4ce5abbddd43a24b1b0a' # Preserve existing data self.vagrant_yaml = os.path.join(self.ctx.path, 'vagrant.yaml') if os.path.isfile(self.vagrant_yaml): self.vagrant_bak = os.path.join(self.ctx.path, 'vagrant.bak') os.rename(self.vagrant_yaml, self.vagrant_bak) self.Vagrant_file = os.path.join(self.ctx.path, 'Vagrantfile') if os.path.isfile(self.Vagrant_file): self.Vagrant_bak = os.path.join(self.ctx.path, 'Vagrantfile.bak') os.rename(self.Vagrant_file, self.Vagrant_bak) self.dotvagrant_dir = os.path.join(self.ctx.path, '.vagrant') if os.path.isdir(self.dotvagrant_dir): self.dotvagrant_bak = os.path.join(self.ctx.path, '.vagrant_bak') os.rename(self.dotvagrant_dir, self.dotvagrant_bak) if not os.path.isdir( os.path.join(self.ctx.path, 'services', 'ccs-data')): service_utils.sync_service(self.ctx.path, 'master', self.ctx.username, 'ccs-data') env_path = os.path.join(self.ctx.path, 'services', 'ccs-data', 'sites', self.site, 'environments', self.tenant) self.hosts_path = os.path.join(env_path, 'hosts.d') self.backup_path = os.path.join(env_path, 'hosts.bak') os.makedirs(self.backup_path) for f in os.listdir(self.hosts_path): file_name = os.path.join(self.hosts_path, f) file_bak = os.path.join(self.backup_path, f) os.rename(file_name, file_bak) # Generate files to consume IPs .5 - .14 for i in range(1, 11): hostname = 'service-holder-' + str(i).zfill(3) + '.yaml' output_file = os.path.join(self.hosts_path, hostname) file_data = { 'interfaces': { 'eth0': { 'ip_address': str(self.addr_subnet.network_address + 4 + i), }, }, } with open(output_file, 'w') as outfile: outfile.write(yaml.dump(file_data, default_flow_style=False)) def tearDown(self): """ Remove the temp directory and files """ for f in os.listdir(self.hosts_path): file_name = os.path.join(self.hosts_path, f) os.remove(file_name) for f in os.listdir(self.backup_path): file_bak = os.path.join(self.backup_path, f) file_name = os.path.join(self.hosts_path, f) os.rename(file_bak, file_name) shutil.rmtree(self.backup_path) if hasattr(self, 'vagrant_bak'): os.rename(self.vagrant_bak, self.vagrant_yaml) elif os.path.isfile(self.vagrant_yaml): os.remove(self.vagrant_yaml) if hasattr(self, 'Vagrant_bak'): os.rename(self.Vagrant_bak, self.Vagrant_file) elif os.path.isfile(self.Vagrant_file): os.remove(self.Vagrant_file) if os.path.isdir(self.dotvagrant_dir): shutil.rmtree(self.dotvagrant_dir) if hasattr(self, 'dotvagrant_bak'): os.rename(self.dotvagrant_bak, self.dotvagrant_dir) self.host = '' os.environ['OS_USERNAME'] = '' os.environ['OS_PASSWORD'] = '' os.environ['OS_REGION_NAME'] = '' os.environ['OS_AUTH_URL'] = '' os.environ['OS_TENANT_NAME'] = '' os.environ['OS_TENANT_ID'] = '' def cmd_up_runner(self, args, hostname, group, remote): """ Run the 'stack up' command for each of the various tests Args: args {list}: CLI args hostname {str}: Name of the vm group {str}: Name of the group the vm belongs to remote {bool}: True - OpenStack hypervisor False - VirtualBox hypervisor Returns: Nothing. Runs tests based on the environment setup Example Usage: self.cmd_up_runner(['--rhel7'], 'rhel7-001', False) """ if not group == 'rhel7': repo_name = str('service-' + group) if not os.path.isdir( os.path.join(self.ctx.path, 'services', repo_name)): service_utils.sync_service(self.ctx.path, 'master', self.ctx.username, repo_name) runner = CliRunner() host_yaml = hostname + '.yaml' result = runner.invoke(cmd_up.cli, args) if result > 0: return vm_yaml_file = os.path.join(self.hosts_path, host_yaml) self.assertTrue(os.path.isfile(vm_yaml_file)) with open(vm_yaml_file, 'r') as yaml_file: vm_yaml_data = yaml.load(yaml_file) self.assertEqual(vm_yaml_data['groups'][1], group) with open(self.vagrant_yaml, 'r') as vagrant_f: vagrant_data = yaml.load(vagrant_f) self.assertEqual(vm_yaml_data['interfaces']['eth0']['ip_address'], vagrant_data['hosts'][hostname]['ip'], '192.168.100.15') if remote: hypervisor = "OpenStack" else: hypervisor = "VirtualBox" ispoweron, isremote = vagrant_utils.vm_isrunning( hostname, self.ctx.path) if ispoweron > 1: print('Unable to contact %s for VM status' % hypervisor) elif ispoweron == 1: print('VM is offline in %s' % hypervisor) else: self.assertEqual(ispoweron, 0) self.assertEqual(isremote, remote) def destroy_vm(self, hostname): """ Destroys vms based on the Vagrantfile data Args: hostname {str}: Name of the vm Returns: Nothing. Destroys the VM within the hypervisor Example Usage: self.destroy_vm('rhel7-001') """ my_vm_connection = vagrant_utils.Connect_to_vagrant(vm_name=hostname, path=self.ctx.path) ispoweron, isremote = vagrant_utils.vm_isrunning( hostname, self.ctx.path) # ispoweron states: # 0 - VM is online # 1 - VM is offline # 2 - Vagrant command error, usually cannot find vm or contact hypervisor # 3 - Other errors if ispoweron == 0: my_vm_connection.v.destroy(vm_name=hostname) def test_local_rhel7(self): """ Tests the 'stack up --rhel7' command """ args = ['--rhel7'] hostname = 'rhel7-001' group = 'rhel7' remote = False self.cmd_up_runner(args, hostname, group, remote) self.destroy_vm(hostname) def test_remote_rhel7(self): """ Tests the 'stack up --rhel7 -r' command """ args = ['--rhel7', '-r'] hostname = 'rhel7-001' group = 'rhel7' remote = True self.cmd_up_runner(args, hostname, group, remote) self.destroy_vm(hostname) def test_local_service(self): """ Tests the 'stack up -s <servicename>' command """ args = ['-s', 'service-sonarqube'] hostname = 'service-sonarqube-001' group = 'sonarqube' remote = False self.cmd_up_runner(args, hostname, group, remote) self.destroy_vm(hostname) self.destroy_vm('infra-001') def test_remote_service(self): """ Tests the 'stack up -s <servicename> -r' comman """ args = ['-s', 'service-sonarqube', '-r'] hostname = 'service-sonarqube-001' group = 'sonarqube' remote = True self.cmd_up_runner(args, hostname, group, remote) self.destroy_vm(hostname) self.destroy_vm('infra-001')
def setUp(self): """ Construct the data needed to create a new host yaml file """ self.ctx = Context() self.ccsdatapath = os.path.join(self.ctx.path, 'testsite') self.hostname1 = 'my-test-001' self.hostname2 = 'my-test-002' self.site = 'test-site-1' self.env = 'test-env-1' self.tcregion = 'csl-a' self.flavor = 'fake.flavor.dne' self.vlanid = '67' self.role = 'none' self.groups = 'default' self.secgroups = ['default'] self.ip1 = None self.ip2 = '10.11.12.152' self.filename1 = os.path.join( self.ccsdatapath, 'sites', self.site, 'environments', self.env, 'hosts.d', str(self.tcregion + '-' + self.hostname1 + '.yaml')) self.filename2 = os.path.join( self.ccsdatapath, 'sites', self.site, 'environments', self.env, 'hosts.d', str(self.tcregion + '-' + self.hostname2 + '.yaml')) self.subnet = ipaddress.IPv4Network(unicode('10.12.14.128/25')) env_path = os.path.join(self.ccsdatapath, 'sites', self.site, 'environments') # Create a fake site to simulate ccs-data repo output_path = os.path.join(env_path, self.site, 'data.d') os.makedirs(output_path) output_file = os.path.join(output_path, 'environment.yaml') yaml_data = { 'domain_name': 'test.site.com', 'region': 'csm', 'controller_internal_vip': '10.1.2.3', } self.write_file(output_file, yaml_data) output_path = os.path.join(env_path, self.site, 'hosts.d') os.makedirs(output_path) output_file = os.path.join(output_path, 'csm-fake-node.yaml') yaml_data = {'type': 'physical'} self.write_file(output_file, yaml_data) output_path = os.path.join(env_path, self.env, 'data.d') os.makedirs(output_path) output_file = os.path.join(output_path, 'environment.yaml') yaml_data = { 'tc_region': self.tcregion, 'vlan66': '10.11.12.0/24', 'vlan67': '10.12.14.128/25', } self.write_file(output_file, yaml_data) output_path = os.path.join(env_path, self.env, 'hosts.d') os.makedirs(output_path) for i in range(1, 21): hostname = str(self.tcregion + '-fake-vm-' + str(i).zfill(3) + '.yaml') output_file = os.path.join(output_path, hostname) yaml_data = { 'interfaces': { 'eth0': { 'ip_address': str(self.subnet.network_address + 4 + i), }, }, 'type': 'virtual', } self.write_file(output_file, yaml_data)