def test_action_create_simple_xml_config(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'create' action - creates new non existent xml file and write proper data where configurations={"Some conf":"Some value"} """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 'Wed 2014-02' result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: XmlConfig( 'file.xml', conf_dir='/dir/conf', configurations={'property1': 'value1'}, configuration_attributes={'attr': { 'property1': 'attr_value' }}) open_mock.assert_called_with('/dir/conf/file.xml', 'wb') result_file.__enter__().write.assert_called_with( u'<!--Wed 2014-02-->\n <configuration>\n \n <property>\n <name>property1</name>\n <value>value1</value>\n <attr>attr_value</attr>\n </property>\n \n </configuration>\n' )
def test_action_create_empty_xml_config(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'create' action - creates new non existent xml file and write proper data where configurations={} """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 'Wed 2014-02' result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: XmlConfig('file.xml', conf_dir='/dir/conf', configurations={}, configuration_attributes={}) open_mock.assert_called_with('/dir/conf/file.xml', 'wb') result_file.__enter__().write.assert_called_with( u'<!--Wed 2014-02-->\n <configuration>\n \n </configuration>\n' )
def test_action_create_properties_with_metacharacters(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'action_create' - creates new non existent file and write proper data 1) properties={"":"", "Some property":"Metacharacters: -%{} ${a.a}/"} 2) dir=None """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 777 result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: PropertiesFile('/dir/new_file', properties={"": "", "prop.1": "'.'yyyy-MM-dd-HH", "prop.3": "%d{ISO8601} %5p %c{1}:%L - %m%n", "prop.2": "INFO, openjpa", "prop.4": "${oozie.log.dir}/oozie.log", "prop.empty": "", }, ) open_mock.assert_called_with('/dir/new_file','wb') result_file.__enter__().write.assert_called_with(u"# Generated by Apache Ambari. 777\n \n=\nprop.1='.'yyyy-MM-dd-HH\nprop.2=INFO, openjpa\nprop.3=%d{ISO8601} %5p %c{1}:%L - %m%n\nprop.4=${oozie.log.dir}/oozie.log\nprop.empty=\n \n") self.assertEqual(open_mock.call_count, 1) ensure_mock.assert_called()
def test_action_create_xml_config_with_metacharacters( self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'create' action - creates new non existent xml file and write proper data where configurations={"Some conf":"Some metacharacters"} """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 'Wed 2014-02' result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: XmlConfig( 'file.xml', conf_dir='/dir/conf', configurations={ "": "", "prop.1": "'.'yyyy-MM-dd-HH", "prop.3": "%d{ISO8601} %5p %c{1}:%L - %m%n", "prop.2": "INFO, openjpa", "prop.4": "${oozie.log.dir}/oozie.log", "prop.empty": "", }, ) open_mock.assert_called_with('/dir/conf/file.xml', 'wb') result_file.__enter__().write.assert_called_with( u'<!--Wed 2014-02-->\n <configuration>\n \n <property>\n <name></name>\n <value></value>\n </property>\n \n <property>\n <name>prop.empty</name>\n <value></value>\n </property>\n \n <property>\n <name>prop.3</name>\n <value>%d{ISO8601} %5p %c{1}:%L - %m%n</value>\n </property>\n \n <property>\n <name>prop.2</name>\n <value>INFO, openjpa</value>\n </property>\n \n <property>\n <name>prop.1</name>\n <value>'.'yyyy-MM-dd-HH</value>\n </property>\n \n <property>\n <name>prop.4</name>\n <value>${oozie.log.dir}/oozie.log</value>\n </property>\n \n </configuration>\n' )
def test_action_create_properties_simple(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'action_create' - creates new non existent file and write proper data 1) properties={"Some property":"Some value"} 2) dir=None """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 777 result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: PropertiesFile( '/dir/new_file', properties={'property1': 'value1'}, ) open_mock.assert_called_with('/dir/new_file', 'wb') result_file.__enter__().write.assert_called_with( u'# Generated by Apache Slider. 777\n \nproperty1=value1\n \n' ) self.assertEqual(open_mock.call_count, 1) ensure_mock.assert_called()
def test_action_create_properties_with_metacharacters( self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'action_create' - creates new non existent file and write proper data 1) properties={"":"", "Some property":"Metacharacters: -%{} ${a.a}/"} 2) dir=None """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 777 result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: PropertiesFile( '/dir/new_file', properties={ "": "", "prop.1": "'.'yyyy-MM-dd-HH", "prop.3": "%d{ISO8601} %5p %c{1}:%L - %m%n", "prop.2": "INFO, openjpa", "prop.4": "${oozie.log.dir}/oozie.log", "prop.empty": "", }, ) open_mock.assert_called_with('/dir/new_file', 'wb') result_file.__enter__().write.assert_called_with( u"# Generated by Apache Slider. 777\n \n=\nprop.1='.'yyyy-MM-dd-HH\nprop.2=INFO, openjpa\nprop.3=%d{ISO8601} %5p %c{1}:%L - %m%n\nprop.4=${oozie.log.dir}/oozie.log\nprop.empty=\n \n" ) self.assertEqual(open_mock.call_count, 1) ensure_mock.assert_called()
def test_action_create_empty_properties_without_dir( self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'action_create' - creates new non existent file and write proper data 1) properties={} 2) dir=None """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 'Today is Wednesday' result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: PropertiesFile('/somewhere_in_system/one_file.properties', dir=None, properties={}) open_mock.assert_called_with( '/somewhere_in_system/one_file.properties', 'wb') result_file.__enter__().write.assert_called_with( u'# Generated by Apache Slider. Today is Wednesday\n \n \n') self.assertEqual(open_mock.call_count, 1) ensure_mock.assert_called()
def test_action_create_empty_properties_with_dir(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'action_create' - creates new non existent file and write proper data 1) properties={} 2) dir='Some directory that exist ' """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 'Some other day' result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: PropertiesFile('file.txt', dir="/dir/and/dir", properties={}, ) open_mock.assert_called_with('/dir/and/dir/file.txt', 'wb') result_file.__enter__().write.assert_called_with(u'# Generated by Apache Ambari. Some other day\n \n \n') self.assertEqual(open_mock.call_count, 1) ensure_mock.assert_called()
def test_action_create_xml_config_sorted_by_key(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'create' action - creates new non existent xml file and writes proper data where configurations={"Key":"Value"} are stored in sorted by key order """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 'Wed 2014-02' result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: XmlConfig('file.xml', conf_dir='/dir/conf', configurations={ "": "", "third": "should be third", "first": "should be first", "z_last": "should be last", "second": "should be second", }, configuration_attributes={}) open_mock.assert_called_with('/dir/conf/file.xml', 'wb') result_file.__enter__().write.assert_called_with( u'<!--Wed 2014-02-->\n <configuration>\n \n <property>\n <name></name>\n <value></value>\n </property>\n \n <property>\n <name>first</name>\n <value>should be first</value>\n </property>\n \n <property>\n <name>second</name>\n <value>should be second</value>\n </property>\n \n <property>\n <name>third</name>\n <value>should be third</value>\n </property>\n \n <property>\n <name>z_last</name>\n <value>should be last</value>\n </property>\n \n </configuration>\n' )
def test_action_create_properties_rewrite_content(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'action_create' - rewrite file that exist 1) properties={"Some property":"Some value"} 2) dir="Some dir" """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = True time_asctime_mock.return_value = 777 result_file = MagicMock() result_file.read.return_value = 'old-content' open_mock.return_value = result_file with Environment('/') as env: PropertiesFile('new_file', dir='/dir1', properties={'property_1': 'value1'}, ) result_file.read.assert_called() open_mock.assert_called_with('/dir1/new_file', 'wb') result_file.__enter__().write.assert_called_with(u'# Generated by Apache Ambari. 777\n \nproperty_1=value1\n \n') self.assertEqual(open_mock.call_count, 2) ensure_mock.assert_called()
def test_action_create_xml_config_sorted_by_key(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'create' action - creates new non existent xml file and writes proper data where configurations={"Key":"Value"} are stored in sorted by key order """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 'Wed 2014-02' result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: XmlConfig('file.xml', conf_dir='/dir/conf', configurations={"": "", "third": "should be third", "first": "should be first", "z_last": "should be last", "second": "should be second", }, configuration_attributes={} ) open_mock.assert_called_with('/dir/conf/file.xml', 'wb') result_file.__enter__().write.assert_called_with(u'<!--Wed 2014-02-->\n <configuration>\n \n <property>\n <name></name>\n <value></value>\n </property>\n \n <property>\n <name>first</name>\n <value>should be first</value>\n </property>\n \n <property>\n <name>second</name>\n <value>should be second</value>\n </property>\n \n <property>\n <name>third</name>\n <value>should be third</value>\n </property>\n \n <property>\n <name>z_last</name>\n <value>should be last</value>\n </property>\n \n </configuration>\n')
def test_action_create_properties_simple(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'action_create' - creates new non existent file and write proper data 1) properties={"Some property":"Some value"} 2) dir=None """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 777 result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: PropertiesFile('/dir/new_file', properties={'property1': 'value1'}, ) open_mock.assert_called_with('/dir/new_file', 'wb') result_file.__enter__().write.assert_called_with(u'# Generated by Apache Ambari. 777\n \nproperty1=value1\n \n') self.assertEqual(open_mock.call_count, 1) ensure_mock.assert_called()
def test_action_create_empty_properties_with_dir(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'action_create' - creates new non existent file and write proper data 1) properties={} 2) dir='Some directory that exist ' """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 'Some other day' result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: PropertiesFile( 'file.txt', dir="/dir/and/dir", properties={}, ) open_mock.assert_called_with(os.path.join('/dir/and/dir', 'file.txt'), 'wb') result_file.__enter__().write.assert_called_with( u'# Generated by Apache Slider. Some other day\n \n \n') self.assertEqual(open_mock.call_count, 1) ensure_mock.assert_called()
def test_action_create_xml_config_with_metacharacters(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'create' action - creates new non existent xml file and write proper data where configurations={"Some conf":"Some metacharacters"} """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 'Wed 2014-02' result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: XmlConfig('file.xml', conf_dir='/dir/conf', configurations={"": "", "prop.1": "'.'yyyy-MM-dd-HH", "prop.3": "%d{ISO8601} %5p %c{1}:%L - %m%n", "prop.2": "INFO, openjpa", "prop.4": "${oozie.log.dir}/oozie.log", "prop.empty": "", }, ) open_mock.assert_called_with('/dir/conf/file.xml', 'wb') result_file.__enter__().write.assert_called_with(u'<!--Wed 2014-02-->\n <configuration>\n \n <property>\n <name></name>\n <value></value>\n </property>\n \n <property>\n <name>prop.empty</name>\n <value></value>\n </property>\n \n <property>\n <name>prop.3</name>\n <value>%d{ISO8601} %5p %c{1}:%L - %m%n</value>\n </property>\n \n <property>\n <name>prop.2</name>\n <value>INFO, openjpa</value>\n </property>\n \n <property>\n <name>prop.1</name>\n <value>'.'yyyy-MM-dd-HH</value>\n </property>\n \n <property>\n <name>prop.4</name>\n <value>${oozie.log.dir}/oozie.log</value>\n </property>\n \n </configuration>\n')
def test_action_create_simple_xml_config(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'create' action - creates new non existent xml file and write proper data where configurations={"Some conf":"Some value"} """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 'Wed 2014-02' result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: XmlConfig('file.xml', conf_dir='/dir/conf', configurations={'property1': 'value1'} ) open_mock.assert_called_with('/dir/conf/file.xml', 'wb') result_file.__enter__().write.assert_called_with(u'<!--Wed 2014-02-->\n <configuration>\n \n <property>\n <name>property1</name>\n <value>value1</value>\n </property>\n \n </configuration>\n')
def test_action_create_properties_rewrite_content(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'action_create' - rewrite file that exist 1) properties={"Some property":"Some value"} 2) dir="Some dir" """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = True time_asctime_mock.return_value = 777 result_file = MagicMock() result_file.read.return_value = 'old-content' open_mock.return_value = result_file with Environment('/') as env: PropertiesFile( 'new_file', dir='/dir1', properties={'property_1': 'value1'}, ) result_file.read.assert_called() open_mock.assert_called_with(os.path.join('/dir1', 'new_file'), 'wb') result_file.__enter__().write.assert_called_with( u'# Generated by Apache Slider. 777\n \nproperty_1=value1\n \n' ) self.assertEqual(open_mock.call_count, 2) ensure_mock.assert_called()
def test_action_create_empty_properties_without_dir(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, open_mock, ensure_mock): """ Tests if 'action_create' - creates new non existent file and write proper data 1) properties={} 2) dir=None """ os_path_isdir_mock.side_effect = [False, True] os_path_exists_mock.return_value = False time_asctime_mock.return_value = 'Today is Wednesday' result_file = MagicMock() open_mock.return_value = result_file with Environment('/') as env: PropertiesFile('/somewhere_in_system/one_file.properties', dir=None, properties={} ) open_mock.assert_called_with('/somewhere_in_system/one_file.properties', 'wb') result_file.__enter__().write.assert_called_with( u'# Generated by Apache Ambari. Today is Wednesday\n \n \n') self.assertEqual(open_mock.call_count, 1) ensure_mock.assert_called()
def test_write(self): mock_fp = MagicMock() mock_fp.__enter__ = MagicMock(return_value=mock_fp) open_fp = MagicMock() finder_mkdir = MagicMock() finder_open = MagicMock(return_value=open_fp) codec_open = MagicMock(return_value=mock_fp) render_return = 'TOKEN' finder_render_to_string = MagicMock(return_value=render_return) with patch(FINDER_OS_MK_DIR, finder_mkdir): with patch(FINDER_OPEN, finder_open): with patch(FINDER_CODECS_OPEN, codec_open): with patch(FINDER_RENDER_TO_STRING, finder_render_to_string): self.writer.write() migration_dir = os.path.dirname(self.writer.path) finder_mkdir.assert_called_once_with(migration_dir) finder_open.assert_called_once_with( os.path.join(migration_dir, "__init__.py"), 'w') open_fp.close.assert_called_once() finder_render_to_string.assert_called_once_with( MIGRATION_TEMPLATE_NAME, { 'snapshot_name': self.writer.snapshot_name, 'app_label': self.writer.app_label, 'initial': True, 'last_migration_name': None }) mock_fp.write.assert_called_once_with(render_return)
def test_action_create_non_existent_file(self, isdir_mock, exists_mock, open_mock, ensure_mock): """ Tests if 'create' action create new non existent file and write proper data """ isdir_mock.side_effect = [False, True] exists_mock.return_value = False new_file = MagicMock() open_mock.return_value = new_file with Environment('/') as env: File('/directory/file', action='create', mode=0777, content='file-content') open_mock.assert_called_with('/directory/file', 'wb') new_file.__enter__().write.assert_called_with('file-content') self.assertEqual(open_mock.call_count, 1) ensure_mock.assert_called()
def test_action_create_non_existent_file(self, isdir_mock, exists_mock, open_mock, ensure_mock): """ Tests if 'create' action create new non existent file and write proper data """ isdir_mock.side_effect = [False, True] exists_mock.return_value = False new_file = MagicMock() open_mock.return_value = new_file with Environment('/') as env: File('/directory/file', action='create', mode=0777, content='file-content' ) open_mock.assert_called_with('/directory/file', 'wb') new_file.__enter__().write.assert_called_with('file-content') self.assertEqual(open_mock.call_count, 1) ensure_mock.assert_called()
def test_write(self): mock_fp = MagicMock() mock_fp.__enter__ = MagicMock(return_value=mock_fp) codec_open = MagicMock(return_value=mock_fp) json_dump = MagicMock() os_mkdir = MagicMock() with patch(FINDER_CODECS_OPEN, codec_open): with patch(FINDER_JSON_DUMP, json_dump): with patch(FINDER_OS_MK_DIR, os_mkdir): self.writer.write() os_mkdir.assert_called_once_with(os.path.dirname(self.writer.path)) json_dump.assert_called_once_with(self.snapshot, fp=mock_fp, cls=DjangoJSONEncoder, ensure_ascii=False, indent=4)
def test_action_create_encoding(self, isdir_mock, exists_mock, open_mock, get_content_mock, ensure_mock): isdir_mock.side_effect = [False, True] exists_mock.return_value = True content_mock = MagicMock() old_content_mock = MagicMock() get_content_mock.return_value = content_mock new_file = MagicMock() open_mock.return_value = new_file enter_file_mock = MagicMock() enter_file_mock.read = MagicMock(return_value=old_content_mock) new_file.__enter__ = MagicMock(return_value=enter_file_mock) with Environment('/') as env: File('/directory/file', action='create', mode=0777, content='file-content', encoding="UTF-8") open_mock.assert_called_with('/directory/file', 'wb') content_mock.encode.assert_called_with('UTF-8') old_content_mock.decode.assert_called_with('UTF-8')
def test_action_create_encoding(self, isdir_mock, exists_mock, open_mock, get_content_mock ,ensure_mock): isdir_mock.side_effect = [False, True] exists_mock.return_value = True content_mock = MagicMock() old_content_mock = MagicMock() get_content_mock.return_value = content_mock new_file = MagicMock() open_mock.return_value = new_file enter_file_mock = MagicMock() enter_file_mock.read = MagicMock(return_value=old_content_mock) new_file.__enter__ = MagicMock(return_value=enter_file_mock) with Environment('/') as env: File('/directory/file', action='create', mode=0777, content='file-content', encoding = "UTF-8" ) open_mock.assert_called_with('/directory/file', 'wb') content_mock.encode.assert_called_with('UTF-8') old_content_mock.decode.assert_called_with('UTF-8')