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_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 Slider. Today is Wednesday\n \n \n') self.assertEqual(open_mock.call_count, 1) ensure_mock.assert_called()
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_with_dir(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, create_file_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' with Environment('/') as env: PropertiesFile( 'file.txt', dir="/dir/and/dir", properties={}, ) create_file_mock.assert_called_with( '/dir/and/dir/file.txt', u'# Generated by Apache Ambari. Some other day\n \n ', encoding=None) ensure_mock.assert_called()
def test_action_create_properties_rewrite_content( self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, create_file_mock, read_file_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 read_file_mock.return_value = 'old-content' with Environment('/') as env: PropertiesFile( 'new_file', dir='/dir1', properties={'property_1': 'value1'}, ) read_file_mock.assert_called() create_file_mock.assert_called_with( '/dir1/new_file', u'# Generated by Apache Ambari. 777\n \nproperty_1=value1\n ', encoding=None) ensure_mock.assert_called()
def test_action_create_properties_with_metacharacters( self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, create_file_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 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": "", }, ) create_file_mock.assert_called_with( '/dir/new_file', 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 ", encoding=None) ensure_mock.assert_called()
def test_action_create_properties_simple(self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, create_file_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 with Environment('/') as env: PropertiesFile( '/dir/new_file', properties={'property1': 'value1'}, ) create_file_mock.assert_called_with( '/dir/new_file', u'# Generated by Apache Ambari. 777\n \nproperty1=value1\n ', encoding=None) ensure_mock.assert_called()
def test_action_create_empty_properties_without_dir( self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock, create_file_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' with Environment('/') as env: PropertiesFile('/somewhere_in_system/one_file.properties', dir=None, properties={}) create_file_mock.assert_called_with( '/somewhere_in_system/one_file.properties', u'# Generated by Apache Ambari. Today is Wednesday\n \n \n') ensure_mock.assert_called()