mirror of
https://github.com/saltstack/salt.git
synced 2025-04-16 09:40:20 +00:00
metadata_azure: added tests and docs
This commit is contained in:
parent
954f04f55c
commit
f384888b6f
4 changed files with 115 additions and 7 deletions
|
@ -25,6 +25,7 @@ grains modules
|
|||
mdadm
|
||||
mdata
|
||||
metadata
|
||||
metadata_azure
|
||||
metadata_gce
|
||||
minion_process
|
||||
napalm
|
||||
|
|
5
doc/ref/grains/all/salt.grains.metadata_azure.rst
Normal file
5
doc/ref/grains/all/salt.grains.metadata_azure.rst
Normal file
|
@ -0,0 +1,5 @@
|
|||
salt.grains.metadata_azure
|
||||
==========================
|
||||
|
||||
.. automodule:: salt.grains.metadata_azure
|
||||
:members:
|
|
@ -1,11 +1,17 @@
|
|||
"""
|
||||
Grains from cloud metadata servers at 169.254.169.254 in azure
|
||||
.. versionadded:: 3005.0
|
||||
Grains from cloud metadata servers at 169.254.169.254 in Azure Virtual Machine
|
||||
|
||||
.. versionadded:: 3006.0
|
||||
|
||||
:depends: requests
|
||||
|
||||
To enable these grains that pull from the http://169.254.169.254/metadata/instance?api-version=2020-09-01
|
||||
metadata server set `metadata_server_grains: True` in the minion config.
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
metadata_server_grains: True
|
||||
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
@ -22,10 +28,10 @@ def __virtual__():
|
|||
# Check if metadata_server_grains minion option is enabled
|
||||
if __opts__.get("metadata_server_grains", False) is False:
|
||||
return False
|
||||
azuretest = http.query(URL, status=True, headers=True, header_list=["Metadata: true"])
|
||||
if (
|
||||
azuretest.get("status", 404) != 200
|
||||
):
|
||||
azuretest = http.query(
|
||||
URL, status=True, headers=True, header_list=["Metadata: true"]
|
||||
)
|
||||
if azuretest.get("status", 404) != 200:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
@ -36,4 +42,4 @@ def metadata():
|
|||
result = http.query(URL, headers=True, header_list=["Metadata: true"])
|
||||
metadata = salt.utils.json.loads(result.get("body", {}))
|
||||
|
||||
return metadata
|
||||
return metadata
|
||||
|
|
96
tests/pytests/unit/grains/test_metadata_azure.py
Normal file
96
tests/pytests/unit/grains/test_metadata_azure.py
Normal file
|
@ -0,0 +1,96 @@
|
|||
"""
|
||||
Unit test for salt.grains.metadata_azure
|
||||
|
||||
|
||||
:codeauthor: :email" `Vishal Gupta <guvishal@vmware.com>
|
||||
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.grains.metadata_azure as metadata
|
||||
import salt.utils.http as http
|
||||
from tests.support.mock import create_autospec, patch
|
||||
|
||||
# from Exception import Exception, ValueError
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def configure_loader_modules():
|
||||
return {metadata: {"__opts__": {"metadata_server_grains": "True"}}}
|
||||
|
||||
|
||||
def test_metadata_azure_search():
|
||||
def mock_http(url="", headers=False, header_list=None):
|
||||
metadata_vals = {
|
||||
"http://169.254.169.254/metadata/instance?api-version=2020-09-01": {
|
||||
"body": '{"compute": {"test": "fulltest"}}',
|
||||
"headers": {"Content-Type": "application/json; charset=utf-8"},
|
||||
},
|
||||
}
|
||||
|
||||
return metadata_vals[url]
|
||||
|
||||
with patch(
|
||||
"salt.utils.http.query",
|
||||
create_autospec(http.query, autospec=True, side_effect=mock_http),
|
||||
):
|
||||
assert metadata.metadata() == {"compute": {"test": "fulltest"}}
|
||||
|
||||
|
||||
def test_metadata_virtual():
|
||||
print("running 1st")
|
||||
with patch(
|
||||
"salt.utils.http.query",
|
||||
create_autospec(
|
||||
http.query,
|
||||
autospec=True,
|
||||
return_value={
|
||||
"error": "Bad request: . Required metadata header not specified"
|
||||
},
|
||||
),
|
||||
):
|
||||
assert metadata.__virtual__() is False
|
||||
with patch(
|
||||
"salt.utils.http.query",
|
||||
create_autospec(
|
||||
http.query,
|
||||
autospec=True,
|
||||
return_value={
|
||||
"body": '{"compute": {"test": "fulltest"}}',
|
||||
"headers": {"Content-Type": "application/json; charset=utf-8"},
|
||||
"status": 200,
|
||||
},
|
||||
),
|
||||
):
|
||||
assert metadata.__virtual__() is True
|
||||
with patch(
|
||||
"salt.utils.http.query",
|
||||
create_autospec(
|
||||
http.query,
|
||||
autospec=True,
|
||||
return_value={
|
||||
"body": "test",
|
||||
"headers": {"Content-Type": "application/json; charset=utf-8"},
|
||||
"status": 404,
|
||||
},
|
||||
),
|
||||
):
|
||||
assert metadata.__virtual__() is False
|
||||
with patch(
|
||||
"salt.utils.http.query",
|
||||
create_autospec(
|
||||
http.query,
|
||||
autospec=True,
|
||||
return_value={
|
||||
"body": "test",
|
||||
"headers": {"Content-Type": "application/json; charset=utf-8"},
|
||||
"status": 400,
|
||||
},
|
||||
),
|
||||
):
|
||||
assert metadata.__virtual__() is False
|
Loading…
Add table
Reference in a new issue