140681896Sopenharmony_ci#!/usr/bin/env python
240681896Sopenharmony_ci# -*- coding: utf-8 -*-
340681896Sopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
440681896Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
540681896Sopenharmony_ci# you may not use this file except in compliance with the License.
640681896Sopenharmony_ci# You may obtain a copy of the License at
740681896Sopenharmony_ci#
840681896Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
940681896Sopenharmony_ci#
1040681896Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
1140681896Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
1240681896Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1340681896Sopenharmony_ci# See the License for the specific language governing permissions and
1440681896Sopenharmony_ci# limitations under the License.
1540681896Sopenharmony_ciimport os
1640681896Sopenharmony_ciimport unittest
1740681896Sopenharmony_ci
1840681896Sopenharmony_cifrom test.create_package_data import create_file
1940681896Sopenharmony_cifrom test.create_package_data import get_target_vendor_data
2040681896Sopenharmony_cifrom script_generator import get_proportion_value_list
2140681896Sopenharmony_cifrom script_generator import Script
2240681896Sopenharmony_cifrom script_generator import adjust_proportion_value_list
2340681896Sopenharmony_cifrom script_generator import get_progress_value
2440681896Sopenharmony_cifrom log_exception import VendorExpandError
2540681896Sopenharmony_cifrom utils import OPTIONS_MANAGER
2640681896Sopenharmony_ci
2740681896Sopenharmony_ci
2840681896Sopenharmony_ciclass TestScriptGenerator(unittest.TestCase):
2940681896Sopenharmony_ci
3040681896Sopenharmony_ci    def setUp(self):
3140681896Sopenharmony_ci        print("set up")
3240681896Sopenharmony_ci
3340681896Sopenharmony_ci    def tearDown(self):
3440681896Sopenharmony_ci        print("tear down")
3540681896Sopenharmony_ci
3640681896Sopenharmony_ci    def test_get_proportion_value_list(self):
3740681896Sopenharmony_ci        """
3840681896Sopenharmony_ci        Get progress allocation
3940681896Sopenharmony_ci        :return:
4040681896Sopenharmony_ci        """
4140681896Sopenharmony_ci        proportion_value_list = get_proportion_value_list([1000, 1])
4240681896Sopenharmony_ci        adjusted_proportion_value_list = adjust_proportion_value_list(
4340681896Sopenharmony_ci            proportion_value_list, 60)
4440681896Sopenharmony_ci        self.assertEqual(adjusted_proportion_value_list, [59, 1])
4540681896Sopenharmony_ci
4640681896Sopenharmony_ci    def test_proportion_value_list(self):
4740681896Sopenharmony_ci        """
4840681896Sopenharmony_ci        Schedule allocation adjustment guarantee sum = 60
4940681896Sopenharmony_ci        (default value)
5040681896Sopenharmony_ci        :return:
5140681896Sopenharmony_ci        """
5240681896Sopenharmony_ci        adjusted_proportion_value_list1 = adjust_proportion_value_list(
5340681896Sopenharmony_ci            [58, 1], 60)
5440681896Sopenharmony_ci        adjusted_proportion_value_list2 = adjust_proportion_value_list(
5540681896Sopenharmony_ci            [60, 1], 60)
5640681896Sopenharmony_ci        self.assertEqual(adjusted_proportion_value_list1, [58, 2])
5740681896Sopenharmony_ci        self.assertEqual(adjusted_proportion_value_list2, [59, 1])
5840681896Sopenharmony_ci
5940681896Sopenharmony_ci    def test_proportion_value_list1(self):
6040681896Sopenharmony_ci        """
6140681896Sopenharmony_ci        Schedule allocation adjustment guarantee sum = 60
6240681896Sopenharmony_ci        (default value)
6340681896Sopenharmony_ci        :return:
6440681896Sopenharmony_ci        """
6540681896Sopenharmony_ci        adjusted_proportion_value_list = adjust_proportion_value_list(
6640681896Sopenharmony_ci            [], 60)
6740681896Sopenharmony_ci        self.assertEqual(adjusted_proportion_value_list, [])
6840681896Sopenharmony_ci
6940681896Sopenharmony_ci    def test_script_command_content(self):
7040681896Sopenharmony_ci        """
7140681896Sopenharmony_ci        script, SuperClass commands.
7240681896Sopenharmony_ci        """
7340681896Sopenharmony_ci        with self.assertRaises(VendorExpandError):
7440681896Sopenharmony_ci            TestScript().sha_check()
7540681896Sopenharmony_ci
7640681896Sopenharmony_ci        with self.assertRaises(VendorExpandError):
7740681896Sopenharmony_ci            TestScript().first_block_check()
7840681896Sopenharmony_ci
7940681896Sopenharmony_ci        with self.assertRaises(VendorExpandError):
8040681896Sopenharmony_ci            TestScript().abort()
8140681896Sopenharmony_ci
8240681896Sopenharmony_ci        with self.assertRaises(VendorExpandError):
8340681896Sopenharmony_ci            TestScript().show_progress()
8440681896Sopenharmony_ci
8540681896Sopenharmony_ci        with self.assertRaises(VendorExpandError):
8640681896Sopenharmony_ci            TestScript().block_update()
8740681896Sopenharmony_ci
8840681896Sopenharmony_ci        with self.assertRaises(VendorExpandError):
8940681896Sopenharmony_ci            TestScript().sparse_image_write()
9040681896Sopenharmony_ci
9140681896Sopenharmony_ci        with self.assertRaises(VendorExpandError):
9240681896Sopenharmony_ci            TestScript().raw_image_write()
9340681896Sopenharmony_ci
9440681896Sopenharmony_ci        with self.assertRaises(VendorExpandError):
9540681896Sopenharmony_ci            TestScript().get_status()
9640681896Sopenharmony_ci
9740681896Sopenharmony_ci        with self.assertRaises(VendorExpandError):
9840681896Sopenharmony_ci            TestScript().set_status()
9940681896Sopenharmony_ci
10040681896Sopenharmony_ci        with self.assertRaises(VendorExpandError):
10140681896Sopenharmony_ci            TestScript().reboot_now()
10240681896Sopenharmony_ci
10340681896Sopenharmony_ci        with self.assertRaises(VendorExpandError):
10440681896Sopenharmony_ci            TestScript().updater_partitions()
10540681896Sopenharmony_ci
10640681896Sopenharmony_ci    def test_get_progress_value(self):
10740681896Sopenharmony_ci        """
10840681896Sopenharmony_ci        script, SuperClass commands.
10940681896Sopenharmony_ci        """
11040681896Sopenharmony_ci        file_path = "./vendor.img"
11140681896Sopenharmony_ci        create_file(file_path, get_target_vendor_data())
11240681896Sopenharmony_ci        with open(file_path) as wo_f:
11340681896Sopenharmony_ci            file_obj = wo_f
11440681896Sopenharmony_ci        OPTIONS_MANAGER.full_img_list = []
11540681896Sopenharmony_ci        OPTIONS_MANAGER.incremental_img_list = ['vendor', 'updater']
11640681896Sopenharmony_ci        OPTIONS_MANAGER.incremental_image_file_obj_dict['vendor'] = file_obj
11740681896Sopenharmony_ci        progress_value_dict = get_progress_value(distributable_value=60)
11840681896Sopenharmony_ci        check_re = len(progress_value_dict) != 0
11940681896Sopenharmony_ci        self.assertEqual(check_re, True)
12040681896Sopenharmony_ci        file_obj.close()
12140681896Sopenharmony_ci        if os.path.exists(file_path):
12240681896Sopenharmony_ci            os.remove(file_path)
12340681896Sopenharmony_ci
12440681896Sopenharmony_ci
12540681896Sopenharmony_ciclass TestScript(Script):
12640681896Sopenharmony_ci    def __init__(self):
12740681896Sopenharmony_ci        super(TestScript, self).__init__()
128