<s> import json <EOL> import os <EOL> import shutil <EOL> import unittest <EOL> import time <EOL> import subprocess <EOL> import requests <EOL> from wcloud . tasks . wcloud_tasks import flask_app , prepare_system , create_weblab_environment , configure_web_server , register_and_start_instance , finish_deployment <EOL> from weblab . admin . script import Creation <EOL> cwd = os . getcwd ( ) <EOL> if cwd . endswith ( os . path . join ( "<STR_LIT>" , "<STR_LIT:test>" ) ) : <EOL> cwd = cwd [ <NUM_LIT:0> : len ( cwd ) - len ( os . path . join ( "<STR_LIT>" , "<STR_LIT:test>" ) ) ] <EOL> os . chdir ( cwd ) <EOL> class TestWcloudTasks ( unittest . TestCase ) : <EOL> wcloud_settings = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:password>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:2> <EOL> } <EOL> def test_prepare_system ( self ) : <EOL> """<STR_LIT>""" <EOL> settings = prepare_system ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> self . wcloud_settings ) <EOL> self . _settings = settings <EOL> def test_create_weblab_environment ( self ) : <EOL> settings = prepare_system ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> self . wcloud_settings ) <EOL> self . _settings = settings <EOL> base_url = os . path . join ( flask_app . config [ "<STR_LIT>" ] , settings [ Creation . BASE_URL ] ) <EOL> create_weblab_environment ( base_url , settings ) <EOL> def test_create_weblab_environment_thorough ( self ) : <EOL> settings = prepare_system ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> self . wcloud_settings ) <EOL> self . _settings = settings <EOL> base_url = os . path . join ( flask_app . config [ "<STR_LIT>" ] , settings [ Creation . BASE_URL ] ) <EOL> create_weblab_environment ( base_url , settings ) <EOL> from weblab . comm . proxy_server import start as start_proxy <EOL> g = globals ( ) <EOL> l = locals ( ) <EOL> execfile ( os . path . join ( base_url , '<STR_LIT>' ) , g , l ) <EOL> print "<STR_LIT>" + base_url <EOL> p = g . get ( "<STR_LIT>" ) or l . get ( "<STR_LIT>" ) <EOL> start_proxy ( <NUM_LIT> , p ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> r = requests . get ( "<STR_LIT>" ) <EOL> assert r . status_code == <NUM_LIT:200> <EOL> assert "<STR_LIT>" in r . text <EOL> def test_configure_web_server ( self ) : <EOL> """<STR_LIT>""" <EOL> settings = prepare_system ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> self . wcloud_settings ) <EOL> self . _settings = settings <EOL> base_url = os . path . join ( flask_app . config [ "<STR_LIT>" ] , settings [ Creation . BASE_URL ] ) <EOL> creation_results = create_weblab_environment ( base_url , settings ) <EOL> configure_web_server ( creation_results ) <EOL> assert os . path . isfile ( os . path . join ( base_url , "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> content = open ( os . path . join ( base_url , "<STR_LIT>" , "<STR_LIT>" ) , "<STR_LIT:r>" ) . read ( ) <EOL> assert "<STR_LIT>" in content <EOL> lines = content . split ( "<STR_LIT:\n>" ) <EOL> line = [ line . strip ( ) for line in lines if "<STR_LIT>" in line ] [ <NUM_LIT:0> ] <EOL> assert os . path . isfile ( line . split ( "<STR_LIT>" ) [ <NUM_LIT:1> ] ) <EOL> def test_register_and_start_instance ( self ) : <EOL> """<STR_LIT>""" <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> settings = prepare_system ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> self . wcloud_settings ) <EOL> self . _settings = settings <EOL> base_url = os . path . join ( flask_app . config [ "<STR_LIT>" ] , settings [ Creation . BASE_URL ] ) <EOL> creation_results = create_weblab_environment ( base_url , settings ) <EOL> configure_web_server ( creation_results ) <EOL> register_and_start_instance ( "<STR_LIT>" , { } ) <EOL> start_port , end_port = creation_results [ "<STR_LIT>" ] , creation_results [ "<STR_LIT>" ] <EOL> def test_finish_deployment ( self ) : <EOL> """<STR_LIT>""" <EOL> settings = prepare_system ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> self . wcloud_settings ) <EOL> self . _settings = settings <EOL> base_url = os . path . join ( flask_app . config [ "<STR_LIT>" ] , settings [ Creation . BASE_URL ] ) <EOL> creation_results = create_weblab_environment ( base_url , settings ) <EOL> configure_web_server ( creation_results ) <EOL> register_and_start_instance ( "<STR_LIT>" , self . wcloud_settings ) <EOL> start_port , end_port = creation_results [ "<STR_LIT>" ] , creation_results [ "<STR_LIT>" ] <EOL> finish_deployment ( "<STR_LIT>" , settings , start_port , end_port , self . wcloud_settings ) <EOL> pass <EOL> def test_finish_deployment_thorough ( self ) : <EOL> """<STR_LIT>""" <EOL> settings = prepare_system ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:password>" , "<STR_LIT>" , <EOL> self . wcloud_settings ) <EOL> self . _settings = settings <EOL> base_url = os . path . join ( flask_app . config [ "<STR_LIT>" ] , settings [ Creation . BASE_URL ] ) <EOL> creation_results = create_weblab_environment ( base_url , settings ) <EOL> configure_web_server ( creation_results ) <EOL> register_and_start_instance ( "<STR_LIT>" , self . wcloud_settings ) <EOL> start_port , end_port = creation_results [ "<STR_LIT>" ] , creation_results [ "<STR_LIT>" ] <EOL> finish_deployment ( "<STR_LIT>" , settings , start_port , end_port , self . wcloud_settings ) <EOL> bot = requests . Session ( ) <EOL> r = bot . get ( "<STR_LIT>" ) <EOL> text = r . text <EOL> print text <EOL> assert "<STR_LIT>" in text <EOL> assert "<STR_LIT:input>" in text <EOL> assert "<STR_LIT>" in text <EOL> r = bot . post ( "<STR_LIT>" , data = """<STR_LIT>""" ) <EOL> str = r . text <EOL> print "<STR_LIT>" + str <EOL> j = json . loads ( str ) <EOL> assert not j [ "<STR_LIT>" ] <EOL> assert "<STR_LIT:id>" in j [ "<STR_LIT:result>" ] <EOL> def setUp ( self ) : <EOL> import wcloud . test . prepare as prepare <EOL> prepare . prepare_test_database ( "<STR_LIT:root>" , "<STR_LIT:password>" ) <EOL> os . system ( "<STR_LIT>" ) <EOL> def tearDown ( self ) : <EOL> try : <EOL> pass <EOL> except : <EOL> pass <EOL> os . system ( "<STR_LIT>" ) <EOL> try : <EOL> instances_file = os . path . join ( flask_app . config [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> f = file ( instances_file ) <EOL> lines = f . readlines ( ) <EOL> lines = [ line . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) + "<STR_LIT:\n>" for line in lines if not "<STR_LIT>" in line ] <EOL> f . close ( ) <EOL> f = file ( instances_file , "<STR_LIT:w>" ) <EOL> f . writelines ( lines ) <EOL> f . close ( ) <EOL> except : <EOL> pass <EOL> apacheconf = os . path . join ( flask_app . config [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> lines = open ( apacheconf , "<STR_LIT:r>" ) . readlines ( ) <EOL> cleared_lines = [ line . strip ( ) + "<STR_LIT:\n>" for line in lines if "<STR_LIT>" not in line ] <EOL> open ( apacheconf , "<STR_LIT:w>" ) . writelines ( cleared_lines ) <EOL> try : <EOL> os . system ( "<STR_LIT>" ) <EOL> except : <EOL> pass <EOL> try : <EOL> base_url = os . path . join ( flask_app . config [ "<STR_LIT>" ] , self . _settings [ Creation . BASE_URL ] ) <EOL> shutil . rmtree ( base_url ) <EOL> except : <EOL> pass <EOL> time . sleep ( <NUM_LIT> ) </s>
