1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15import os 16import unittest 17 18from test.create_package_data import create_file 19from test.create_package_data import get_target_vendor_data 20from script_generator import get_proportion_value_list 21from script_generator import Script 22from script_generator import adjust_proportion_value_list 23from script_generator import get_progress_value 24from log_exception import VendorExpandError 25from utils import OPTIONS_MANAGER 26 27 28class TestScriptGenerator(unittest.TestCase): 29 30 def setUp(self): 31 print("set up") 32 33 def tearDown(self): 34 print("tear down") 35 36 def test_get_proportion_value_list(self): 37 """ 38 Get progress allocation 39 :return: 40 """ 41 proportion_value_list = get_proportion_value_list([1000, 1]) 42 adjusted_proportion_value_list = adjust_proportion_value_list( 43 proportion_value_list, 60) 44 self.assertEqual(adjusted_proportion_value_list, [59, 1]) 45 46 def test_proportion_value_list(self): 47 """ 48 Schedule allocation adjustment guarantee sum = 60 49 (default value) 50 :return: 51 """ 52 adjusted_proportion_value_list1 = adjust_proportion_value_list( 53 [58, 1], 60) 54 adjusted_proportion_value_list2 = adjust_proportion_value_list( 55 [60, 1], 60) 56 self.assertEqual(adjusted_proportion_value_list1, [58, 2]) 57 self.assertEqual(adjusted_proportion_value_list2, [59, 1]) 58 59 def test_proportion_value_list1(self): 60 """ 61 Schedule allocation adjustment guarantee sum = 60 62 (default value) 63 :return: 64 """ 65 adjusted_proportion_value_list = adjust_proportion_value_list( 66 [], 60) 67 self.assertEqual(adjusted_proportion_value_list, []) 68 69 def test_script_command_content(self): 70 """ 71 script, SuperClass commands. 72 """ 73 with self.assertRaises(VendorExpandError): 74 TestScript().sha_check() 75 76 with self.assertRaises(VendorExpandError): 77 TestScript().first_block_check() 78 79 with self.assertRaises(VendorExpandError): 80 TestScript().abort() 81 82 with self.assertRaises(VendorExpandError): 83 TestScript().show_progress() 84 85 with self.assertRaises(VendorExpandError): 86 TestScript().block_update() 87 88 with self.assertRaises(VendorExpandError): 89 TestScript().sparse_image_write() 90 91 with self.assertRaises(VendorExpandError): 92 TestScript().raw_image_write() 93 94 with self.assertRaises(VendorExpandError): 95 TestScript().get_status() 96 97 with self.assertRaises(VendorExpandError): 98 TestScript().set_status() 99 100 with self.assertRaises(VendorExpandError): 101 TestScript().reboot_now() 102 103 with self.assertRaises(VendorExpandError): 104 TestScript().updater_partitions() 105 106 def test_get_progress_value(self): 107 """ 108 script, SuperClass commands. 109 """ 110 file_path = "./vendor.img" 111 create_file(file_path, get_target_vendor_data()) 112 with open(file_path) as wo_f: 113 file_obj = wo_f 114 OPTIONS_MANAGER.two_step = True 115 OPTIONS_MANAGER.full_img_list = [] 116 OPTIONS_MANAGER.incremental_img_list = ['vendor', 'updater'] 117 OPTIONS_MANAGER.incremental_image_file_obj_list = [file_obj] 118 progress_value_dict = get_progress_value(distributable_value=60) 119 check_re = len(progress_value_dict) != 0 120 self.assertEqual(check_re, True) 121 file_obj.close() 122 if os.path.exists(file_path): 123 os.remove(file_path) 124 125 126class TestScript(Script): 127 def __init__(self): 128 super(TestScript, self).__init__() 129