From a3d4fc92c1cfc2431adb55255793e0077c0a5bd1 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Wed, 29 Jul 2015 15:12:58 -0400 Subject: [PATCH] 6592: Add crunch-job integration tests. --- sdk/cli/bin/crunch-job | 4 +- sdk/cli/test/binstub_clean_fail/mount | 3 + sdk/cli/test/binstub_docker_noop/docker.io | 2 + sdk/cli/test/binstub_sanity_check/docker.io | 2 + sdk/cli/test/binstub_sanity_check/true | 2 + sdk/cli/test/test_crunch-job.rb | 125 ++++++++++++++++++++ 6 files changed, 136 insertions(+), 2 deletions(-) create mode 100755 sdk/cli/test/binstub_clean_fail/mount create mode 100755 sdk/cli/test/binstub_docker_noop/docker.io create mode 100755 sdk/cli/test/binstub_sanity_check/docker.io create mode 100755 sdk/cli/test/binstub_sanity_check/true create mode 100644 sdk/cli/test/test_crunch-job.rb diff --git a/sdk/cli/bin/crunch-job b/sdk/cli/bin/crunch-job index f03d432a9c..13001e7f92 100755 --- a/sdk/cli/bin/crunch-job +++ b/sdk/cli/bin/crunch-job @@ -126,7 +126,7 @@ my $jobspec; my $job_api_token; my $no_clear_tmp; my $resume_stash; -my $docker_bin = "/usr/bin/docker.io"; +my $docker_bin = "docker.io"; GetOptions('force-unlock' => \$force_unlock, 'git-dir=s' => \$git_dir, 'job=s' => \$jobspec, @@ -598,7 +598,7 @@ else { unless ($? == 0 && $sha1 =~ /^([0-9a-f]{40})$/) { croak("`$gitcmd rev-list` exited " .exit_status_s($?) - .", '$treeish' not found. Giving up."); + .", '$treeish' not found, giving up"); } $commit = $1; Log(undef, "Version $treeish is commit $commit"); diff --git a/sdk/cli/test/binstub_clean_fail/mount b/sdk/cli/test/binstub_clean_fail/mount new file mode 100755 index 0000000000..961ac28a9f --- /dev/null +++ b/sdk/cli/test/binstub_clean_fail/mount @@ -0,0 +1,3 @@ +#!/bin/sh +echo >&2 Failing mount stub was called +exit 1 diff --git a/sdk/cli/test/binstub_docker_noop/docker.io b/sdk/cli/test/binstub_docker_noop/docker.io new file mode 100755 index 0000000000..af3a4e4bec --- /dev/null +++ b/sdk/cli/test/binstub_docker_noop/docker.io @@ -0,0 +1,2 @@ +#!/bin/sh +true diff --git a/sdk/cli/test/binstub_sanity_check/docker.io b/sdk/cli/test/binstub_sanity_check/docker.io new file mode 100755 index 0000000000..8f1569deca --- /dev/null +++ b/sdk/cli/test/binstub_sanity_check/docker.io @@ -0,0 +1,2 @@ +#!/bin/sh +exit 8 diff --git a/sdk/cli/test/binstub_sanity_check/true b/sdk/cli/test/binstub_sanity_check/true new file mode 100755 index 0000000000..4b88b91e4c --- /dev/null +++ b/sdk/cli/test/binstub_sanity_check/true @@ -0,0 +1,2 @@ +#!/bin/sh +exit 7 diff --git a/sdk/cli/test/test_crunch-job.rb b/sdk/cli/test/test_crunch-job.rb new file mode 100644 index 0000000000..4aea60aa00 --- /dev/null +++ b/sdk/cli/test/test_crunch-job.rb @@ -0,0 +1,125 @@ +require 'minitest/autorun' + +class TestCrunchJob < Minitest::Test + SPECIAL_EXIT = { + EX_RETRY_UNLOCKED: 93, + EX_TEMPFAIL: 75, + } + + JOBSPEC = { + grep_local: { + script: 'grep', + script_version: 'master', + repository: File.absolute_path('../../../..', __FILE__), + script_parameters: {foo: 'bar'}, + }, + } + + def setup + end + + def crunchjob + File.absolute_path '../../bin/crunch-job', __FILE__ + end + + # Return environment suitable for running crunch-job. + def crunchenv opts={} + env = ENV.to_h + env['PERLLIB'] = File.absolute_path('../../../perl/lib', __FILE__) + env + end + + def jobspec label + JOBSPEC[label].dup + end + + # Encode job record to json and run it with crunch-job. + # + # opts[:binstubs] is an array of X where ./binstub_X is added to + # PATH in order to mock system programs. + def tryjobrecord jobrecord, opts={} + env = crunchenv + (opts[:binstubs] || []).each do |binstub| + env['PATH'] = File.absolute_path('../binstub_'+binstub, __FILE__) + ':' + env['PATH'] + end + system env, crunchjob, '--job', jobrecord.to_json + end + + def test_bogus_json + out, err = capture_subprocess_io do + system crunchenv, crunchjob, '--job', '"}{"' + end + assert_equal false, $?.success? + # Must not conflict with our special exit statuses + assert_jobfail $? + assert_match /JSON/, err + end + + def test_fail_sanity_check + out, err = capture_subprocess_io do + j = {} + tryjobrecord j, binstubs: ['sanity_check'] + end + assert_equal 75, $?.exitstatus + assert_match /Sanity check failed: 7/, err + end + + def test_fail_docker_sanity_check + out, err = capture_subprocess_io do + j = {} + j[:docker_image_locator] = '4d449b9d34f2e2222747ef79c53fa3ff+1234' + tryjobrecord j, binstubs: ['sanity_check'] + end + assert_equal 75, $?.exitstatus + assert_match /Sanity check failed: 8/, err + end + + def test_no_script_specified + out, err = capture_subprocess_io do + j = jobspec :grep_local + j.delete :script + tryjobrecord j + end + assert_match /No script specified/, err + assert_jobfail $? + end + + def test_fail_clean_tmp + out, err = capture_subprocess_io do + j = jobspec :grep_local + tryjobrecord j, binstubs: ['clean_fail'] + end + assert_match /Failing mount stub was called/, err + assert_match /Clean work dirs: exit 1\n$/, err + assert_equal SPECIAL_EXIT[:EX_RETRY_UNLOCKED], $?.exitstatus + end + + def test_docker_image_missing + skip 'API bug: it refuses to create this job in Running state' + out, err = capture_subprocess_io do + j = jobspec :grep_local + j[:docker_image_locator] = '4d449b9d34f2e2222747ef79c53fa3ff+1234' + tryjobrecord j, binstubs: ['docker_noop'] + end + assert_match /No Docker image hash found from locator/, err + assert_jobfail $? + end + + def test_script_version_not_found_in_repository + bogus_version = 'f8b72707c1f5f740dbf1ed56eb429a36e0dee770' + out, err = capture_subprocess_io do + j = jobspec :grep_local + j[:script_version] = bogus_version + tryjobrecord j + end + assert_match /'#{bogus_version}' not found, giving up/, err + assert_jobfail $? + end + + # Ensure procstatus is not interpreted as a temporary infrastructure + # problem. Would be assert_http_4xx if this were http. + def assert_jobfail procstatus + refute_includes SPECIAL_EXIT.values, procstatus.exitstatus + assert_equal false, procstatus.success? + end +end -- 2.39.5