-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for extensions disabled; refactor VirtualMachine and VmExten…
…sion (#2824) * Add test for extensions disabled; refactor VirtualMachine and VmExtension --------- Co-authored-by: narrieta <narrieta>
- Loading branch information
Showing
16 changed files
with
456 additions
and
284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ azure-identity | |
azure-mgmt-compute>=22.1.0 | ||
azure-mgmt-resource>=15.0.0 | ||
msrestazure | ||
pytz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Microsoft Azure Linux Agent | ||
# | ||
# Copyright 2018 Microsoft Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
set -euo pipefail | ||
|
||
# | ||
# The service name is walinuxagent in Ubuntu/debian and waagent elsewhere | ||
# | ||
|
||
usage() ( | ||
echo "Usage: agent-service command" | ||
exit 1 | ||
) | ||
|
||
if [ "$#" -lt 1 ]; then | ||
usage | ||
fi | ||
cmd=$1 | ||
shift | ||
|
||
if [ "$#" -ne 0 ] || [ -z ${cmd+x} ] ; then | ||
usage | ||
fi | ||
|
||
if command -v systemctl &> /dev/null; then | ||
service-status() { systemctl --no-pager -l status $1; } | ||
service-stop() { systemctl stop $1; } | ||
service-restart() { systemctl restart $1; } | ||
service-start() { systemctl start $1; } | ||
else | ||
service-status() { service $1 status; } | ||
service-stop() { service $1 stop; } | ||
service-restart() { service $1 restart; } | ||
service-start() { service $1 start; } | ||
fi | ||
|
||
python=$(get-agent-python) | ||
distro=$($python -c 'from azurelinuxagent.common.version import get_distro; print(get_distro()[0])') | ||
distro=$(echo $distro | tr '[:upper:]' '[:lower:]') | ||
|
||
if [[ $distro == *"ubuntu"* || $distro == *"debian"* ]]; then | ||
service_name="walinuxagent" | ||
else | ||
service_name="waagent" | ||
fi | ||
|
||
echo "Service name: $service_name" | ||
|
||
if [[ "$cmd" == "restart" ]]; then | ||
echo "Restarting service..." | ||
service-restart $service_name | ||
echo "Service status..." | ||
service-status $service_name | ||
fi | ||
|
||
if [[ "$cmd" == "start" ]]; then | ||
echo "Starting service..." | ||
service-start $service_name | ||
fi | ||
|
||
if [[ "$cmd" == "stop" ]]; then | ||
echo "Stopping service..." | ||
service-stop $service_name | ||
fi | ||
|
||
if [[ "$cmd" == "status" ]]; then | ||
echo "Service status..." | ||
service-status $service_name | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Microsoft Azure Linux Agent | ||
# | ||
# Copyright 2018 Microsoft Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
# | ||
# Updates waagent.conf with the specified setting and value and restarts the Agent. | ||
# | ||
|
||
set -euo pipefail | ||
|
||
if [[ $# -ne 2 ]]; then | ||
echo "Usage: update-waagent-conf <setting> <value>" | ||
exit 1 | ||
fi | ||
|
||
name=$1 | ||
value=$2 | ||
|
||
PYTHON=$(get-agent-python) | ||
waagent_conf=$($PYTHON -c 'from azurelinuxagent.common.osutil import get_osutil; print(get_osutil().agent_conf_file_path)') | ||
echo "Setting $name=$value in $waagent_conf" | ||
sed -i -E "/^$name=/d" "$waagent_conf" | ||
sed -i -E "\$a $name=$value" "$waagent_conf" | ||
updated=$(grep "$name" "$waagent_conf") | ||
echo "Updated value: $updated" | ||
agent-service restart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# | ||
# The test suite disables extension processing and verifies that extensions | ||
# are not processed, but the agent continues reporting status. | ||
# | ||
name: "ExtensionsDisabled" | ||
tests: | ||
- "extensions_disabled.py" | ||
images: "random(endorsed)" | ||
owns_vm: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Microsoft Azure Linux Agent | ||
# | ||
# Copyright 2018 Microsoft Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
# | ||
# This test disables extension processing on waagent.conf and verifies that extensions are not processed, but the | ||
# agent continues reporting status. | ||
# | ||
|
||
import datetime | ||
import pytz | ||
|
||
from assertpy import assert_that, fail | ||
|
||
from azure.mgmt.compute.models import VirtualMachineInstanceView | ||
|
||
from tests_e2e.tests.lib.agent_test import AgentTest | ||
from tests_e2e.tests.lib.identifiers import VmExtensionIds | ||
from tests_e2e.tests.lib.logging import log | ||
from tests_e2e.tests.lib.ssh_client import SshClient | ||
from tests_e2e.tests.lib.virtual_machine_client import VirtualMachineClient | ||
from tests_e2e.tests.lib.virtual_machine_extension_client import VirtualMachineExtensionClient | ||
|
||
|
||
class ExtensionsDisabled(AgentTest): | ||
def run(self): | ||
ssh_client: SshClient = self._context.create_ssh_client() | ||
|
||
# Disable extension processing on the test VM | ||
log.info("Disabling extension processing on the test VM [%s]", self._context.vm.name) | ||
output = ssh_client.run_command("update-waagent-conf Extensions.Enabled n", use_sudo=True) | ||
log.info("Disable completed:\n%s", output) | ||
|
||
# From now on, extensions will time out; set the timeout to the minimum allowed(15 minutes) | ||
log.info("Setting the extension timeout to 15 minutes") | ||
vm: VirtualMachineClient = VirtualMachineClient(self._context.vm) | ||
|
||
vm.update({"extensionsTimeBudget": "PT15M"}) | ||
|
||
disabled_timestamp: datetime.datetime = datetime.datetime.utcnow() - datetime.timedelta(minutes=60) | ||
|
||
# | ||
# Validate that the agent is not processing extensions by attempting to run CustomScript | ||
# | ||
log.info("Executing CustomScript; it should time out after 15 min or so.") | ||
custom_script = VirtualMachineExtensionClient(self._context.vm, VmExtensionIds.CustomScript, resource_name="CustomScript") | ||
try: | ||
custom_script.enable(settings={'commandToExecute': "date"}, force_update=True, timeout=20 * 60) | ||
fail("CustomScript should have timed out") | ||
except Exception as error: | ||
assert_that("VMExtensionProvisioningTimeout" in str(error)) \ | ||
.described_as(f"Expected a VMExtensionProvisioningTimeout: {error}") \ | ||
.is_true() | ||
log.info("CustomScript timed out as expected") | ||
|
||
# | ||
# Validate that the agent continued reporting status even if it is not processing extensions | ||
# | ||
instance_view: VirtualMachineInstanceView = vm.get_instance_view() | ||
log.info("Instance view of VM Agent:\n%s", instance_view.vm_agent.serialize()) | ||
assert_that(instance_view.vm_agent.statuses).described_as("The VM agent should have exactly 1 status").is_length(1) | ||
assert_that(instance_view.vm_agent.statuses[0].display_status).described_as("The VM Agent should be ready").is_equal_to('Ready') | ||
# The time in the status is time zone aware and 'disabled_timestamp' is not; we need to make the latter time zone aware before comparing them | ||
assert_that(instance_view.vm_agent.statuses[0].time)\ | ||
.described_as("The VM Agent should be have reported status even after extensions were disabled")\ | ||
.is_greater_than(pytz.utc.localize(disabled_timestamp)) | ||
log.info("The VM Agent reported status after extensions were disabled, as expected.") | ||
|
||
|
||
if __name__ == "__main__": | ||
ExtensionsDisabled.run_from_command_line() |
Oops, something went wrong.