Exemplo n.º 1
0
def test_can_match_any_arg():
    mock = Mock(name="test_class.prop1")
    PyMock.mock(mock, return_values=[1])
    mock(1, 4)
    assert_that(mock).was_called(with_args=[1, MatchArg.any()])

    assert_exception(
        lambda: assert_that(mock).was_called(with_args=[2, MatchArg.any()]),
        "Expected to have been called 1 or more times. But call count was: 0")
Exemplo n.º 2
0
def test_can_match_custom_matcher():
    mock = Mock(name="test_class.prop1")
    PyMock.mock(mock, return_values=[1])
    mock(4)
    assert_that(mock).was_called(with_args=[MatchArg.match(lambda x: x == 4)])

    assert_exception(
        lambda: assert_that(mock).was_called(
            with_args=[MatchArg.match(lambda x: x == 5)]),
        "Expected to have been called 1 or more times. But call count was: 0")
Exemplo n.º 3
0
    def mock_kubectl(self, cluster_name: str, command: str, return_value: E.Either[Any, Any]):
        cmd = '{cwd}/{cache_dir_name}/kubectl ' \
              '--kubeconfig {cwd}/{cache_dir_name}/{cluster_name}-kubeconfig.yaml {command}' \
            .format(cache_dir_name=Config.cache_folder_name(), cwd=MockFileSystem.cwd(),
                    cluster_name=cluster_name, command=command)

        PyMock.mock(self.os_system.run, args=[cmd, MatchArg.any()], return_values=[return_value])
Exemplo n.º 4
0
    def mock_gcloud_download(self, return_value: E.Either[Any, Any]):
        os = 'darwin' if self.os_system.is_macos() else 'linux'
        cmd = 'cd {cwd}/{cache_dir_name} && ' \
              'curl -L https://dl.google.com/dl/cloudsdk/channels/rapid/' \
              'downloads/google-cloud-sdk-284.0.0-{os}-x86_64.tar.gz | tar zx' \
              .format(cache_dir_name=Config.cache_folder_name(), cwd=MockFileSystem.cwd(), os=os)

        PyMock.mock(self.os_system.run, args=[cmd, MatchArg.any()], return_values=[return_value])
Exemplo n.º 5
0
    def mock_download_install_kubectl(self, version: str, return_value: E.Either[Any, Any]):
        os = 'darwin' if self.os_system.is_macos() else 'linux'
        cmd = 'curl -L https://storage.googleapis.com/kubernetes-release/release' \
              '/{version}/bin/{os}/amd64/kubectl > {cwd}/{cache_dir_name}/kubectl && ' \
              'chmod u+x {cwd}/{cache_dir_name}/kubectl'\
            .format(version=version, cache_dir_name=Config.cache_folder_name(), cwd=MockFileSystem.cwd(), os=os)

        PyMock.mock(self.os_system.run, args=[cmd, MatchArg.any()], return_values=[return_value])
Exemplo n.º 6
0
    def mock_create_kube_config(self, cluster_name: str, return_value: E.Either[Any, Any]):
        cmd = '{script_dir}/create-kube-config.sh {cluster_name} {cwd}/{cache_dir_name} ' \
              '{cwd}/{cache_dir_name}/service_account.json ' \
              '"{cwd}/{cache_dir_name}/{cluster_name}-kubeconfig.yaml" project1' \
            .format(cache_dir_name=Config.cache_folder_name(), cwd=MockFileSystem.cwd(),
                    cluster_name=cluster_name, script_dir=MockFileSystem.script_dir())

        PyMock.mock(self.os_system.run, args=[cmd, MatchArg.any()], return_values=[return_value])
Exemplo n.º 7
0
    def mock_clusters_list(self, cluster_names: List[str]):
        for cluster_name in cluster_names:
            self.mock_create_kube_config(cluster_name, return_value=E.success())
            self.mock_kubectl_apply_temp_file(cluster_name, return_value=E.success())

        cmd = '{cwd}/{cache_dir_name}/google-cloud-sdk/bin/gcloud container clusters list --format=\"value(name)\"' \
            .format(cache_dir_name=Config.cache_folder_name(), cwd=MockFileSystem.cwd())

        PyMock.mock(self.os_system.run, args=[cmd, MatchArg.any()], return_values=[E.success('\n'.join(cluster_names))])
Exemplo n.º 8
0
 def mock_kubernetes_release(self, return_value: E.Either[Any, Any]):
     cmd = 'curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt'
     PyMock.mock(self.os_system.run, args=[cmd, MatchArg.any()], return_values=[return_value])
Exemplo n.º 9
0
    def mock_activate_service_account(self, key_file_path: str, return_values: List[E.Either[Any, Any]]):
        cmd = '{cwd}/{cache_dir_name}/google-cloud-sdk/bin/gcloud auth activate-service-account --key-file ' \
              '{cwd}/{cache_dir_name}/{key_file}' \
            .format(cache_dir_name=Config.cache_folder_name(), cwd=MockFileSystem.cwd(), key_file=key_file_path)

        PyMock.mock(self.os_system.run, args=[cmd, MatchArg.any()], return_values=return_values)
Exemplo n.º 10
0
    def mock_set_project_id(self, project_id: str, return_values: List[E.Either[Any, Any]]):
        cmd = '{cwd}/{cache_dir_name}/google-cloud-sdk/bin/gcloud config set project {project}' \
            .format(cache_dir_name=Config.cache_folder_name(), cwd=MockFileSystem.cwd(), project=project_id)

        PyMock.mock(self.os_system.run, args=[cmd, MatchArg.any()], return_values=return_values)
Exemplo n.º 11
0
def test_can_mock_any_arg():
    mock = Mock(name="test_class.prop1")
    PyMock.mock(mock, args=[1, MatchArg.any()], return_values=[1])
    result = mock(1, 4)
    assert result == 1