Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/ut/src/asys/config_cmd/asys_config.py: 72%
64 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-19 17:46 +0800
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-19 17:46 +0800
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# ----------------------------------------------------------------------------
4# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17# ----------------------------------------------------------------------------
19import os
20import re
22from common import log_error
23from common import AsysConfigSupportedChip, get_device
24from common.cmd_run import run_linux_cmd
25from params import ParamDict
26from config_cmd.interface import get_stress_detect_config, restore_stress_detect_config
29class AsysConfig:
30 def __init__(self):
31 self.is_get_mode = ParamDict().get_arg('get')
32 self.is_restore_mode = ParamDict().get_arg('restore')
33 self.device_id = int(ParamDict().get_arg('device_id'))
34 self.device_obj = get_device(self.device_id)
35 self.options = []
37 def _check_support(self):
38 # not support VMs and docker
39 if not run_linux_cmd('systemd-detect-virt', 'none'):
40 log_error('The config command cannot be executed on VMs and docker.')
41 return False
42 # restore mode only support root
43 if os.getuid() != 0:
44 log_error('The config --restore command must be executed as the root user.')
45 return False
47 # without device
48 devices_num = self.device_obj.get_device_count()
49 if devices_num == 0:
50 return False
51 # device_id out of range
52 if self.device_id >= devices_num:
53 log_error(f"'-d' value should be in [0, {devices_num}), input {self.device_id}.")
54 return False
55 ret, chip_info = AsysConfigSupportedChip().get_supported_chip_info(self.device_id)
56 if not ret:
57 log_error(f'The config command does not support {chip_info}.')
58 return False
60 # check run mode
61 if not (self.is_get_mode or self.is_restore_mode):
62 log_error('The config command requires either the --get or --restore argument.')
63 return False
64 # check options
65 all_options = ['stress_detect']
66 current_options = []
67 for opt in all_options:
68 if ParamDict().get_arg(opt):
69 current_options.append(opt)
70 if not current_options:
71 log_error("At least one configuration option is required.")
72 return False
73 self.options = current_options
75 return True
77 def _run_get_mode(self):
78 ret = True
79 for opt in self.options:
80 if opt == 'stress_detect':
81 ret = ret and get_stress_detect_config(self.device_id, self.device_obj)
82 return ret
84 def _run_restore_mode(self):
85 ret = True
86 for opt in self.options:
87 if opt == 'stress_detect':
88 ret = ret and restore_stress_detect_config(self.device_id, self.device_obj)
89 return ret
91 def run(self):
92 if not self._check_support():
93 return False
95 if self.is_get_mode:
96 return self._run_get_mode()
97 if self.is_restore_mode:
98 return self._run_restore_mode()
100 return False