def test_website_pack_unpack(): """Integration test: initializes, installs, verifies, packs, removes, unpacks and removes website.""" with patch(_INPUT, return_value="y"): website = Website("example.com") assert not website.is_installed(), "Website 'example.com' should not exist on this server." assert not website.verify(), "Verification on a non-existing website should fail." with patch(_INPUT, return_value="y"): website.install() # Create a test file in htdocs the_file = File({"path": website.htdocs + "/index.html"}) the_file.data = "Test file." the_file.create() # Pack the site website.pack() website.remove(ask=False) assert not website.is_installed() assert not website.verify(), "Verification on a non-existing website should fail." assert not the_file.exists() with patch(_INPUT, return_value="y"): website.unpack() assert website.is_installed() def verify(website): """Verify function to wrap with localhost decorator.""" assert website.verify() localhost(verify)(website) assert the_file.exists() # Remove and check again for good measure website.remove(ask=False) assert not website.is_installed() assert not website.verify(), "Verification on a non-existing website should fail." assert not the_file.exists()
def test_file_create_already_existing_file_replacing(mock_exists, mock_prompt, mock_chmod, mock_chown): """Tests file creation of an already existing file (replacing the file).""" mock_exists.return_value = True mock_chown.return_value = mock_chmod.return_value = True mock_prompt.return_value = True # Answer yes, replace the_file = File(DEFAULT_ARGS) m_open = mock_open() with patch(BUILTINS + '.open', m_open, create=True): assert the_file.create() m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w') m_open().close.assert_called_once_with()
def test_section_file_apply_to_file(): """[Integration Test] Test apply_to_file() method of Section class.""" # Setup a root dir to use to test root_dir = Dir({"path": "/tmp/ext_pylib/"}) assert root_dir.remove(False) # If it already exists, remove it. assert root_dir.create() assert root_dir.exists() file_without_section = File({"path": "/tmp/ext_pylib/file"}) assert file_without_section.create() assert file_without_section.overwrite(FILE_CONTENTS_WITHOUT_SECTION) assert file_without_section.exists() class SectionFile(Section, File): """Dummy Class extending Section and File.""" section = SectionFile({"path": "/tmp/ext_pylib/section"}) assert section.create() assert section.overwrite(SECTION_FILE_CONTENTS) assert section.exists() file_with_section = File({"path": "/tmp/ext_pylib/file_with_section"}) assert file_with_section.create() assert file_with_section.overwrite(FILE_CONTENTS_WITH_SECTION) assert file_with_section.exists() assert section.is_in(file_with_section.read()) print("Without:") print(file_without_section.read()) print("Without (Applied):") print(section.apply_to(file_without_section.read())) print("With:") print(file_with_section.read()) assert section.apply_to(file_without_section.read()) == file_with_section.read() # Cleanup assert root_dir.remove(False) assert not root_dir.exists()
def test_file_create_and_create_parent_dirs(mock_exists, mock_chmod, mock_chown): """Tests file creation while creating parent dirs.""" mock_parent_dir = MockParentDir(False) mock_exists.return_value = False mock_chown.return_value = mock_chmod.return_value = True File.parent_dir = mock_parent_dir the_file = File(DEFAULT_ARGS) m_open = mock_open() with patch(BUILTINS + '.open', m_open, create=True): assert the_file.create() m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w') m_open().close.assert_called_once_with() assert mock_parent_dir.exists_called == 1 assert mock_parent_dir.create_called == 1
def test_file_create_with_data(mock_write, mock_exists, mock_chmod, mock_chown): """Tests file creation with data.""" mock_parent_dir = MockParentDir(True) mock_exists.return_value = False mock_chown.return_value = mock_chmod.return_value = True File.parent_dir = mock_parent_dir the_file = File(DEFAULT_ARGS) m_open = mock_open() with patch(BUILTINS + '.open', m_open, create=True): data = 'The data...' assert the_file.create(data) m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w') mock_write.assert_called_once_with(data, False, m_open()) m_open().close.assert_called_once_with() assert mock_parent_dir.exists_called == 1 assert mock_parent_dir.create_called == 0
def test_file_create_already_existing_file_not_replacing(mock_exists, mock_prompt): """Tests file creation of an already existing file (NOT replacing the file).""" mock_exists.return_value = True mock_prompt.return_value = False # Answer no, don't replace the_file = File(DEFAULT_ARGS) assert not the_file.create()