def test_input_csar_no_gi(self): test_base_dir = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'data') template_file = os.path.join(test_base_dir, "ping_pong_csar.zip") template = '--template-file='+template_file temp_dir = tempfile.mkdtemp() output_dir = "--output-dir=" + temp_dir no_gi = '--no-gi' try: shell.main([template, output_dir, no_gi, '--archive'], log=self.log) except Exception as e: self.log.exception(e) self.fail("Exception in input_csar_no_gi: {}".format(e)) else: self.check_output(temp_dir, archive=True) finally: if self.log_level != logging.DEBUG: if os.path.exists(temp_dir): shutil.rmtree(temp_dir) else: self.log.warn("Generated desc in {}".format(temp_dir))
def test_output_dir(self): test_base_dir = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'data') template_file = os.path.join( test_base_dir, "tosca_ping_pong_epa/Definitions/ping_pong_nsd.yaml") template = '--template-file=' + template_file temp_dir = tempfile.mkdtemp() output_dir = "--output-dir=" + temp_dir try: shell.main([template, output_dir], log=self.log) except Exception as e: self.log.exception(e) self.fail("Exception in test_output_dir: {}".format(e)) else: self.check_output(temp_dir) finally: if self.log_level != logging.DEBUG: if os.path.exists(temp_dir): shutil.rmtree(temp_dir) else: self.log.warn("Generated desc in {}".format(temp_dir))
def test_validate_only(self): try: shell.main([self.template_file, self.template_validation]) except Exception as e: self.log.exception(e) self.fail(self.failure_msg) template = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data/tosca_helloworld_invalid.yaml") invalid_template = '--template-file=' + template self.assertRaises(TOSCAException, shell.main, [invalid_template, self.template_validation])
def test_validate_only(self): try: shell.main([self.template_file, self.template_validation]) except Exception as e: self.log.exception(e) self.fail(self.failure_msg) template = os.path.join( os.path.dirname(os.path.abspath(__file__)), "data/tosca_helloworld_invalid.yaml") invalid_template = '--template-file=' + template self.assertRaises(TOSCAException, shell.main, [invalid_template, self.template_validation])
def test_output(self): test_base_dir = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'data') temp_dir = tempfile.mkdtemp() args = [] for f in os.listdir(self.desc_dir): fpath = os.path.join(self.desc_dir, f) if os.path.isfile(fpath): template = '--template-file=' + fpath args.append(template) output_dir = "--output-dir=" + temp_dir args.append(output_dir) self.log.debug("Args passed: {}".format(args)) try: shell.main(args, log=self.log) # Check the dirs are present out_dir = os.path.join(temp_dir, 'ping_pong_nsd') self.assertTrue(os.path.isdir(out_dir)) dirs = os.listdir(out_dir) expected_dirs = ['TOSCA-Metadata', 'Definitions'] self.assertTrue(set(expected_dirs) <= set(dirs)) # Compare the descriptors gen_desc = os.path.join(out_dir, 'Definitions', 'ping_pong_nsd.yaml') exp_desc = os.path.join(test_base_dir, 'ping_pong_tosca.yaml') self.compare_tosca(gen_desc, exp_desc) # Convert back to yang and compare template = '--template-file=' + gen_desc yang_out_dir = os.path.join(temp_dir, 'ping_pong_yang') output_dir = "--output-dir=" + yang_out_dir tshell.main([template, output_dir], log=self.log) # Check the dirs are present dirs = os.listdir(yang_out_dir) self.assertTrue(len(dirs) >= 3) except Exception as e: self.log.exception(e) self.fail(_("Exception {}").format(e)) finally: if temp_dir: shutil.rmtree(temp_dir)
def translate(self): try: out_file = None prevdir = os.getcwd() # Create a temp directory to generate the yang descriptors with tempfile.TemporaryDirectory() as tmpdirname: output_dir = tmpdirname+'/yang' tmpl_file = '--template-file='+self.in_file out_dir = '--output-dir='+output_dir trans_args = [tmpl_file, out_dir] self.log.debug("Calling tosca-translator with args:{}". format(trans_args)) shell.main(args=trans_args, log=self.log) # Get the list of translated files os.chdir(output_dir) flist = [] for root, dirs, files in os.walk(output_dir): rel_dir = (root.replace(output_dir, '')).lstrip('/') for f in files: flist.append(os.path.join(rel_dir, f)) self.log.debug("File list to archive: {}".format(flist)) # Generate a tar file with the output files with tarfile.open(self.out_file, 'w:gz') as tar: for f in flist: tar.add(f) out_file = self.out_file self.log.debug("Output file: {}".format(out_file)) except Exception as e: self.log.error("Error processing TOSCA file {}: {}". format(self.in_file, e)) self.log.exception(e) finally: os.chdir(prevdir) return out_file
def test_output_dir(self): test_base_dir = os.path.join(os.path.dirname( os.path.abspath(__file__)), 'data') template_file = os.path.join(test_base_dir, "ping_pong_csar/Definitions/ping_pong_nsd.yaml") template = '--template-file='+template_file temp_dir = tempfile.mkdtemp() output_dir = "--output-dir=" + temp_dir try: shell.main([template, output_dir], log=self.log) except Exception as e: self.log.exception(e) self.fail("Exception in test_output_dir: {}".format(e)) else: self.check_output(temp_dir) finally: if self.log_level != logging.DEBUG: if os.path.exists(temp_dir): shutil.rmtree(temp_dir) else: self.log.warn("Generated desc in {}".format(temp_dir))
def test_valid_template(self): try: shell.main([self.template_file]) except Exception as e: self.log.exception(e) self.fail(self.failure_msg)