def test_start_interactive_stateboard_only(cartridge_cmd, project_with_patched_init): project = project_with_patched_init cli = Cli(cartridge_cmd) # start with stateboard-only flag cli.start(project, stateboard_only=True) check_instances_running(cli, project, stateboard_only=True)
def test_start_interactive_by_id(cartridge_cmd, project_with_patched_init): project = project_with_patched_init cli = Cli(cartridge_cmd) ID1 = get_instance_id(project.name, 'instance-1') # start instance-1 cli.start(project, [ID1]) check_instances_running(cli, project, [ID1])
def test_start_stop_status_run_dir(cartridge_cmd, project_with_patched_init): project = project_with_patched_init cli = Cli(cartridge_cmd) ID1 = get_instance_id(project.name, 'instance-1') # ID2 = get_instance_id(project.name, 'instance-2') RUN_DIR = 'my-run' status = cli.get_status(project, [ID1], run_dir=RUN_DIR) assert status.get(ID1) == STATUS_NOT_STARTED # assert status.get(ID2) == STATUS_NOT_STARTED cli.start(project, [ID1], stateboard=True, daemonized=True, run_dir=RUN_DIR) check_instances_running( cli, project, [ID1], stateboard=True, run_dir=RUN_DIR, daemonized=True ) status = cli.get_status(project, [ID1], run_dir=RUN_DIR) assert status.get(ID1) == STATUS_RUNNING # assert status.get(ID2) == STATUS_RUNNING cli.stop(project, [ID1], stateboard=True, run_dir=RUN_DIR) check_instances_stopped(cli, project, [ID1], run_dir=RUN_DIR) status = cli.get_status(project, [ID1], run_dir=RUN_DIR) assert status.get(ID1) == STATUS_STOPPED
def test_start_stop_status_cfg(cartridge_cmd, project_with_patched_init): project = project_with_patched_init cli = Cli(cartridge_cmd) ID1 = get_instance_id(project.name, 'instance-1') ID2 = get_instance_id(project.name, 'instance-2') CFG = 'my-conf.yml' write_conf(os.path.join(project.path, CFG), { ID1: {}, ID2: {}, }) status = cli.get_status(project, cfg=CFG) assert status.get(ID1) == STATUS_NOT_STARTED assert status.get(ID2) == STATUS_NOT_STARTED cli.start(project, stateboard=True, daemonized=True, cfg=CFG) check_instances_running( cli, project, [ID1, ID2], stateboard=True, cfg=CFG, daemonized=True, ) status = cli.get_status(project, cfg=CFG) assert status.get(ID1) == STATUS_RUNNING assert status.get(ID2) == STATUS_RUNNING cli.stop(project, stateboard=True, cfg=CFG) check_instances_stopped(cli, project, [ID1, ID2]) status = cli.get_status(project, cfg=CFG) assert status.get(ID1) == STATUS_STOPPED assert status.get(ID2) == STATUS_STOPPED
def test_start_interactive_run_dir(cartridge_cmd, project_with_patched_init): project = project_with_patched_init cli = Cli(cartridge_cmd) ID1 = get_instance_id(project.name, 'instance-1') # ID2 = get_instance_id(project.name, 'instance-2') RUN_DIR = 'my-run' cli.start(project, [ID1], stateboard=True, run_dir=RUN_DIR) check_instances_running( cli, project, [ID1], stateboard=True, run_dir=RUN_DIR )
def test_start_interactive_from_conf(cartridge_cmd, project_with_patched_init): project = project_with_patched_init cli = Cli(cartridge_cmd) ID1 = get_instance_id(project.name, 'instance-1') ID2 = get_instance_id(project.name, 'instance-2') write_conf(os.path.join(project.path, DEFAULT_CFG), { ID1: {}, ID2: {}, }) # start instances cli.start(project) check_instances_running(cli, project, [ID1, ID2])
def test_projects_option(self, RuntimeMock): projects = [ Stubs.project(True), Stubs.project(True), ] RuntimeMock.return_value.installed_projects = [ projects[0], projects[1], Stubs.project(True), ] with patch.object( RuntimeMock.return_value, 'activate_project', wraps=self.stub_activate_project ) as activate_project_mock: result = Cli.run('up', '--projects=' + ','.join([projects[0].name, projects[1].name])) assert activate_project_mock.call_count == 2 assert activate_project_mock.call_args_list[0][0][0] == projects[0] assert activate_project_mock.call_args_list[1][0][0] == projects[1] assert result.exit_code == 0 assert result.output == \ 'Starting [%s]...\n' % projects[0].name + \ 'Starting [%s]...\n' % projects[1].name + \ '[%s] running @ %s\n' % (projects[0].name, 'http://%s.test' % projects[0].name) + \ '[%s] running @ %s\n' % (projects[1].name, 'http://%s.test' % projects[1].name)
def test_without_projects(self, RuntimeMock): RuntimeMock.return_value.installed_projects = [] result = Cli.run('ls') assert result.exit_code == 0 assert result.output == 'There are no projects installed, run metal install to install new projects\n'
def test_start_script(cartridge_cmd, project_with_patched_init): project = project_with_patched_init cli = Cli(cartridge_cmd) ID1 = get_instance_id(project.name, 'instance-1') # ID2 = get_instance_id(project.name, 'instance-2') SCRIPT = 'my-init.lua' shutil.copyfile(os.path.join(project.path, DEFAULT_SCRIPT), os.path.join(project.path, SCRIPT)) cli.start(project, [ID1], stateboard=True, script=SCRIPT) check_instances_running( cli, project, [ID1], stateboard=True, script=SCRIPT )
def test_stopped_projects(self, RuntimeMock): project = Stubs.project() RuntimeMock.return_value.installed_projects = [project] result = Cli.run('ls') assert result.exit_code == 0 assert result.output == '[%s] stopped\n' % project.name
def test_exec(self, RuntimeMock): dir_name = os.path.basename(os.getcwd()) command = 'foo bar' result = Cli.run('exec', command) assert result.exit_code == 0 RuntimeMock.return_value.execute_command.assert_called_with( dir_name, command)
def test_ongoing_projects(self, RuntimeMock): project = Stubs.project(True, True) RuntimeMock.return_value.installed_projects = [project] result = Cli.run('ls') assert result.exit_code == 0 assert result.output == '[%s] running @ http://%s.test\n' % ( project.name, project.name)
def test_start_interactive_cfg(cartridge_cmd, project_with_patched_init): project = project_with_patched_init cli = Cli(cartridge_cmd) ID1 = get_instance_id(project.name, 'instance-1') ID2 = get_instance_id(project.name, 'instance-2') CFG = 'my-conf.yml' write_conf(os.path.join(project.path, CFG), { ID1: {}, ID2: {}, }) cli.start(project, stateboard=True, cfg=CFG) check_instances_running( cli, project, [ID1, ID2], stateboard=True, cfg=CFG )
def test_installed_project(self, RuntimeMock): project = Stubs.project() RuntimeMock.return_value.get_project.return_value = project result = Cli.run('build', '--project=' + project.name) RuntimeMock.return_value.build_project.assert_called_once_with(project) assert result.exit_code == 0 assert result.output == 'Building [%s]...\n' % project.name
def test_existing_project(self, RuntimeMock): project = Stubs.project(True) RuntimeMock.return_value.get_project.return_value = project result = Cli.run('uninstall', '--project=' + project.name) RuntimeMock.return_value.uninstall_project.assert_called_once_with( project) assert result.exit_code == 0 assert result.output == 'Uninstalling [%s]...\n' % project.name
def test_default_project(self, RuntimeMock): dir_name = os.path.basename(os.getcwd()) RuntimeMock.return_value.get_project.return_value = None result = Cli.run('build') RuntimeMock.return_value.get_project.assert_called_once_with(dir_name) assert result.exit_code != 0 assert isinstance(result.exception, ValueError) assert result.exception.message == 'Project [%s] is not installed' % dir_name
def test_uninstalled_project(self, RuntimeMock): project = Stubs.project() RuntimeMock.return_value.get_project.return_value = None result = Cli.run('build', '--project=' + project.name) RuntimeMock.return_value.get_project.assert_called_once_with( project.name) assert result.exit_code != 0 assert isinstance(result.exception, ValueError) assert result.exception.message == 'Project [%s] is not installed' % project.name
def test_mixed_projects(self, RuntimeMock): projects = [ Stubs.project(True, True), Stubs.project(), ] RuntimeMock.return_value.installed_projects = projects result = Cli.run('ls') assert result.exit_code == 0 assert result.output == '[%s] running @ http://%s.test\n[%s] stopped\n' % ( projects[0].name, projects[0].name, projects[1].name)
def test_new_project(self, RuntimeMock): path = os.getcwd() project_name = 'foobar' RuntimeMock.return_value.get_project.return_value = None result = Cli.run('new', project_name, '--framework=laravel') RuntimeMock.return_value.get_project.assert_called_once_with( project_name) RuntimeMock.return_value.create_project.assert_called_once_with( project_name, path, 'laravel') assert result.exit_code == 0 assert result.output == 'Creating [%s]...\n' % project_name
def test_start_stop_by_id_with_stateboard(cartridge_cmd, project_with_patched_init): project = project_with_patched_init cli = Cli(cartridge_cmd) ID1 = get_instance_id(project.name, 'instance-1') ID2 = get_instance_id(project.name, 'instance-2') # start instance-1 and stateboard cli.start(project, [ID1], daemonized=True, stateboard=True) # start instance-2 cli.start(project, [ID2], daemonized=True) check_instances_running(cli, project, [ID1, ID2], daemonized=True, stateboard=True) # stop instance-1 and stateboard cli.stop(project, [ID1], stateboard=True) check_instances_running(cli, project, [ID2], daemonized=True) check_instances_stopped(cli, project, [ID1], stateboard=True)
def test_start_stop_stateboard_only(cartridge_cmd, project_with_patched_init): project = project_with_patched_init cli = Cli(cartridge_cmd) # start with stateboard-only flag cli.start(project, daemonized=True, stateboard_only=True) check_instances_running(cli, project, daemonized=True, stateboard_only=True) # stop stateboard cli.stop(project, stateboard_only=True) check_instances_stopped(cli, project, stateboard_only=True)
def test_new_project(self, RuntimeMock): dir_name = os.path.basename(os.getcwd()) RuntimeMock.return_value.get_project.return_value = None result = Cli.run('install', '--framework=laravel') RuntimeMock.return_value.get_project.assert_called_once_with(dir_name) RuntimeMock.return_value.install_project.assert_called_once() RuntimeMock.return_value.build_project.assert_called_once() project = RuntimeMock.return_value.install_project.call_args[0][0] isinstance(project, Project) assert project.name == dir_name assert project.framework == 'laravel' project = RuntimeMock.return_value.build_project.call_args[0][0] isinstance(project, Project) assert project.name == dir_name assert project.framework == 'laravel' assert result.exit_code == 0 assert result.output == 'Installing [%s]...\n' % dir_name
def test_only_flag(self, RuntimeMock): dir_name = os.path.basename(os.getcwd()) project = Stubs.project(True, name=dir_name) RuntimeMock.return_value.installed_projects = [ project, Stubs.project(True), ] with patch.object( RuntimeMock.return_value, 'activate_project', wraps=self.stub_activate_project ) as activate_project_mock: result = Cli.run('up', '--only') activate_project_mock.assert_called_once_with(project) assert result.exit_code == 0 assert result.output == \ 'Starting [%s]...\n' % project.name + \ '[%s] running @ %s\n' % (project.name, 'http://%s.test' % project.name)
def test_project_name_slugify(self, RuntimeMock): RuntimeMock.return_value.get_project.return_value = None name = 'Foobar and sons' slug = 'foobar-and-sons' result = Cli.run('install', '--name=' + name, '--framework=rails') RuntimeMock.return_value.get_project.assert_called_once_with( 'foobar-and-sons') RuntimeMock.return_value.install_project.assert_called_once() RuntimeMock.return_value.build_project.assert_called_once() project = RuntimeMock.return_value.install_project.call_args[0][0] isinstance(project, Project) assert project.name == slug assert project.framework == 'rails' project = RuntimeMock.return_value.build_project.call_args[0][0] isinstance(project, Project) assert project.name == slug assert project.framework == 'rails' assert result.exit_code == 0 assert result.output == 'Installing [%s]...\n' % slug
def test_start_stop_from_conf_stateboard_only(cartridge_cmd, project_with_patched_init): project = project_with_patched_init cli = Cli(cartridge_cmd) ID1 = get_instance_id(project.name, 'instance-1') ID2 = get_instance_id(project.name, 'instance-2') write_conf(os.path.join(project.path, DEFAULT_CFG), { ID1: {}, ID2: {}, }) # start instances cli.start(project, daemonized=True, stateboard_only=True) check_instances_running(cli, project, daemonized=True, stateboard_only=True) # stop instances cli.stop(project, stateboard=True) check_instances_stopped(cli, project, stateboard_only=True)
def test_api(cartridge_cmd, project_getting_started): project = project_getting_started cli = Cli(cartridge_cmd) APP_INSTANCES = [get_instance_id(project.name, 'router')] S1_INSTANCES = [get_instance_id(project.name, 's1-master'), get_instance_id(project.name, 's1-replica')] S2_INSTANCES = [get_instance_id(project.name, 's2-master'), get_instance_id(project.name, 's2-replica')] # build app process = subprocess.run([cartridge_cmd, 'build'], cwd=project.path) assert process.returncode == 0 patch_cartridge_proc_titile(project) # check config and get instances instances_conf = get_instances_from_conf(project) instances = list(instances_conf.keys()) assert all([ instance in instances for instance in APP_INSTANCES + S1_INSTANCES + S2_INSTANCES ]) router_http_port = instances_conf[APP_INSTANCES[0]]['http_port'] admin_api_url = 'http://localhost:{}/admin/api'.format(router_http_port) # start application in interactive mode (to easily check logs on debug) cli.start(project) check_instances_running(cli, project, instances) # create app replicaset uris = [instances_conf[instance]['advertise_uri'] for instance in APP_INSTANCES] roles = ['api'] app_replicaset_uuid = create_replicaset(admin_api_url, uris, roles) wait_for_replicaset_is_healthy(admin_api_url, app_replicaset_uuid) replicaset_roles = get_replicaset_roles(admin_api_url, app_replicaset_uuid) # api role should contain vshard-router dependency assert set(replicaset_roles) == set(['api', 'vshard-router']) # create s1 replicaset uris = [instances_conf[instance]['advertise_uri'] for instance in S1_INSTANCES] roles = ['storage'] s1_replicaset_uuid = create_replicaset(admin_api_url, uris, roles) wait_for_replicaset_is_healthy(admin_api_url, s1_replicaset_uuid) replicaset_roles = get_replicaset_roles(admin_api_url, s1_replicaset_uuid) # storage role should contain vshard-storage dependency assert set(replicaset_roles) == set(['storage', 'vshard-storage']) # create s2 replicaset uris = [instances_conf[instance]['advertise_uri'] for instance in S2_INSTANCES] roles = ['storage'] s2_replicaset_uuid = create_replicaset(admin_api_url, uris, roles) wait_for_replicaset_is_healthy(admin_api_url, s2_replicaset_uuid) replicaset_roles = get_replicaset_roles(admin_api_url, s2_replicaset_uuid) # storage role should contain vshard-storage dependency assert set(replicaset_roles) == set(['storage', 'vshard-storage']) # bootstrap vshard bootstrap_vshard(admin_api_url) # test HTTP API CUSTOMER_ID = 10 CUSTOMER_NAME = 'Elizabeth' customer = { 'customer_id': CUSTOMER_ID, 'name': CUSTOMER_NAME } # create new customer url = 'http://localhost:{}/storage/customers/create'.format(router_http_port) r = requests.post(url, json=customer) assert r.status_code == requests.status_codes.codes.CREATED resp = r.json() assert 'info' in resp assert resp['info'] == 'Successfully created' # # create the same customer again # r = requests.post(url, json=customer) # # XXX: r.status_code is 500 now # get customer url = 'http://localhost:{}/storage/customers/{}'.format(router_http_port, CUSTOMER_ID) r = requests.get(url, json=customer) assert r.status_code == requests.status_codes.codes.OK resp = r.json() assert resp == { 'customer_id': CUSTOMER_ID, 'name': CUSTOMER_NAME, 'accounts': [], } # get customer by wrong id url = 'http://localhost:{}/storage/customers/{}'.format(router_http_port, CUSTOMER_ID+1) r = requests.get(url, json=customer) assert r.status_code == requests.status_codes.codes.NOT_FOUND resp = r.json() assert 'info' in resp assert resp['info'] == 'Customer not found'
def __init__(self, cli=None): self.cli = cli if cli else Cli()
def start_stop_cli(cartridge_cmd, request): cli = Cli(cartridge_cmd) request.addfinalizer(lambda: cli.terminate()) return cli
def test_status_from_conf(cartridge_cmd, project_with_patched_init): project = project_with_patched_init cli = Cli(cartridge_cmd) ID1 = get_instance_id(project.name, 'instance-1') ID2 = get_instance_id(project.name, 'instance-2') STATEBOARD_ID = get_stateboard_name(project.name) write_conf(os.path.join(project.path, DEFAULT_CFG), { ID1: {}, ID2: {}, }) # get status w/o stateboard status = cli.get_status(project) assert len(status) == 2 assert status.get(ID1) == STATUS_NOT_STARTED assert status.get(ID2) == STATUS_NOT_STARTED # get status w/ stateboard status = cli.get_status(project, stateboard=True) assert len(status) == 3 assert status.get(ID1) == STATUS_NOT_STARTED assert status.get(ID2) == STATUS_NOT_STARTED assert status.get(STATEBOARD_ID) == STATUS_NOT_STARTED # get status stateboard-only status = cli.get_status(project, stateboard_only=True) assert len(status) == 1 assert status.get(STATEBOARD_ID) == STATUS_NOT_STARTED # start instance-1 and stateboard cli.start(project, [ID1], stateboard=True, daemonized=True) wait_instances(cli, project, [ID1], stateboard=True) # get status w/o stateboard status = cli.get_status(project) assert len(status) == 2 assert status.get(ID1) == STATUS_RUNNING assert status.get(ID2) == STATUS_NOT_STARTED # get status w/ stateboard status = cli.get_status(project, stateboard=True) assert len(status) == 3 assert status.get(ID1) == STATUS_RUNNING assert status.get(ID2) == STATUS_NOT_STARTED assert status.get(STATEBOARD_ID) == STATUS_RUNNING # get status stateboard-only status = cli.get_status(project, stateboard_only=True) assert len(status) == 1 assert status.get(STATEBOARD_ID) == STATUS_RUNNING # stop instance-1 cli.stop(project, [ID1]) # get status w/o stateboard status = cli.get_status(project) assert len(status) == 2 assert status.get(ID1) == STATUS_STOPPED assert status.get(ID2) == STATUS_NOT_STARTED # get status w/ stateboard status = cli.get_status(project, stateboard=True) assert len(status) == 3 assert status.get(ID1) == STATUS_STOPPED assert status.get(ID2) == STATUS_NOT_STARTED assert status.get(STATEBOARD_ID) == STATUS_RUNNING # get status stateboard-only status = cli.get_status(project, stateboard_only=True) assert len(status) == 1 assert status.get(STATEBOARD_ID) == STATUS_RUNNING