def test_process_name_setproctitle_integration( tmpdir_factory: TempdirFactory) -> None: # NB: This test forks and then loads this module: we declare it so that it is inferred. import setproctitle # noqa: F401 buildroot = tmpdir_factory.mktemp("buildroot") manager_name = "Bob" process_name = f"{manager_name} [{buildroot}]" metadata_base_dir = tmpdir_factory.mktemp(".pids") subprocess.check_call( args=[ sys.executable, "-c", dedent(f"""\ from setproctitle import setproctitle as set_process_title from pants.pantsd.process_manager import ProcessManager set_process_title({process_name!r}) pm = ProcessManager({manager_name!r}, metadata_base_dir="{metadata_base_dir}") pm.write_pid() pm.write_process_name() """), ], env=dict(PYTHONPATH=os.pathsep.join(sys.path)), ) pm = ProcessManager(manager_name, metadata_base_dir=str(metadata_base_dir)) assert process_name == pm.process_name
def test_mktemp(self, tmp_path): config = cast(Config, FakeConfig(tmp_path)) t = TempdirFactory(TempPathFactory.from_config(config)) tmp = t.mktemp("world") assert tmp.relto(t.getbasetemp()) == "world0" tmp = t.mktemp("this") assert tmp.relto(t.getbasetemp()).startswith("this") tmp2 = t.mktemp("this") assert tmp2.relto(t.getbasetemp()).startswith("this") assert tmp2 != tmp
def test_mktemp(self, testdir): from _pytest.tmpdir import TempdirFactory config = testdir.parseconfig() config.option.basetemp = testdir.mkdir("hello") t = TempdirFactory(config) tmp = t.mktemp("world") assert tmp.relto(t.getbasetemp()) == "world0" tmp = t.mktemp("this") assert tmp.relto(t.getbasetemp()).startswith("this") tmp2 = t.mktemp("this") assert tmp2.relto(t.getbasetemp()).startswith("this") assert tmp2 != tmp
def test_mktemp(self, tmp_path): from _pytest.tmpdir import TempdirFactory, TempPathFactory config = FakeConfig(tmp_path) t = TempdirFactory(TempPathFactory.from_config(config)) tmp = t.mktemp("world") assert tmp.relto(t.getbasetemp()) == "world0" tmp = t.mktemp("this") assert tmp.relto(t.getbasetemp()).startswith("this") tmp2 = t.mktemp("this") assert tmp2.relto(t.getbasetemp()).startswith("this") assert tmp2 != tmp
def build_dcos_file_from_universe_definition( package: Dict, dcos_files_path: str, tmpdir_factory: TempdirFactory, ) -> Tuple[str, str, str]: """ Build the .dcos file if its not already present in the given directory. Returns a Tuple containing (path of .dcos file, name, and version) """ # TODO Ideally we should `migrate` and then `build`. name = package["name"] version = package["version"] target = os.path.join(dcos_files_path, "{}-{}.dcos".format(name, version)) if os.path.isfile(target): log.info("Skipping build, using cached file : {}".format(target)) else: del package["releaseVersion"] del package["selected"] package_json_file = tmpdir_factory.mktemp( sdk_utils.random_string()).join(sdk_utils.random_string()) package_json_file.write(json.dumps(package)) rc, _, _ = sdk_cmd.run_cli(" ".join([ "registry", "build", "--build-definition-file={}".format(str(package_json_file)), "--output-directory={}".format(dcos_files_path), "--json", ])) assert rc == 0 assert os.path.isfile(target), "No valid .dcos file is built" return target, name, version
def use_temp_dir_for_cache(monkeypatch: MonkeyPatch, tmp_path_factory: TempdirFactory) -> None: # This fixture makes sure that a single test function has a constant cache # cache. cache_dir = tmp_path_factory.mktemp(uuid.uuid4().hex) monkeypatch.setattr(LocalTrainingCache, "_get_cache_location", lambda: cache_dir)
def test_enterprise( self, enterprise_artifact: Path, enterprise_1_11_artifact: Path, enterprise_1_10_artifact: Path, enterprise_1_9_artifact: Path, tmpdir_factory: TempdirFactory, ) -> None: """ ``True`` is returned when given a DC/OS Enterprise artifact. """ artifacts = [ enterprise_artifact, enterprise_1_11_artifact, enterprise_1_10_artifact, enterprise_1_9_artifact, ] for artifact in artifacts: random = uuid.uuid4().hex workspace_dir = Path(str(tmpdir_factory.mktemp(random))) assert _is_enterprise( build_artifact=artifact, workspace_dir=workspace_dir, ) # We delete the workspace during the test so as not to use too much # space for the test. shutil.rmtree(path=str(workspace_dir))
def test_load_domain_from_directory_tree(tmpdir_factory: TempdirFactory): root = tmpdir_factory.mktemp("Parent Bot") root_domain = {"actions": ["utter_root", "utter_root2"]} utils.dump_obj_as_yaml_to_file(root / "domain_pt1.yml", root_domain) subdirectory_1 = root / "Skill 1" subdirectory_1.mkdir() skill_1_domain = {"actions": ["utter_skill_1"]} utils.dump_obj_as_yaml_to_file(subdirectory_1 / "domain_pt2.yml", skill_1_domain) subdirectory_2 = root / "Skill 2" subdirectory_2.mkdir() skill_2_domain = {"actions": ["utter_skill_2"]} utils.dump_obj_as_yaml_to_file(subdirectory_2 / "domain_pt3.yml", skill_2_domain) subsubdirectory = subdirectory_2 / "Skill 2-1" subsubdirectory.mkdir() skill_2_1_domain = {"actions": ["utter_subskill", "utter_root"]} # Check if loading from `.yaml` also works utils.dump_obj_as_yaml_to_file(subsubdirectory / "domain_pt4.yaml", skill_2_1_domain) actual = Domain.load(str(root)) expected = [ "utter_root", "utter_root2", "utter_skill_1", "utter_skill_2", "utter_subskill", ] assert set(actual.user_actions) == set(expected)
def calicoctl( tmpdir_factory: TempdirFactory ) -> Callable[[List[str], Optional[dict]], dict]: tmpdir = tmpdir_factory.mktemp('calicoctl') path = os.path.join(str(tmpdir), "calicoctl") system = platform.system().lower() download_url = ('https://downloads.mesosphere.io/dcos-calicoctl/bin' '/v3.12.0-d2iq.1/fd5d699-b80546e' '/calicoctl-{}-amd64').format(system) with open(path, 'wb') as f: r = requests.get(download_url, stream=True, verify=True) for chunk in r.iter_content(8192): f.write(chunk) # make binary executable st = os.stat(path) os.chmod(path, st.st_mode | stat.S_IEXEC) def exec(cmd: List[str], env: Optional[dict] = None) -> dict: process = subprocess.run([path] + cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) return { 'stdout': process.stdout.decode('utf-8'), 'stderr': process.stderr.decode('utf-8'), 'returncode': process.returncode, } return exec
def test_not_in_imports(input_path: Text, tmpdir_factory: TempdirFactory): root = tmpdir_factory.mktemp("Parent Bot") config_path = str(root / "config.yml") utils.dump_obj_as_yaml_to_file(root / "config.yml", {"imports": ["A/A/A", "A/B/A"]}) importer = MultiProjectImporter(config_path, project_directory=os.getcwd()) assert not importer.is_imported(input_path)
def test_not_importing_e2e_conversation_tests_in_project( tmpdir_factory: TempdirFactory, ): root = tmpdir_factory.mktemp("Parent Bot") config = {"imports": ["bots/Bot A"]} config_path = str(root / "config.yml") utils.dump_obj_as_yaml_to_file(config_path, config) story_file = root / "bots" / "Bot A" / "data" / "stories.md" story_file.write("""## story""", ensure=True) e2e_story_test_file = ( root / "bots" / "Bot A" / DEFAULT_E2E_TESTS_PATH / "conversation_tests.md" ) e2e_story_test_file.write( """## story test""", ensure=True, ) selector = MultiProjectImporter(config_path) # Conversation tests should not be included in story paths expected = { "story_paths": [str(story_file)], "e2e_story_paths": [str(e2e_story_test_file)], } actual = { "story_paths": selector._story_paths, "e2e_story_paths": selector._e2e_story_paths, } assert expected == actual
def add_dcos_files_to_registry(tmpdir_factory: TempdirFactory) -> None: # Use DCOS_FILES_PATH if its set to a valid path OR use pytest's tmpdir. dcos_files_path = os.environ.get("DCOS_FILES_PATH", "") valid_path_set = os.path.isdir(dcos_files_path) if valid_path_set and not os.access(dcos_files_path, os.W_OK): log.warning("{} is not writable.".format(dcos_files_path)) valid_path_set = False if not valid_path_set: dcos_files_path = str(tmpdir_factory.mktemp(sdk_utils.random_string())) stub_universe_urls = sdk_repository.get_repos() log.info("Using {} to build .dcos files (if not exists) from {}".format( dcos_files_path, stub_universe_urls)) dcos_files_list = build_dcos_files_from_stubs(stub_universe_urls, dcos_files_path, tmpdir_factory) log.info("Bundled .dcos files : {}".format(dcos_files_list)) @retrying.retry(stop_max_delay=5 * 60 * 1000, wait_fixed=5 * 1000) def wait_for_added_registry(name: str, version: str) -> None: code, stdout, stderr = sdk_cmd.run_cli( "registry describe --package-name={} --package-version={} --json". format(name, version), print_output=False, ) assert code == 0 and json.loads(stdout).get("status") == "Added" for file_path, name, version in dcos_files_list: rc, out, err = sdk_cmd.run_cli( "registry add --dcos-file={} --json".format(file_path)) assert rc == 0 assert len(json.loads(out)["packages"]) > 0, "No packages were added" wait_for_added_registry(name, version)
def add_dcos_files_to_registry(tmpdir_factory: TempdirFactory) -> None: # Use DCOS_FILES_PATH if its set to a valid path OR use pytest's tmpdir. dcos_files_path = os.environ.get("DCOS_FILES_PATH", "") valid_path_set = os.path.isdir(dcos_files_path) if valid_path_set and not os.access(dcos_files_path, os.W_OK): log.warning("{} is not writable.".format(dcos_files_path)) valid_path_set = False if not valid_path_set: dcos_files_path = str(tmpdir_factory.mktemp(sdk_utils.random_string())) stub_universe_urls = sdk_repository.get_repos() log.info( "Using {} to build .dcos files (if not exists) from {}".format( dcos_files_path, stub_universe_urls ) ) dcos_files_list = build_dcos_files_from_stubs( stub_universe_urls, dcos_files_path, tmpdir_factory ) log.info("Bundled .dcos files : {}".format(dcos_files_list)) @retrying.retry(stop_max_delay=5 * 60 * 1000, wait_fixed=5 * 1000) def wait_for_added_registry(name: str, version: str) -> None: code, stdout, stderr = sdk_cmd.run_cli( "registry describe --package-name={} --package-version={} --json".format(name, version), print_output=False, ) assert code == 0 and json.loads(stdout).get("status") == "Added" for file_path, name, version in dcos_files_list: rc, out, err = sdk_cmd.run_cli("registry add --dcos-file={} --json".format(file_path)) assert rc == 0 assert len(json.loads(out)["packages"]) > 0, "No packages were added" wait_for_added_registry(name, version)
def build_dcos_file_from_universe_definition( package: Dict, dcos_files_path: str, tmpdir_factory: TempdirFactory, ) -> Tuple[str, str, str]: """ Build the .dcos file if its not already present in the given directory. Returns a Tuple containing (path of .dcos file, name, and version) """ # TODO Ideally we should `migrate` and then `build`. name = package["name"] version = package["version"] target = os.path.join(dcos_files_path, "{}-{}.dcos".format(name, version)) if os.path.isfile(target): log.info("Skipping build, using cached file : {}".format(target)) else: del package["releaseVersion"] del package["selected"] package_json_file = tmpdir_factory.mktemp(sdk_utils.random_string()).join( sdk_utils.random_string() ) package_json_file.write(json.dumps(package)) rc, _, _ = sdk_cmd.run_cli( " ".join( [ "registry", "build", "--build-definition-file={}".format(str(package_json_file)), "--output-directory={}".format(dcos_files_path), "--json", ] ) ) assert rc == 0 assert os.path.isfile(target), "No valid .dcos file is built" return target, name, version
def init_default_project(tmpdir_factory: TempdirFactory) -> str: path = tmpdir_factory.mktemp("agent").strpath os.environ["LOG_LEVEL"] = "ERROR" check_call(["rasa", "init", "--no-prompt"], cwd=path) return path
def test_load_imports_from_directory_tree(tmpdir_factory: TempdirFactory): root = tmpdir_factory.mktemp("Parent Bot") root_imports = {"imports": ["Skill A"]} utils.dump_obj_as_yaml_to_file(root / "config.yml", root_imports) skill_a_directory = root / "Skill A" skill_a_directory.mkdir() skill_a_imports = {"imports": ["../Skill B"]} utils.dump_obj_as_yaml_to_file(skill_a_directory / "config.yml", skill_a_imports) skill_b_directory = root / "Skill B" skill_b_directory.mkdir() skill_b_imports = {"some other": ["../Skill C"]} utils.dump_obj_as_yaml_to_file(skill_b_directory / "config.yml", skill_b_imports) skill_b_subskill_directory = skill_b_directory / "Skill B-1" skill_b_subskill_directory.mkdir() skill_b_1_imports = {"imports": ["../../Skill A"]} # Check if loading from `.yaml` also works utils.dump_obj_as_yaml_to_file( skill_b_subskill_directory / "config.yaml", skill_b_1_imports ) # should not be imported subdirectory_3 = root / "Skill C" subdirectory_3.mkdir() actual = SkillSelector.load(root / "config.yml") expected = { os.path.join(str(skill_a_directory)), os.path.join(str(skill_b_directory)), } assert actual._imports == expected
def test_intent_evaluation_report_large(tmpdir_factory: TempdirFactory): path = tmpdir_factory.mktemp("evaluation") report_folder = path / "reports" report_filename = report_folder / "intent_report.json" rasa.utils.io.create_directory(str(report_folder)) def correct(label: Text) -> IntentEvaluationResult: return IntentEvaluationResult(label, label, "", 1.0) def incorrect(label: Text, _label: Text) -> IntentEvaluationResult: return IntentEvaluationResult(label, _label, "", 1.0) a_results = [correct("A")] * 10 b_results = [correct("B")] * 7 + [incorrect("B", "C")] * 3 c_results = [correct("C")] * 3 + [incorrect("C", "D") ] + [incorrect("C", "E")] d_results = [correct("D")] * 29 + [incorrect("D", "B")] * 3 e_results = [incorrect("E", "C")] * 5 + [incorrect("E", "")] * 5 intent_results = a_results + b_results + c_results + d_results + e_results evaluate_intents( intent_results, report_folder, successes=False, errors=False, confmat_filename=None, intent_hist_filename=None, disable_plotting=False, ) report = json.loads(rasa.utils.io.read_file(str(report_filename))) a_results = { "precision": 1.0, "recall": 1.0, "f1-score": 1.0, "support": 10, "confused_with": {}, } e_results = { "precision": 0.0, "recall": 0.0, "f1-score": 0.0, "support": 10, "confused_with": { "C": 5, "": 5 }, } c_confused_with = {"D": 1, "E": 1} assert len(report.keys()) == 8 assert report["A"] == a_results assert report["E"] == e_results assert report["C"]["confused_with"] == c_confused_with
def fixture_config(tmpdir_factory: TempdirFactory) -> Config: """Configure the Config object for the tests.""" data = tmpdir_factory.mktemp("data") config_file = str(data.join("config.yaml")) copyfile("tests/assets/config.yaml", config_file) config = Config(config_file) return config
def test_load_from_none(input_dict: Dict, tmpdir_factory: TempdirFactory): root = tmpdir_factory.mktemp("Parent Bot") config_path = root / "config.yml" utils.dump_obj_as_yaml_to_file(root / "config.yml", input_dict) actual = MultiProjectImporter(str(config_path)) assert actual._imports == list()
def trained_simple_project(tmpdir_factory: TempdirFactory) -> Text: path = tmpdir_factory.mktemp("simple") create_simple_project(path) os.environ["LOG_LEVEL"] = "ERROR" check_call(["rasa", "train"], cwd=path.strpath) return path.strpath
def fixture_config(tmpdir_factory: TempdirFactory) -> Config: """Configure the Config object for the tests.""" data = tmpdir_factory.mktemp("data") config_file = str(data.join("config.yaml")) copyfile("tests/assets/config.yaml", config_file) config = Config(config_file) config["database_url"] = f"tinydb://{data}/database.tinydb" config.save() return config
def test_get_model_from_directory_with_subdirectories( trained_rasa_model, tmpdir_factory: TempdirFactory): unpacked = get_model(trained_rasa_model) unpacked_core, unpacked_nlu = get_model_subdirectories(unpacked) assert unpacked_core assert unpacked_nlu directory = tmpdir_factory.mktemp("empty_model_dir").strpath with pytest.raises(ModelNotFound): get_model_subdirectories(directory)
async def _trained_default_agent(tmpdir_factory: TempdirFactory) -> Agent: model_path = tmpdir_factory.mktemp("model").strpath agent = Agent( "data/test_domains/default_with_slots.yml", policies=[AugmentedMemoizationPolicy(max_history=3)], ) training_data = await agent.load_data(DEFAULT_STORIES_FILE) agent.train(training_data) agent.persist(model_path) return agent
async def trained_moodbot_path(tmpdir_factory: TempdirFactory) -> Text: output = tmpdir_factory.mktemp("moodbot").strpath tmp_config_file = os.path.join(output, "config.yml") update_number_of_epochs("examples/moodbot/config.yml", tmp_config_file) return await train_async( domain="examples/moodbot/domain.yml", config=tmp_config_file, training_files="examples/moodbot/data/", output_path=MOODBOT_MODEL_PATH, )
def test_main_modular_filename(tmpdir_factory: TempdirFactory) -> None: """Test main function on modular file with filename.""" output_directory = Path(tmpdir_factory.mktemp('output')) input_filename = OPEN_API_DATA_PATH / 'modular.yaml' output_filename = output_directory / 'model.py' assert (main( ['--input', str(input_filename), '--output', str(output_filename)]) == Exit.ERROR)
def test_plain_text_consume_and_produce_newly_created_topic( consumer_group: str, interactive_cli_runner: CliRunner, producer: ConfluenceProducer, topic: str, topic_id: str, tmpdir_factory: TempdirFactory, ): source_topic_id = topic target_topic_id = topic_id output_directory = tmpdir_factory.mktemp("output_directory") produced_messages = produce_test_messages(producer, (source_topic_id, 1)) file_consumer = ConsumerFactory().create_consumer(consumer_group, source_topic_id, output_directory, last=False, avro=False) file_consumer.consume(10) result = interactive_cli_runner.invoke( produce, args=["-d", output_directory, target_topic_id], input="y\n", catch_exceptions=False) assert result.exit_code == 0 # Check assertions: assertion_check_directory = tmpdir_factory.mktemp( "assertion_check_directory") file_consumer = ConsumerFactory().create_consumer( (consumer_group + "assertion_check"), target_topic_id, assertion_check_directory, last=False, avro=False) file_consumer.consume(10) consumed_messages = get_consumed_messages(assertion_check_directory, False) assert produced_messages == consumed_messages
def install_dcos_cli(tmpdir_factory: TempdirFactory) -> Generator: """ Install the CLI. """ tmpdir = tmpdir_factory.mktemp('dcos_cli') cli = dcos_cli.DcosCli.new_cli( download_url='https://downloads.dcos.io/cli/releases/binaries/dcos/linux/x86-64/latest/dcos', core_plugin_url='https://downloads.dcos.io/cli/releases/plugins/dcos-core-cli/linux/x86-64/dcos-core-cli-1.14-patch.2.zip', # noqa: E501 ee_plugin_url='https://downloads.mesosphere.io/cli/releases/plugins/dcos-enterprise-cli/linux/x86-64/dcos-enterprise-cli-1.13-patch.1.zip', # noqa: E501 tmpdir=str(tmpdir) ) yield cli cli.clear_cli_dir()
def test_load_if_subskill_is_more_specific_than_parent(tmpdir_factory: TempdirFactory): root = tmpdir_factory.mktemp("Parent Bot") config_path = root / "config.yml" utils.dump_obj_as_yaml_to_file(root / "config.yml", {}) skill_a_directory = root / "Skill A" skill_a_directory.mkdir() skill_a_imports = {"imports": ["Skill B"]} utils.dump_obj_as_yaml_to_file(skill_a_directory / "config.yml", skill_a_imports) actual = SkillSelector.load(config_path) assert actual.is_imported(str(skill_a_directory))
async def test_only_getting_e2e_conversation_tests_if_e2e_enabled( tmpdir_factory: TempdirFactory, ): from rasa.core.interpreter import RegexInterpreter from rasa.core.training.structures import StoryGraph import rasa.core.training.loading as core_loading root = tmpdir_factory.mktemp("Parent Bot") config = {"imports": ["bots/Bot A"]} config_path = str(root / "config.yml") utils.dump_obj_as_yaml_to_file(config_path, config) story_file = root / "bots" / "Bot A" / "data" / "stories.md" story_file.write( """ ## story * greet - utter_greet """, ensure=True, ) e2e_story_test_file = ( root / "bots" / "Bot A" / DEFAULT_E2E_TESTS_PATH / "conversation_tests.md" ) e2e_story_test_file.write( """ ## story test * greet : "hello" - utter_greet """, ensure=True, ) selector = MultiProjectImporter(config_path) story_steps = await core_loading.load_data_from_resource( resource=str(e2e_story_test_file), domain=Domain.empty(), interpreter=RegexInterpreter(), template_variables=None, use_e2e=True, exclusion_percentage=None, ) expected = StoryGraph(story_steps) actual = await selector.get_stories(use_e2e=True) assert expected.as_story_string() == actual.as_story_string()
def test_main_modular(tmpdir_factory: TempdirFactory) -> None: """Test main function on modular file.""" output_directory = Path(tmpdir_factory.mktemp('output')) input_filename = OPEN_API_DATA_PATH / 'modular.yaml' output_path = output_directory / 'model' with freeze_time(TIMESTAMP): main(['--input', str(input_filename), '--output', str(output_path)]) main_modular_dir = EXPECTED_MAIN_PATH / 'main_modular' for path in main_modular_dir.rglob('*.py'): result = output_path.joinpath(path.relative_to(main_modular_dir)).read_text() assert result == path.read_text()
def test_load_if_subproject_is_more_specific_than_parent( tmpdir_factory: TempdirFactory): root = tmpdir_factory.mktemp("Parent Bot") config_path = str(root / "config.yml") utils.dump_obj_as_yaml_to_file(root / "config.yml", {}) project_a_directory = root / "Project A" project_a_directory.mkdir() project_a_imports = {"imports": ["Project B"]} utils.dump_obj_as_yaml_to_file(project_a_directory / "config.yml", project_a_imports) actual = MultiProjectImporter(config_path) assert actual.is_imported(str(project_a_directory))
async def _trained_default_agent(tmpdir_factory: TempdirFactory, stories_path: Text) -> Agent: model_path = tmpdir_factory.mktemp("model").strpath agent = Agent( "data/test_domains/default_with_slots.yml", policies=[AugmentedMemoizationPolicy(max_history=3), RulePolicy()], model_directory=model_path, ) training_data = await agent.load_data(stories_path) agent.train(training_data) agent.persist(model_path) return agent
def test_main_modular(tmpdir_factory: TempdirFactory, expected: Mapping[str, str]) -> None: """Test main function on modular file.""" output_directory = Path(tmpdir_factory.mktemp('output')) input_filename = OPEN_API_DATA_PATH / 'modular.yaml' output_path = output_directory / 'model' with freeze_time(TIMESTAMP): main(['--input', str(input_filename), '--output', str(output_path)]) for key, value in expected.items(): result = output_path.joinpath(*key).read_text() assert result == value