def proof(path, is_bundle, debug): messages = [] exit_code = 0 path = os.path.abspath(path) if not os.access(path, os.R_OK): messages.append('Unable to read from {}'.format(path)) exit_code = 200 return messages, exit_code if not is_bundle: try: c = Charm(path) except Exception: try: c = Bundle(path, debug) except Exception as e: return [ "FATAL: No bundle.yaml (Bundle) or metadata.yaml " "(Charm) found, cannot proof" ], 200 else: try: c = Bundle(path, debug) except Exception as e: return ["FATAL: %s" % e.message], 200 lint, err_code = c.proof() return lint, err_code
def proof(path, is_bundle, debug): messages = [] exit_code = 0 path = os.path.abspath(path) home_path = utils.get_home() home_msg = ('For security reasons, only paths under ' 'your home directory can be accessed') if not home_path: # expansion failed messages.append('Could not determine home directory') messages.append(home_msg) elif not path.startswith(home_path): messages.append('The path {} is not under your ' 'home directory'.format(path)) messages.append(home_msg) if not os.access(path, os.R_OK): messages.append('Unable to read from {}'.format(path)) exit_code = 200 return messages, exit_code if not is_bundle: try: c = Charm(path) except: try: c = Bundle(path, debug) except Exception as e: return ["FATAL: No bundle.yaml (Bundle) or metadata.yaml " "(Charm) found, cannot proof"], 200 else: try: c = Bundle(path, debug) except Exception as e: return ["FATAL: %s" % e.message], 200 lint, err_code = c.proof() return lint, err_code
def test_load_proof_extensions(self): mocks = { 'validate_storage': None, 'validate_devices': None, 'validate_resources': None, 'validate_payloads': None, } for validator in mocks.keys(): patcher = patch('charmtools.charms.{}'.format(validator)) mocks[validator] = patcher.start() self.addCleanup(patcher.stop) self.write_metadata("{}") with open(join(self.charm_dir, 'layer.yaml'), 'w') as f: f.write(dedent(""" proof: storage: - name: ext type: Boolean devices: - name: ext type: Boolean resources: - name: ext type: Boolean payloads: - name: ext type: Boolean """)) charm = Charm(self.charm_dir, self.linter) charm.proof() for mock in mocks.values(): mock.assert_called_once_with({}, self.linter, [{'name': 'ext', 'type': 'Boolean'}])
def copy_file(tpl_file, charm_dir, is_bundle=False, debug=False): c = Charm(charm_dir) if not c.is_charm(): raise Exception('%s is not a charm' % charm_dir) shutil.copy(os.path.join(CHARM_TPL, tpl_file), charm_dir)
def test_valid_layer_yaml(self): self.write_metadata("{}") with open(join(self.charm_dir, 'layer.yaml'), 'w') as f: f.write("valid: {}") with patch.object(Charm, 'is_charm'): charm = Charm(self.charm_dir, self.linter) charm.proof() assert not any(msg.startswith('W: cannot parse {}/layer.yaml: ' ''.format(self.charm_dir)) for msg in self.linter.lint)
def test_valid_layer_yaml(self): with open(join(self.charm_dir, 'metadata.yaml'), 'w') as f: f.write("{}") with open(join(self.charm_dir, 'layer.yaml'), 'w') as f: f.write("valid: {}") with patch.object(Charm, 'is_charm'): charm = Charm(self.charm_dir, self.linter) charm.proof() assert not any(msg.startswith('W: cannot parse {}/layer.yaml: ' ''.format(self.charm_dir)) for msg in self.linter.lint)
def tests(charm_dir, is_bundle=False, debug=False, series='trusty'): c = Charm(charm_dir) if not c.is_charm(): raise Exception('Not a Charm') mdata = c.metadata() interfaces = {} deploy = [mdata['name']] relations = [] for rel_type in ['provides', 'requires']: if rel_type in mdata: interfaces[rel_type] = {} for rel, data in mdata[rel_type].items(): iface = data['interface'] if iface and iface not in interfaces[rel_type]: r = graph(iface, rel_type, series=series) # If we dont find an interface, do nothing if r is None: continue interfaces[rel_type][iface] = r deploy.append(r.name) relations.append(['%s:%s' % (mdata['name'], rel), r.name]) t = Template(file=os.path.join(TPL_DIR, 'tests', '99-autogen.tpl'), searchList=[{'deploy': deploy, 'relate': relations, 'series': series}]) if not os.path.exists(os.path.join(charm_dir, 'tests')): os.mkdir(os.path.join(charm_dir, 'tests')) with open(os.path.join(charm_dir, 'tests', '99-autogen'), 'w') as f: f.write(str(t)) if not os.path.exists(os.path.join(charm_dir, 'tests', '00-setup')): with open(os.path.join(charm_dir, 'tests', '00-setup'), 'w') as f: f.write("""#!/bin/bash sudo add-apt-repository ppa:juju/stable -y sudo apt-get update sudo apt-get install amulet python3-requests -y """) os.chmod(os.path.join(charm_dir, 'tests', '99-autogen'), 0o755) os.chmod(os.path.join(charm_dir, 'tests', '00-setup'), 0o755)
def test_invalid_layer_yaml(self): self.write_metadata("{}") with open(join(self.charm_dir, 'layer.yaml'), 'w') as f: f.write("invalid: {") Charm(self.charm_dir, self.linter).proof() assert any(msg.startswith('W: cannot parse {}/layer.yaml: ' ''.format(self.charm_dir)) for msg in self.linter.lint)
def test_load_proof_extensions(self): mocks = { 'validate_storage': None, 'validate_devices': None, 'validate_resources': None, 'validate_payloads': None, } for validator in mocks.keys(): patcher = patch('charmtools.charms.{}'.format(validator)) mocks[validator] = patcher.start() self.addCleanup(patcher.stop) with open(join(self.charm_dir, 'metadata.yaml'), 'w') as f: f.write("{}") with open(join(self.charm_dir, 'layer.yaml'), 'w') as f: f.write(dedent(""" proof: storage: - name: ext type: Boolean devices: - name: ext type: Boolean resources: - name: ext type: Boolean payloads: - name: ext type: Boolean """)) charm = Charm(self.charm_dir, self.linter) charm.proof() for mock in mocks.values(): mock.assert_called_once_with({}, self.linter, [{'name': 'ext', 'type': 'Boolean'}])