Skip to content

Commit 57a2d72

Browse files
authored
Merge pull request #186 from stfc/Fallback_to_sane_metadata
BUG: Fall back to image metadata for invalid entries
2 parents 350a633 + 8849346 commit 57a2d72

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

OpenStack-Rabbit-Consumer/rabbit_consumer/aq_metadata.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
"""
77
import logging
88
from dataclasses import dataclass
9-
from typing import Dict, Optional
9+
from typing import Dict, Optional, Union
1010

1111
from mashumaro import DataClassDictMixin
1212
from mashumaro.config import BaseConfig
1313

1414
logger = logging.getLogger(__name__)
1515

1616

17+
# Case in-sensitive values that are considered invalid
18+
_INVALID_VALUES = ["none", "null", ""]
19+
20+
1721
@dataclass
1822
class AqMetadata(DataClassDictMixin):
1923
"""
@@ -48,8 +52,33 @@ class Config(BaseConfig):
4852
def override_from_vm_meta(self, vm_meta: Dict[str, str]):
4953
"""
5054
Overrides the values in the metadata with the values from the VM's
51-
metadata
55+
metadata if they are present and sane
5256
"""
5357
for attr, alias in self.Config.aliases.items():
54-
if alias in vm_meta:
55-
setattr(self, attr, vm_meta[alias])
58+
if alias not in vm_meta:
59+
continue
60+
61+
if not self._is_metadata_val_valid(vm_meta[alias]):
62+
logger.warning(
63+
"Invalid metadata value '%s' found for metadata property '%s', skipping",
64+
vm_meta[alias],
65+
alias,
66+
)
67+
continue
68+
69+
setattr(self, attr, vm_meta[alias])
70+
71+
@staticmethod
72+
def _is_metadata_val_valid(val: Union[str, None]) -> bool:
73+
"""
74+
Tests if an individual metadata value is sane, i.e.
75+
a str which is not null, or a blocked value.
76+
If this is valid, it returns true
77+
"""
78+
if not val:
79+
return False
80+
81+
user_val = val.lower().strip()
82+
if user_val in _INVALID_VALUES:
83+
return False
84+
return True

OpenStack-Rabbit-Consumer/tests/test_aq_metadata.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,33 @@ def test_aq_metadata_override_all(image_metadata):
6262
assert returned.aq_os_version == "osversion_mock"
6363

6464

65+
def test_aq_metadata_override_with_none_values(image_metadata):
66+
"""
67+
Tests that any invalid values, such as none, null or
68+
whitespace are all ignored when overriding from the image
69+
layer
70+
"""
71+
returned = AqMetadata.from_dict(image_metadata)
72+
returned.override_from_vm_meta(
73+
{
74+
"AQ_ARCHETYPE": "archetype_mock_override",
75+
"AQ_DOMAIN": "None",
76+
"AQ_PERSONALITY": "null",
77+
"AQ_OS": "none",
78+
"AQ_OSVERSION": " ", # Space intentionally left
79+
"AQ_SANDBOX": None,
80+
}
81+
)
82+
83+
assert returned.aq_archetype == "archetype_mock_override"
84+
85+
reference_metadata = AqMetadata.from_dict(image_metadata)
86+
assert returned.aq_domain == reference_metadata.aq_domain
87+
assert returned.aq_os == reference_metadata.aq_os
88+
assert returned.aq_os_version == reference_metadata.aq_os_version
89+
assert returned.aq_sandbox == reference_metadata.aq_sandbox
90+
91+
6592
def test_aq_metadata_sandbox(image_metadata):
6693
"""
6794
Tests the sandbox value in an AQ metadata object

OpenStack-Rabbit-Consumer/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.1
1+
3.1.0

charts/rabbit-consumer/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ type: application
66
# This is the chart version. This version number should be incremented each time you make changes
77
# to the chart and its templates, including the app version.
88
# Versions are expected to follow Semantic Versioning (https://semver.org/)
9-
version: 1.7.1
9+
version: 1.8.0
1010

1111
# This is the version number of the application being deployed. This version number should be
1212
# incremented each time you make changes to the application. Versions are not expected to
1313
# follow Semantic Versioning. They should reflect the version the application is using.
1414
# It is recommended to use it with quotes.
15-
appVersion: "v3.0.1"
15+
appVersion: "v3.1.0"

0 commit comments

Comments
 (0)