def test_never_run_condition(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) expected_job_name = "test_never_run_condition" jb = jk.create_job(expected_job_name, FreestyleJob) with clean_job(jb): expected_output = "Here is my sample output..." shell_builder = ShellBuilder.create("echo " + expected_output) condition = NeverRun.create() conditional_builder = ConditionalBuilder.create(condition, shell_builder) jb.add_builder(conditional_builder) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(expected_job_name).builders) builders = jk.find_job(expected_job_name).builders # Make sure the builder was correctly configured assert builders[0].condition is not None assert isinstance(builders[0].condition, NeverRun) assert builders[0].condition.get_friendly_name() == "never" # Finally, just to be sure our build actually did something relevant # we make sure the output from our shell command appears in the # build output for a build (ie: to ensure the conditional build step # actually ran) jb.start_build() async_assert(lambda: jb.last_build) assert expected_output not in jb.last_build.console_output
def test_never_run_condition(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) expected_job_name = "test_never_run_condition" jb = jk.create_job(expected_job_name, FreestyleJob) with clean_job(jb): expected_output = "Here is my sample output..." shell_builder = ShellBuilder.instantiate("echo " + expected_output) condition = NeverRun.instantiate() conditional_builder = ConditionalBuilder.instantiate(condition, shell_builder) jb.add_builder(conditional_builder) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(expected_job_name).builders) builders = jk.find_job(expected_job_name).builders # Make sure the builder was correctly configured assert builders[0].condition is not None assert isinstance(builders[0].condition, NeverRun) assert builders[0].condition.get_friendly_name() == "never" # Finally, just to be sure our build actually did something relevant # we make sure the output from our shell command appears in the # build output for a build (ie: to ensure the conditional build step # actually ran) jb.start_build() async_assert(lambda: jb.last_build) assert expected_output not in jb.last_build.console_output
def test_add_then_edit_build_blocker(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_add_then_edit_build_blocker" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): build_blocker = BuildBlockerProperty.create("MyCoolJob") jb.add_property(build_blocker) # edit the original build blocker object - changes should still get # applied to the underlying Jenkins job expected_job_names = ["SomeOtherJob1", "SomeOtherJob2"] build_blocker.blockers = expected_job_names # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).properties) # Now, get a clean reference to the job jb2 = jk.find_job(job_name) props = jb2.properties blockers = props[0].blockers assert isinstance(blockers, list) assert len(blockers) == 2 assert expected_job_names[0] in blockers assert expected_job_names[1] in blockers
def test_add_then_edit_build_blocker(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_add_then_edit_build_blocker" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): build_blocker = BuildBlockerProperty.instantiate("MyCoolJob") jb.add_property(build_blocker) # edit the original build blocker object - changes should still get # applied to the underlying Jenkins job expected_job_names = ["SomeOtherJob1", "SomeOtherJob2"] build_blocker.blockers = expected_job_names # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).properties) # Now, get a clean reference to the job jb2 = jk.find_job(job_name) props = jb2.properties blockers = props[0].blockers assert isinstance(blockers, list) assert len(blockers) == 2 assert expected_job_names[0] in blockers assert expected_job_names[1] in blockers
def test_artifacts_archived(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_artifacts_archived_job" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): publisher = ArtifactArchiverPublisher.create("*.txt") jb.add_publisher(publisher) # Wait until our publisher config get's applied async_assert(lambda: jk.find_job(job_name).publishers) expected_file = "test_artifacts_archived_job.txt" shell_builder = ShellBuilder.create("echo hello > " + expected_file) jb.add_builder(shell_builder) # Wait until our builder get's applied async_assert(lambda: jk.find_job(job_name).builders) # Next, trigger a build jb.start_build() async_assert(lambda: len(jb.all_builds) == 1) # finally, make sure the list or archived artifacts looks correct bld = jb.all_builds[0] results = bld.artifact_urls assert isinstance(results, list) assert len(results) == 1 assert expected_file in results[0]
def test_artifacts_archived(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_artifacts_archived_job" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): publisher = ArtifactArchiverPublisher.instantiate("*.txt") jb.add_publisher(publisher) # Wait until our publisher config get's applied async_assert(lambda: jk.find_job(job_name).publishers) expected_file = "test_artifacts_archived_job.txt" shell_builder = ShellBuilder.instantiate("echo hello > " + expected_file) jb.add_builder(shell_builder) # Wait until our builder get's applied async_assert(lambda: jk.find_job(job_name).builders) # Next, trigger a build jb.start_build() async_assert(lambda: len(jb.all_builds) == 1) # finally, make sure the list or archived artifacts looks correct bld = jb.all_builds[0] results = bld.artifact_urls assert isinstance(results, list) assert len(results) == 1 assert expected_file in results[0]
def test_add_string_parameter(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_add_string_parameter" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): expected_param_name = "param1" expected_param_val = "fubar" expected_param_desc = "My Description" expected_param_trim = False str_param = ParameterizedBuildStringParameter.create( expected_param_name, expected_param_val, expected_param_desc, expected_param_trim) build_params = ParameterizedBuild.create([str_param]) jb.add_property(build_params) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).properties) properties = jk.find_job(job_name).properties assert isinstance(properties, list) assert len(properties) == 1 assert isinstance(properties[0], ParameterizedBuild) params = properties[0].parameters assert isinstance(params, list) assert len(params) == 1 act_param = params[0] assert isinstance(act_param, ParameterizedBuildStringParameter) assert act_param.name == expected_param_name assert act_param.default_value == expected_param_val assert act_param.description == expected_param_desc assert act_param.trim == expected_param_trim
def test_always_run_condition(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) expected_job_name = "test_always_run_condition" jb = jk.create_job(expected_job_name, FreestyleJob) with clean_job(jb): expected_output = "Here is my sample output..." expected_cmd = "echo " + expected_output shell_builder = ShellBuilder.create(expected_cmd) condition = AlwaysRun.create() conditional_builder = ConditionalBuilder.create(condition, shell_builder) jb.add_builder(conditional_builder) # Wait until our job config has been applied successfully async_assert(lambda: jk.find_job(expected_job_name).builders) # Make sure the condition is loaded correctly builders = jk.find_job(expected_job_name).builders assert isinstance(builders[0].condition, AlwaysRun) assert builders[0].condition.get_friendly_name() == "always" # Run a build and make sure the build operation actually executed jb.start_build() async_assert(lambda: jb.last_build) assert expected_output in jb.last_build.console_output
def test_trigger_with_current_build_params(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) upstream_name = "test_trigger_with_current_build_params" jb = jk.create_job(upstream_name, FreestyleJob) with clean_job(jb): downstream_name = "sample_job" new_trigger = BuildTriggerConfig.instantiate([downstream_name]) cur_bld_params = CurrentBuildParams.instantiate() new_trigger.add_build_param(cur_bld_params) new_pub = ParameterizedBuildTrigger.instantiate([new_trigger]) jb.add_publisher(new_pub) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(upstream_name).publishers) pubs = jk.find_job(upstream_name).publishers assert len(pubs) == 1 cur_pub = pubs[0] assert len(cur_pub.triggers) == 1 cur_trig = cur_pub.triggers[0] assert len(cur_trig.build_params) == 1 cur_param_cfg = cur_trig.build_params[0] assert isinstance(cur_param_cfg, CurrentBuildParams)
def test_param_trigger(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) upstream_name = "test_param_trigger" jb = jk.create_job(upstream_name, FreestyleJob) with clean_job(jb): downstream_name = "sample_job" new_trigger = BuildTriggerConfig.instantiate([downstream_name]) pub = ParameterizedBuildTrigger.instantiate([new_trigger]) jb.add_publisher(pub) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(upstream_name).publishers) pubs = jk.find_job(upstream_name).publishers assert isinstance(pubs, list) assert len(pubs) == 1 assert isinstance(pubs[0], ParameterizedBuildTrigger) triggers = pubs[0].triggers assert isinstance(triggers, list) assert len(triggers) == 1 names = triggers[0].job_names assert isinstance(names, list) assert len(names) == 1 assert names[0] == downstream_name assert triggers[0].condition == "SUCCESS"
def test_basic_publisher(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) upstream_name = "test_basic_publisher" jb = jk.create_job(upstream_name, FreestyleJob) with clean_job(jb): expected_regex = "*.log" new_condition = AlwaysRun.instantiate() new_pub = ArtifactArchiverPublisher.instantiate(expected_regex) new_action = ConditionalAction.instantiate(new_condition, [new_pub]) pub = FlexiblePublisher.instantiate([new_action]) jb.add_publisher(pub) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(upstream_name).publishers) pubs = jk.find_job(upstream_name).publishers assert isinstance(pubs, list) assert len(pubs) == 1 cur_pub = pubs[0] assert isinstance(cur_pub, FlexiblePublisher) assert isinstance(cur_pub.actions, list) assert len(cur_pub.actions) == 1 cur_action = cur_pub.actions[0] assert isinstance(cur_action, ConditionalAction) assert isinstance(cur_action.publishers, list) assert len(cur_action.publishers) == 1 cur_nested_pub = cur_action.publishers[0] assert isinstance(cur_nested_pub, ArtifactArchiverPublisher) assert cur_nested_pub.artifact_regex == expected_regex
def test_add_artifact_deployer_publisher_entry(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_add_artifact_deployer_publisher_entry" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): publisher = ArtifactDeployer.instantiate() jb.add_publisher(publisher) expected_regex = "*.txt" expected_path = "/bin/data" entry = ArtifactDeployerEntry.instantiate(expected_regex, expected_path) publisher.add_entry(entry) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).publishers) pubs = jk.find_job(job_name).publishers assert len(pubs) == 1 cur_pub = pubs[0] assert isinstance(cur_pub, ArtifactDeployer) assert isinstance(cur_pub.entries, list) assert len(cur_pub.entries) == 1 cur_entry = cur_pub.entries[0] assert isinstance(cur_entry, ArtifactDeployerEntry) assert cur_entry.includes == expected_regex assert cur_entry.remote == expected_path
def test_add_then_edit_unstable_return_code(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_add_then_edit_unstable_return_code" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): jb.quiet_period = 0 rcode = 12 failing_step = ShellBuilder.create("exit " + str(rcode)) failing_step.unstable_return_code = 1 jb.add_builder(failing_step) # Edit the build step using the original failed_step object - # these changes should still get applied to the job the step is # associated with failing_step.unstable_return_code = rcode # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).builders) jb2 = jk.find_job(job_name) # run a build to see if the changes were auto applied jb2.start_build() async_assert(lambda: jb2.last_build) bld = jb2.last_build # Because of our changes to the configuration, the returned error code # should have resulted in an unstable build instead of a failed build assert bld.result == "UNSTABLE"
def test_always_run_condition(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) expected_job_name = "test_always_run_condition" jb = jk.create_job(expected_job_name, FreestyleJob) with clean_job(jb): expected_output = "Here is my sample output..." expected_cmd = "echo " + expected_output shell_builder = ShellBuilder.instantiate(expected_cmd) condition = AlwaysRun.instantiate() conditional_builder = ConditionalBuilder.instantiate(condition, shell_builder) jb.add_builder(conditional_builder) # Wait until our job config has been applied successfully async_assert(lambda: jk.find_job(expected_job_name).builders) # Make sure the condition is loaded correctly builders = jk.find_job(expected_job_name).builders assert isinstance(builders[0].condition, AlwaysRun) assert builders[0].condition.get_friendly_name() == "always" # Run a build and make sure the build operation actually executed jb.start_build() async_assert(lambda: jb.last_build) assert expected_output in jb.last_build.console_output
def test_add_conditional_builder(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) expected_job_name = "test_add_conditional_builder" jb = jk.create_job(expected_job_name, FreestyleJob) with clean_job(jb): expected_output = "Here is my sample output..." expected_cmd = "echo " + expected_output shell_builder = ShellBuilder.instantiate(expected_cmd) condition = AlwaysRun.instantiate() conditional_builder = ConditionalBuilder.instantiate(condition, shell_builder) jb.add_builder(conditional_builder) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(expected_job_name).builders) builders = jk.find_job(expected_job_name).builders # Make sure the builder was successfully added and it's response # data can be parsed correctly assert isinstance(builders, list) assert len(builders) == 1 assert isinstance(builders[0], ConditionalBuilder) assert builders[0].builder is not None assert isinstance(builders[0].builder, ShellBuilder) assert builders[0].builder.script == expected_cmd assert_elements_equal(builders[0].builder.node, shell_builder.node)
def test_add_conditional_builder(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) expected_job_name = "test_add_conditional_builder" jb = jk.create_job(expected_job_name, FreestyleJob) with clean_job(jb): expected_output = "Here is my sample output..." expected_cmd = "echo " + expected_output shell_builder = ShellBuilder.create(expected_cmd) condition = AlwaysRun.create() conditional_builder = ConditionalBuilder.create(condition, shell_builder) jb.add_builder(conditional_builder) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(expected_job_name).builders) builders = jk.find_job(expected_job_name).builders # Make sure the builder was successfully added and it's response # data can be parsed correctly assert isinstance(builders, list) assert len(builders) == 1 assert isinstance(builders[0], ConditionalBuilder) assert builders[0].builder is not None assert isinstance(builders[0].builder, ShellBuilder) assert builders[0].builder.script == expected_cmd assert_elements_equal(builders[0].builder.node, shell_builder.node)
def test_disable_build_blocker(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_disable_build_blocker" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): build_blocker = BuildBlockerProperty.instantiate("MyJob") build_blocker.disable() jb.quiet_period = 0 jb.add_property(build_blocker) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).properties) properties = jk.find_job(job_name).properties assert properties[0].is_enabled is False
def test_find_job_no_jobs(self): mock_data_io = MagicMock() mock_data_io.get_api_data.return_value = {'jobs':{}} j = Jenkins(mock_data_io) njob = j.find_job("MyJob") self.assertEqual(njob, None, "No jobs should be found by the search method")
def test_wait_for_idle(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) node = jk.nodes[0] expected_job_name = "test_wait_for_idle_job" jb = jk.create_job(expected_job_name, FreestyleJob) with clean_job(jb): jb.quiet_period = 0 shell_builder = ShellBuilder.instantiate("sleep 2") jb.add_builder(shell_builder) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(expected_job_name).builders) # Trigger a build jb.start_build() # The 'last_build' reference becomes available as soon as the previously # triggered build exits the queue and starts running. So we wait for the # last build to become valid before checking the node activity async_assert(lambda: jb.last_build) assert node.is_idle is False assert node.wait_for_idle() assert node.is_idle
def test_build_blocker_functionality(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name1 = "test_build_blocker_functionality1" jb1 = jk.create_job(job_name1, FreestyleJob) with clean_job(jb1): job_name2 = "test_build_blocker_functionality2" jb2 = jk.create_job(job_name2, FreestyleJob) with clean_job(jb2): expected_jobs = job_name2 build_blocker = BuildBlockerProperty.create(expected_jobs) jb1.quiet_period = 0 jb1.add_property(build_blocker) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name1).properties) build_step = ShellBuilder.create("sleep 10") jb2.quiet_period = 0 jb2.add_builder(build_step) async_assert(lambda: jb2.builders) queue2 = jb2.start_build() async_assert(lambda: not queue2.waiting) queue1 = jb1.start_build() assert job_name2 in queue1.reason queue2.build.abort() assert queue1.waiting is False
def test_build_git_scm(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_build_git_scm" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): jb.quiet_period = 0 expected_url = "https://github.com/TheFriendlyCoder/pyjen.git" test_scm = GitSCM.instantiate(expected_url) jb.scm = test_scm async_assert(lambda: isinstance(jb.scm, GitSCM)) # If the Git SCM was set up correctly, the job should check out the # source code for pyjen into the workspace when building. That being # the case there should be a setup.py script in the root folder. We # can therefore check to see if the SCM operation completed successfully # by looking for that file and setting a non-zero error code as part # of a shell builder operation shell_builder = ShellBuilder.instantiate("[ -f setup.py ]") jb.add_builder(shell_builder) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).builders) jb.start_build() async_assert(lambda: jb.last_good_build or jb.last_failed_build) assert jb.last_good_build is not None
def test_build_git_scm(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_build_git_scm" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): jb.quiet_period = 0 expected_url = "https://github.com/TheFriendlyCoder/pyjen.git" test_scm = GitSCM.create(expected_url) jb.scm = test_scm async_assert(lambda: isinstance(jb.scm, GitSCM)) # If the Git SCM was set up correctly, the job should check out the # source code for pyjen into the workspace when building. That being # the case there should be a setup.py script in the root folder. We # can therefore check to see if the SCM operation completed successfully # by looking for that file and setting a non-zero error code as part # of a shell builder operation shell_builder = ShellBuilder.create("[ -f setup.py ]") jb.add_builder(shell_builder) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).builders) jb.start_build() async_assert(lambda: jb.last_good_build or jb.last_failed_build) assert jb.last_good_build is not None
def test_build_blocker_functionality(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name1 = "test_build_blocker_functionality1" jb1 = jk.create_job(job_name1, FreestyleJob) with clean_job(jb1): job_name2 = "test_build_blocker_functionality2" jb2 = jk.create_job(job_name2, FreestyleJob) with clean_job(jb2): expected_jobs = job_name2 build_blocker = BuildBlockerProperty.instantiate(expected_jobs) jb1.quiet_period = 0 jb1.add_property(build_blocker) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name1).properties) build_step = ShellBuilder.instantiate("sleep 10") jb2.quiet_period = 0 jb2.add_builder(build_step) async_assert(lambda: jb2.builders) queue2 = jb2.start_build() async_assert(lambda: not queue2.waiting) queue1 = jb1.start_build() assert job_name2 in queue1.reason queue2.build.abort() assert queue1.waiting is False
def test_delete_job(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) expected_name = "test_delete_job" jb = jk.create_job(expected_name, FreestyleJob) jb.delete() res = jk.find_job(expected_name) assert res is None
def test_wait_for_idle(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) node = jk.nodes[0] expected_job_name = "test_wait_for_idle_job" jb = jk.create_job(expected_job_name, FreestyleJob) with clean_job(jb): jb.quiet_period = 0 shell_builder = ShellBuilder.create("sleep 2") jb.add_builder(shell_builder) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(expected_job_name).builders) # Trigger a build jb.start_build() # The 'last_build' reference becomes available as soon as the previously # triggered build exits the queue and starts running. So we wait for the # last build to become valid before checking the node activity async_assert(lambda: jb.last_build) assert node.is_idle is False assert node.wait_for_idle() assert node.is_idle
def test_default_block_level(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_default_block_level" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): expected_jobs = ["MyCoolJob1", "MyCoolJob2"] build_blocker = BuildBlockerProperty.instantiate(expected_jobs) jb.add_property(build_blocker) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).properties) properties = jk.find_job(job_name).properties prop = properties[0] assert prop.level == "GLOBAL"
def test_add_build_blocker(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_add_build_blocker" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): expected_job_name = "MyCoolJob" build_blocker = BuildBlockerProperty.instantiate(expected_job_name) jb.add_property(build_blocker) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).properties) properties = jk.find_job(job_name).properties assert isinstance(properties, list) assert len(properties) == 1 assert isinstance(properties[0], BuildBlockerProperty)
def test_disable_build_blocker(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_disable_build_blocker" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): build_blocker = BuildBlockerProperty.create("MyJob") build_blocker.disable() jb.quiet_period = 0 jb.add_property(build_blocker) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).properties) properties = jk.find_job(job_name).properties assert properties[0].is_enabled is False
def test_default_block_level(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_default_block_level" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): expected_jobs = ["MyCoolJob1", "MyCoolJob2"] build_blocker = BuildBlockerProperty.create(expected_jobs) jb.add_property(build_blocker) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).properties) properties = jk.find_job(job_name).properties prop = properties[0] assert prop.level == "GLOBAL"
def test_disable_custom_workspace(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) jb = jk.create_job("test_disable_custom_workspace", FreestyleJob) with clean_job(jb): jb.custom_workspace = "/delme" jb2 = jk.find_job(jb.name) jb2.custom_workspace = "" assert jb2.custom_workspace_enabled is False
def test_add_build_blocker(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_add_build_blocker" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): expected_job_name = "MyCoolJob" build_blocker = BuildBlockerProperty.create(expected_job_name) jb.add_property(build_blocker) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).properties) properties = jk.find_job(job_name).properties assert isinstance(properties, list) assert len(properties) == 1 assert isinstance(properties[0], BuildBlockerProperty)
def test_disable_assigned_node(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) jb = jk.create_job("test_disable_assigned_node", FreestyleJob) with clean_job(jb): jb.assigned_node = "MyNodeLabel && UnknownLabel" jb2 = jk.find_job(jb.name) jb2.assigned_node = "" assert jb2.assigned_node_enabled is False
def test_custom_queue_scan(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_custom_queue_scan" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): expected_jobs = ["MyCoolJob1", "MyCoolJob2"] expected_type = "ALL" build_blocker = BuildBlockerProperty.instantiate(expected_jobs) build_blocker.queue_scan = expected_type jb.add_property(build_blocker) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).properties) properties = jk.find_job(job_name).properties prop = properties[0] assert prop.queue_scan == expected_type
def test_add_artifact_archiver_publisher(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_add_artifact_archiver_publisher_job" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): expected_regex = "*.txt" publisher = ArtifactArchiverPublisher.instantiate(expected_regex) jb.add_publisher(publisher) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).publishers) pubs = jk.find_job(job_name).publishers assert isinstance(pubs, list) assert len(pubs) == 1 assert isinstance(pubs[0], ArtifactArchiverPublisher) assert pubs[0].artifact_regex == expected_regex
def test_add_artifact_deployer_publisher(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_add_artifact_deployer_publisher" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): publisher = ArtifactDeployer.instantiate() jb.add_publisher(publisher) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).publishers) pubs = jk.find_job(job_name).publishers assert isinstance(pubs, list) assert len(pubs) == 1 assert isinstance(pubs[0], ArtifactDeployer) assert isinstance(pubs[0].entries, list) assert len(pubs[0].entries) == 0
def test_add_artifact_archiver_publisher(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_add_artifact_archiver_publisher_job" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): expected_regex = "*.txt" publisher = ArtifactArchiverPublisher.create(expected_regex) jb.add_publisher(publisher) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).publishers) pubs = jk.find_job(job_name).publishers assert isinstance(pubs, list) assert len(pubs) == 1 assert isinstance(pubs[0], ArtifactArchiverPublisher) assert pubs[0].artifact_regex == expected_regex
def test_disable_quiet_period(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) jb = jk.create_job("test_disable_quiet_period", FreestyleJob) with clean_job(jb): jb.quiet_period = 0 jb2 = jk.find_job(jb.name) assert jb2 is not None jb2.quiet_period = -1 assert jb2.quiet_period_enabled is False
def test_assigned_node(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) jb = jk.create_job("test_assigned_node", FreestyleJob) with clean_job(jb): expected_label = "MyNodeLabel && UnknownLabel" jb.assigned_node = expected_label jb2 = jk.find_job(jb.name) assert jb2.assigned_node_enabled is True assert jb2.assigned_node == expected_label
def test_custom_queue_scan(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_custom_queue_scan" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): expected_jobs = ["MyCoolJob1", "MyCoolJob2"] expected_type = "ALL" build_blocker = BuildBlockerProperty.create(expected_jobs) build_blocker.queue_scan = expected_type jb.add_property(build_blocker) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).properties) properties = jk.find_job(job_name).properties prop = properties[0] assert prop.queue_scan == expected_type
def test_add_simple_shell_builder(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_add_simple_shell_builder" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): expected_script = "echo hello" shell_builder = ShellBuilder.create(expected_script) jb.add_builder(shell_builder) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).builders) builders = jk.find_job(job_name).builders assert isinstance(builders, list) assert len(builders) == 1 assert isinstance(builders[0], ShellBuilder) assert builders[0].script == expected_script assert builders[0].unstable_return_code is None
def test_unstable_return_code(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_unstable_return_code" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): rcode = 12 failing_step = ShellBuilder.create("exit " + str(rcode)) failing_step.unstable_return_code = rcode jb.add_builder(failing_step) async_assert(lambda: jb.builders) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).builders) builders = jk.find_job(job_name).builders assert isinstance(builders, list) assert len(builders) == 1 assert builders[0].unstable_return_code == rcode
def test_rename_job(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) original_job_name = "test_rename_job1" new_job_name = "test_rename_job2" jb = jk.create_job(original_job_name, FreestyleJob) try: jb.rename(new_job_name) assert jk.find_job(original_job_name) is None jb_copy = jk.find_job(new_job_name) assert jb_copy is not None assert jb.name == new_job_name assert jb_copy == jb finally: tmp = jk.find_job(original_job_name) if tmp: tmp.delete() tmp = jk.find_job(new_job_name) if tmp: tmp.delete()
def test_multiple_blocking_jobs(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_multiple_blocking_jobs" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): expected_jobs = ["MyCoolJob1", "MyCoolJob2"] build_blocker = BuildBlockerProperty.instantiate(["ShouldNotSeeMe"]) build_blocker.blockers = expected_jobs jb.add_property(build_blocker) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).properties) properties = jk.find_job(job_name).properties prop = properties[0] blockers = prop.blockers assert isinstance(blockers, list) assert len(blockers) == 2 assert expected_jobs[0] in blockers assert expected_jobs[1] in blockers
def test_multiple_blocking_jobs(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_multiple_blocking_jobs" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): expected_jobs = ["MyCoolJob1", "MyCoolJob2"] build_blocker = BuildBlockerProperty.create(["ShouldNotSeeMe"]) build_blocker.blockers = expected_jobs jb.add_property(build_blocker) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(job_name).properties) properties = jk.find_job(job_name).properties prop = properties[0] blockers = prop.blockers assert isinstance(blockers, list) assert len(blockers) == 2 assert expected_jobs[0] in blockers assert expected_jobs[1] in blockers
def test_add_build_trigger_publisher(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) upstream_name = "test_add_build_trigger_publisher1" jb = jk.create_job(upstream_name, FreestyleJob) with clean_job(jb): downstream_name = "test_add_build_trigger_publisher2" jb2 = jk.create_job(downstream_name, FreestyleJob) with clean_job(jb2): publisher = BuildTriggerPublisher.instantiate([downstream_name]) jb.add_publisher(publisher) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(upstream_name).publishers) pubs = jk.find_job(upstream_name).publishers assert isinstance(pubs, list) assert len(pubs) == 1 assert isinstance(pubs[0], BuildTriggerPublisher) names = pubs[0].job_names assert isinstance(names, list) assert len(names) == 1 assert names[0] == downstream_name
def test_add_git_scm(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) job_name = "test_add_git_scm" jb = jk.create_job(job_name, FreestyleJob) with clean_job(jb): expected_url = "https://github.com/TheFriendlyCoder/pyjen.git" test_scm = GitSCM.instantiate(expected_url) jb.scm = test_scm async_assert(lambda: isinstance(jb.scm, GitSCM)) jb2 = jk.find_job(job_name) result = jb2.scm assert result is not None assert isinstance(result, GitSCM)
def test_console_text(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) expected_job_name = "test_console_text_job" jb = jk.create_job(expected_job_name, FreestyleJob) with clean_job(jb): jb.quiet_period = 0 expected_output = "Here is my sample output..." shell_builder = ShellBuilder.instantiate("echo " + expected_output) jb.add_builder(shell_builder) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(expected_job_name).builders) # Trigger a build and wait for it to complete jb.start_build() async_assert(lambda: jb.last_build) assert expected_output in jb.last_build.console_output
def test_abort(jenkins_env): jk = Jenkins(jenkins_env["url"], (jenkins_env["admin_user"], jenkins_env["admin_token"])) expected_job_name = "test_abort" jb = jk.create_job(expected_job_name, FreestyleJob) with clean_job(jb): jb.quiet_period = 0 shell_builder = ShellBuilder.instantiate("echo 'waiting for sleep' && sleep 40") jb.add_builder(shell_builder) # Get a fresh copy of our job to ensure we have an up to date # copy of the config.xml for the job async_assert(lambda: jk.find_job(expected_job_name).builders) # Trigger a build and wait for it to complete jb.start_build() async_assert(lambda: jb.last_build) async_assert(lambda: "waiting for sleep" in jb.last_build.console_output) jb.last_build.abort() assert jb.last_build.is_building is False assert jb.last_build.result == "ABORTED"