Ejemplo n.º 1
0
    def test_view_manifest_only_trainable(self):
        json = get_har_json()
        har = har_from_json(json)
        res_list = har_entries_to_resources(har)
        push_groups = resource_list_to_push_groups(
            res_list, train_domain_globs=["*reddit.com"])
        config = EnvironmentConfig(replay_dir="",
                                   request_url="https://www.reddit.com/",
                                   push_groups=push_groups,
                                   har_resources=res_list)
        with mock.patch("builtins.print") as mock_print:
            with tempfile.NamedTemporaryFile() as config_file:
                config.save_file(config_file.name)
                view_manifest(["--trainable", config_file.name])
        assert mock_print.call_count > 5

        printed_text = "\n".join(call[0][0]
                                 for call in mock_print.call_args_list
                                 if call[0])
        assert config.replay_dir in printed_text
        assert config.request_url in printed_text
        assert all(group.name in printed_text for group in config.push_groups
                   if group.trainable)

        pre_graph_text = printed_text.split("Execution Graph")[0]
        assert not any(group.name in pre_graph_text
                       for group in config.push_groups if not group.trainable)
Ejemplo n.º 2
0
    def test_simulator(self):
        har_json = get_har_json()
        har = har_from_json(har_json)
        res_list = har_entries_to_resources(har)
        push_groups = resource_list_to_push_groups(res_list)
        env_config = EnvironmentConfig(replay_dir="",
                                       request_url="https://www.reddit.com/",
                                       push_groups=push_groups,
                                       har_resources=res_list)

        client_env = get_fast_mobile_client_environment()

        simulator = Simulator(env_config)
        time_ms = simulator.simulate_load_time(client_env)
        assert time_ms > 0
Ejemplo n.º 3
0
    def test_view_manifest(self):
        har = har_from_json(get_har_json())
        res_list = har_entries_to_resources(har)
        push_groups = resource_list_to_push_groups(res_list)
        config = EnvironmentConfig(replay_dir="",
                                   request_url="https://www.reddit.com/",
                                   push_groups=push_groups,
                                   har_resources=res_list)

        with mock.patch("builtins.print") as mock_print:
            with tempfile.NamedTemporaryFile() as config_file:
                config.save_file(config_file.name)
                view_manifest([config_file.name])
        assert mock_print.call_count > 5

        printed_text = "\n".join(call[0][0]
                                 for call in mock_print.call_args_list
                                 if call[0])
        assert config.replay_dir in printed_text
        assert config.request_url in printed_text
        assert all(group.name in printed_text for group in config.push_groups)
        assert all(
            Url.parse(res.url).resource[:61] in printed_text
            for group in config.push_groups for res in group.resources)
Ejemplo n.º 4
0
 def setup(self):
     self.config = get_config()
     self.har = har_from_json(get_har_json())
Ejemplo n.º 5
0
 def setup(self):
     self.client_env = get_default_client_environment()
     self.har_json = get_har_json()
     self.har = har_from_json(self.har_json)
Ejemplo n.º 6
0
class TestCaptureHarInReplayServer:
    def setup(self):
        self.client_env = get_default_client_environment()
        self.har_json = get_har_json()
        self.har = har_from_json(self.har_json)

    def test_raises_on_no_replay_dir(self):
        config = _get_config()
        with pytest.raises(ValueError):
            capture_har_in_replay_server("https://www.cs.ucla.edu", config,
                                         self.client_env)

        config = _get_config(
            EnvironmentConfig(request_url="https://www.cs.ucla.edu",
                              replay_dir=""))
        with pytest.raises(ValueError):
            capture_har_in_replay_server("https://www.cs.ucla.edu", config,
                                         self.client_env)

    @mock.patch("tempfile.TemporaryDirectory")
    @mock.patch("builtins.open",
                new_callable=mock.mock_open,
                read_data=get_har_json())
    @mock.patch("subprocess.run")
    def test_writes_mahimahi_files_correctly(self, mock_run, mock_open,
                                             mock_tmpdir):
        tmp_dir = "/tmp/blaze_test_123"
        mock_run.return_value = subprocess.CompletedProcess(args=[],
                                                            returncode=0)
        mock_tmpdir.return_value.__enter__.return_value = tmp_dir
        config = _get_config(
            EnvironmentConfig(request_url="https://www.cs.ucla.edu",
                              replay_dir=tmp_dir))

        capture_har_in_replay_server("https://www.cs.ucla.edu", config,
                                     self.client_env)

        assert mock_open.call_args_list[0][0][0].startswith(tmp_dir)
        assert mock_open.call_args_list[1][0][0].startswith(tmp_dir)
        assert mock_open.call_args_list[0][0][1] == "w"
        assert mock_open.call_args_list[1][0][1] == "w"

    @mock.patch("builtins.open",
                new_callable=mock.mock_open,
                read_data=get_har_json())
    @mock.patch("subprocess.run")
    def test_calls_capture_har_with_correct_arguments(self, mock_run,
                                                      mock_open):
        mock_run.return_value = subprocess.CompletedProcess(args=[],
                                                            returncode=0)

        config = _get_config(
            EnvironmentConfig(request_url="https://www.cs.ucla.edu",
                              replay_dir="/tmp/dir"))
        har = capture_har_in_replay_server("https://www.cs.ucla.edu", config,
                                           self.client_env)

        run_args = mock_run.call_args_list[0][0][0]
        assert run_args[0] == "docker"
        assert run_args[-1] == "https://www.cs.ucla.edu"
        assert har == self.har