#NEWLINE# def test_launch( self ) : #NEWLINE# #INDENT# conf_files = ( 'proxy-server.conf' , 'auth-server.conf' , 'object-server/1.conf' , 'object-server/2.conf' , 'object-server/3.conf' , 'object-server/4.conf' ) #NEWLINE# pid_files = ( ( 'proxy-server.pid' , 1 ) , ( 'proxy-server/2.pid' , 2 ) ) #NEWLINE# class MockSpawn ( object ) : #NEWLINE# #INDENT# def __init__( self , pids = None ) : #NEWLINE# #INDENT# self . conf_files = [ ] #NEWLINE# self . kwargs = [ ] #NEWLINE# if ( not pids ) : #NEWLINE# #INDENT# def one_forever( ) : #NEWLINE# #INDENT# while True : #NEWLINE# #INDENT# ( yield 1 ) #NEWLINE# #UNINDENT# #UNINDENT# self . pids = one_forever ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . pids = ( x for x in pids ) #NEWLINE# #UNINDENT# #UNINDENT# def __call__( self , conf_file , **kwargs ) : #NEWLINE# #INDENT# self . conf_files . append ( conf_file ) #NEWLINE# self . kwargs . append ( kwargs ) #NEWLINE# rv = next ( self . pids ) #NEWLINE# if isinstance ( rv , Exception ) : #NEWLINE# #INDENT# raise rv #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return rv #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# with temptree ( conf_files ) as swift_dir : #NEWLINE# #INDENT# manager . SWIFT_DIR = swift_dir #NEWLINE# ( files , pids ) = zip ( * pid_files ) #NEWLINE# with temptree ( files , pids ) as t : #NEWLINE# #INDENT# manager . RUN_DIR = t #NEWLINE# old_stdout = sys . stdout #NEWLINE# try : #NEWLINE# #INDENT# with open ( os . path . join ( t , 'output' ) , 'w+' ) as f : #NEWLINE# #INDENT# sys . stdout = f #NEWLINE# server = manager . Server ( 'test' , run_dir = t ) #NEWLINE# self . assertFalse ( server . launch ( ) ) #NEWLINE# manager . os = MockOs ( pids ) #NEWLINE# proc_files = ( ( '1/cmdline' , 'swift-proxy-server' ) , ( '2/cmdline' , 'swift-proxy-server' ) ) #NEWLINE# ( files , contents ) = zip ( * proc_files ) #NEWLINE# with temptree ( files , contents ) as proc_dir : #NEWLINE# #INDENT# manager . PROC_DIR = proc_dir #NEWLINE# server = manager . Server ( 'proxy' , run_dir = t ) #NEWLINE# self . assertFalse ( server . launch ( ) ) #NEWLINE# output = pop_stream ( f ) #NEWLINE# self . assertTrue ( ( 'running' in output ) ) #NEWLINE# conf_file = self . join_swift_dir ( 'proxy-server.conf' ) #NEWLINE# self . assertTrue ( ( conf_file in output ) ) #NEWLINE# pid_file = self . join_run_dir ( 'proxy-server/2.pid' ) #NEWLINE# self . assertTrue ( ( pid_file in output ) ) #NEWLINE# self . assertTrue ( ( 'already started' in output ) ) #NEWLINE# #UNINDENT# manager . os = MockOs ( [ ] ) #NEWLINE# with temptree ( [ ] , [ ] ) as proc_dir : #NEWLINE# #INDENT# manager . PROC_DIR = proc_dir #NEWLINE# mock_spawn = MockSpawn ( [ 1 ] ) #NEWLINE# server . spawn = mock_spawn #NEWLINE# conf_file = self . join_swift_dir ( 'proxy-server.conf' ) #NEWLINE# expected = { #NEWLINE# #INDENT# 1 : conf_file , #NEWLINE# #UNINDENT# } #NEWLINE# self . assertEqual ( server . launch ( once = True ) , expected ) #NEWLINE# self . assertEqual ( mock_spawn . conf_files , [ conf_file ] ) #NEWLINE# expected = { #NEWLINE# #INDENT# 'once' : False , #NEWLINE# #UNINDENT# } #NEWLINE# self . assertEqual ( mock_spawn . kwargs , [ expected ] ) #NEWLINE# output = pop_stream ( f ) #NEWLINE# self . assertTrue ( ( 'Starting' in output ) ) #NEWLINE# self . assertTrue ( ( 'once' not in output ) ) #NEWLINE# #UNINDENT# server = manager . Server ( 'object-replicator' ) #NEWLINE# with temptree ( [ ] , [ ] ) as proc_dir : #NEWLINE# #INDENT# manager . PROC_DIR = proc_dir #NEWLINE# mock_spawn = MockSpawn ( [ 1 , 2 , 3 , 4 ] ) #NEWLINE# server . spawn = mock_spawn #NEWLINE# conf1 = self . join_swift_dir ( 'object-server/1.conf' ) #NEWLINE# conf2 = self . join_swift_dir ( 'object-server/2.conf' ) #NEWLINE# conf3 = self . join_swift_dir ( 'object-server/3.conf' ) #NEWLINE# conf4 = self . join_swift_dir ( 'object-server/4.conf' ) #NEWLINE# expected = { #NEWLINE# #INDENT# 1 : conf1 , #NEWLINE# 2 : conf2 , #NEWLINE# 3 : conf3 , #NEWLINE# 4 : conf4 , #NEWLINE# #UNINDENT# } #NEWLINE# self . assertEqual ( server . launch ( once = True ) , expected ) #NEWLINE# self . assertEqual ( mock_spawn . conf_files , [ conf1 , conf2 , conf3 , conf4 ] ) #NEWLINE# expected = { #NEWLINE# #INDENT# 'once' : True , #NEWLINE# #UNINDENT# } #NEWLINE# self . assertEqual ( len ( mock_spawn . kwargs ) , 4 ) #NEWLINE# for kwargs in mock_spawn . kwargs : #NEWLINE# #INDENT# self . assertEqual ( kwargs , expected ) #NEWLINE# #UNINDENT# mock_spawn = MockSpawn ( [ 4 ] ) #NEWLINE# manager . PROC_DIR = proc_dir #NEWLINE# server . spawn = mock_spawn #NEWLINE# expected = { #NEWLINE# #INDENT# 4 : conf4 , #NEWLINE# #UNINDENT# } #NEWLINE# self . assertEqual ( server . launch ( number = 4 ) , expected ) #NEWLINE# self . assertEqual ( mock_spawn . conf_files , [ conf4 ] ) #NEWLINE# expected = { #NEWLINE# #INDENT# 'number' : 4 , #NEWLINE# #UNINDENT# } #NEWLINE# self . assertEqual ( mock_spawn . kwargs , [ expected ] ) #NEWLINE# #UNINDENT# server = manager . Server ( 'auth' ) #NEWLINE# with temptree ( [ ] , [ ] ) as proc_dir : #NEWLINE# #INDENT# manager . PROC_DIR = proc_dir #NEWLINE# mock_spawn = MockSpawn ( [ OSError ( errno . ENOENT , 'blah' ) ] ) #NEWLINE# server . spawn = mock_spawn #NEWLINE# self . assertEqual ( server . launch ( ) , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# self . assertTrue ( ( 'swift-auth-server does not exist' in pop_stream ( f ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# finally : #NEWLINE# #INDENT# sys . stdout = old_stdout