def test_install_dependencies_snap_str_dict(self): mock_run_and_check = self.patch( maas_run_remote_scripts, 'run_and_check') scripts_dir = self.useFixture(TempDirectory()).path scripts = make_scripts(scripts_dir=scripts_dir, with_output=False) packages = [ {'name': factory.make_name('pkg')}, { 'name': factory.make_name('pkg'), 'channel': random.choice([ 'edge', 'beta', 'candidate', 'stable']), }, { 'name': factory.make_name('pkg'), 'channel': random.choice([ 'edge', 'beta', 'candidate', 'stable']), 'mode': random.choice(['dev', 'jail']), }, { 'name': factory.make_name('pkg'), 'channel': random.choice([ 'edge', 'beta', 'candidate', 'stable']), 'mode': random.choice(['dev', 'jail']), }, ] for script in scripts: script['packages'] = {'snap': packages} self.assertTrue(install_dependencies(scripts)) for script in scripts: self.assertThat(self.mock_output_and_send, MockAnyCall( 'Installing snap packages for %s' % script['msg_name'], True, status='INSTALLING')) # Verify cleanup self.assertFalse(os.path.exists(script['combined_path'])) self.assertFalse(os.path.exists(script['stdout_path'])) self.assertFalse(os.path.exists(script['stderr_path'])) self.assertThat(mock_run_and_check, MockAnyCall( ['snap', 'install', packages[0]['name']], scripts, True, True)) self.assertThat(mock_run_and_check, MockAnyCall( [ 'snap', 'install', packages[1]['name'], '--%s' % packages[1]['channel'] ], scripts, True, True)) self.assertThat(mock_run_and_check, MockAnyCall( [ 'snap', 'install', packages[2]['name'], '--%s' % packages[2]['channel'], '--%smode' % packages[2]['mode'], ], scripts, True, True)) self.assertThat(mock_run_and_check, MockAnyCall( [ 'snap', 'install', packages[3]['name'], '--%s' % packages[3]['channel'], '--%smode' % packages[3]['mode'], ], scripts, True, True))
def test_install_dependencies_url_snap(self): mock_run_and_check = self.patch( maas_run_remote_scripts, 'run_and_check') scripts_dir = self.useFixture(TempDirectory()).path scripts = make_scripts(scripts_dir=scripts_dir, with_output=False) snap_file = os.path.join(scripts[0]['download_path'], 'file.snap') open(snap_file, 'w').close() with open(scripts[0]['combined_path'], 'w') as output: output.write("Saving to: '%s'" % snap_file) for script in scripts: script['packages'] = {'url': [snap_file]} self.assertTrue(install_dependencies(scripts)) self.assertThat(mock_run_and_check, MockAnyCall( ['snap', snap_file], scripts, True, True))
def test_install_dependencies_url_errors(self): mock_run_and_check = self.patch( maas_run_remote_scripts, 'run_and_check') mock_run_and_check.return_value = False scripts_dir = self.useFixture(TempDirectory()).path scripts = make_scripts(scripts_dir=scripts_dir) packages = [factory.make_name('url') for _ in range(3)] for script in scripts: script['packages'] = {'url': packages} self.assertFalse(install_dependencies(scripts)) for script in scripts: self.assertThat(self.mock_output_and_send, MockAnyCall( 'Downloading and extracting URLs for %s' % script['msg_name'], True, status='INSTALLING'))
def test_install_dependencies_apt_errors(self): mock_run_and_check = self.patch( maas_run_remote_scripts, 'run_and_check') mock_run_and_check.return_value = False scripts_dir = self.useFixture(TempDirectory()).path scripts = make_scripts(scripts_dir=scripts_dir, with_output=False) packages = [factory.make_name('apt_pkg') for _ in range(3)] for script in scripts: script['packages'] = {'apt': packages} self.assertFalse(install_dependencies(scripts)) for script in scripts: self.assertThat(self.mock_output_and_send, MockAnyCall( 'Installing apt packages for %s' % script['msg_name'], True, status='INSTALLING')) self.assertThat(mock_run_and_check, MockCalledOnceWith( ['apt-get', '-qy', 'install'] + packages, scripts, True, True))
def test_install_dependencies_url_deb_errors(self): mock_run_and_check = self.patch( maas_run_remote_scripts, 'run_and_check') mock_run_and_check.side_effect = (True, True, False) scripts_dir = self.useFixture(TempDirectory()).path scripts = make_scripts(scripts_dir=scripts_dir, with_output=False) deb_file = os.path.join(scripts[0]['download_path'], 'file.deb') open(deb_file, 'w').close() with open(scripts[0]['combined_path'], 'w') as output: output.write("Saving to: '%s'" % deb_file) for script in scripts: script['packages'] = {'url': [deb_file]} self.assertFalse(install_dependencies(scripts)) self.assertThat(mock_run_and_check, MockAnyCall( ['dpkg', '-i', deb_file], scripts, False, True)) self.assertThat(mock_run_and_check, MockAnyCall( ['apt-get', 'install', '-qyf'], scripts, True, True))
def test_install_dependencies_url_zip(self): self.patch(maas_run_remote_scripts, 'run_and_check') scripts_dir = self.useFixture(TempDirectory()).path scripts = make_scripts(scripts_dir=scripts_dir, with_output=False) zip_file = os.path.join(scripts[0]['download_path'], 'file.zip') file_content = factory.make_bytes() with ZipFile(zip_file, 'w') as z: z.writestr('test-file', file_content) with open(scripts[0]['combined_path'], 'w') as output: output.write("Saving to: '%s'" % zip_file) for script in scripts: script['packages'] = {'url': [zip_file]} self.assertTrue(install_dependencies(scripts)) with open( os.path.join(scripts[0]['download_path'], 'test-file'), 'rb') as f: self.assertEquals(file_content, f.read())
def test_install_dependencies_url_tar(self): self.patch(maas_run_remote_scripts, 'run_and_check') scripts_dir = self.useFixture(TempDirectory()).path scripts = make_scripts(scripts_dir=scripts_dir, with_output=False) tar_file = os.path.join(scripts[0]['download_path'], 'file.tar.xz') file_content = factory.make_bytes() with tarfile.open(tar_file, 'w:xz') as tar: tarinfo = tarfile.TarInfo(name='test-file') tarinfo.size = len(file_content) tarinfo.mode = 0o755 tar.addfile(tarinfo, BytesIO(file_content)) with open(scripts[0]['combined_path'], 'w') as output: output.write("Saving to: '%s'" % tar_file) for script in scripts: script['packages'] = {'url': [tar_file]} self.assertTrue(install_dependencies(scripts)) with open( os.path.join(scripts[0]['download_path'], 'test-file'), 'rb') as f: self.assertEquals(file_content, f.read())
def test_install_dependencies_url(self): mock_run_and_check = self.patch( maas_run_remote_scripts, 'run_and_check') scripts_dir = self.useFixture(TempDirectory()).path scripts = make_scripts(scripts_dir=scripts_dir) packages = [factory.make_name('url') for _ in range(3)] for script in scripts: script['packages'] = {'url': packages} self.assertTrue(install_dependencies(scripts)) for package in packages: self.assertThat(mock_run_and_check, MockAnyCall( ['wget', package, '-P', scripts[0]['download_path']], scripts, True)) for script in scripts: self.assertThat(self.mock_output_and_send, MockAnyCall( 'Downloading and extracting URLs for %s' % script['msg_name'], True, status='INSTALLING')) # Verify cleanup self.assertFalse(os.path.exists(scripts[0]['combined_path'])) self.assertFalse(os.path.exists(scripts[0]['stdout_path'])) self.assertFalse(os.path.exists(scripts[0]['stderr_path']))
def test_install_dependencies_snap_str_list(self): mock_run_and_check = self.patch( maas_run_remote_scripts, 'run_and_check') scripts_dir = self.useFixture(TempDirectory()).path scripts = make_scripts(scripts_dir=scripts_dir, with_output=False) packages = [factory.make_name('snap_pkg') for _ in range(3)] for script in scripts: script['packages'] = {'snap': packages} self.assertTrue(install_dependencies(scripts)) for script in scripts: self.assertThat(self.mock_output_and_send, MockAnyCall( 'Installing snap packages for %s' % script['msg_name'], True, status='INSTALLING')) # Verify cleanup self.assertFalse(os.path.exists(script['combined_path'])) self.assertFalse(os.path.exists(script['stdout_path'])) self.assertFalse(os.path.exists(script['stderr_path'])) for package in packages: self.assertThat(mock_run_and_check, MockAnyCall( ['snap', 'install', package], scripts, True, True))
def test_install_dependencies_does_nothing_when_empty(self): self.assertTrue(install_dependencies(make_scripts())) self.assertThat(self.mock_output_and_send, MockNotCalled())