<s> """<STR_LIT>""" <EOL> from json import loads , dumps <EOL> from time import sleep <EOL> from datetime import timedelta <EOL> from testtools import run_test_with <EOL> from twisted . internet import reactor <EOL> from twisted . internet . defer import gatherResults <EOL> from ... common import loop_until <EOL> from ... testtools import AsyncTestCase , async_runner , flaky , random_name <EOL> from . . testtools import ( <EOL> require_cluster , require_moving_backend , create_dataset , <EOL> create_python_container , verify_socket , post_http_server , <EOL> assert_http_server , query_http_server , is_process_running , <EOL> ACCEPTANCE_TEST_TIMEOUT <EOL> ) <EOL> from . . scripts import SCRIPTS <EOL> class ContainerAPITests ( AsyncTestCase ) : <EOL> """<STR_LIT>""" <EOL> run_tests_with = async_runner ( timeout = ACCEPTANCE_TEST_TIMEOUT ) <EOL> def _create_container ( self , cluster , script ) : <EOL> """<STR_LIT>""" <EOL> d = create_python_container ( <EOL> self , cluster , { <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : <NUM_LIT> , u"<STR_LIT>" : <NUM_LIT> } ] , <EOL> u"<STR_LIT>" : cluster . nodes [ <NUM_LIT:0> ] . uuid , <EOL> } , script ) <EOL> def check_result ( response ) : <EOL> dl = verify_socket ( cluster . nodes [ <NUM_LIT:0> ] . public_address , <NUM_LIT> ) <EOL> dl . addCallback ( lambda _ : response ) <EOL> return dl <EOL> d . addCallback ( check_result ) <EOL> return d <EOL> @ require_cluster ( <NUM_LIT:1> , require_container_agent = True ) <EOL> def test_create_container_with_ports ( self , cluster ) : <EOL> """<STR_LIT>""" <EOL> return self . _create_container ( cluster , SCRIPTS . child ( b"<STR_LIT>" ) ) <EOL> @ require_cluster ( <NUM_LIT:1> , require_container_agent = True ) <EOL> def test_create_container_restart_stopped ( self , cluster ) : <EOL> """<STR_LIT>""" <EOL> responses = [ ] <EOL> def query_and_save ( ) : <EOL> querying = query_http_server ( <EOL> cluster . nodes [ <NUM_LIT:0> ] . public_address , <NUM_LIT> <EOL> ) <EOL> querying . addCallback ( responses . append ) <EOL> return querying <EOL> created = self . _create_container ( <EOL> cluster , SCRIPTS . child ( b"<STR_LIT>" ) <EOL> ) <EOL> created . addCallback ( lambda ignored : query_and_save ( ) ) <EOL> created . addCallback ( lambda ignored : query_and_save ( ) ) <EOL> def check_different_response ( ignored ) : <EOL> self . assertNotEqual ( <EOL> responses [ <NUM_LIT:0> ] , <EOL> responses [ <NUM_LIT:1> ] , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> ) <EOL> created . addCallback ( check_different_response ) <EOL> return created <EOL> @ require_cluster ( <NUM_LIT:1> , require_container_agent = True ) <EOL> def test_create_container_with_environment ( self , cluster ) : <EOL> """<STR_LIT>""" <EOL> environment = { u"<STR_LIT>" : u"<STR_LIT>" , u"<STR_LIT>" : u"<STR_LIT>" } <EOL> d = create_python_container ( <EOL> self , cluster , { <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : <NUM_LIT> , u"<STR_LIT>" : <NUM_LIT> } ] , <EOL> u"<STR_LIT>" : cluster . nodes [ <NUM_LIT:0> ] . uuid , <EOL> u"<STR_LIT>" : environment , <EOL> } , SCRIPTS . child ( b"<STR_LIT>" ) ) <EOL> def checked ( _ ) : <EOL> host = cluster . nodes [ <NUM_LIT:0> ] . public_address <EOL> d = query_http_server ( host , <NUM_LIT> ) <EOL> d . addCallback ( lambda data : dict ( loads ( data ) ) ) <EOL> return d <EOL> d . addCallback ( checked ) <EOL> d . addCallback ( <EOL> lambda response : <EOL> self . assertDictContainsSubset ( environment , response ) <EOL> ) <EOL> return d <EOL> @ require_moving_backend <EOL> @ run_test_with ( async_runner ( timeout = timedelta ( minutes = <NUM_LIT:6> ) ) ) <EOL> @ require_cluster ( <NUM_LIT:2> , require_container_agent = True ) <EOL> def test_move_container_with_dataset ( self , cluster ) : <EOL> """<STR_LIT>""" <EOL> data = { u"<STR_LIT>" : u"<STR_LIT>" } <EOL> post_data = { "<STR_LIT:data>" : dumps ( data ) } <EOL> node1 , node2 = cluster . nodes <EOL> container_name = random_name ( self ) <EOL> creating_dataset = create_dataset ( self , cluster ) <EOL> def create_container ( dataset ) : <EOL> d = create_python_container ( <EOL> self , cluster , { <EOL> u"<STR_LIT:name>" : container_name , <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : <NUM_LIT> , u"<STR_LIT>" : <NUM_LIT> } ] , <EOL> u"<STR_LIT>" : node1 . uuid , <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : unicode ( dataset . dataset_id ) , <EOL> u"<STR_LIT>" : u"<STR_LIT>" } ] , <EOL> } , SCRIPTS . child ( b"<STR_LIT>" ) , <EOL> additional_arguments = [ u"<STR_LIT>" ] , <EOL> ) <EOL> return d <EOL> creating_dataset . addCallback ( create_container ) <EOL> creating_dataset . addCallback ( <EOL> lambda _ : post_http_server ( <EOL> self , node1 . public_address , <NUM_LIT> , post_data ) <EOL> ) <EOL> def move_container ( _ ) : <EOL> moved = cluster . move_container ( <EOL> container_name , node2 . uuid <EOL> ) <EOL> return moved <EOL> creating_dataset . addCallback ( move_container ) <EOL> creating_dataset . addCallback ( <EOL> lambda _ : assert_http_server ( <EOL> self , node2 . public_address , <NUM_LIT> , <EOL> expected_response = post_data [ "<STR_LIT:data>" ] ) <EOL> ) <EOL> return creating_dataset <EOL> @ require_cluster ( <NUM_LIT:1> , require_container_agent = True ) <EOL> def test_create_container_with_dataset ( self , cluster ) : <EOL> """<STR_LIT>""" <EOL> data = { u"<STR_LIT>" : u"<STR_LIT>" } <EOL> post_data = { "<STR_LIT:data>" : dumps ( data ) } <EOL> node = cluster . nodes [ <NUM_LIT:0> ] <EOL> container_name = random_name ( self ) <EOL> creating_dataset = create_dataset ( self , cluster ) <EOL> self . dataset_id = None <EOL> def create_container ( dataset ) : <EOL> self . dataset_id = unicode ( dataset . dataset_id ) <EOL> d = create_python_container ( <EOL> self , cluster , { <EOL> u"<STR_LIT:name>" : container_name , <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : <NUM_LIT> , u"<STR_LIT>" : <NUM_LIT> } ] , <EOL> u"<STR_LIT>" : node . uuid , <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : self . dataset_id , <EOL> u"<STR_LIT>" : u"<STR_LIT>" } ] , <EOL> } , SCRIPTS . child ( b"<STR_LIT>" ) , <EOL> additional_arguments = [ u"<STR_LIT>" ] , <EOL> cleanup = False , <EOL> ) <EOL> return d <EOL> creating_dataset . addCallback ( create_container ) <EOL> creating_dataset . addCallback ( <EOL> lambda _ : post_http_server ( <EOL> self , node . public_address , <NUM_LIT> , post_data ) <EOL> ) <EOL> creating_dataset . addCallback ( <EOL> lambda _ : assert_http_server ( <EOL> self , node . public_address , <NUM_LIT> , <EOL> expected_response = post_data [ "<STR_LIT:data>" ] ) <EOL> ) <EOL> creating_dataset . addCallback ( <EOL> lambda _ : cluster . remove_container ( container_name ) ) <EOL> def create_second_container ( _ ) : <EOL> d = create_python_container ( <EOL> self , cluster , { <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : <NUM_LIT> , u"<STR_LIT>" : <NUM_LIT> } ] , <EOL> u"<STR_LIT>" : node . uuid , <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : self . dataset_id , <EOL> u"<STR_LIT>" : u"<STR_LIT>" } ] , <EOL> } , SCRIPTS . child ( b"<STR_LIT>" ) , <EOL> additional_arguments = [ u"<STR_LIT>" ] , <EOL> ) <EOL> return d <EOL> creating_dataset . addCallback ( create_second_container ) <EOL> creating_dataset . addCallback ( <EOL> lambda _ : assert_http_server ( <EOL> self , node . public_address , <NUM_LIT> , <EOL> expected_response = post_data [ "<STR_LIT:data>" ] ) <EOL> ) <EOL> return creating_dataset <EOL> @ require_cluster ( <NUM_LIT:1> , require_container_agent = True ) <EOL> def test_current ( self , cluster ) : <EOL> """<STR_LIT>""" <EOL> creating = self . _create_container ( <EOL> cluster , SCRIPTS . child ( b"<STR_LIT>" ) <EOL> ) <EOL> def created ( data ) : <EOL> data [ u"<STR_LIT>" ] = True <EOL> def in_current ( ) : <EOL> current = cluster . current_containers ( ) <EOL> current . addCallback ( lambda result : data in result ) <EOL> return current <EOL> return loop_until ( reactor , in_current ) <EOL> creating . addCallback ( created ) <EOL> return creating <EOL> @ require_cluster ( <NUM_LIT:1> , require_container_agent = True ) <EOL> def test_non_root_container_can_access_dataset ( self , cluster ) : <EOL> """<STR_LIT>""" <EOL> node = cluster . nodes [ <NUM_LIT:0> ] <EOL> creating_dataset = create_dataset ( self , cluster ) <EOL> def created_dataset ( dataset ) : <EOL> return create_python_container ( <EOL> self , cluster , { <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : <NUM_LIT> , u"<STR_LIT>" : <NUM_LIT> } ] , <EOL> u"<STR_LIT>" : node . uuid , <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : unicode ( dataset . dataset_id ) , <EOL> u"<STR_LIT>" : u"<STR_LIT>" } ] , <EOL> } , SCRIPTS . child ( b"<STR_LIT>" ) , <EOL> additional_arguments = [ u"<STR_LIT>" ] ) <EOL> creating_dataset . addCallback ( created_dataset ) <EOL> creating_dataset . addCallback ( <EOL> lambda _ : assert_http_server ( self , node . public_address , <NUM_LIT> ) ) <EOL> return creating_dataset <EOL> @ require_cluster ( <NUM_LIT:2> , require_container_agent = True ) <EOL> def test_linking ( self , cluster ) : <EOL> """<STR_LIT>""" <EOL> destination_port = <NUM_LIT> <EOL> origin_port = <NUM_LIT> <EOL> [ destination , origin ] = cluster . nodes <EOL> running = gatherResults ( [ <EOL> create_python_container ( <EOL> self , cluster , { <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : <NUM_LIT> , <EOL> u"<STR_LIT>" : destination_port } ] , <EOL> u"<STR_LIT>" : destination . uuid , <EOL> } , SCRIPTS . child ( b"<STR_LIT>" ) ) , <EOL> create_python_container ( <EOL> self , cluster , { <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : <NUM_LIT> , <EOL> u"<STR_LIT>" : origin_port } ] , <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : "<STR_LIT>" , u"<STR_LIT>" : <NUM_LIT> , <EOL> u"<STR_LIT>" : destination_port } ] , <EOL> u"<STR_LIT>" : origin . uuid , <EOL> } , SCRIPTS . child ( b"<STR_LIT>" ) ) , <EOL> verify_socket ( destination . public_address , destination_port ) , <EOL> verify_socket ( origin . public_address , origin_port ) , <EOL> ] ) <EOL> running . addCallback ( <EOL> lambda _ : assert_http_server ( <EOL> self , origin . public_address , origin_port ) ) <EOL> return running <EOL> @ flaky ( [ u"<STR_LIT>" ] ) <EOL> @ require_cluster ( <NUM_LIT:2> , require_container_agent = True ) <EOL> def test_traffic_routed ( self , cluster ) : <EOL> """<STR_LIT>""" <EOL> port = <NUM_LIT> <EOL> [ destination , origin ] = cluster . nodes <EOL> running = gatherResults ( [ <EOL> create_python_container ( <EOL> self , cluster , { <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : <NUM_LIT> , u"<STR_LIT>" : port } ] , <EOL> u"<STR_LIT>" : destination . uuid , <EOL> } , SCRIPTS . child ( b"<STR_LIT>" ) ) , <EOL> verify_socket ( destination . public_address , port ) , <EOL> verify_socket ( origin . public_address , port ) , <EOL> ] ) <EOL> running . addCallback ( <EOL> lambda _ : assert_http_server ( self , origin . public_address , port ) ) <EOL> return running <EOL> @ run_test_with ( async_runner ( timeout = timedelta ( minutes = <NUM_LIT:20> ) ) ) <EOL> @ require_cluster ( <NUM_LIT:2> , require_container_agent = True ) <EOL> def test_reboot ( self , cluster ) : <EOL> """<STR_LIT>""" <EOL> node = [ node for node in cluster . nodes if <EOL> node . public_address != cluster . control_node . public_address ] [ <NUM_LIT:0> ] <EOL> def query ( ) : <EOL> d = query_http_server ( node . public_address , <NUM_LIT> ) <EOL> d . addCallback ( loads ) <EOL> return d <EOL> creating_dataset = create_dataset ( self , cluster , node = node ) <EOL> def created_dataset ( dataset ) : <EOL> return create_python_container ( <EOL> self , cluster , { <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : <NUM_LIT> , u"<STR_LIT>" : <NUM_LIT> } ] , <EOL> u"<STR_LIT>" : node . uuid , <EOL> u"<STR_LIT>" : [ { u"<STR_LIT>" : unicode ( dataset . dataset_id ) , <EOL> u"<STR_LIT>" : u"<STR_LIT>" } ] , <EOL> } , SCRIPTS . child ( b"<STR_LIT>" ) , <EOL> additional_arguments = [ u"<STR_LIT>" ] ) <EOL> creating_dataset . addCallback ( created_dataset ) <EOL> creating_dataset . addCallback ( lambda _ : query ( ) ) <EOL> def got_initial_result ( initial_result ) : <EOL> self . addCleanup ( node . run_script , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> d = node . run_script ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> d . addCallback ( lambda _ : node . reboot ( ) ) <EOL> d . addCallback ( lambda _ : sleep ( <NUM_LIT:20> ) ) <EOL> changed = d . addCallback ( lambda _ : <EOL> verify_socket ( node . public_address , <NUM_LIT> ) ) <EOL> changed . addCallback ( <EOL> lambda _ : loop_until ( reactor , lambda : is_process_running ( <EOL> node , b'<STR_LIT>' ) ) ) <EOL> def up_again ( _ ) : <EOL> sleep ( <NUM_LIT:10> ) <EOL> return node . run_script ( <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> changed . addCallback ( up_again ) <EOL> changed . addCallback ( lambda _ : loop_until ( <EOL> reactor , lambda : query ( ) . addCallback ( <EOL> lambda result : result != initial_result ) ) ) <EOL> changed . addCallback ( lambda _ : query ( ) ) <EOL> def result_changed ( new_result ) : <EOL> self . assertEqual ( <EOL> dict ( current_same = ( new_result [ "<STR_LIT>" ] == <EOL> initial_result [ "<STR_LIT>" ] ) , <EOL> written_same = ( new_result [ "<STR_LIT>" ] == <EOL> initial_result [ "<STR_LIT>" ] ) ) , <EOL> dict ( current_same = False , <EOL> written_same = True , <EOL> ) ) <EOL> changed . addCallback ( result_changed ) <EOL> return changed <EOL> creating_dataset . addCallback ( got_initial_result ) <EOL> return creating_dataset </s>
