def test_git_install_skip_existing_nongit(path=None): with swallow_logs(new_level=logging.WARNING) as log: dist_dir = GitDistribution(name="git", packages=[ GitRepo(path=path, remotes={ "origin": { "url": "doesnt-matter", "contains": True } }) ]) dist_dir.install_packages() assert "not a Git repository; skipping" in log.out with swallow_logs(new_level=logging.WARNING) as log: dist_dir = GitDistribution(name="git", packages=[ GitRepo(path=op.join(path, "foo"), remotes={ "origin": { "url": "doesnt-matter", "contains": True } }) ]) dist_dir.install_packages() assert "not a directory; skipping" in log.out
def test_multi_debian_files(): with swallow_logs() as log: args = ['diff', multi_debian_yaml, diff_1_yaml] with raises(SystemExit): main(args) assert_in_in("multiple <class 'niceman.distributions.debian.DebianDistribution'> found", log.lines) with swallow_logs() as log: args = ['diff', diff_1_yaml, multi_debian_yaml] with raises(SystemExit): main(args) assert_in_in("multiple <class 'niceman.distributions.debian.DebianDistribution'> found", log.lines)
def test_exec_interface(niceman_cfg_path, docker_container): with patch('niceman.resource.ResourceManager.set_inventory'), \ patch('niceman.resource.ResourceManager.get_inventory') as get_inventory, \ swallow_logs(new_level=logging.DEBUG) as log: config = { "status": "running", "engine_url": "unix:///var/run/docker.sock", "type": "docker-container", "name": "testing-container", } path = '/tmp/{}'.format(str(uuid.uuid4())) get_inventory.return_value = { "testing-container": config } main([ 'exec', 'mkdir', path, '--name', 'testing-container', '--config', niceman_cfg_path ]) session = ResourceManager.factory(config).get_session() assert session.exists(path)
def test_parse_dpkgquery_line(): parse = DebTracer()._parse_dpkgquery_line mock_values = { "unique": { "name": "pkg", "path": "/path/to/file", "pkgs_rest": None }, "multi_dir": { "name": "pkg", "path": os.getcwd(), "pkgs_rest": ", more, packages" }, "multi_file": { "name": "pkg", "path": __file__, "pkgs_rest": ", more, packages" } } with mock.patch("niceman.distributions.debian.parse_dpkgquery_line", mock_values.get): assert parse("unique") == {"name": "pkg", "path": "/path/to/file"} assert parse("multi_dir") is None with swallow_logs(new_level=logging.WARNING) as log: assert parse("multi_file") == {"name": "pkg", "path": __file__} assert any("multiple packages " in ln for ln in log.lines)
def test_git_install(traced_repo_copy, tmpdir): git_dist = traced_repo_copy["git_dist"] git_pkg = git_dist.packages[0] tmpdir = str(tmpdir) # Install package to a new location. install_dir = op.join(tmpdir, "installed") git_pkg.path = install_dir install(git_dist, install_dir, check=True) # Installing a second time works if the root hexsha's match. install(git_dist, install_dir, check=True) runner = GitRunner(cwd=install_dir) # We don't try to change the state of the repository if it's dirty. runner(["git", "reset", "--hard", "HEAD^"]) hexsha_existing = current_hexsha(runner) create_tree(install_dir, {"dirt": "dirt"}) with swallow_logs(new_level=logging.WARNING) as log: install(git_dist, install_dir) assert "repository is dirty" in log.out assert current_hexsha(runner) == hexsha_existing # We end up on the intended commit (detached) if the existing installation # repo is clean. os.remove(op.join(install_dir, "dirt")) install(git_dist, install_dir) assert current_hexsha(runner) == git_pkg.hexsha assert not current_branch(runner)
def test_distributions(demo1_spec): provenance = Provenance.factory(demo1_spec) distributions = provenance.get_distributions() distributions = items_to_dict(distributions) assert set(distributions.keys()) == {'conda', 'debian'} # Test DebianDistribution class. debian_distribution = distributions['debian'] environment = MagicMock() with swallow_logs(new_level=logging.DEBUG) as log: debian_distribution.initiate(environment) debian_distribution.install_packages(environment) calls = [ call.execute_command(['apt-get', 'update']), call.execute_command([ 'apt-get', 'install', '-y', 'libc6-dev=2.19-18+deb8u4', 'afni=16.2.07~dfsg.1-2~nd90+1' ]), ] environment.assert_has_calls(calls, any_order=True) assert_in("Adding Debian update to environment command list.", log.lines) """
def test_git_install_no_remote(): dist = GitDistribution(name="git", packages=[GitRepo(path="/tmp/shouldnt/matter")]) with swallow_logs(new_level=logging.WARNING) as log: dist.initiate() dist.install_packages() assert "No remote known" in log.out
def test_get_distributions(demo1_spec): # Test reading the distributions from the NICEMAN spec file. provenance = Provenance.factory(demo1_spec, 'niceman') with swallow_logs(new_level=logging.DEBUG) as log: distributions = provenance.get_distributions() assert len(distributions) == 2 # a bit of testing is done within test_niceman.py since it is niceman specific example?
def test_git_install_hexsha_not_found(traced_repo_copy, tmpdir): git_dist = traced_repo_copy["git_dist"] git_pkg = git_dist.packages[0] tmpdir = str(tmpdir) # Install package to a new location. install_dir = op.join(tmpdir, "installed") git_pkg.path = install_dir install(git_dist, install_dir) # No existing hexsha. git_pkg.hexsha = "0" * 40 with swallow_logs(new_level=logging.WARNING) as log: install(git_dist, install_dir) assert "expected hexsha wasn't found" in log.out
def test_git_install_skip_different_git(git_repo): with swallow_logs(new_level=logging.WARNING) as log: dist_dir = GitDistribution( name="git", packages=[ GitRepo(path=git_repo, root_hexsha="definitely doesn't match", remotes={ "origin": { "url": "doesnt-matter", "contains": True } }) ]) dist_dir.install_packages() assert "doesn't match expected hexsha; skipping" in log.out
def test_delete_interface(niceman_cfg_path): """ Test deleting a resource. """ with patch('docker.Client') as client, \ patch('niceman.resource.ResourceManager.set_inventory'), \ patch('niceman.resource.ResourceManager.get_inventory') as get_inventory, \ swallow_logs(new_level=logging.DEBUG) as log: client.return_value = MagicMock( containers=lambda all: [{ 'Id': '326b0fdfbf838', 'Names': ['/my-resource'], 'State': 'running' }]) get_inventory.return_value = { "my-resource": { "status": "running", "engine_url": "tcp://127.0.0.1:2375", "type": "docker-container", "name": "my-resource", "id": "326b0fdfbf838" } } args = [ 'delete', '--name', 'my-resource', '--config', niceman_cfg_path, '--skip-confirmation' ] main(args) calls = [ call(base_url='tcp://127.0.0.1:2375'), call().remove_container( { 'State': 'running', 'Id': '326b0fdfbf838', 'Names': ['/my-resource'] }, force=True) ] client.assert_has_calls(calls, any_order=True) assert_in('Deleted the environment my-resource', log.lines)
def test_login_interface(): """ Test logging into an environment """ with patch('docker.Client') as client, \ patch('niceman.resource.ResourceManager._get_inventory') as get_inventory, \ patch('dockerpty.start'), \ swallow_logs(new_level=logging.DEBUG) as log: client.return_value = MagicMock( containers=lambda all: [{ 'Id': '18b31b30e3a5', 'Names': ['/my-test-resource'], 'State': 'running' }], ) get_inventory.return_value = { "my-test-resource": { "status": "running", "engine_url": "tcp://127.0.0.1:2375", "type": "docker-container", "name": "my-test-resource", "id": "18b31b30e3a5" } } args = ['login', 'my-test-resource'] with patch("niceman.interface.login.get_manager", return_value=ResourceManager()): main(args) assert client.call_count == 1 calls = [call(base_url='tcp://127.0.0.1:2375')] client.assert_has_calls(calls, any_order=True) assert_in("Opening TTY connection to docker container.", log.lines)