|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | +import os |
| 6 | +import tempfile |
| 7 | +import unittest |
| 8 | +from unittest.mock import MagicMock |
| 9 | + |
| 10 | +from azure.cli.testsdk import ScenarioTest, LocalContextScenarioTest |
| 11 | +from knack.util import CLIError |
| 12 | + |
| 13 | + |
| 14 | +class ConfigTest(ScenarioTest): |
| 15 | + |
| 16 | + def test_config(self): |
| 17 | + |
| 18 | + # [test_section1] |
| 19 | + # test_option1 = test_value1 |
| 20 | + # |
| 21 | + # [test_section2] |
| 22 | + # test_option21 = test_value21 |
| 23 | + # test_option22 = test_value22 |
| 24 | + |
| 25 | + # C:\Users\{username}\AppData\Local\Temp |
| 26 | + tempdir = tempfile.gettempdir() |
| 27 | + original_path = os.getcwd() |
| 28 | + os.chdir(tempdir) |
| 29 | + print("Using temp dir: {}".format(tempdir)) |
| 30 | + |
| 31 | + global_test_args = {"source": os.path.expanduser(os.path.join('~', '.azure', 'config')), "flag": ""} |
| 32 | + local_test_args = {"source": os.path.join(tempdir, '.azure', 'config'), "flag": " --local"} |
| 33 | + |
| 34 | + for args in (global_test_args, local_test_args): |
| 35 | + test_option1_expected = {'name': 'test_option1', 'source': args["source"], 'value': 'test_value1'} |
| 36 | + test_option21_expected = {'name': 'test_option21', 'source': args["source"], 'value': 'test_value21'} |
| 37 | + test_option22_expected = {'name': 'test_option22', 'source': args["source"], 'value': 'test_value22'} |
| 38 | + |
| 39 | + test_section1_expected = [test_option1_expected] |
| 40 | + test_section2_expected = [test_option21_expected, test_option22_expected] |
| 41 | + |
| 42 | + # 1. set |
| 43 | + # Test setting one option |
| 44 | + self.cmd('config set test_section1.test_option1=test_value1' + args['flag']) |
| 45 | + # Test setting multiple options |
| 46 | + self.cmd('config set test_section2.test_option21=test_value21 test_section2.test_option22=test_value22' + args['flag']) |
| 47 | + |
| 48 | + # 2. get |
| 49 | + # 2.1 Test get all sections |
| 50 | + output = self.cmd('config get' + args['flag']).get_output_in_json() |
| 51 | + self.assertListEqual(output['test_section1'], test_section1_expected) |
| 52 | + self.assertListEqual(output['test_section2'], test_section2_expected) |
| 53 | + |
| 54 | + # 2.2 Test get one section |
| 55 | + output = self.cmd('config get test_section1' + args['flag']).get_output_in_json() |
| 56 | + self.assertListEqual(output, test_section1_expected) |
| 57 | + output = self.cmd('config get test_section2' + args['flag']).get_output_in_json() |
| 58 | + self.assertListEqual(output, test_section2_expected) |
| 59 | + |
| 60 | + # 2.3 Test get one item |
| 61 | + output = self.cmd('config get test_section1.test_option1' + args['flag']).get_output_in_json() |
| 62 | + self.assertDictEqual(output, test_option1_expected) |
| 63 | + output = self.cmd('config get test_section2.test_option21' + args['flag']).get_output_in_json() |
| 64 | + self.assertDictEqual(output, test_option21_expected) |
| 65 | + output = self.cmd('config get test_section2.test_option22' + args['flag']).get_output_in_json() |
| 66 | + self.assertDictEqual(output, test_option22_expected) |
| 67 | + |
| 68 | + with self.assertRaises(CLIError): |
| 69 | + self.cmd('config get test_section1.test_option22' + args['flag']) |
| 70 | + |
| 71 | + # 3. unset |
| 72 | + # Test unsetting one option |
| 73 | + self.cmd('config unset test_section1.test_option1' + args['flag']) |
| 74 | + # Test unsetting multiple options |
| 75 | + self.cmd('config unset test_section2.test_option21 test_section2.test_option22' + args['flag']) |
| 76 | + |
| 77 | + os.chdir(original_path) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + unittest.main() |
0 commit comments