def test_deploy_single_provider_list(app): s = """deploy: - provider: script script: echo test""" f = io.StringIO(s) spec = Specification.parse_file(f) assert len(spec.deploy) == 1 deploy = spec.deploy[0] assert not deploy.tag assert not deploy.branch assert deploy.provider == 'script' assert deploy.script == ['echo test']
def test_parse_vault_with_single_env(app): s = '''vault: url: http://localhost:8200 token: abc123 env: API_TOKEN secret/api:token ''' f = io.StringIO(s) spec = Specification.parse_file(f) assert spec.vault.url == 'http://localhost:8200' assert spec.vault.token == 'abc123' env = spec.vault.env['API_TOKEN'] assert env == ('secret/api', 'token')
def test_parse_linter_with_pattern(app): s = """linter: - {name: "flake8", pattern: "*.py", whatever: 123} - {name: "jsonlint", pattern: "*.mapping"} - yamllint""" f = io.StringIO(s) spec = Specification.parse_file(f) assert len(spec.linters) == 3 linter0 = spec.linters[0] assert linter0.name == 'flake8' assert linter0.pattern == '*.py' assert linter0.whatever == 123
def test_parse_multi_linters_with_pattern(app): s = """linter: - {name: "flake8", pattern: "*.py"} - eslint""" f = io.StringIO(s) spec = Specification.parse_file(f) assert len(spec.linters) == 2 linter0 = spec.linters[0] assert linter0.name == 'flake8' assert linter0.pattern == '*.py' linter1 = spec.linters[1] assert linter1.name == 'eslint' assert linter1.pattern is None
def test_generate_script_after_success(app): s = """script: - ls service: - redis-server after_success: - pwd""" f = io.StringIO(s) spec = Specification.parse_file(f) script = spec.shell_script assert 'echo + pwd\npwd' in script assert 'if [ $SCRIPT_EXIT_CODE -eq 0 ]; then' in script assert 'if [ $SCRIPT_EXIT_CODE -ne 0 ]; then' not in script
def test_generate_script_after_failure(app): s = """script: - ls service: - redis-server after_failure: - exit""" f = io.StringIO(s) spec = Specification.parse_file(f) script = spec.shell_script assert 'echo + exit\nexit' in script assert 'if [ $SCRIPT_EXIT_CODE -eq 0 ]; then' not in script assert 'if [ $SCRIPT_EXIT_CODE -ne 0 ]; then' in script
def test_generate_script_full_feature(app): s = """script: - ls service: - redis-server after_success: - pwd after_failure: - exit""" f = io.StringIO(s) spec = Specification.parse_file(f) script = spec.shell_script assert 'echo + pwd\npwd' in script assert 'echo + exit\nexit' in script
def test_deploy_pypi_provider_no_auth(app): s = """deploy: - provider: pypi tag: true""" f = io.StringIO(s) spec = Specification.parse_file(f) assert len(spec.deploy) == 1 pypi = spec.deploy[0] assert pypi.tag assert not pypi.branch assert pypi.provider == 'pypi' assert not pypi.username assert not pypi.password assert pypi.repository
def test_parse_env_multi_list(app): s = """env: - X=1 Y=2 Z=3 - X=3 Y=2 Z=1""" f = io.StringIO(s) spec = Specification.parse_file(f) assert len(spec.environments) == 2 env0 = spec.environments[0] assert env0['X'] == '1' assert env0['Y'] == '2' assert env0['Z'] == '3' env1 = spec.environments[1] assert env1['X'] == '3' assert env1['Y'] == '2' assert env1['Z'] == '1'
def test_parse_file_single_string(): s = """script: ls dockerfile: MyDockerfile service: redis-server after_success: pwd after_failure: exit notification: email: [email protected]""" f = io.StringIO(s) spec = Specification.parse_file(f) assert spec.dockerfile == 'MyDockerfile' assert spec.services == ['redis-server'] assert spec.scripts == ['ls'] assert spec.after_success == ['pwd'] assert spec.after_failure == ['exit'] assert spec.notification.emails == ['*****@*****.**']
def validate_settings(self): conf_file = os.path.join(self.clone_path, current_app.config['BADWOLF_PROJECT_CONF']) if not os.path.exists(conf_file): logger.warning('No project configuration file found for repo: %s', self.repo_full_name) return False self.spec = spec = Specification.parse_file(conf_file) if self.context.type == 'commit' and spec.branch and self.branch not in spec.branch: logger.info( 'Ignore tests since branch %s test is not enabled. Allowed branches: %s', self.branch, spec.branch) return False if not spec.scripts and not spec.linters: logger.warning('No script(s) or linter(s) to run') return False return True
def test_parse_file_single_string(app): s = """script: ls dockerfile: MyDockerfile service: redis-server after_success: pwd after_failure: exit notification: email: [email protected] slack_webhook: https://1""" f = io.StringIO(s) spec = Specification.parse_file(f) assert spec.dockerfile == 'MyDockerfile' assert spec.services == ['redis-server'] assert spec.scripts == ['ls'] assert spec.after_success == ['pwd'] assert spec.after_failure == ['exit'] assert spec.notification.email.recipients == ['*****@*****.**'] assert spec.notification.email.on_success == 'never' assert spec.notification.email.on_failure == 'always' assert spec.notification.slack_webhook.webhooks == ['https://1'] assert spec.notification.slack_webhook.on_success == 'always' assert spec.notification.slack_webhook.on_failure == 'always'
def test_parse_notification_object_no_option(app): s = """script: - ls notification: email: recipients: - [email protected] - [email protected] slack_webhook: webhooks: - https://1 - https://2""" f = io.StringIO(s) spec = Specification.parse_file(f) assert spec.notification.email.recipients == [ '*****@*****.**', '*****@*****.**' ] assert spec.notification.email.on_success == 'never' assert spec.notification.email.on_failure == 'always' assert len(spec.notification.slack_webhook.webhooks) == 2 assert spec.notification.slack_webhook.on_success == 'always' assert spec.notification.slack_webhook.on_failure == 'always'
def test_parse_vault_with_secretfile(app): s = '''vault: url: http://localhost:8200 token: abc123 env: API_TOKEN secret/api:token ''' f = io.StringIO(s) spec = Specification.parse_file(f) assert spec.vault.url == 'http://localhost:8200' assert spec.vault.token == 'abc123' env = spec.vault.env['API_TOKEN'] assert env == ('secret/api', 'token') s = '''API_TOKEN secret/API:token # comment API_KEY secret/API:key ''' f = io.StringIO(s) spec.parse_secretfile(f) env = spec.vault.env['API_TOKEN'] assert env == ('secret/API', 'token') env = spec.vault.env['API_KEY'] assert env == ('secret/API', 'key')
def test_deploy_multiple_provider(app): s = """deploy: - provider: script script: echo test - provider: pypi username: test password: test tag: true""" f = io.StringIO(s) spec = Specification.parse_file(f) assert len(spec.deploy) == 2 script = spec.deploy[0] assert not script.tag assert not script.branch assert script.provider == 'script' assert script.script == ['echo test'] pypi = spec.deploy[1] assert pypi.tag assert not pypi.branch assert pypi.provider == 'pypi' assert pypi.username == 'test' assert pypi.password == 'test'
def parse_spec(self): '''Parse repository build/lint spec''' logger.info('Parsing specification for repository %s', self.context.repository) conf_file = os.path.join(self.context.clone_path, current_app.config['BADWOLF_PROJECT_CONF']) try: spec = Specification.parse_file(conf_file) except OSError: logger.warning('No project configuration file found for repo: %s', self.context.repository) raise SpecificationNotFound() branch = self.context.source['branch']['name'] if self.context.type == 'commit' and not spec.is_branch_enabled( branch): logger.info( 'Ignore tests since branch %s test is not enabled. Allowed branches: %s', branch, spec.branch) raise BuildDisabled() if not spec.scripts and not spec.linters: logger.warning('No script(s) or linter(s) to run') raise InvalidSpecification() self.spec = spec
def test_parse_file_multi_list(app): s = """script: - ls - ps dockerfile: MyDockerfile service: - redis-server - postgresql after_success: - pwd - rm after_failure: - echo - exit notification: email: - [email protected] - [email protected] slack_webhook: - https://1 - https://2""" f = io.StringIO(s) spec = Specification.parse_file(f) assert spec.dockerfile == 'MyDockerfile' assert spec.services == ['redis-server', 'postgresql'] assert spec.scripts == ['ls', 'ps'] assert spec.after_success == ['pwd', 'rm'] assert spec.after_failure == ['echo', 'exit'] assert spec.notification.email.recipients == [ '*****@*****.**', '*****@*****.**' ] assert spec.notification.email.on_success == 'never' assert spec.notification.email.on_failure == 'always' assert len(spec.notification.slack_webhook.webhooks) == 2 assert spec.notification.slack_webhook.on_success == 'always' assert spec.notification.slack_webhook.on_failure == 'always'
def test_parse_simple_artifacts(app): s = 'artifacts: true' f = io.StringIO(s) spec = Specification.parse_file(f) assert spec.artifacts.paths == ['$(git ls-files -o | tr "\\n" ":")'] assert spec.artifacts.excludes == []