<s> import re <EOL> import sys <EOL> import fixtures <EOL> from keystoneclient . v3 import client as ksclient <EOL> from mox3 import mox <EOL> import six <EOL> import testtools <EOL> from monascaclient . common import http <EOL> from monascaclient import exc <EOL> import monascaclient . shell <EOL> from monascaclient . tests import fakes <EOL> class TestCase ( testtools . TestCase ) : <EOL> def set_fake_env ( self , fake_env ) : <EOL> client_env = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> for key in client_env : <EOL> self . useFixture ( <EOL> fixtures . EnvironmentVariable ( key , fake_env . get ( key ) ) ) <EOL> def assertRegexpMatches ( self , text , expected_regexp , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( expected_regexp , six . string_types ) : <EOL> expected_regexp = re . compile ( expected_regexp ) <EOL> if not expected_regexp . search ( text ) : <EOL> msg = msg or "<STR_LIT>" <EOL> msg = '<STR_LIT>' % ( <EOL> msg , expected_regexp . pattern , text ) <EOL> raise self . failureException ( msg ) <EOL> def shell_error ( self , argstr , error_match ) : <EOL> orig = sys . stderr <EOL> sys . stderr = six . StringIO ( ) <EOL> _shell = monascaclient . shell . MonascaShell ( ) <EOL> e = self . assertRaises ( Exception , _shell . main , argstr . split ( ) ) <EOL> self . assertRegexpMatches ( e . __str__ ( ) , error_match ) <EOL> err = sys . stderr . getvalue ( ) <EOL> sys . stderr . close ( ) <EOL> sys . stderr = orig <EOL> return err <EOL> class ShellBase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( ShellBase , self ) . setUp ( ) <EOL> self . m = mox . Mox ( ) <EOL> self . m . StubOutWithMock ( ksclient , '<STR_LIT>' ) <EOL> self . m . StubOutWithMock ( http . HTTPClient , '<STR_LIT>' ) <EOL> self . m . StubOutWithMock ( http . HTTPClient , '<STR_LIT>' ) <EOL> self . addCleanup ( self . m . VerifyAll ) <EOL> self . addCleanup ( self . m . UnsetStubs ) <EOL> def unset_exc_verbose ( ) : <EOL> exc . verbose = <NUM_LIT:0> <EOL> self . addCleanup ( unset_exc_verbose ) <EOL> def shell ( self , argstr ) : <EOL> orig = sys . stdout <EOL> try : <EOL> sys . stdout = six . StringIO ( ) <EOL> _shell = monascaclient . shell . MonascaShell ( ) <EOL> _shell . main ( argstr . split ( ) ) <EOL> self . subcommands = _shell . subcommands . keys ( ) <EOL> except SystemExit : <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> self . assertEqual ( <NUM_LIT:0> , exc_value . code ) <EOL> finally : <EOL> out = sys . stdout . getvalue ( ) <EOL> sys . stdout . close ( ) <EOL> sys . stdout = orig <EOL> return out <EOL> class ShellTestCommon ( ShellBase ) : <EOL> def setUp ( self ) : <EOL> super ( ShellTestCommon , self ) . setUp ( ) <EOL> def test_help_unknown_command ( self ) : <EOL> self . assertRaises ( exc . CommandError , self . shell , '<STR_LIT>' ) <EOL> def test_help ( self ) : <EOL> required = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> for argstr in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> help_text = self . shell ( argstr ) <EOL> for r in required : <EOL> self . assertRegexpMatches ( help_text , r ) <EOL> def test_command_help ( self ) : <EOL> output = self . shell ( '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , output ) <EOL> subcommands = list ( self . subcommands ) <EOL> for command in subcommands : <EOL> if command . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) == '<STR_LIT>' : <EOL> continue <EOL> output1 = self . shell ( '<STR_LIT>' % command ) <EOL> output2 = self . shell ( '<STR_LIT>' % command ) <EOL> self . assertEqual ( output1 , output2 ) <EOL> self . assertRegexpMatches ( output1 , '<STR_LIT>' % command ) <EOL> def test_help_on_subcommand ( self ) : <EOL> required = [ <EOL> '<STR_LIT>' , <EOL> "<STR_LIT>" , <EOL> ] <EOL> argstrings = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> for argstr in argstrings : <EOL> help_text = self . shell ( argstr ) <EOL> for r in required : <EOL> self . assertRegexpMatches ( help_text , r ) <EOL> class ShellTestMonascaCommands ( ShellBase ) : <EOL> def setUp ( self ) : <EOL> super ( ShellTestMonascaCommands , self ) . setUp ( ) <EOL> self . _set_fake_env ( ) <EOL> def _set_fake_env ( self ) : <EOL> fake_env = { <EOL> '<STR_LIT>' : '<STR_LIT:username>' , <EOL> '<STR_LIT>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . set_fake_env ( fake_env ) <EOL> def _script_keystone_client ( self ) : <EOL> fakes . script_keystone_client ( ) <EOL> def test_bad_metrics_create_subcommand ( self ) : <EOL> argstrings = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> _shell = monascaclient . shell . MonascaShell ( ) <EOL> for argstr in argstrings : <EOL> self . assertRaises ( SystemExit , _shell . main , argstr . split ( ) ) <EOL> def test_good_metrics_create_subcommand ( self ) : <EOL> self . _script_keystone_client ( ) <EOL> resp = fakes . FakeHTTPResponse ( <EOL> <NUM_LIT> , <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT:location>' : '<STR_LIT>' } , <EOL> None ) <EOL> http . HTTPClient . json_request ( <EOL> '<STR_LIT:POST>' , <EOL> '<STR_LIT>' , <EOL> data = { '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:value>' : <NUM_LIT> } , <EOL> headers = { '<STR_LIT>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : '<STR_LIT:username>' } ) . AndReturn ( ( resp , <EOL> None ) ) <EOL> self . m . ReplayAll ( ) <EOL> argstrings = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> for argstr in argstrings : <EOL> retvalue = self . shell ( argstr ) <EOL> self . assertRegexpMatches ( retvalue , "<STR_LIT>" ) <EOL> def test_bad_notifications_create_missing_args_subcommand ( self ) : <EOL> argstrings = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> _shell = monascaclient . shell . MonascaShell ( ) <EOL> for argstr in argstrings : <EOL> self . assertRaises ( SystemExit , _shell . main , argstr . split ( ) ) <EOL> def test_bad_notifications_create_type_subcommand ( self ) : <EOL> self . _script_keystone_client ( ) <EOL> argstrings = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> self . m . ReplayAll ( ) <EOL> for argstr in argstrings : <EOL> retvalue = self . shell ( argstr ) <EOL> self . assertRegexpMatches ( retvalue , "<STR_LIT>" ) <EOL> def test_notifications_create_type_sms ( self ) : <EOL> self . _script_keystone_client ( ) <EOL> argstrings = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> self . m . ReplayAll ( ) <EOL> for argstr in argstrings : <EOL> retvalue = self . shell ( argstr ) <EOL> self . assertRegexpMatches ( retvalue , "<STR_LIT>" ) <EOL> def test_good_notifications_create_subcommand ( self ) : <EOL> self . _script_keystone_client ( ) <EOL> resp = fakes . FakeHTTPResponse ( <EOL> <NUM_LIT> , <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT:location>' : '<STR_LIT>' } , <EOL> None ) <EOL> http . HTTPClient . json_request ( <EOL> '<STR_LIT:POST>' , <EOL> '<STR_LIT>' , <EOL> data = { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:address>' : '<STR_LIT>' } , <EOL> headers = { '<STR_LIT>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : '<STR_LIT:username>' } ) . AndReturn ( ( resp , '<STR_LIT:id>' ) ) <EOL> self . m . ReplayAll ( ) <EOL> argstrings = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> for argstr in argstrings : <EOL> retvalue = self . shell ( argstr ) <EOL> self . assertRegexpMatches ( retvalue , "<STR_LIT:id>" ) <EOL> def test_good_notifications_create_subcommand_webhook ( self ) : <EOL> self . _script_keystone_client ( ) <EOL> resp = fakes . FakeHTTPResponse ( <EOL> <NUM_LIT> , <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT:location>' : '<STR_LIT>' } , <EOL> None ) <EOL> http . HTTPClient . json_request ( <EOL> '<STR_LIT:POST>' , <EOL> '<STR_LIT>' , <EOL> data = { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:address>' : '<STR_LIT>' } , <EOL> headers = { '<STR_LIT>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : '<STR_LIT:username>' } ) . AndReturn ( ( resp , '<STR_LIT:id>' ) ) <EOL> self . m . ReplayAll ( ) <EOL> argstrings = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> for argstr in argstrings : <EOL> retvalue = self . shell ( argstr ) <EOL> self . assertRegexpMatches ( retvalue , "<STR_LIT:id>" ) <EOL> def test_good_alarm_definition_update ( self ) : <EOL> self . _script_keystone_client ( ) <EOL> cmd = '<STR_LIT>' <EOL> id = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> description = '<STR_LIT>' <EOL> expression = '<STR_LIT>' <EOL> notif_id = '<STR_LIT>' <EOL> enabled = '<STR_LIT:True>' <EOL> match_by = '<STR_LIT>' <EOL> severity = '<STR_LIT>' <EOL> resp = fakes . FakeHTTPResponse ( <EOL> <NUM_LIT> , <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT:location>' : '<STR_LIT>' } , <EOL> None ) <EOL> http . HTTPClient . json_request ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' + id , <EOL> data = { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:description>' : description , <EOL> '<STR_LIT>' : expression , <EOL> '<STR_LIT>' : [ notif_id ] , <EOL> '<STR_LIT>' : [ notif_id ] , <EOL> '<STR_LIT>' : [ notif_id ] , <EOL> '<STR_LIT>' : [ match_by ] , <EOL> '<STR_LIT>' : bool ( enabled ) , <EOL> '<STR_LIT>' : severity <EOL> } , <EOL> headers = { '<STR_LIT>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : '<STR_LIT:username>' } ) . AndReturn ( ( resp , '<STR_LIT:id>' ) ) <EOL> self . m . ReplayAll ( ) <EOL> args = [ cmd , id , name , description , expression , notif_id , <EOL> notif_id , notif_id , enabled , match_by , severity ] <EOL> argstring = "<STR_LIT:U+0020>" . join ( args ) <EOL> retvalue = self . shell ( argstring ) <EOL> self . assertRegexpMatches ( retvalue , "<STR_LIT:id>" ) </s>
