<s> import os <EOL> import tempfile <EOL> import shutil <EOL> import contextlib <EOL> import mock <EOL> import pytest <EOL> from flask . ext . webtest import SessionScope <EOL> import kozmic . builds . tasks <EOL> from kozmic import docker <EOL> from kozmic . models import DeployKey , Build , Job <EOL> from . import TestCase , factories , utils <EOL> @ pytest . mark . docker <EOL> class TestDoJob ( TestCase ) : <EOL> def setup_method ( self , method ) : <EOL> TestCase . setup_method ( self , method ) <EOL> self . working_dir = tempfile . mkdtemp ( ) <EOL> repo_dir = os . path . join ( self . working_dir , '<STR_LIT>' ) <EOL> self . prev_head_sha = utils . create_git_repo ( repo_dir ) <EOL> self . head_sha = utils . add_commit_to_git_repo ( repo_dir ) <EOL> self . user = factories . UserFactory . create ( ) <EOL> self . project = factories . ProjectFactory . create ( <EOL> owner = self . user , <EOL> gh_https_clone_url = '<STR_LIT>' , <EOL> gh_ssh_clone_url = '<STR_LIT>' ) <EOL> self . hook = factories . HookFactory . create ( <EOL> project = self . project , <EOL> docker_image = '<STR_LIT>' , <EOL> install_script = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> build_script = '<STR_LIT>' ) <EOL> def _do_job ( self , hook_call ) : <EOL> @ contextlib . contextmanager <EOL> def create_temp_dir ( ) : <EOL> working_dir = tempfile . mktemp ( ) <EOL> shutil . copytree ( self . working_dir , working_dir ) <EOL> yield working_dir <EOL> shutil . rmtree ( working_dir ) <EOL> with SessionScope ( self . db ) : <EOL> with mock . patch . object ( Build , '<STR_LIT>' ) , mock . patch . object ( DeployKey , '<STR_LIT>' ) as ensure_deploy_key_mock , mock . patch . object ( Job , '<STR_LIT>' , return_value = '<STR_LIT>' ) , mock . patch ( '<STR_LIT>' , create_temp_dir ) : <EOL> kozmic . builds . tasks . do_job ( hook_call_id = hook_call . id ) <EOL> self . db . session . rollback ( ) <EOL> if hook_call . hook . project . is_public : <EOL> assert not ensure_deploy_key_mock . called <EOL> return Job . query . filter_by ( hook_call = hook_call ) . first ( ) <EOL> def test_private_project ( self ) : <EOL> cache_id = '<STR_LIT>' <EOL> cached_image = '<STR_LIT>' . format ( cache_id ) <EOL> try : <EOL> for image_data in docker . images ( cached_image ) : <EOL> for repo_tag in image_data [ '<STR_LIT>' ] : <EOL> if repo_tag . startswith ( cached_image ) : <EOL> docker . remove_image ( image_data [ '<STR_LIT>' ] ) <EOL> break <EOL> except : <EOL> pass <EOL> assert not docker . images ( cached_image ) <EOL> build = factories . BuildFactory . create ( <EOL> project = self . project , <EOL> gh_commit_sha = self . prev_head_sha ) <EOL> hook_call = factories . HookCallFactory . create ( <EOL> hook = self . hook , <EOL> build = build ) <EOL> job = self . _do_job ( hook_call ) <EOL> assert job . return_code == <NUM_LIT:0> <EOL> assert job . stdout == ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( self . hook . docker_image ) ) <EOL> assert docker . images ( cached_image ) <EOL> build = factories . BuildFactory . create ( <EOL> project = self . project , <EOL> gh_commit_sha = self . head_sha ) <EOL> hook_call = factories . HookCallFactory . create ( <EOL> hook = self . hook , <EOL> build = build ) <EOL> job = self . _do_job ( hook_call ) <EOL> assert job . return_code == <NUM_LIT:0> <EOL> assert job . stdout == ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( self . hook . docker_image ) ) <EOL> def test_public_project ( self ) : <EOL> self . hook . install_script = '<STR_LIT>' <EOL> self . hook . build_script = '<STR_LIT>' <EOL> self . project . is_public = True <EOL> self . db . session . delete ( self . project . deploy_key ) <EOL> self . db . session . commit ( ) <EOL> build = factories . BuildFactory . create ( <EOL> project = self . project , <EOL> gh_commit_sha = self . prev_head_sha ) <EOL> hook_call = factories . HookCallFactory . create ( <EOL> hook = self . hook , <EOL> build = build ) <EOL> job = self . _do_job ( hook_call ) <EOL> assert job . return_code == <NUM_LIT:0> <EOL> assert job . stdout == ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( self . hook . docker_image ) ) <EOL> def teardown_method ( self , method ) : <EOL> shutil . rmtree ( self . working_dir ) <EOL> TestCase . teardown_method ( self , method ) </s>
