Skip to content

Commit

Permalink
Add new function call to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emredjan committed Jul 15, 2023
1 parent d1553e8 commit e1545af
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 34 deletions.
86 changes: 67 additions & 19 deletions tests/providers/hashicorp/_internal_client/test_vault_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
# under the License.
from __future__ import annotations

import sys
if sys.version_info < (3, 8):
from importlib_metadata import version
else:
from importlib.metadata import version

from unittest import mock
from unittest.mock import mock_open, patch

Expand Down Expand Up @@ -630,7 +636,7 @@ def test_userpass_different_auth_mount_point(self, mock_hvac):
assert 2 == vault_client.kv_engine_version

@mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")
def test_get_non_existing_key_v2(self, mock_hvac):
def test_get_non_existing_key_v2(self, mock_hvac):
mock_client = mock.MagicMock()
mock_hvac.Client.return_value = mock_client
# Response does not contain the requested key
Expand All @@ -640,9 +646,16 @@ def test_get_non_existing_key_v2(self, mock_hvac):
)
secret = vault_client.get_secret(secret_path="missing")
assert secret is None
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None
)
hvac_version = version("hvac")
if hvac_version >= "1.1.0":
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None, raise_on_deleted_version=True
)
else:
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None
)


@mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")
def test_get_non_existing_key_v2_different_auth(self, mock_hvac):
Expand All @@ -660,9 +673,16 @@ def test_get_non_existing_key_v2_different_auth(self, mock_hvac):
secret = vault_client.get_secret(secret_path="missing")
assert secret is None
assert "secret" == vault_client.mount_point
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None
)
hvac_version = version("hvac")
if hvac_version >= "1.1.0":
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None, raise_on_deleted_version=True
)
else:
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None
)


@mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")
def test_get_non_existing_key_v1(self, mock_hvac):
Expand Down Expand Up @@ -715,9 +735,16 @@ def test_get_existing_key_v2(self, mock_hvac):
)
secret = vault_client.get_secret(secret_path="path/to/secret")
assert {"secret_key": "secret_value"} == secret
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="path/to/secret", version=None
)
hvac_version = version("hvac")
if hvac_version >= "1.1.0":
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="path/to/secret", version=None, raise_on_deleted_version=True
)
else:
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="path/to/secret", version=None
)


@mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")
def test_get_existing_key_v2_without_preconfigured_mount_point(self, mock_hvac):
Expand Down Expand Up @@ -753,9 +780,16 @@ def test_get_existing_key_v2_without_preconfigured_mount_point(self, mock_hvac):
)
secret = vault_client.get_secret(secret_path="mount_point/path/to/secret")
assert {"secret_key": "secret_value"} == secret
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="mount_point", path="path/to/secret", version=None
)
hvac_version = version("hvac")
if hvac_version >= "1.1.0":
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="mount_point", path="path/to/secret", version=None, raise_on_deleted_version=True
)
else:
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="mount_point", path="path/to/secret", version=None
)


@mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")
def test_get_existing_key_v2_version(self, mock_hvac):
Expand Down Expand Up @@ -790,9 +824,16 @@ def test_get_existing_key_v2_version(self, mock_hvac):
)
secret = vault_client.get_secret(secret_path="missing", secret_version=1)
assert {"secret_key": "secret_value"} == secret
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=1
)
hvac_version = version("hvac")
if hvac_version >= "1.1.0":
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=1, raise_on_deleted_version=True
)
else:
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=1
)


@mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")
def test_get_existing_key_v1(self, mock_hvac):
Expand Down Expand Up @@ -1014,9 +1055,16 @@ def test_get_secret_including_metadata_v2(self, mock_hvac):
"warnings": None,
"auth": None,
} == metadata
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None
)
hvac_version = version("hvac")
if hvac_version >= "1.1.0":
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None, raise_on_deleted_version=True
)
else:
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None
)


@mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")
def test_get_secret_including_metadata_v1(self, mock_hvac):
Expand Down
44 changes: 35 additions & 9 deletions tests/providers/hashicorp/hooks/test_vault.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
# under the License.
from __future__ import annotations

import sys
if sys.version_info < (3, 8):
from importlib_metadata import version
else:
from importlib.metadata import version

from unittest import mock
from unittest.mock import PropertyMock, mock_open, patch

Expand Down Expand Up @@ -1001,9 +1007,16 @@ def test_get_existing_key_v2(self, mock_hvac, mock_get_connection):
test_hook = VaultHook(**kwargs)
secret = test_hook.get_secret(secret_path="missing")
assert {"secret_key": "secret_value"} == secret
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None
)
hvac_version = version("hvac")
if hvac_version >= "1.1.0":
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None, raise_on_deleted_version=True
)
else:
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None
)


@mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")
@mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")
Expand Down Expand Up @@ -1040,9 +1053,15 @@ def test_get_existing_key_v2_version(self, mock_hvac, mock_get_connection):
test_hook = VaultHook(**kwargs)
secret = test_hook.get_secret(secret_path="missing", secret_version=1)
assert {"secret_key": "secret_value"} == secret
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=1
)
hvac_version = version("hvac")
if hvac_version >= "1.1.0":
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=1, raise_on_deleted_version=True
)
else:
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=1
)

@mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")
@mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")
Expand Down Expand Up @@ -1185,9 +1204,16 @@ def test_get_secret_including_metadata_v2(self, mock_hvac, mock_get_connection):
"warnings": None,
"auth": None,
} == metadata
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None
)
hvac_version = version("hvac")
if hvac_version >= "1.1.0":
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None, raise_on_deleted_version=True
)
else:
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="secret", path="missing", version=None
)


@mock.patch("airflow.providers.hashicorp.hooks.vault.VaultHook.get_connection")
@mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")
Expand Down
30 changes: 24 additions & 6 deletions tests/providers/hashicorp/secrets/test_vault.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
# under the License.
from __future__ import annotations

import sys
if sys.version_info < (3, 8):
from importlib_metadata import version
else:
from importlib.metadata import version

from unittest import mock

import pytest
Expand Down Expand Up @@ -301,9 +307,15 @@ def test_get_conn_uri_non_existent_key(self, mock_hvac):

test_client = VaultBackend(**kwargs)
assert test_client.get_conn_uri(conn_id="test_mysql") is None
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="airflow", path="connections/test_mysql", version=None
)
hvac_version = version("hvac")
if hvac_version >= "1.1.0":
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="airflow", path="connections/test_mysql", version=None, raise_on_deleted_version=True
)
else:
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="airflow", path="connections/test_mysql", version=None
)
assert test_client.get_connection(conn_id="test_mysql") is None

@mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")
Expand Down Expand Up @@ -453,9 +465,15 @@ def test_get_variable_value_non_existent_key(self, mock_hvac):

test_client = VaultBackend(**kwargs)
assert test_client.get_variable("hello") is None
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="airflow", path="variables/hello", version=None
)
hvac_version = version("hvac")
if hvac_version >= "1.1.0":
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="airflow", path="variables/hello", version=None, raise_on_deleted_version=True
)
else:
mock_client.secrets.kv.v2.read_secret_version.assert_called_once_with(
mount_point="airflow", path="variables/hello", version=None
)
assert test_client.get_variable("hello") is None

@mock.patch("airflow.providers.hashicorp._internal_client.vault_client.hvac")
Expand Down

0 comments on commit e1545af

Please sign in to comment.