<s> import time <EOL> import uuid <EOL> import mock <EOL> from openstackclient . common import exceptions <EOL> from openstackclient . common import utils <EOL> from openstackclient . tests import fakes <EOL> from openstackclient . tests import utils as test_utils <EOL> PASSWORD = "<STR_LIT>" <EOL> WASSPORD = "<STR_LIT>" <EOL> DROWSSAP = "<STR_LIT>" <EOL> class FakeOddballResource ( fakes . FakeResource ) : <EOL> def get ( self , attr ) : <EOL> """<STR_LIT>""" <EOL> if attr == '<STR_LIT:id>' : <EOL> return self . id <EOL> elif attr == '<STR_LIT:name>' : <EOL> return self . name <EOL> else : <EOL> return None <EOL> class TestUtils ( test_utils . TestCase ) : <EOL> def test_get_password_good ( self ) : <EOL> with mock . patch ( "<STR_LIT>" , return_value = PASSWORD ) : <EOL> mock_stdin = mock . Mock ( ) <EOL> mock_stdin . isatty = mock . Mock ( ) <EOL> mock_stdin . isatty . return_value = True <EOL> self . assertEqual ( PASSWORD , utils . get_password ( mock_stdin ) ) <EOL> def test_get_password_bad_once ( self ) : <EOL> answers = [ PASSWORD , WASSPORD , DROWSSAP , DROWSSAP ] <EOL> with mock . patch ( "<STR_LIT>" , side_effect = answers ) : <EOL> mock_stdin = mock . Mock ( ) <EOL> mock_stdin . isatty = mock . Mock ( ) <EOL> mock_stdin . isatty . return_value = True <EOL> self . assertEqual ( DROWSSAP , utils . get_password ( mock_stdin ) ) <EOL> def test_get_password_no_tty ( self ) : <EOL> mock_stdin = mock . Mock ( ) <EOL> mock_stdin . isatty = mock . Mock ( ) <EOL> mock_stdin . isatty . return_value = False <EOL> self . assertRaises ( exceptions . CommandError , <EOL> utils . get_password , <EOL> mock_stdin ) <EOL> def test_get_password_cntrl_d ( self ) : <EOL> with mock . patch ( "<STR_LIT>" , side_effect = EOFError ( ) ) : <EOL> mock_stdin = mock . Mock ( ) <EOL> mock_stdin . isatty = mock . Mock ( ) <EOL> mock_stdin . isatty . return_value = True <EOL> self . assertRaises ( exceptions . CommandError , <EOL> utils . get_password , <EOL> mock_stdin ) <EOL> def get_test_items ( self ) : <EOL> item1 = { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } <EOL> item2 = { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:3> } <EOL> item3 = { '<STR_LIT:a>' : <NUM_LIT:2> , '<STR_LIT:b>' : <NUM_LIT:2> } <EOL> item4 = { '<STR_LIT:a>' : <NUM_LIT:2> , '<STR_LIT:b>' : <NUM_LIT:1> } <EOL> return [ item1 , item2 , item3 , item4 ] <EOL> def test_sort_items_with_one_key ( self ) : <EOL> items = self . get_test_items ( ) <EOL> sort_str = '<STR_LIT:b>' <EOL> expect_items = [ items [ <NUM_LIT:3> ] , items [ <NUM_LIT:0> ] , items [ <NUM_LIT:2> ] , items [ <NUM_LIT:1> ] ] <EOL> self . assertEqual ( expect_items , utils . sort_items ( items , sort_str ) ) <EOL> def test_sort_items_with_multiple_keys ( self ) : <EOL> items = self . get_test_items ( ) <EOL> sort_str = '<STR_LIT>' <EOL> expect_items = [ items [ <NUM_LIT:0> ] , items [ <NUM_LIT:1> ] , items [ <NUM_LIT:3> ] , items [ <NUM_LIT:2> ] ] <EOL> self . assertEqual ( expect_items , utils . sort_items ( items , sort_str ) ) <EOL> def test_sort_items_all_with_direction ( self ) : <EOL> items = self . get_test_items ( ) <EOL> sort_str = '<STR_LIT>' <EOL> expect_items = [ items [ <NUM_LIT:2> ] , items [ <NUM_LIT:3> ] , items [ <NUM_LIT:1> ] , items [ <NUM_LIT:0> ] ] <EOL> self . assertEqual ( expect_items , utils . sort_items ( items , sort_str ) ) <EOL> def test_sort_items_some_with_direction ( self ) : <EOL> items = self . get_test_items ( ) <EOL> sort_str = '<STR_LIT>' <EOL> expect_items = [ items [ <NUM_LIT:1> ] , items [ <NUM_LIT:0> ] , items [ <NUM_LIT:2> ] , items [ <NUM_LIT:3> ] ] <EOL> self . assertEqual ( expect_items , utils . sort_items ( items , sort_str ) ) <EOL> def test_sort_items_with_object ( self ) : <EOL> item1 = mock . Mock ( a = <NUM_LIT:1> , b = <NUM_LIT:2> ) <EOL> item2 = mock . Mock ( a = <NUM_LIT:1> , b = <NUM_LIT:3> ) <EOL> item3 = mock . Mock ( a = <NUM_LIT:2> , b = <NUM_LIT:2> ) <EOL> item4 = mock . Mock ( a = <NUM_LIT:2> , b = <NUM_LIT:1> ) <EOL> items = [ item1 , item2 , item3 , item4 ] <EOL> sort_str = '<STR_LIT>' <EOL> expect_items = [ item4 , item1 , item3 , item2 ] <EOL> self . assertEqual ( expect_items , utils . sort_items ( items , sort_str ) ) <EOL> def test_sort_items_with_empty_key ( self ) : <EOL> items = self . get_test_items ( ) <EOL> sort_srt = '<STR_LIT>' <EOL> self . assertEqual ( items , utils . sort_items ( items , sort_srt ) ) <EOL> sort_srt = None <EOL> self . assertEqual ( items , utils . sort_items ( items , sort_srt ) ) <EOL> def test_sort_items_with_invalid_key ( self ) : <EOL> items = self . get_test_items ( ) <EOL> sort_str = '<STR_LIT:c>' <EOL> self . assertRaises ( exceptions . CommandError , <EOL> utils . sort_items , <EOL> items , sort_str ) <EOL> def test_sort_items_with_invalid_direction ( self ) : <EOL> items = self . get_test_items ( ) <EOL> sort_str = '<STR_LIT>' <EOL> self . assertRaises ( exceptions . CommandError , <EOL> utils . sort_items , <EOL> items , sort_str ) <EOL> @ mock . patch . object ( time , '<STR_LIT>' ) <EOL> def test_wait_for_status_ok ( self , mock_sleep ) : <EOL> resource = mock . MagicMock ( status = '<STR_LIT>' ) <EOL> status_f = mock . Mock ( return_value = resource ) <EOL> res_id = str ( uuid . uuid4 ( ) ) <EOL> self . assertTrue ( utils . wait_for_status ( status_f , res_id , ) ) <EOL> mock_sleep . assert_not_called ( ) <EOL> @ mock . patch . object ( time , '<STR_LIT>' ) <EOL> def test_wait_for_status_ok__with_overrides ( self , mock_sleep ) : <EOL> resource = mock . MagicMock ( my_status = '<STR_LIT>' ) <EOL> status_f = mock . Mock ( return_value = resource ) <EOL> res_id = str ( uuid . uuid4 ( ) ) <EOL> self . assertTrue ( utils . wait_for_status ( status_f , res_id , <EOL> status_field = '<STR_LIT>' , <EOL> success_status = [ '<STR_LIT>' ] ) ) <EOL> mock_sleep . assert_not_called ( ) <EOL> @ mock . patch . object ( time , '<STR_LIT>' ) <EOL> def test_wait_for_status_error ( self , mock_sleep ) : <EOL> resource = mock . MagicMock ( status = '<STR_LIT>' ) <EOL> status_f = mock . Mock ( return_value = resource ) <EOL> res_id = str ( uuid . uuid4 ( ) ) <EOL> self . assertFalse ( utils . wait_for_status ( status_f , res_id ) ) <EOL> mock_sleep . assert_not_called ( ) <EOL> @ mock . patch . object ( time , '<STR_LIT>' ) <EOL> def test_wait_for_status_error_with_overrides ( self , mock_sleep ) : <EOL> resource = mock . MagicMock ( my_status = '<STR_LIT>' ) <EOL> status_f = mock . Mock ( return_value = resource ) <EOL> res_id = str ( uuid . uuid4 ( ) ) <EOL> self . assertFalse ( utils . wait_for_status ( status_f , res_id , <EOL> status_field = '<STR_LIT>' , <EOL> error_status = [ '<STR_LIT>' ] ) ) <EOL> mock_sleep . assert_not_called ( ) <EOL> @ mock . patch . object ( time , '<STR_LIT>' ) <EOL> def test_wait_for_delete_ok ( self , mock_sleep ) : <EOL> resource = mock . MagicMock ( status = '<STR_LIT>' , progress = None ) <EOL> mock_get = mock . Mock ( side_effect = [ resource , <EOL> exceptions . NotFound ( <NUM_LIT> ) ] ) <EOL> manager = mock . MagicMock ( get = mock_get ) <EOL> res_id = str ( uuid . uuid4 ( ) ) <EOL> callback = mock . Mock ( ) <EOL> self . assertTrue ( utils . wait_for_delete ( manager , res_id , <EOL> callback = callback ) ) <EOL> mock_sleep . assert_called_once_with ( <NUM_LIT:5> ) <EOL> callback . assert_called_once_with ( <NUM_LIT:0> ) <EOL> @ mock . patch . object ( time , '<STR_LIT>' ) <EOL> def test_wait_for_delete_timeout ( self , mock_sleep ) : <EOL> resource = mock . MagicMock ( status = '<STR_LIT>' ) <EOL> mock_get = mock . Mock ( return_value = resource ) <EOL> manager = mock . MagicMock ( get = mock_get ) <EOL> res_id = str ( uuid . uuid4 ( ) ) <EOL> self . assertFalse ( utils . wait_for_delete ( manager , res_id , sleep_time = <NUM_LIT:1> , <EOL> timeout = <NUM_LIT:1> ) ) <EOL> mock_sleep . assert_called_once_with ( <NUM_LIT:1> ) <EOL> @ mock . patch . object ( time , '<STR_LIT>' ) <EOL> def test_wait_for_delete_error ( self , mock_sleep ) : <EOL> resource = mock . MagicMock ( status = '<STR_LIT>' ) <EOL> mock_get = mock . Mock ( return_value = resource ) <EOL> manager = mock . MagicMock ( get = mock_get ) <EOL> res_id = str ( uuid . uuid4 ( ) ) <EOL> self . assertFalse ( utils . wait_for_delete ( manager , res_id ) ) <EOL> mock_sleep . assert_not_called ( ) <EOL> @ mock . patch . object ( time , '<STR_LIT>' ) <EOL> def test_wait_for_delete_error_with_overrides ( self , mock_sleep ) : <EOL> resource = mock . MagicMock ( my_status = '<STR_LIT>' ) <EOL> mock_get = mock . Mock ( return_value = resource ) <EOL> manager = mock . MagicMock ( get = mock_get ) <EOL> res_id = str ( uuid . uuid4 ( ) ) <EOL> self . assertFalse ( utils . wait_for_delete ( manager , res_id , <EOL> status_field = '<STR_LIT>' , <EOL> error_status = [ '<STR_LIT>' ] ) ) <EOL> mock_sleep . assert_not_called ( ) <EOL> @ mock . patch . object ( time , '<STR_LIT>' ) <EOL> def test_wait_for_delete_error_with_overrides_exception ( self , mock_sleep ) : <EOL> mock_get = mock . Mock ( side_effect = Exception ) <EOL> manager = mock . MagicMock ( get = mock_get ) <EOL> res_id = str ( uuid . uuid4 ( ) ) <EOL> self . assertTrue ( utils . wait_for_delete ( manager , res_id , <EOL> exception_name = [ '<STR_LIT>' ] ) ) <EOL> mock_sleep . assert_not_called ( ) <EOL> def test_build_kwargs_dict_value_set ( self ) : <EOL> self . assertEqual ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> utils . build_kwargs_dict ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_build_kwargs_dict_value_None ( self ) : <EOL> self . assertEqual ( { } , utils . build_kwargs_dict ( '<STR_LIT>' , None ) ) <EOL> def test_build_kwargs_dict_value_empty_str ( self ) : <EOL> self . assertEqual ( { } , utils . build_kwargs_dict ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> class NoUniqueMatch ( Exception ) : <EOL> pass <EOL> class TestFindResource ( test_utils . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestFindResource , self ) . setUp ( ) <EOL> self . name = '<STR_LIT>' <EOL> self . expected = mock . Mock ( ) <EOL> self . manager = mock . Mock ( ) <EOL> self . manager . resource_class = mock . Mock ( ) <EOL> self . manager . resource_class . __name__ = '<STR_LIT>' <EOL> def test_find_resource_get_int ( self ) : <EOL> self . manager . get = mock . Mock ( return_value = self . expected ) <EOL> result = utils . find_resource ( self . manager , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . expected , result ) <EOL> self . manager . get . assert_called_with ( <NUM_LIT:1> ) <EOL> def test_find_resource_get_int_string ( self ) : <EOL> self . manager . get = mock . Mock ( return_value = self . expected ) <EOL> result = utils . find_resource ( self . manager , "<STR_LIT:2>" ) <EOL> self . assertEqual ( self . expected , result ) <EOL> self . manager . get . assert_called_with ( <NUM_LIT:2> ) <EOL> def test_find_resource_get_uuid ( self ) : <EOL> uuid = '<STR_LIT>' <EOL> self . manager . get = mock . Mock ( return_value = self . expected ) <EOL> result = utils . find_resource ( self . manager , uuid ) <EOL> self . assertEqual ( self . expected , result ) <EOL> self . manager . get . assert_called_with ( uuid ) <EOL> def test_find_resource_get_whatever ( self ) : <EOL> self . manager . get = mock . Mock ( return_value = self . expected ) <EOL> result = utils . find_resource ( self . manager , '<STR_LIT>' ) <EOL> self . assertEqual ( self . expected , result ) <EOL> self . manager . get . assert_called_with ( '<STR_LIT>' ) <EOL> def test_find_resource_find ( self ) : <EOL> self . manager . get = mock . Mock ( side_effect = Exception ( '<STR_LIT>' ) ) <EOL> self . manager . find = mock . Mock ( return_value = self . expected ) <EOL> result = utils . find_resource ( self . manager , self . name ) <EOL> self . assertEqual ( self . expected , result ) <EOL> self . manager . get . assert_called_with ( self . name ) <EOL> self . manager . find . assert_called_with ( name = self . name ) <EOL> def test_find_resource_find_not_found ( self ) : <EOL> self . manager . get = mock . Mock ( side_effect = Exception ( '<STR_LIT>' ) ) <EOL> self . manager . find = mock . Mock ( <EOL> side_effect = exceptions . NotFound ( <NUM_LIT> , "<STR_LIT:2>" ) <EOL> ) <EOL> result = self . assertRaises ( exceptions . CommandError , <EOL> utils . find_resource , <EOL> self . manager , <EOL> self . name ) <EOL> self . assertEqual ( "<STR_LIT>" , <EOL> str ( result ) ) <EOL> self . manager . get . assert_called_with ( self . name ) <EOL> self . manager . find . assert_called_with ( name = self . name ) <EOL> def test_find_resource_list_forbidden ( self ) : <EOL> self . manager . get = mock . Mock ( side_effect = Exception ( '<STR_LIT>' ) ) <EOL> self . manager . find = mock . Mock ( side_effect = Exception ( '<STR_LIT>' ) ) <EOL> self . manager . list = mock . Mock ( <EOL> side_effect = exceptions . Forbidden ( <NUM_LIT> ) <EOL> ) <EOL> self . assertRaises ( exceptions . Forbidden , <EOL> utils . find_resource , <EOL> self . manager , <EOL> self . name ) <EOL> self . manager . list . assert_called_with ( ) <EOL> def test_find_resource_find_no_unique ( self ) : <EOL> self . manager . get = mock . Mock ( side_effect = Exception ( '<STR_LIT>' ) ) <EOL> self . manager . find = mock . Mock ( side_effect = NoUniqueMatch ( ) ) <EOL> result = self . assertRaises ( exceptions . CommandError , <EOL> utils . find_resource , <EOL> self . manager , <EOL> self . name ) <EOL> self . assertEqual ( "<STR_LIT>" , <EOL> str ( result ) ) <EOL> self . manager . get . assert_called_with ( self . name ) <EOL> self . manager . find . assert_called_with ( name = self . name ) <EOL> def test_find_resource_silly_resource ( self ) : <EOL> self . manager = mock . Mock ( ) <EOL> self . manager . get = mock . Mock ( side_effect = Exception ( '<STR_LIT>' ) ) <EOL> self . manager . find = mock . Mock ( <EOL> side_effect = AttributeError ( <EOL> "<STR_LIT>" , <EOL> ) <EOL> ) <EOL> silly_resource = FakeOddballResource ( <EOL> None , <EOL> { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:name>' : self . name } , <EOL> loaded = True , <EOL> ) <EOL> self . manager . list = mock . Mock ( <EOL> return_value = [ silly_resource , ] , <EOL> ) <EOL> result = utils . find_resource ( self . manager , self . name ) <EOL> self . assertEqual ( silly_resource , result ) <EOL> self . manager . get . assert_called_with ( self . name ) <EOL> self . manager . find . assert_called_with ( name = self . name ) <EOL> def test_find_resource_silly_resource_not_found ( self ) : <EOL> self . manager = mock . Mock ( ) <EOL> self . manager . get = mock . Mock ( side_effect = Exception ( '<STR_LIT>' ) ) <EOL> self . manager . find = mock . Mock ( <EOL> side_effect = AttributeError ( <EOL> "<STR_LIT>" , <EOL> ) <EOL> ) <EOL> self . manager . list = mock . Mock ( return_value = [ ] ) <EOL> result = self . assertRaises ( exceptions . CommandError , <EOL> utils . find_resource , <EOL> self . manager , <EOL> self . name ) <EOL> self . assertEqual ( "<STR_LIT>" , <EOL> str ( result ) ) <EOL> self . manager . get . assert_called_with ( self . name ) <EOL> self . manager . find . assert_called_with ( name = self . name ) <EOL> def test_format_dict ( self ) : <EOL> expected = "<STR_LIT>" <EOL> self . assertEqual ( expected , <EOL> utils . format_dict ( { '<STR_LIT:a>' : '<STR_LIT:b>' , '<STR_LIT:c>' : '<STR_LIT:d>' , '<STR_LIT:e>' : '<STR_LIT:f>' } ) ) <EOL> self . assertEqual ( expected , <EOL> utils . format_dict ( { '<STR_LIT:e>' : '<STR_LIT:f>' , '<STR_LIT:c>' : '<STR_LIT:d>' , '<STR_LIT:a>' : '<STR_LIT:b>' } ) ) <EOL> def test_format_list ( self ) : <EOL> expected = '<STR_LIT>' <EOL> self . assertEqual ( expected , utils . format_list ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) ) <EOL> self . assertEqual ( expected , utils . format_list ( [ '<STR_LIT:c>' , '<STR_LIT:b>' , '<STR_LIT:a>' ] ) ) <EOL> def test_format_list_of_dicts ( self ) : <EOL> expected = "<STR_LIT>" <EOL> sorted_data = [ { '<STR_LIT:a>' : '<STR_LIT:b>' , '<STR_LIT:c>' : '<STR_LIT:d>' } , { '<STR_LIT:e>' : '<STR_LIT:f>' } ] <EOL> unsorted_data = [ { '<STR_LIT:c>' : '<STR_LIT:d>' , '<STR_LIT:a>' : '<STR_LIT:b>' } , { '<STR_LIT:e>' : '<STR_LIT:f>' } ] <EOL> self . assertEqual ( expected , utils . format_list_of_dicts ( sorted_data ) ) <EOL> self . assertEqual ( expected , utils . format_list_of_dicts ( unsorted_data ) ) <EOL> self . assertEqual ( '<STR_LIT>' , utils . format_list_of_dicts ( [ ] ) ) <EOL> self . assertEqual ( '<STR_LIT>' , utils . format_list_of_dicts ( [ { } ] ) ) <EOL> def test_format_list_separator ( self ) : <EOL> expected = '<STR_LIT>' <EOL> actual_pre_sorted = utils . format_list ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] , separator = '<STR_LIT:\n>' ) <EOL> actual_unsorted = utils . format_list ( [ '<STR_LIT:c>' , '<STR_LIT:b>' , '<STR_LIT:a>' ] , separator = '<STR_LIT:\n>' ) <EOL> self . assertEqual ( expected , actual_pre_sorted ) <EOL> self . assertEqual ( expected , actual_unsorted ) </s>
