@@ -81,6 +81,22 @@ def mock_is_dir_func(path):
8181 return original_is_dir_func (path )
8282
8383
84+ def mock_get_gitref (repo , ref ):
85+ """
86+ Mock get_gitref function which return mocked object if ref passed is
87+ PACK_INDEX['test']['version']
88+ """
89+ if PACK_INDEX ['test' ]['version' ] in ref :
90+ if ref [0 ] == 'v' :
91+ return mock .MagicMock (hexsha = PACK_INDEX ['test' ]['version' ])
92+ else :
93+ return None
94+ elif ref :
95+ return mock .MagicMock (hexsha = "abcDef" )
96+ else :
97+ return None
98+
99+
84100@mock .patch .object (pack_service , 'fetch_pack_index' , mock .MagicMock (return_value = (PACK_INDEX , {})))
85101class DownloadGitRepoActionTestCase (BaseActionTestCase ):
86102 action_cls = DownloadGitRepoAction
@@ -520,7 +536,7 @@ def side_effect(ref):
520536
521537 # Fool _get_gitref into working when its ref == our ref
522538 def fake_commit (arg_ref ):
523- if arg_ref == ref :
539+ if not ref or arg_ref == ref :
524540 return gitref
525541 else :
526542 raise BadName ()
@@ -572,3 +588,28 @@ def test_run_pack_download_local_directory(self):
572588 destination_path = os .path .join (self .repo_base , 'test4' )
573589 self .assertTrue (os .path .exists (destination_path ))
574590 self .assertTrue (os .path .exists (os .path .join (destination_path , 'pack.yaml' )))
591+
592+ @mock .patch ('st2common.util.pack_management.get_gitref' , mock_get_gitref )
593+ def test_run_pack_download_with_tag (self ):
594+ action = self .get_action_instance ()
595+ result = action .run (packs = ['test' ], abs_repo_base = self .repo_base )
596+ temp_dir = hashlib .md5 (PACK_INDEX ['test' ]['repo_url' ].encode ()).hexdigest ()
597+
598+ self .assertEqual (result , {'test' : 'Success.' })
599+ self .clone_from .assert_called_once_with (PACK_INDEX ['test' ]['repo_url' ],
600+ os .path .join (os .path .expanduser ('~' ), temp_dir ))
601+ self .assertTrue (os .path .isfile (os .path .join (self .repo_base , 'test/pack.yaml' )))
602+
603+ # Check repo.git.checkout is called three times
604+ self .assertEqual (self .repo_instance .git .checkout .call_count , 3 )
605+
606+ # Check repo.git.checkout called with latest tag or branch
607+ self .assertEqual (PACK_INDEX ['test' ]['version' ],
608+ self .repo_instance .git .checkout .call_args_list [1 ][0 ][0 ])
609+
610+ # Check repo.git.checkout called with head
611+ self .assertEqual (self .repo_instance .head .reference ,
612+ self .repo_instance .git .checkout .call_args_list [2 ][0 ][0 ])
613+
614+ self .repo_instance .git .branch .assert_called_with (
615+ '-f' , self .repo_instance .head .reference , PACK_INDEX ['test' ]['version' ])
0 commit comments