Skip to content

Commit

Permalink
naming improvements; add another test case
Browse files Browse the repository at this point in the history
  • Loading branch information
pgombar committed Sep 18, 2019
1 parent a4714e6 commit 88831b4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
5 changes: 3 additions & 2 deletions azurelinuxagent/common/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import azurelinuxagent.common.logger as logger
from azurelinuxagent.common.exception import EventError
from azurelinuxagent.common.future import ustr
from azurelinuxagent.common.protocol.wire import CONTAINER_ID
from azurelinuxagent.common.protocol.wire import CONTAINER_ID_ENV_VARIABLE
from azurelinuxagent.common.protocol.restapi import TelemetryEventParam, \
TelemetryEvent, \
get_properties
Expand Down Expand Up @@ -279,7 +279,8 @@ def _add_event(self, duration, evt_type, is_internal, is_success, message, name,
event.parameters.append(TelemetryEventParam('Message', message))
event.parameters.append(TelemetryEventParam('Duration', duration))
event.parameters.append(TelemetryEventParam('ExtensionType', evt_type))
event.parameters.append(TelemetryEventParam('ContainerId', os.environ.get(CONTAINER_ID, None)))
event.parameters.append(TelemetryEventParam('ContainerId',
os.environ.get(CONTAINER_ID_ENV_VARIABLE, "UNINITIALIZED")))

data = get_properties(event)
try:
Expand Down
5 changes: 3 additions & 2 deletions azurelinuxagent/common/protocol/wire.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
AGENTS_MANIFEST_FILE_NAME = "{0}.{1}.agentsManifest"
TRANSPORT_CERT_FILE_NAME = "TransportCert.pem"
TRANSPORT_PRV_FILE_NAME = "TransportPrivate.pem"
CONTAINER_ID = "AZURE_GUEST_AGENT_CONTAINER_ID"
# Store the last retrieved container id as an environment variable to be shared between threads for telemetry purposes
CONTAINER_ID_ENV_VARIABLE = "AZURE_GUEST_AGENT_CONTAINER_ID"

PROTOCOL_VERSION = "2012-11-30"
ENDPOINT_FINE_NAME = "WireServer"
Expand Down Expand Up @@ -1352,7 +1353,7 @@ def parse(self, xml_text):
self.role_config_name = findtext(role_config, "ConfigName")
container = find(xml_doc, "Container")
self.container_id = findtext(container, "ContainerId")
os.environ[CONTAINER_ID] = self.container_id
os.environ[CONTAINER_ID_ENV_VARIABLE] = self.container_id
self.remote_access_uri = findtext(container, "RemoteAccessInfo")
lbprobe_ports = find(xml_doc, "LBProbePorts")
self.load_balancer_probe_port = findtext(lbprobe_ports, "Port")
Expand Down
23 changes: 18 additions & 5 deletions tests/common/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ def patch_save_event(json_data):

with patch("azurelinuxagent.common.event.EventLogger.save_event", side_effect=patch_save_event):
# No container id is set
os.environ.pop(event.CONTAINER_ID, None)
os.environ.pop(event.CONTAINER_ID_ENV_VARIABLE, None)
event.add_event(name='dummy_name')
data = fileutil.read_file(tmp_file)
self.assertIn('{"name": "ContainerId", "value": null}', data)
self.assertIn('{"name": "ContainerId", "value": "UNINITIALIZED"}', data)

# Container id is set as an environment variable explicitly
os.environ[event.CONTAINER_ID] = '424242'
os.environ[event.CONTAINER_ID_ENV_VARIABLE] = '424242'
event.add_event(name='dummy_name')
data = fileutil.read_file(tmp_file)
self.assertIn('{{"name": "ContainerId", "value": "{0}"}}'.format(os.environ[event.CONTAINER_ID]), data)
self.assertIn('{{"name": "ContainerId", "value": "{0}"}}'.format(os.environ[event.CONTAINER_ID_ENV_VARIABLE]), data)

# Container id is set as an environment variable when parsing the goal state
xml_text = load_data("wire/goal_state.xml")
Expand All @@ -63,7 +63,20 @@ def patch_save_event(json_data):
data = fileutil.read_file(tmp_file)
self.assertIn('{{"name": "ContainerId", "value": "{0}"}}'.format(container_id), data)

os.environ.pop(event.CONTAINER_ID)
# Container id is updated as the goal state changes, both in telemetry event and in environment variables
new_container_id = "z6d5526c-5ac2-4200-b6e2-56f2b70c5ab2"
xml_text = load_data("wire/goal_state.xml")
xml_text_updated = xml_text.replace("c6d5526c-5ac2-4200-b6e2-56f2b70c5ab2", new_container_id)
goal_state = GoalState(xml_text_updated)

event.add_event(name='dummy_name')
data = fileutil.read_file(tmp_file)

# Assert both the environment variable and telemetry event got updated
self.assertEquals(os.environ[event.CONTAINER_ID_ENV_VARIABLE], new_container_id)
self.assertIn('{{"name": "ContainerId", "value": "{0}"}}'.format(new_container_id), data)

os.environ.pop(event.CONTAINER_ID_ENV_VARIABLE)

def test_event_status_event_marked(self):
es = event.__event_status__
Expand Down
1 change: 1 addition & 0 deletions tests/ga/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from azurelinuxagent.ga.exthandlers import *
from azurelinuxagent.common.protocol.wire import WireProtocol, InVMArtifactsProfile

# Mock sleep to reduce test execution time
SLEEP = time.sleep


Expand Down

0 comments on commit 88831b4

Please sign in to comment.