Skip to content

Commit

Permalink
fix git tests to prevent dependency on remote credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
asampat3090 committed Apr 3, 2018
1 parent d7ed73b commit eff9974
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 39 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ install:
- pip install pytest pytest-cov
- pip install coveralls
- python setup.py install
before_script:
-
script:
- pytest --cov-config .coveragerc --cov=datmo
after_script:
Expand Down
27 changes: 14 additions & 13 deletions datmo/controller/code/driver/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def init(self):
})
return True

def clone(self, original_git_url, repo_name=None, unsecure=False):
clone_git_url = self._parse_git_url(original_git_url, unsecure)
def clone(self, original_git_url, repo_name=None, mode="https"):
clone_git_url = self._parse_git_url(original_git_url, mode=mode)

if not repo_name:
repo_name = clone_git_url.split('/')[-1][:-4]
Expand All @@ -119,28 +119,29 @@ def clone(self, original_git_url, repo_name=None, unsecure=False):
})
return True

def _parse_git_url(self, original_git_url, unsecure=False):
def _parse_git_url(self, original_git_url, mode="https"):
if original_git_url[-4:] != ".git":
original_git_url = original_git_url + ".git"
p = parse(original_git_url)

if not p.valid:
raise GitUrlArgumentException("exception.code_driver.git._parse_git_url.not_valid", {
"original_git_url": original_git_url
raise GitUrlArgumentException("exception.code_driver.git._parse_git_url", {
"original_git_url": original_git_url,
"exception": "Url is not valid"
})

if self.git_host_manager.ssh_enabled:
if mode=="ssh":
clone_git_url = p.url2ssh
elif self.git_host_manager.https_enabled:
elif mode=="https":
clone_git_url = p.url2https
else:
raise GitUrlArgumentException("exception.code_driver.git._parse_git_url.mode_unrecognized", {
"original_git_url": original_git_url
})

if unsecure:
elif mode=="http":
# If unsecured specified http connection used instead
clone_git_url = '://'.join(['http', p.url2https.split('://')[1]])
else:
raise GitUrlArgumentException("exception.code_driver.git._parse_git_url", {
"original_git_url": original_git_url,
"exception": "Remote access not configured for https or ssh"
})

return clone_git_url

Expand Down
57 changes: 33 additions & 24 deletions datmo/controller/code/driver/test/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,31 @@ def test_init_then_instantiation(self):
assert result == True

def test_clone(self):
result = self.git_code_manager.clone("https://github.com/datmo/hello-world.git")
assert os.path.exists(os.path.join(self.temp_dir, "hello-world",'.git'))
result = self.git_code_manager.clone("https://github.com/datmo/hello-world.git", mode="https")
assert result and os.path.exists(os.path.join(self.temp_dir, "hello-world",".git"))
shutil.rmtree(os.path.join(self.temp_dir, "hello-world"))


def test_clone_unsecure(self):
result = self.git_code_manager.clone("https://github.com/datmo/hello-world.git", unsecure=True)
assert os.path.exists(os.path.join(self.temp_dir, "hello-world",'.git'))
result = self.git_code_manager.clone("https://github.com/datmo/hello-world.git", mode="http")
assert result and os.path.exists(os.path.join(self.temp_dir, "hello-world", ".git"))
shutil.rmtree(os.path.join(self.temp_dir, "hello-world"))
result = self.git_code_manager.clone("https://github.com/datmo/hello-world.git", mode="ssh")
assert result and os.path.exists(os.path.join(self.temp_dir, "hello-world", ".git"))
shutil.rmtree(os.path.join(self.temp_dir, "hello-world"))


def test_giturl_parse(self):
parsed = self.git_code_manager._parse_git_url("https://github.com/datmo/hello-world.git")
def test_parse_git_url(self):
parsed = self.git_code_manager._parse_git_url("https://github.com/datmo/hello-world.git", mode="ssh")
assert parsed == "git@github.com:datmo/hello-world.git"
parsed = self.git_code_manager._parse_git_url("https://github.com/datmo/hello-world.git", mode="https")
assert parsed == "https://github.com/datmo/hello-world.git"
parsed = self.git_code_manager._parse_git_url("https://github.com/datmo/hello-world.git", mode="http")
assert parsed == "http://github.com/datmo/hello-world.git"
# git@github.com:gitpython-developers/GitPython.git
# https://github.com/gitpython-developers/GitPython.git
parsed = self.git_code_manager._parse_git_url("git://github.com/datmo/hello-world.git")
parsed = self.git_code_manager._parse_git_url("git://github.com/datmo/hello-world.git", mode="ssh")
assert parsed == "git@github.com:datmo/hello-world.git"
parsed = self.git_code_manager._parse_git_url("git://github.com/datmo/hello-world.git", mode="https")
assert parsed == "https://github.com/datmo/hello-world.git"
parsed = self.git_code_manager._parse_git_url("git://github.com/datmo/hello-world.git", mode="http")
assert parsed == "http://github.com/datmo/hello-world.git"

def test_commit(self):
# TODO: try out more options
Expand All @@ -63,7 +70,7 @@ def test_commit(self):
with open(test_filepath, "wb") as f:
f.write(str("test"))
self.git_code_manager.add(test_filepath)
result = self.git_code_manager.commit(['-m', 'test'])
result = self.git_code_manager.commit(["-m", "test"])
commit_id = self.git_code_manager.latest_commit()
assert result == True and commit_id

Expand Down Expand Up @@ -93,7 +100,7 @@ def test_commit(self):
#
def test_reset(self):
self.git_code_manager.init()
self.git_code_manager.commit(options=['-m', 'test'])
self.git_code_manager.commit(options=["-m", "test"])
commit_id = self.git_code_manager.latest_commit()
result = self.git_code_manager.reset(git_commit=commit_id)
assert result == True
Expand Down Expand Up @@ -141,7 +148,7 @@ def test_get_remote_url(self):
# with open(test_filepath, "wb") as f:
# f.write(str("test"))
# self.git_code_manager.add(test_filepath)
# self.git_code_manager.commit(['-m', 'test'])
# self.git_code_manager.commit(["-m", "test"])
# if self.git_code_manager.git_host_manager.host == "github":
# self.git_code_manager.remote("add", "origin",
# "https://github.com/datmo/test.git")
Expand Down Expand Up @@ -205,7 +212,7 @@ def test_create_code_ref(self):
f.write(str("test"))
code_id = self.git_code_manager.create_code_ref()
code_ref_path = os.path.join(self.git_code_manager.filepath,
'.git/refs/datmo/',
".git/refs/datmo/",
code_id)
assert code_id and \
os.path.isfile(code_ref_path)
Expand All @@ -218,7 +225,7 @@ def test_exists_code_ref(self):
f.write(str("test"))
code_id = self.git_code_manager.create_code_ref()
code_ref_path = os.path.join(self.git_code_manager.filepath,
'.git/refs/datmo/',
".git/refs/datmo/",
code_id)
result = self.git_code_manager.exists_code_ref(code_id)
assert result == True and \
Expand All @@ -232,7 +239,7 @@ def test_delete_code_ref(self):
f.write(str("test"))
code_id = self.git_code_manager.create_code_ref()
code_ref_path = os.path.join(self.git_code_manager.filepath,
'.git/refs/datmo/',
".git/refs/datmo/",
code_id)
result = self.git_code_manager.delete_code_ref(code_id)
assert result == True and \
Expand Down Expand Up @@ -286,19 +293,21 @@ def teardown_class(self):

def test_netrc(self):
hostm = GitHostManager(self.netrc_temp_dir)
assert hostm.create_git_netrc('foobar','foo')
assert hostm.create_git_netrc("foobar","foo")
hostm = GitHostManager(self.netrc_temp_dir)
assert os.path.exists(os.path.join(self.netrc_temp_dir, ".netrc"))
assert hostm.https_enabled

def test_ssh_git(self):
hostm = GitHostManager(self.ssh_temp_dir)
assert hostm.ssh_enabled == False
shutil.copytree(
os.path.join(os.path.expanduser('~'), '.ssh'),
os.path.join(self.ssh_temp_dir,'.ssh'))
assert os.path.exists(os.path.join(self.ssh_temp_dir, ".ssh", 'id_rsa'))
hostm = GitHostManager(self.ssh_temp_dir)
assert hostm.ssh_enabled == True
# If id_rsa is already present and used in remote git
if os.path.join(os.path.expanduser("~"), ".ssh", "id_rsa"):
shutil.copytree(
os.path.join(os.path.expanduser("~"), ".ssh"),
os.path.join(self.ssh_temp_dir,".ssh"))
assert os.path.exists(os.path.join(self.ssh_temp_dir, ".ssh", "id_rsa"))
hostm = GitHostManager(self.ssh_temp_dir)
assert hostm.ssh_enabled == True


0 comments on commit eff9974

Please sign in to comment.