def test_get_telegram_key(self): print("Testing get_telegram_key()") from stallmanbot import read_configuration, get_telegram_key import os import configparser fs = mockfs.replace_builtins() SESSION = "TELEGRAM" fs.add_entries({"configuration.conf" : "[TELEGRAM]\n" + \ "STALLBOT = abc:123456\n" + \ "STALLBOTADM = HelioLoureiro\n"}) sys = Mock() error = Mock() debug = Mock() cfg = read_configuration("configuration.conf") print(" * testing existent values") result = get_telegram_key(cfg, "STALLBOT") self.assertEqual(result, "abc:123456", "Resulting is mismatching expected value.") print(" * testing non-existent values") result = get_telegram_key(cfg, "ROCKNROLL") self.assertIsNone(result, "Command returned value (expected empty).") mockfs.restore_builtins()
def setup_method(self, method): with open(pipeline_libraries_schema_path, 'r') as f: mock_pipeline_libraries_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries( {pipeline_libraries_schema_path: mock_pipeline_libraries_schema}) self.schema = yaml_reader.read(pipeline_libraries_schema_path)
def fs(request): mfs = mockfs.replace_builtins() os.path.lexists = mfs.exists glob.iglob = mfs.glob mfs.add_entries({ "jasmine.yml": """ src_files: - src/player.js - src/**/*.js - http://cdn.jquery.com/jquery.js - vendor/test.js - vendor/**/*.{js,coffee} """, "/spec/javascripts/helpers/spec_helper.js": '', "/lib/jams/jam_spec.js": '', "/src/player.js": '', "/src/mixer/mixer.js": '', "/src/tuner/fm/fm_tuner.js": '', "/spec/javascripts/player_spec.js": '', "/spec/javascripts/mixer/mixer_spec.js": '', "/spec/javascripts/tuner/fm/fm_tuner_spec.js": '', "/spec/javascripts/tuner/am/AMSpec.js": '', "/vendor/test.js": '', "/vendor/pants.coffee": '', "/vendor_spec/pantsSpec.js": '', "/main.css": '', }) request.addfinalizer(lambda: mockfs.restore_builtins()) return mfs
def test_cli_call(self, mock_subp, mock_modules): with open(gearman_schema_path, 'r') as f: mock_gearman_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, gearman_schema_path: mock_gearman_schema, jenkins_yaml_path: '\n'.join([ 'jenkins:', ' gearman:', ' enable: true', ' host: test.infra.mirantis.net', ' port: 4732' ]) }) sys.path.insert(0, modules_dir) import gearman import read_source sys.path.pop(0) mock_modules.return_value = [gearman, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) mock_subp.assert_called_with([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'gearman/resources/jenkins.groovy', 'True', '4732', "'test.infra.mirantis.net'" ], shell=False) assert 1 == mock_subp.call_count, "subprocess call should be equal to 1"
def test_cli_call_for_unsecured_conf(self, mock_subp, mock_modules): with open(security_schema_path, 'r') as f: mock_security_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, security_schema_path: mock_security_schema, jenkins_yaml_path: '\n'.join(['jenkins:', ' security:', ' unsecured: true']) }) sys.path.insert(0, modules_dir) import security import read_source sys.path.pop(0) mock_modules.return_value = [security, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) calls = [ call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'security/resources/jenkins.groovy', 'setUnsecured' ], shell=False) ] mock_subp.assert_has_calls(calls, any_order=True) assert 1 == mock_subp.call_count, "subprocess call should be equal to 1"
def test_valid_repo_data(self): self.mfs = mockfs.replace_builtins() self.mfs.add_entries({jimmy_schema_path: self.jimmy_schema, jimmy_yaml_path: self.mock_jimmy_yaml}) schema = yaml_reader.read(jimmy_schema_path) repo_data = yaml_reader.read(jimmy_yaml_path) jsonschema.validate(repo_data, schema)
def test_cli_call(self, mock_subp, mock_modules): with open(jenkins_schema_path, 'r') as f: mock_jenkins_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jim_dir, 'lib', 'schema.yaml'): self.jim_schema, os.path.join(jim_dir, 'jim.yaml'): self.mock_jim_yaml, jenkins_schema_path: mock_jenkins_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' configuration:', ' admin_email: CI <*****@*****.**>', ' markup_format: raw-html', ' num_of_executors: 2', ' scm_checkout_retry_count: 1' ]) }) sys.path.insert(0, plugins_dir) import jenkins_configuration import read_source sys.path.pop(0) mock_modules.return_value = [jenkins_configuration, read_source] os.chdir(jim_dir) self.runner.invoke(cli) mock_subp.assert_called_with( ['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://*****:*****@example.com>'", 'raw-html', '2', '1' ], shell=False) assert 1 == mock_subp.call_count, "subproccess call should be equal to 1"
def test_cli_call(self, mock_subp, mock_modules): with open(jenkins_schema_path, 'r') as f: mock_jenkins_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, jenkins_schema_path: mock_jenkins_schema, jenkins_yaml_path: '\n'.join([ 'jenkins:', ' node-labels:', ' node1: ', ' -label1', ]) }) sys.path.insert(0, modules_dir) import node_envvars import read_source sys.path.pop(0) mock_modules.return_value = [node_envvars, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) mock_subp.assert_called_with([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'node_envvars/resources/jenkins.groovy', '{"node1": ["label1"]}' ], shell=False) assert 1 == mock_subp.call_count, "subproccess call " \ "should be equal to 1"
def test_cli_call_for_unsecured_conf(self, mock_subp, mock_modules): with open(security_schema_path, 'r') as f: mock_security_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, security_schema_path: mock_security_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' security:', ' unsecured: true' ]) }) sys.path.insert(0, modules_dir) import security import read_source sys.path.pop(0) mock_modules.return_value = [security, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) calls = [call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'security/resources/jenkins.groovy', 'setUnsecured'], shell=False)] mock_subp.assert_has_calls(calls, any_order=True) assert 1 == mock_subp.call_count, "subprocess call should be equal to 1"
def test_cli_call(self, mock_subp, mock_modules): with open(gearman_schema_path, 'r') as f: mock_gearman_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, gearman_schema_path: mock_gearman_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' gearman:', ' enable: true', ' host: test.infra.mirantis.net', ' port: 4732' ]) }) sys.path.insert(0, plugins_dir) import gearman import read_source sys.path.pop(0) mock_modules.return_value = [gearman, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) mock_subp.assert_called_with( ['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', plugins_dir + '/' + 'gearman/resources/jenkins.groovy', 'True', '4732', "'test.infra.mirantis.net'" ], shell=False) assert 1 == mock_subp.call_count, "subprocess call should be equal to 1"
def test_cli_call(self, mock_subp, mock_modules): with open(pipeline_libraries_schema_path, 'r') as f: mock_pipeline_libraries_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, pipeline_libraries_schema_path: mock_pipeline_libraries_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' pipeline_libraries:', ' libraries:', ' - name: shared-lib', ' git_url: https://github.com/example/shared-lib', ' git_branch: master', ' default_version: release-0.1', ' load_implicitly: true', ' allow_version_override: false', ' - name: shared-lib-dev', ' git_url: https://github.com/example/shared-lib-dev', ' git_branch: master', ' default_version: master', ' load_implicitly: false', ' allow_version_override: true' ]) }) sys.path.insert(0, plugins_dir) import pipeline_libraries import read_source sys.path.pop(0) mock_modules.return_value = [pipeline_libraries, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) calls = [call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', plugins_dir + '/' + 'pipeline_libraries/resources/jenkins.groovy', 'set_global_library', 'shared-lib', 'https://github.com/example/shared-lib', 'master', 'release-0.1', 'True', 'False'], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', plugins_dir + '/' + 'pipeline_libraries/resources/jenkins.groovy', 'set_global_library', 'shared-lib-dev', 'https://github.com/example/shared-lib-dev', 'master', 'master', 'False', 'True'], shell=False)] print calls mock_subp.assert_has_calls(calls, any_order=True) assert 2 == mock_subp.call_count, "subprocess call should be equal to 2"
def test_cli_call(self, mock_subp, mock_modules): with open(jenkins_schema_path, 'r') as f: mock_jenkins_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, jenkins_schema_path: mock_jenkins_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' git:', ' user:'******' email: [email protected]', ' name: Jenkins' ]) }) sys.path.insert(0, modules_dir) import git import read_source sys.path.pop(0) mock_modules.return_value = [git, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) mock_subp.assert_called_with( ['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://*****:*****@example.com'", "'Jenkins'" ], shell=False) assert 1 == mock_subp.call_count, "subproccess call should be equal to 1"
def setUp(self, klass): self.mfs = mockfs.replace_builtins() params = dict(env='s', account_name='acc1', vpc_id='vpc-xxxx') self.c = klass(**params) for p in self.c.required_paths: self.mfs.add_entries({p: 'magic'})
def test_cli_call_for_password_conf(self, mock_subp, mock_modules): with open(security_schema_path, 'r') as f: mock_security_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, security_schema_path: mock_security_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' security:', ' password:'******' access:', ' - name: amihura', ' email: [email protected]', ' password: passwd', ' permissions:', ' - overall', ' - credentials', ' - gerrit', ' cli_user:'******' name: jenkins-manager', ' public_key: sssh-rsa AAAAB3NzaC', ' password: password' ]) }) sys.path.insert(0, modules_dir) import security import read_source sys.path.pop(0) mock_modules.return_value = [security, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) calls = [call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://*****:*****@example.com', 'passwd', '', '', 'password'], shell=False)] mock_subp.assert_has_calls(calls, any_order=True) assert 2 == mock_subp.call_count, "subprocess call should be equal to 2"
def setup(self): def mock_getmtime(_): return time.time() os.path.getmtime = mock_getmtime self.mfs = mockfs.replace_builtins() self.responders = OpenStruct() for name in self.ATTRS: self._intercept(name)
def test_valid_repo_data(self): self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ jimmy_schema_path: self.jimmy_schema, jimmy_yaml_path: self.mock_jimmy_yaml }) schema = yaml_reader.read(jimmy_schema_path) repo_data = yaml_reader.read(jimmy_yaml_path) jsonschema.validate(repo_data, schema)
def test_cli_call(self, mock_subp, mock_modules): with open(pipeline_libraries_schema_path, 'r') as f: mock_pipeline_libraries_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, pipeline_libraries_schema_path: mock_pipeline_libraries_schema, jenkins_yaml_path: '\n'.join([ 'jenkins:', ' pipeline_libraries:', ' libraries:', ' - name: shared-lib', ' git_url: https://github.com/example/shared-lib', ' git_branch: master', ' default_version: release-0.1', ' load_implicitly: true', ' allow_version_override: false', ' - name: shared-lib-dev', ' git_url: https://github.com/example/shared-lib-dev', ' git_branch: master', ' default_version: master', ' load_implicitly: false', ' allow_version_override: true' ]) }) sys.path.insert(0, modules_dir) import pipeline_libraries import read_source sys.path.pop(0) mock_modules.return_value = [pipeline_libraries, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) calls = [ call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'pipeline_libraries/resources/jenkins.groovy', 'set_global_library', 'shared-lib', 'https://github.com/example/shared-lib', 'master', 'release-0.1', 'True', 'False' ], shell=False), call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'pipeline_libraries/resources/jenkins.groovy', 'set_global_library', 'shared-lib-dev', 'https://github.com/example/shared-lib-dev', 'master', 'master', 'False', 'True' ], shell=False) ] print calls mock_subp.assert_has_calls(calls, any_order=True) assert 2 == mock_subp.call_count, "subprocess call should be equal to 2"
def test_cli_call(self, mock_subp, mock_modules): with open(throttle_schema_path, 'r') as f: mock_throttle_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jim_dir, 'lib', 'schema.yaml'): self.jim_schema, os.path.join(jim_dir, 'jim.yaml'): self.mock_jim_yaml, throttle_schema_path: mock_throttle_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' plugins:', ' throttle:', ' categories:', ' - category_name: category1', ' max_total_concurrent_builds: 1', ' max_concurrent_bulds_per_node: 0', ' max_per_labeled_node:', ' - throttled_node_label: slave-label1', ' max_concurrent_per_labeled: 1', ' - throttled_node_label: slave-label2', ' max_concurrent_per_labeled: 1', ' - category_name: category2', ' max_total_concurrent_builds: 1', ' max_concurrent_bulds_per_node: 0' ]) }) sys.path.insert(0, plugins_dir) import throttle import read_source sys.path.pop(0) mock_modules.return_value = [throttle, read_source] os.chdir(jim_dir) self.runner.invoke(cli) calls = [call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', plugins_dir + '/' + 'throttle/resources/jenkins.groovy', 'clear_categories'], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', plugins_dir + '/' + 'throttle/resources/jenkins.groovy', 'create_throttle_category', 'category1', '1', '0', 'slave-label1,slave-label2', '1,1'], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', plugins_dir + '/' + 'throttle/resources/jenkins.groovy', 'create_throttle_category', 'category2', '1', '0', '', ''], shell=False) ] mock_subp.assert_has_calls(calls, any_order=True) assert 3 == mock_subp.call_count, "subprocess call should be equal to 3"
def mockfs(request): mfs = replace_builtins() mfs.add_entries({ "/spec/javascripts/support/jasmine.yml": """ src_dir: src spec_dir: spec """ }) request.addfinalizer(lambda: restore_builtins()) return mfs
def test_cli_call(self, mock_subp, mock_modules): with open(throttle_schema_path, 'r') as f: mock_throttle_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, throttle_schema_path: mock_throttle_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' throttle:', ' categories:', ' - category_name: category1', ' max_total_concurrent_builds: 1', ' max_concurrent_builds_per_node: 0', ' max_per_labeled_node:', ' - throttled_node_label: slave-label1', ' max_concurrent_per_labeled: 1', ' - throttled_node_label: slave-label2', ' max_concurrent_per_labeled: 1', ' - category_name: category2', ' max_total_concurrent_builds: 1', ' max_concurrent_builds_per_node: 0' ]) }) sys.path.insert(0, modules_dir) import throttle import read_source sys.path.pop(0) mock_modules.return_value = [throttle, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) calls = [call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'throttle/resources/jenkins.groovy', 'clearCategories'], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'throttle/resources/jenkins.groovy', 'makeThrottleCategory', 'category1', '1', '0', 'slave-label1,slave-label2', '1,1'], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'throttle/resources/jenkins.groovy', 'makeThrottleCategory', 'category2', '1', '0', '', ''], shell=False) ] mock_subp.assert_has_calls(calls, any_order=True) assert 3 == mock_subp.call_count, "subprocess call should be equal to 3"
def test_save_file(self): print("Testing check_if_run()") from stallmanbot import save_file fs = mockfs.replace_builtins() fs.add_entries({"/tmp/testing" : ""}) save_file("12345", "/tmp/testing") self.assertEqual("12345", fs.read("/tmp/testing"), "Saving data failed") mockfs.restore_builtins()
def mockfs_with_config(request): mfs = replace_builtins() mfs.add_entries({ "/spec/javascripts/support/jasmine.yml": """ src_dir: src spec_dir: spec """ }) request.addfinalizer(lambda: restore_builtins()) return mfs
def test_cli_call_for_ldap_conf(self, mock_subp, mock_modules): with open(security_schema_path, 'r') as f: mock_security_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, security_schema_path: mock_security_schema, jenkins_yaml_path: '\n'.join([ 'jenkins:', ' security:', ' ldap:', ' server: ldap://mirantis.com:3268', ' root_bind:', ' dn: dc=mirantis,dc=com', ' allow_blank: false', ' search:', ' user_filter: userPrincipalName={0}', ' manager:', ' name: [email protected]', ' password: passwd', ' access:', ' - name: amihura', ' permissions:', ' - overall', ' - credentials', ' - gerrit', ' cli_user:'******' name: jenkins-manager', ' public_key: sssh-rsa AAAAB3NzaC' ]) }) sys.path.insert(0, modules_dir) import security import read_source sys.path.pop(0) mock_modules.return_value = [security, read_source] os.chdir(jimmy_dir) result = self.runner.invoke(cli) print result.output calls = [ call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://*****:*****@mirantis.com', 'passwd', 'jenkins-manager', 'sssh-rsa AAAAB3NzaC' ], shell=False), call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'security/resources/jenkins.groovy', 'setPermissionsMatrix', 'amihura', 'overall,credentials,gerrit', '', '', '', '', 'ldap' ], shell=False) ] mock_subp.assert_has_calls(calls, any_order=True) assert 2 == mock_subp.call_count, "subprocess call should be equal to 2"
def test_cli_call(self, mock_subp, mock_modules): with open(gerrit_schema_path, 'r') as f: mock_gerrit_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jim_dir, 'lib', 'schema.yaml'): self.jim_schema, os.path.join(jim_dir, 'jim.yaml'): self.mock_jim_yaml, gerrit_schema_path: mock_gerrit_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' plugins:', ' gerrit:', ' servers:', ' - servername: test-gerrit-name', ' hostname: test-hostname', ' username: test-username', ' url: http://test.com', ' auth_key: /var/lib/jenkins/.ssh/id_rsa', ' - servername: test-gerrit-name2', ' hostname: test-hostname2', ' username: test-username2', ' url: http://test.com2', ' auth_key: /var/lib/jenkins/.ssh/id_rsa2' ]) }) sys.path.insert(0, plugins_dir) import gerrit import read_source sys.path.pop(0) mock_modules.return_value = [gerrit, read_source] os.chdir(jim_dir) self.runner.invoke(cli) calls = [call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', plugins_dir + '/' + 'gerrit/resources/jenkins.groovy', "'test-hostname2'", "'/var/lib/jenkins/.ssh/id_rsa2'", "'test-gerrit-name2'", "'http://test.com2'", "'test-username2'"], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', plugins_dir + '/' + 'gerrit/resources/jenkins.groovy', "'test-hostname'", "'/var/lib/jenkins/.ssh/id_rsa'", "'test-gerrit-name'", "'http://test.com'", "'test-username'"], shell=False)] mock_subp.assert_has_calls(calls, any_order=True) assert 2 == mock_subp.call_count, "subprocess call should be equal to 2"
def test_validation_fail_for_envs_required_property(self): with open(jimmy_yaml_path, 'r') as f: jimmy_yaml = f.read() mock_jimmy_yaml = jimmy_yaml.replace("envs:", "") self.mfs = mockfs.replace_builtins() self.mfs.add_entries({jimmy_yaml_path: mock_jimmy_yaml, jimmy_schema_path: self.jimmy_schema}) schema = yaml_reader.read(jimmy_schema_path) jimmy_yaml_data = yaml_reader.read(jimmy_yaml_path) with pytest.raises(jsonschema.ValidationError) as excinfo: jsonschema.validate(jimmy_yaml_data, schema) assert excinfo.value.message == "'envs' is a required property"
def test_validation_fail_for_additional_properties(self): with open(jimmy_yaml_path, 'r') as f: jimmy_yaml = f.read() mock_jimmy_yaml = "\n".join([jimmy_yaml, "test:\n"]) self.mfs = mockfs.replace_builtins() self.mfs.add_entries({jimmy_yaml_path: mock_jimmy_yaml, jimmy_schema_path: self.jimmy_schema}) schema = yaml_reader.read(jimmy_schema_path) jimmy_yaml_data = yaml_reader.read(jimmy_yaml_path) with pytest.raises(jsonschema.ValidationError) as excinfo: jsonschema.validate(jimmy_yaml_data, schema) assert excinfo.value.message == "Additional properties are not allowed ('test' was unexpected)"
def test_cli_call(self, mock_subp, mock_modules): with open(credentials_schema_path, 'r') as f: mock_credentials_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jim_dir, 'lib', 'schema.yaml'): self.jim_schema, os.path.join(jim_dir, 'jim.yaml'): self.mock_jim_yaml, credentials_schema_path: mock_credentials_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' plugins:', ' credentials:', ' password:'******' - scope: global', ' username: user', ' password: passwd', ' description: test username/password user', ' ssh:', ' - scope: global', ' username: user2', ' private_key: /home/user/.ssh/id_rsa' ]) }) sys.path.insert(0, plugins_dir) import credentials import read_source sys.path.pop(0) mock_modules.return_value = [credentials, read_source] os.chdir(jim_dir) self.runner.invoke(cli) calls = [call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', plugins_dir + '/' + 'credentials/resources/jenkins.groovy', 'update_credentials', "'global'", "'user'", "'passwd'", "'test username/password user'"], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', plugins_dir + '/' + 'credentials/resources/jenkins.groovy', 'update_credentials', "'global'", "'user2'", "''", "''", "'/home/user/.ssh/id_rsa'"], shell=False)] mock_subp.assert_has_calls(calls, any_order=True) assert 2 == mock_subp.call_count, "subprocess call should be equal to 2"
def test_cli_call(self, mock_subp, mock_modules): with open(gerrit_schema_path, 'r') as f: mock_gerrit_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, gerrit_schema_path: mock_gerrit_schema, jenkins_yaml_path: '\n'.join([ 'jenkins:', ' gerrit:', ' servers:', ' - servername: test-gerrit-name', ' hostname: test-hostname', ' username: test-username', ' url: http://test.com', ' auth_key: /var/lib/jenkins/.ssh/id_rsa', ' - servername: test-gerrit-name2', ' hostname: test-hostname2', ' username: test-username2', ' url: http://test.com2', ' auth_key: /var/lib/jenkins/.ssh/id_rsa2' ]) }) sys.path.insert(0, modules_dir) import gerrit import read_source sys.path.pop(0) mock_modules.return_value = [gerrit, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) calls = [ call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'gerrit/resources/jenkins.groovy', "'test-hostname2'", "'/var/lib/jenkins/.ssh/id_rsa2'", "'test-gerrit-name2'", "'http://test.com2'", "'test-username2'" ], shell=False), call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'gerrit/resources/jenkins.groovy', "'test-hostname'", "'/var/lib/jenkins/.ssh/id_rsa'", "'test-gerrit-name'", "'http://test.com'", "'test-username'" ], shell=False) ] mock_subp.assert_has_calls(calls, any_order=True) assert 2 == mock_subp.call_count, "subprocess call should be equal to 2"
def test_validation_fail_for_additional_properties(self): with open(jimmy_yaml_path, 'r') as f: jimmy_yaml = f.read() mock_jimmy_yaml = "\n".join([jimmy_yaml, "test:\n"]) self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ jimmy_yaml_path: mock_jimmy_yaml, jimmy_schema_path: self.jimmy_schema }) schema = yaml_reader.read(jimmy_schema_path) jimmy_yaml_data = yaml_reader.read(jimmy_yaml_path) with pytest.raises(jsonschema.ValidationError) as excinfo: jsonschema.validate(jimmy_yaml_data, schema) assert excinfo.value.message == "Additional properties are not allowed ('test' was unexpected)"
def test_cli_call_for_password_conf(self, mock_subp, mock_modules): with open(security_schema_path, 'r') as f: mock_security_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, security_schema_path: mock_security_schema, jenkins_yaml_path: '\n'.join([ 'jenkins:', ' security:', ' password:'******' access:', ' - name: amihura', ' email: [email protected]', ' password: passwd', ' permissions:', ' - overall', ' - credentials', ' - gerrit', ' cli_user:'******' name: jenkins-manager', ' public_key: sssh-rsa AAAAB3NzaC', ' password: password' ]) }) sys.path.insert(0, modules_dir) import security import read_source sys.path.pop(0) mock_modules.return_value = [security, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) calls = [ call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://*****:*****@example.com', 'passwd', '', '', 'password' ], shell=False) ] mock_subp.assert_has_calls(calls, any_order=True) assert 2 == mock_subp.call_count, "subprocess call should be equal to 2"
def test_cli_call(self, mock_subp, mock_modules): with open(http_request_schema_path, 'r') as f: mock_http_request_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, http_request_schema_path: mock_http_request_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' http_request:', ' basic_auth:', ' - key_name: testauth', ' username: user', ' password: secret', ' - key_name: testauth2', ' username: user2', ' password: secret2' ]) }) sys.path.insert(0, plugins_dir) import http_request import read_source sys.path.pop(0) mock_modules.return_value = [http_request, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) calls = [call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', plugins_dir + '/' + 'http_request/resources/jenkins.groovy', 'setBasicDigestAuth', 'testauth', 'user', 'secret'], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', plugins_dir + '/' + 'http_request/resources/jenkins.groovy', 'setBasicDigestAuth', 'testauth2', 'user2', 'secret2'], shell=False)] mock_subp.assert_has_calls(calls, any_order=True) assert 2 == mock_subp.call_count, "subprocess call should be equal to 2"
def test_cli_call(self, mock_subp, mock_modules): with open(http_request_schema_path, 'r') as f: mock_http_request_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, http_request_schema_path: mock_http_request_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' http_request:', ' basic_auth:', ' - key_name: testauth', ' username: user', ' password: secret', ' - key_name: testauth2', ' username: user2', ' password: secret2' ]) }) sys.path.insert(0, modules_dir) import http_request import read_source sys.path.pop(0) mock_modules.return_value = [http_request, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) calls = [call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'http_request/resources/jenkins.groovy', 'setBasicDigestAuth', 'testauth', 'user', 'secret'], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'http_request/resources/jenkins.groovy', 'setBasicDigestAuth', 'testauth2', 'user2', 'secret2'], shell=False)] mock_subp.assert_has_calls(calls, any_order=True) assert 2 == mock_subp.call_count, "subprocess call should be equal to 2"
def setUp(self): self.conf_path = "tests/test_data/configuration.json" self.conf_content = open(self.conf_path).read() self.starting_path = "tests/test_data/images/" self.conf = tecaconf.ConfigHandler( self.conf_path, {"starting_path": self.starting_path} ) self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ "tests": { "test_data":{ "configuration.json" : self.conf_content, "images": { "cutegirlsarecute": { "yukinon.jpg": "", "charlotte.jpg": "", "misato.bmp": "" }, "hiddenfolder": { "uselessdoc.txt": "useless content", "uselessimage.png": "" }, "emptyFolder": {}, "ohwait": { "imagesonly": { "yukinon.jpg": "", "specialchàr.jpg": "", "thumb_lol.png": "" } }, "you": { "shall": { "notpass": { "kaiki.gif": "" }, "pass": { "eruna.jpg": "" } } } } } } })
def test_validation_fail_for_envs_required_property(self): # Create config without envs entry with open(jimmy_yaml_path, 'r') as f: mock_jimmy_yaml = "" for line in f: if line.startswith("envs:"): break mock_jimmy_yaml += line self.mfs = mockfs.replace_builtins() self.mfs.add_entries({jimmy_yaml_path: mock_jimmy_yaml, jimmy_schema_path: self.jimmy_schema}) schema = yaml_reader.read(jimmy_schema_path) jimmy_yaml_data = yaml_reader.read(jimmy_yaml_path) with pytest.raises(jsonschema.ValidationError) as excinfo: jsonschema.validate(jimmy_yaml_data, schema) assert excinfo.value.message == "'envs' is a required property"
def test_validation_fail_for_pipelines_required_property(self): self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ jimmy_schema_path: self.jimmy_schema, jimmy_yaml_path: '\n'.join([ 'module-directories:', ' - ./modules', 'defaults:', ' inject:', ' jenkins_cli_path: /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar', 'setup:', ' - name: setup', 'teardown:', ' - name: teardown', 'envs:', ' main:', ' jenkins_url: http://localhost:8080' ]) }) schema = yaml_reader.read(jimmy_schema_path) jimmy_yaml_data = yaml_reader.read(jimmy_yaml_path) with pytest.raises(jsonschema.ValidationError) as excinfo: jsonschema.validate(jimmy_yaml_data, schema) assert excinfo.value.message == "'pipelines' is a required property"
def test_validation_fail_for_envs_required_property(self): # Create config without envs entry with open(jimmy_yaml_path, 'r') as f: mock_jimmy_yaml = "" for line in f: if line.startswith("envs:"): break mock_jimmy_yaml += line self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ jimmy_yaml_path: mock_jimmy_yaml, jimmy_schema_path: self.jimmy_schema }) schema = yaml_reader.read(jimmy_schema_path) jimmy_yaml_data = yaml_reader.read(jimmy_yaml_path) with pytest.raises(jsonschema.ValidationError) as excinfo: jsonschema.validate(jimmy_yaml_data, schema) assert excinfo.value.message == "'envs' is a required property"
def test_read_configuration(self): print("Testing read_configuration()") from stallmanbot import read_configuration import sys import time import os import configparser fs = mockfs.replace_builtins() SESSION = "TELEGRAM" fs.add_entries({"configuration.conf" : "[TELEGRAM]\n" + \ "STALLBOT = abc:123456\n" + \ "STALLBOTADM = HelioLoureiro\n"}) sys.exit = MagicMock() error = MagicMock() print(" * correct configuration") cfg = read_configuration("configuration.conf") self.assertEqual(cfg.get(SESSION, "STALLBOT"), "abc:123456", "Parameter didn't match.") self.assertEqual(cfg.get(SESSION, "STALLBOTADM"), "HelioLoureiro", "Parameter didn't match.") print(" * missing session") SESSION = "FAKE" self.assertRaises(configparser.NoSectionError, cfg.get, SESSION, "STALLBOT") print(" * missing session using utf-8") SESSION = "FåKEçÉ" self.assertRaises(configparser.NoSectionError, cfg.get, SESSION, "STÁLLBÖT") print(" * missing parameter") SESSION = "TELEGRAM" self.assertRaises(configparser.NoOptionError, cfg.get, SESSION, "WHATEVER") mockfs.restore_builtins()
def test_read_file(self): print("Testing read_file()") from stallmanbot import read_file, error fs = mockfs.replace_builtins() fs.add_entries({ "/etc/python" : "zen of python", "/étç/två" : "ett två três"}) print(" * file exists and it is ASCII") result = read_file("/etc/python") self.assertEqual(result, "zen of python", "Failed to read content") print(" * file exists and it is UTF-8") result = read_file("/étç/två") self.assertEqual(result, "ett två três", "Failed to read content") print(" * file doesn't exist") result = read_file("/etc/helio_loureiro") self.assertEqual(result, None, "None return failed from non-existent file") mockfs.restore_builtins()
def test_cli_call(self, mock_subp, mock_modules): with open(slack_schema_path, 'r') as f: mock_slack_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, slack_schema_path: mock_slack_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' slack:', ' team_subdomain: slackteam', ' token: access-token', ' channel: "#build-notifications"', ' webhook_token: webhook-access-token', ' webhook_url: http://jenkins.example.com/slack-webhook/' ]) }) sys.path.insert(0, modules_dir) import slack import read_source sys.path.pop(0) mock_modules.return_value = [slack, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) mock_subp.assert_called_with( ['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'slack/resources/jenkins.groovy', 'setSlackConfig', 'slackteam', 'access-token', '', '#build-notifications', 'webhook-access-token', 'http://jenkins.example.com/slack-webhook/' ], shell=False) assert 1 == mock_subp.call_count, "subprocess call should be equal to 1"
def test_read_configuration(self): print("Testing read_configuration()") from educatuxbot import TelegramBotInterface import sys import time import os import configparser fs = mockfs.replace_builtins() SESSION = "TELEGRAM" fs.add_entries({"configuration.conf" : "[TELEGRAM]\n" + \ "EDUCATUXBOT = abc:123456\n" + \ "EDUCATUXBOTADMS = HelioLoureiro\n"}) sys.exit = MagicMock() error = MagicMock() print(" * correct configuration") cfg = TelegramBotInterface() cfg.config_file = "configuration.conf" cfg.read_configuration() self.assertEqual(cfg.settings["token"], "abc:123456", "Parameter didn't match.") self.assertEqual(cfg.settings["botadms"], "HelioLoureiro", "Parameter didn't match.")
def test_cli_call(self, mock_subp, mock_modules): with open(slack_schema_path, 'r') as f: mock_slack_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, slack_schema_path: mock_slack_schema, jenkins_yaml_path: '\n'.join([ 'jenkins:', ' slack:', ' team_subdomain: slackteam', ' token: access-token', ' channel: "#build-notifications"', ' webhook_token: webhook-access-token', ' webhook_url: http://jenkins.example.com/slack-webhook/' ]) }) sys.path.insert(0, modules_dir) import slack import read_source sys.path.pop(0) mock_modules.return_value = [slack, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) mock_subp.assert_called_with([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'slack/resources/jenkins.groovy', 'setSlackConfig', 'slackteam', 'access-token', '', '#build-notifications', 'webhook-access-token', 'http://jenkins.example.com/slack-webhook/' ], shell=False) assert 1 == mock_subp.call_count, "subprocess call should be equal to 1"
def test_check_if_run(self): print("Testing check_if_run()") from stallmanbot import check_if_run, PIDFILE import sys import time import os fs = mockfs.replace_builtins() fs.add_entries({"%s/.stallmanbot.pid" % os.environ["HOME"] : "12345", "/proc/12345/running" : "yes"}) sys.exit = MagicMock(return_value="exiting") os.getpid = MagicMock(return_value=12345) log = MagicMock() print(" * process exists and is running") print("mypid=%d" % os.getpid()) check_if_run() #sys.exit.assert_called_with(os.EX_OK) #print(" * process doesn't exist and isn't running") #fs.remove("%s/.stallmanbot.pid" % os.environ["HOME"]) #check_if_run() mockfs.restore_builtins()
def test_validation_fail_for_pipelines_required_property(self): self.mfs = mockfs.replace_builtins() self.mfs.add_entries({jimmy_schema_path: self.jimmy_schema, jimmy_yaml_path: '\n'.join( [ 'module-directories:', ' - ./modules', 'defaults:', ' inject:', ' jenkins_cli_path: /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar', 'setup:', ' - name: setup', 'teardown:', ' - name: teardown', 'envs:', ' main:', ' jenkins_url: http://localhost:8080' ]) }) schema = yaml_reader.read(jimmy_schema_path) jimmy_yaml_data = yaml_reader.read(jimmy_yaml_path) with pytest.raises(jsonschema.ValidationError) as excinfo: jsonschema.validate(jimmy_yaml_data, schema) assert excinfo.value.message == "'pipelines' is a required property"
def setup_method(self, method): with open(gearman_schema_path, 'r') as f: mock_gearman_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({gearman_schema_path: mock_gearman_schema}) self.schema = yaml_reader.read(gearman_schema_path)
def setup_method(self, method): with open(jenkins_schema_path, 'r') as f: mock_jenkins_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({jenkins_schema_path: mock_jenkins_schema}) self.schema = yaml_reader.read(jenkins_schema_path)
def test_cli_call(self, mock_subp, mock_modules): with open(jenkins_schema_path, 'r') as f: mock_jenkins_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, jenkins_schema_path: mock_jenkins_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' configuration:', ' admin_email: CI <*****@*****.**>', ' agent_tcp_port: 50000', ' location_url: http://example.com/jenkins/', ' markup_format: raw-html', ' num_of_executors: 2', ' scm_checkout_retry_count: 1' ]) }) sys.path.insert(0, modules_dir) import jenkins_configuration import read_source sys.path.pop(0) mock_modules.return_value = [jenkins_configuration, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) calls = [call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://*****:*****@example.com>'"], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'jenkins_configuration/resources/jenkins.groovy', 'setAgentTcpPort', '50000'], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'jenkins_configuration/resources/jenkins.groovy', 'setLocationUrl', "'http://example.com/jenkins/'"], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'jenkins_configuration/resources/jenkins.groovy', 'setMarkupFormatter', 'raw-html'], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'jenkins_configuration/resources/jenkins.groovy', 'setNumExecutors', '2'], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'jenkins_configuration/resources/jenkins.groovy', 'setScmCheckoutRetryCount', '1'], shell=False)] mock_subp.assert_has_calls(calls, any_order=True) assert 6 == mock_subp.call_count, "subprocess call should be equal to 6"
def test_cli_call_for_ldap_conf(self, mock_subp, mock_modules): with open(security_schema_path, 'r') as f: mock_security_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, security_schema_path: mock_security_schema, jenkins_yaml_path: '\n'.join( [ 'jenkins:', ' security:', ' ldap:', ' server: ldap://mirantis.com:3268', ' root_bind:', ' dn: dc=mirantis,dc=com', ' allow_blank: false', ' search:', ' user_filter: userPrincipalName={0}', ' manager:', ' name: [email protected]', ' password: passwd', ' access:', ' - name: amihura', ' permissions:', ' - overall', ' - credentials', ' - gerrit', ' cli_user:'******' name: jenkins-manager', ' public_key: sssh-rsa AAAAB3NzaC' ]) }) sys.path.insert(0, modules_dir) import security import read_source sys.path.pop(0) mock_modules.return_value = [security, read_source] os.chdir(jimmy_dir) result = self.runner.invoke(cli) print result.output calls = [call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://*****:*****@mirantis.com', 'passwd', 'jenkins-manager', 'sssh-rsa AAAAB3NzaC'], shell=False), call(['java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'security/resources/jenkins.groovy', 'setPermissionsMatrix', 'amihura', 'overall,credentials,gerrit', '', '', '', '', 'ldap'], shell=False)] mock_subp.assert_has_calls(calls, any_order=True) assert 2 == mock_subp.call_count, "subprocess call should be equal to 2"
def setUp(self): self.mfs = mockfs.replace_builtins()
def mockfs(request): mfs = replace_builtins() request.addfinalizer(lambda: restore_builtins()) return mfs
def setup(self): self.mfs = mockfs.replace_builtins() os.makedirs('/home')
def setup_method(self, method): with open(http_request_schema_path, 'r') as f: mock_http_request_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({http_request_schema_path: mock_http_request_schema}) self.schema = yaml_reader.read(http_request_schema_path)
def test_cli_call(self, mock_subp, mock_modules): with open(credentials_schema_path, 'r') as f: mock_credentials_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, credentials_schema_path: mock_credentials_schema, jenkins_yaml_path: '\n'.join([ 'jenkins:', ' credentials:', ' password:'******' - scope: global', ' username: user', ' password: passwd', ' description: test username/password user', ' ssh:', ' - scope: global', ' username: user2', ' private_key: /home/user/.ssh/id_rsa', ' id: this-is-an-id', ' file:', ' - scope: global', ' id: secret-key', ' file: /home/user/secret_key', ' description: Secret key', ' kubernetes:', ' - id: kubernetes-credentials', ' scope: global', ' description: kubernetes.example.com service creds', ' token:', ' - scope: global', ' username: user', ' id: user-token', ' description: test token credentials' ]) }) sys.path.insert(0, modules_dir) import credentials import read_source sys.path.pop(0) mock_modules.return_value = [credentials, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) calls = [ call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'credentials/resources/jenkins.groovy', 'updateCredentials', "'global'", "'user'", "'passwd'", "'test username/password user'", "''", "''", "''" ], shell=False), call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'credentials/resources/jenkins.groovy', 'updateCredentials', "'global'", "'user2'", "''", "''", "'/home/user/.ssh/id_rsa'", "''", "'this-is-an-id'" ], shell=False), call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'credentials/resources/jenkins.groovy', 'updateCredentials', "'global'", "''", "''", "'Secret key'", "''", "'/home/user/secret_key'", "'secret-key'" ], shell=False), call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'credentials/resources/kubernetes.groovy', 'updateCredentials', 'global', 'kubernetes-credentials', 'kubernetes.example.com service creds' ], shell=False), call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'credentials/resources/jenkins.groovy', 'updateCredentials', "'global'", "'user'", "''", "'test token credentials'", "''", "''", "'user-token'" ], shell=False) ] mock_subp.assert_has_calls(calls, any_order=True) assert 5 == mock_subp.call_count, "subprocess call should be equal to 5"
def test_cli_call(self, mock_subp, mock_modules): with open(jenkins_schema_path, 'r') as f: mock_jenkins_schema = f.read() self.mfs = mockfs.replace_builtins() self.mfs.add_entries({ os.path.join(jimmy_dir, 'lib', 'schema.yaml'): self.jimmy_schema, os.path.join(jimmy_dir, 'jimmy.yaml'): self.mock_jimmy_yaml, jenkins_schema_path: mock_jenkins_schema, jenkins_yaml_path: '\n'.join([ 'jenkins:', ' configuration:', ' admin_email: CI <*****@*****.**>', ' agent_tcp_port: 50000', ' location_url: http://example.com/jenkins/', ' markup_format: raw-html', ' num_of_executors: 2', ' scm_checkout_retry_count: 1' ]) }) sys.path.insert(0, modules_dir) import jenkins_configuration import read_source sys.path.pop(0) mock_modules.return_value = [jenkins_configuration, read_source] os.chdir(jimmy_dir) self.runner.invoke(cli) calls = [ call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://*****:*****@example.com>'" ], shell=False), call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'jenkins_configuration/resources/jenkins.groovy', 'setAgentTcpPort', '50000' ], shell=False), call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'jenkins_configuration/resources/jenkins.groovy', 'setLocationUrl', "'http://example.com/jenkins/'" ], shell=False), call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'jenkins_configuration/resources/jenkins.groovy', 'setMarkupFormatter', 'raw-html' ], shell=False), call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'jenkins_configuration/resources/jenkins.groovy', 'setNumExecutors', '2' ], shell=False), call([ 'java', '-jar', '<< path to jenkins-cli.jar >>', '-s', 'http://localhost:8080', 'groovy', modules_dir + '/' + 'jenkins_configuration/resources/jenkins.groovy', 'setScmCheckoutRetryCount', '1' ], shell=False) ] mock_subp.assert_has_calls(calls, any_order=True) assert 6 == mock_subp.call_count, "subprocess call should be equal to 6"