|
13 | 13 | # under the License. |
14 | 14 | # |
15 | 15 |
|
| 16 | +import copy |
16 | 17 | import mock |
17 | 18 | import os |
18 | 19 |
|
|
36 | 37 | DEFAULT_SERVICE_URL = "http://127.0.0.1:8771/v3.0/" |
37 | 38 | DEFAULT_AUTH_PLUGIN = "v2password" |
38 | 39 |
|
39 | | - |
40 | 40 | DEFAULT_COMPUTE_API_VERSION = "2" |
41 | 41 | DEFAULT_IDENTITY_API_VERSION = "2" |
42 | 42 | DEFAULT_IMAGE_API_VERSION = "2" |
|
49 | 49 | LIB_VOLUME_API_VERSION = "1" |
50 | 50 | LIB_NETWORK_API_VERSION = "2" |
51 | 51 |
|
| 52 | +CLOUD_1 = { |
| 53 | + 'clouds': { |
| 54 | + 'scc': { |
| 55 | + 'auth': { |
| 56 | + 'auth_url': DEFAULT_AUTH_URL, |
| 57 | + 'project_name': DEFAULT_PROJECT_NAME, |
| 58 | + 'username': 'zaphod', |
| 59 | + }, |
| 60 | + 'region_name': 'occ-cloud', |
| 61 | + 'donut': 'glazed', |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +CLOUD_2 = { |
| 67 | + 'clouds': { |
| 68 | + 'megacloud': { |
| 69 | + 'cloud': 'megadodo', |
| 70 | + 'auth': { |
| 71 | + 'project_name': 'heart-o-gold', |
| 72 | + 'username': 'zaphod', |
| 73 | + }, |
| 74 | + 'region_name': 'occ-cloud', |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +PUBLIC_1 = { |
| 80 | + 'public-clouds': { |
| 81 | + 'megadodo': { |
| 82 | + 'auth': { |
| 83 | + 'auth_url': DEFAULT_AUTH_URL, |
| 84 | + 'project_name': DEFAULT_PROJECT_NAME, |
| 85 | + }, |
| 86 | + 'region_name': 'occ-public', |
| 87 | + 'donut': 'cake', |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
52 | 92 |
|
53 | 93 | def make_shell(): |
54 | 94 | """Create a new command shell and mock out some bits.""" |
@@ -516,3 +556,217 @@ def test_empty_env(self): |
516 | 556 | "network_api_version": LIB_NETWORK_API_VERSION |
517 | 557 | } |
518 | 558 | self._assert_cli(flag, kwargs) |
| 559 | + |
| 560 | + @mock.patch("os_client_config.config.OpenStackConfig._load_config_file") |
| 561 | + def test_shell_args_cloud_no_vendor(self, config_mock): |
| 562 | + config_mock.return_value = copy.deepcopy(CLOUD_1) |
| 563 | + _shell = make_shell() |
| 564 | + |
| 565 | + fake_execute( |
| 566 | + _shell, |
| 567 | + "--os-cloud scc list user", |
| 568 | + ) |
| 569 | + self.assertEqual( |
| 570 | + 'scc', |
| 571 | + _shell.cloud.name, |
| 572 | + ) |
| 573 | + |
| 574 | + # These come from clouds.yaml |
| 575 | + self.assertEqual( |
| 576 | + DEFAULT_AUTH_URL, |
| 577 | + _shell.cloud.config['auth']['auth_url'], |
| 578 | + ) |
| 579 | + self.assertEqual( |
| 580 | + DEFAULT_PROJECT_NAME, |
| 581 | + _shell.cloud.config['auth']['project_name'], |
| 582 | + ) |
| 583 | + self.assertEqual( |
| 584 | + 'zaphod', |
| 585 | + _shell.cloud.config['auth']['username'], |
| 586 | + ) |
| 587 | + self.assertEqual( |
| 588 | + 'occ-cloud', |
| 589 | + _shell.cloud.config['region_name'], |
| 590 | + ) |
| 591 | + self.assertEqual( |
| 592 | + 'glazed', |
| 593 | + _shell.cloud.config['donut'], |
| 594 | + ) |
| 595 | + |
| 596 | + @mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file") |
| 597 | + @mock.patch("os_client_config.config.OpenStackConfig._load_config_file") |
| 598 | + def test_shell_args_cloud_public(self, config_mock, public_mock): |
| 599 | + config_mock.return_value = copy.deepcopy(CLOUD_2) |
| 600 | + public_mock.return_value = copy.deepcopy(PUBLIC_1) |
| 601 | + _shell = make_shell() |
| 602 | + |
| 603 | + fake_execute( |
| 604 | + _shell, |
| 605 | + "--os-cloud megacloud list user", |
| 606 | + ) |
| 607 | + self.assertEqual( |
| 608 | + 'megacloud', |
| 609 | + _shell.cloud.name, |
| 610 | + ) |
| 611 | + |
| 612 | + # These come from clouds-public.yaml |
| 613 | + self.assertEqual( |
| 614 | + DEFAULT_AUTH_URL, |
| 615 | + _shell.cloud.config['auth']['auth_url'], |
| 616 | + ) |
| 617 | + self.assertEqual( |
| 618 | + 'cake', |
| 619 | + _shell.cloud.config['donut'], |
| 620 | + ) |
| 621 | + |
| 622 | + # These come from clouds.yaml |
| 623 | + self.assertEqual( |
| 624 | + 'heart-o-gold', |
| 625 | + _shell.cloud.config['auth']['project_name'], |
| 626 | + ) |
| 627 | + self.assertEqual( |
| 628 | + 'zaphod', |
| 629 | + _shell.cloud.config['auth']['username'], |
| 630 | + ) |
| 631 | + self.assertEqual( |
| 632 | + 'occ-cloud', |
| 633 | + _shell.cloud.config['region_name'], |
| 634 | + ) |
| 635 | + |
| 636 | + @mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file") |
| 637 | + @mock.patch("os_client_config.config.OpenStackConfig._load_config_file") |
| 638 | + def test_shell_args_precedence(self, config_mock, vendor_mock): |
| 639 | + config_mock.return_value = copy.deepcopy(CLOUD_2) |
| 640 | + vendor_mock.return_value = copy.deepcopy(PUBLIC_1) |
| 641 | + _shell = make_shell() |
| 642 | + |
| 643 | + # Test command option overriding config file value |
| 644 | + fake_execute( |
| 645 | + _shell, |
| 646 | + "--os-cloud megacloud --os-region-name krikkit list user", |
| 647 | + ) |
| 648 | + self.assertEqual( |
| 649 | + 'megacloud', |
| 650 | + _shell.cloud.name, |
| 651 | + ) |
| 652 | + |
| 653 | + # These come from clouds-public.yaml |
| 654 | + self.assertEqual( |
| 655 | + DEFAULT_AUTH_URL, |
| 656 | + _shell.cloud.config['auth']['auth_url'], |
| 657 | + ) |
| 658 | + self.assertEqual( |
| 659 | + 'cake', |
| 660 | + _shell.cloud.config['donut'], |
| 661 | + ) |
| 662 | + |
| 663 | + # These come from clouds.yaml |
| 664 | + self.assertEqual( |
| 665 | + 'heart-o-gold', |
| 666 | + _shell.cloud.config['auth']['project_name'], |
| 667 | + ) |
| 668 | + self.assertEqual( |
| 669 | + 'zaphod', |
| 670 | + _shell.cloud.config['auth']['username'], |
| 671 | + ) |
| 672 | + self.assertEqual( |
| 673 | + 'krikkit', |
| 674 | + _shell.cloud.config['region_name'], |
| 675 | + ) |
| 676 | + |
| 677 | + |
| 678 | +class TestShellCliEnv(TestShell): |
| 679 | + def setUp(self): |
| 680 | + super(TestShellCliEnv, self).setUp() |
| 681 | + env = { |
| 682 | + 'OS_REGION_NAME': 'occ-env', |
| 683 | + } |
| 684 | + self.orig_env, os.environ = os.environ, env.copy() |
| 685 | + |
| 686 | + def tearDown(self): |
| 687 | + super(TestShellCliEnv, self).tearDown() |
| 688 | + os.environ = self.orig_env |
| 689 | + |
| 690 | + @mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file") |
| 691 | + @mock.patch("os_client_config.config.OpenStackConfig._load_config_file") |
| 692 | + def test_shell_args_precedence_1(self, config_mock, vendor_mock): |
| 693 | + config_mock.return_value = copy.deepcopy(CLOUD_2) |
| 694 | + vendor_mock.return_value = copy.deepcopy(PUBLIC_1) |
| 695 | + _shell = make_shell() |
| 696 | + |
| 697 | + # Test env var |
| 698 | + fake_execute( |
| 699 | + _shell, |
| 700 | + "--os-cloud megacloud list user", |
| 701 | + ) |
| 702 | + self.assertEqual( |
| 703 | + 'megacloud', |
| 704 | + _shell.cloud.name, |
| 705 | + ) |
| 706 | + |
| 707 | + # These come from clouds-public.yaml |
| 708 | + self.assertEqual( |
| 709 | + DEFAULT_AUTH_URL, |
| 710 | + _shell.cloud.config['auth']['auth_url'], |
| 711 | + ) |
| 712 | + self.assertEqual( |
| 713 | + 'cake', |
| 714 | + _shell.cloud.config['donut'], |
| 715 | + ) |
| 716 | + |
| 717 | + # These come from clouds.yaml |
| 718 | + self.assertEqual( |
| 719 | + 'heart-o-gold', |
| 720 | + _shell.cloud.config['auth']['project_name'], |
| 721 | + ) |
| 722 | + self.assertEqual( |
| 723 | + 'zaphod', |
| 724 | + _shell.cloud.config['auth']['username'], |
| 725 | + ) |
| 726 | + self.assertEqual( |
| 727 | + 'occ-env', |
| 728 | + _shell.cloud.config['region_name'], |
| 729 | + ) |
| 730 | + |
| 731 | + @mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file") |
| 732 | + @mock.patch("os_client_config.config.OpenStackConfig._load_config_file") |
| 733 | + def test_shell_args_precedence_2(self, config_mock, vendor_mock): |
| 734 | + config_mock.return_value = copy.deepcopy(CLOUD_2) |
| 735 | + vendor_mock.return_value = copy.deepcopy(PUBLIC_1) |
| 736 | + _shell = make_shell() |
| 737 | + |
| 738 | + # Test command option overriding config file value |
| 739 | + fake_execute( |
| 740 | + _shell, |
| 741 | + "--os-cloud megacloud --os-region-name krikkit list user", |
| 742 | + ) |
| 743 | + self.assertEqual( |
| 744 | + 'megacloud', |
| 745 | + _shell.cloud.name, |
| 746 | + ) |
| 747 | + |
| 748 | + # These come from clouds-public.yaml |
| 749 | + self.assertEqual( |
| 750 | + DEFAULT_AUTH_URL, |
| 751 | + _shell.cloud.config['auth']['auth_url'], |
| 752 | + ) |
| 753 | + self.assertEqual( |
| 754 | + 'cake', |
| 755 | + _shell.cloud.config['donut'], |
| 756 | + ) |
| 757 | + |
| 758 | + # These come from clouds.yaml |
| 759 | + self.assertEqual( |
| 760 | + 'heart-o-gold', |
| 761 | + _shell.cloud.config['auth']['project_name'], |
| 762 | + ) |
| 763 | + self.assertEqual( |
| 764 | + 'zaphod', |
| 765 | + _shell.cloud.config['auth']['username'], |
| 766 | + ) |
| 767 | + |
| 768 | + # These come from the command line |
| 769 | + self.assertEqual( |
| 770 | + 'krikkit', |
| 771 | + _shell.cloud.config['region_name'], |
| 772 | + ) |
0 commit comments