|
| 1 | +# Copyright 2026 Northern.tech AS |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import shutil |
| 16 | +import os |
| 17 | +import subprocess |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +from .. import conftest |
| 22 | +from .common_update import common_update_procedure |
| 23 | +from ..helpers import Helpers |
| 24 | +from ..MenderAPI import DeviceAuthV2, Deployments, logger, image |
| 25 | +from .mendertesting import MenderTesting |
| 26 | +from testutils.infra.device import MenderDeviceGroup |
| 27 | + |
| 28 | +from ..common_setup import ( |
| 29 | + class_persistent_standard_setup_one_client_bootstrapped, |
| 30 | + class_persistent_enterprise_one_client_bootstrapped, |
| 31 | +) |
| 32 | +from .test_state_scripts import ( |
| 33 | + class_persistent_setup_client_state_scripts_update_module, |
| 34 | + class_persistent_enterprise_setup_client_state_scripts_update_module, |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +class BaseTestCorruptDeploymentLog(MenderTesting): |
| 39 | + def do_test_corrupt_deployment_log(self, env): |
| 40 | + """Test that corrupted deployment log is sanitized and successfully |
| 41 | + uploaded to the server.""" |
| 42 | + |
| 43 | + mender_device = env.device |
| 44 | + devauth = DeviceAuthV2(env.auth) |
| 45 | + deploy = Deployments(env.auth, devauth) |
| 46 | + |
| 47 | + work_dir = "test_corrupt_depl_log.%s" % mender_device.host_string |
| 48 | + deployment_id = None |
| 49 | + try: |
| 50 | + artifact_script_dir = os.path.join(work_dir, "artifact-scripts") |
| 51 | + os.makedirs(artifact_script_dir) |
| 52 | + with open( |
| 53 | + os.path.join( |
| 54 | + artifact_script_dir, "ArtifactInstall_Leave_01_corrupt-depl-log" |
| 55 | + ), |
| 56 | + "w", |
| 57 | + ) as fd: |
| 58 | + fd.write( |
| 59 | + """#!/bin/sh |
| 60 | + log_file=$(ls /var/lib/mender/deployments.0000.*.log) |
| 61 | + echo break >> $log_file |
| 62 | + sync $log_file |
| 63 | + exit 1 |
| 64 | + """ |
| 65 | + ) |
| 66 | + |
| 67 | + # Callback for our custom artifact maker |
| 68 | + def make_artifact(filename, artifact_name): |
| 69 | + return image.make_module_artifact( |
| 70 | + "module-state-scripts-test", |
| 71 | + conftest.machine_name, |
| 72 | + artifact_name, |
| 73 | + filename, |
| 74 | + scripts=[artifact_script_dir], |
| 75 | + ) |
| 76 | + |
| 77 | + # Now create the artifact, and make the deployment. |
| 78 | + device_id = Helpers.ip_to_device_id_map( |
| 79 | + MenderDeviceGroup([mender_device.host_string]), devauth=devauth, |
| 80 | + )[mender_device.host_string] |
| 81 | + deployment_id = common_update_procedure( |
| 82 | + verify_status=False, |
| 83 | + devices=[device_id], |
| 84 | + scripts=[artifact_script_dir], |
| 85 | + make_artifact=make_artifact, |
| 86 | + devauth=devauth, |
| 87 | + deploy=deploy, |
| 88 | + )[0] |
| 89 | + |
| 90 | + deploy.check_expected_statistics(deployment_id, "failure", 1) |
| 91 | + logs = deploy.get_logs(device_id, deployment_id) |
| 92 | + assert "(THE ORIGINAL LOGS CONTAINED INVALID ENTRIES)" in logs |
| 93 | + except: |
| 94 | + output = mender_device.run( |
| 95 | + "cat /data/mender/deployment*.log", warn_only=True |
| 96 | + ) |
| 97 | + logger.info(output) |
| 98 | + raise |
| 99 | + finally: |
| 100 | + shutil.rmtree(work_dir, ignore_errors=True) |
| 101 | + if deployment_id: |
| 102 | + try: |
| 103 | + deploy.abort(deployment_id) |
| 104 | + except: |
| 105 | + pass |
| 106 | + |
| 107 | + |
| 108 | +class TestStateScriptsOpenSource(BaseTestCorruptDeploymentLog): |
| 109 | + def test_corrupt_deployment_log( |
| 110 | + self, class_persistent_setup_client_state_scripts_update_module, |
| 111 | + ): |
| 112 | + self.do_test_corrupt_deployment_log( |
| 113 | + class_persistent_setup_client_state_scripts_update_module, |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +class TestStateScriptsEnterprise(BaseTestCorruptDeploymentLog): |
| 118 | + def test_corrupt_deployment_log( |
| 119 | + self, class_persistent_enterprise_setup_client_state_scripts_update_module, |
| 120 | + ): |
| 121 | + self.do_test_corrupt_deployment_log( |
| 122 | + class_persistent_enterprise_setup_client_state_scripts_update_module, |
| 123 | + ) |
0 commit comments