Example #1
0
 def test_set_default_config_file_acount_config_does_not_exist(
         self, mock_exists, mock_config_path):
     paths = mock.Mock()
     paths.default_new_account_config.return_value = 'account-config'
     mock_config_path.return_value = paths
     mock_exists.return_value = False
     Config.set_default_config_file('account-name')
Example #2
0
 def __default(self):
     Config.set_default_config_file(
         account_name=self.command_args['--name']
     )
     log.info(
         'Account %s has been set as default configuration',
         self.command_args['--name']
     )
Example #3
0
 def test_set_default_config_file_exists_as_file(self, mock_islink,
                                                 mock_exists,
                                                 mock_config_path):
     paths = mock.Mock()
     paths.default_new_account_config.return_value = 'account-config'
     paths.default_config.return_value = 'default-config'
     mock_config_path.return_value = paths
     mock_exists.return_value = True
     mock_islink.return_value = False
     Config.set_default_config_file('account-name')
Example #4
0
 def test_get_config_file(self, mock_isfile):
     mock_isfile.return_value = True
     with patch.dict('os.environ', {'HOME': 'foo'}):
         assert Config.get_config_file(
             account_name='bob',
             platform='lin') == 'foo/.config/azurectl/bob.config'
     with patch.dict('os.environ', {'HOME': 'foo'}):
         assert Config.get_config_file(filename='bob',
                                       platform='lin') == 'bob'
     with patch.dict('os.environ', {'HOME': 'foo'}):
         assert Config.get_config_file(
             platform='lin') == 'foo/.config/azurectl/config'
Example #5
0
 def test_set_default_config_file(self, mock_islink, mock_exists,
                                  mock_symlink, mock_remove,
                                  mock_config_path):
     paths = mock.Mock()
     paths.default_new_account_config.return_value = 'account-config'
     paths.default_config.return_value = None
     paths.default_new_config.return_value = 'default-config'
     mock_config_path.return_value = paths
     mock_exists.return_value = True
     mock_islink.return_value = True
     Config.set_default_config_file('account-name')
     mock_config_path.assert_called_once_with('account-name', None)
     mock_remove.assert_called_once_with('default-config')
     mock_symlink.assert_called_once_with('account-config',
                                          'default-config')
 def test_subscription_id_missing(self, base64_decode,
                                  mock_dump_certificate, mock_dump_pkey,
                                  mock_pkcs12):
     account_invalid = AzureAccount(
         Config(region_name='East US 2',
                filename='../data/config.missing_publishsettings_id'))
     account_invalid.get_management_service()
 def test_config_without_publishsettings(self):
     account = AzureAccount(
         Config(region_name='East US 2',
                filename='../data/config.no_publishsettings'))
     assert account.get_management_url() == 'test.url'
     assert account.certificate_filename() == '../data/pemfile'
     assert account.subscription_id() == 'id1234'
Example #8
0
 def setup(self):
     account = AzureAccount(
         Config(region_name='East US 2', filename='../data/config'))
     self.service = mock.Mock()
     account.get_management_service = mock.Mock(return_value=self.service)
     account.get_blob_service_host_base = mock.Mock(return_value='test.url')
     account.storage_key = mock.Mock()
     # now that that's done, instantiate a DataDisk with the account
     self.data_disk = DataDisk(account)
     # asynchronous API operations return a request object
     self.my_request = mock.Mock(request_id=Defaults.unify_id(42))
     # variables used in multiple tests
     self.cloud_service_name = 'mockcloudservice'
     self.instance_name = 'mockcloudserviceinstance1'
     self.lun = 0
     self.host_caching = 'ReadWrite'
     self.disk_filename = 'mockcloudserviceinstance1-data-disk-0.vhd'
     self.disk_name = 'mockcloudserviceinstance1-data-disk-0'
     self.disk_url = ('https://' + account.storage_name() + '.blob.' +
                      account.get_blob_service_host_base() + '/' +
                      account.storage_container() + '/' +
                      self.disk_filename)
     self.disk_label = 'Mock data disk'
     self.disk_size = 42
     self.timestamp = datetime.utcnow()
     self.time_string = datetime.isoformat(self.timestamp).replace(':', '_')
     self.account = account
Example #9
0
 def setup(self, mock_temp):
     tempfile = mock.Mock()
     tempfile.name = 'tempfile'
     mock_temp.return_value = tempfile
     self.account = AzureAccount(
         Config(region_name='East US 2', filename='../data/config'))
     azurectl.account.service.load_pkcs12 = mock.Mock()
Example #10
0
 def load_config(self):
     self.config = Config(self.global_args['--account'],
                          self.global_args['--region'],
                          self.global_args['--storage-account'],
                          self.global_args['--storage-container'],
                          self.global_args['--config'])
     self.config_file = self.config.config_file
Example #11
0
 def setup(self):
     # construct an account
     account = AzureAccount(
         Config(region_name='East US 2', filename='../data/config'))
     self.service = mock.Mock()
     account.get_management_service = mock.Mock(return_value=self.service)
     account.get_blob_service_host_base = mock.Mock(return_value='test.url')
     account.storage_key = mock.Mock()
     # now that that's done, instantiate an Endpoint with the account
     self.endpoint = Endpoint(account)
     # asynchronous API operations return a request object
     self.my_request = mock.Mock(request_id=Defaults.unify_id(42))
     # variables used in multiple tests
     self.cloud_service_name = 'mockcloudservice'
     self.instance_name = 'mockcloudserviceinstance1'
     self.endpoint_name = 'HTTPS'
     self.port = '443'
     self.idle_timeout = 10
     self.protocol = 'tcp'
     self.udp_endpoint_name = 'SNMP'
     self.udp_port = '131'
     self.instance_port = '10000'
     self.udp_protocol = 'udp'
     # identify the instance for the Endpoint to work on
     self.endpoint.set_instance(self.cloud_service_name, self.instance_name)
     # mock out the get_role function of service
     self.service.get_role = mock.Mock(return_value=self.mock_role())
Example #12
0
 def __check_account_existing_in_default_config(self):
     default_config = None
     try:
         default_config = Config()
     except Exception:
         # ignore exception thrown if no config file exists
         pass
     if default_config:
         account_section_name = 'account:' + self.command_args['--name']
         if default_config.config.has_section(account_section_name):
             raise AzureAccountConfigurationError(
                 'Account %s already configured in file %s' % (
                     self.command_args['--name'],
                     Config.get_config_file()
                 )
             )
Example #13
0
 def test_get_config_file_list(self, mock_config_path):
     paths = mock.Mock()
     paths.default_config.return_value = 'a'
     paths.account_config.return_value = ['b', 'c']
     mock_config_path.return_value = paths
     assert Config.get_config_file_list() == ['a', 'b', 'c']
     paths.default_config.assert_called_once_with()
     paths.account_config.assert_called_once_with()
Example #14
0
 def remove(self):
     """
         remove account configuration file
     """
     default_config_file = Config.get_config_file()
     os.remove(self.filename)
     if default_config_file and os.path.islink(default_config_file):
         link_target = os.readlink(default_config_file)
         if not os.path.exists(link_target):
             # account configuration was also configured as default account
             # remove the now broken default config symlink too
             os.remove(default_config_file)
Example #15
0
    def setup(self):
        sys.argv = [
            sys.argv[0], '--config', '../data/config', 'compute', 'shell'
        ]

        account = AzureAccount(
            Config(region_name='East US 2', filename='../data/config'))
        account.get_management_service = mock.Mock()
        account.get_blob_service_host_base = mock.Mock(
            return_value='.blob.test.url')
        account.storage_key = mock.Mock()

        azurectl.commands.compute_shell.AzureAccount = mock.Mock(
            return_value=account)

        self.task = ComputeShellTask()
Example #16
0
    def __list(self):
        config_files = Config.get_config_file_list()
        default_config_file = config_files[0] or '<missing>'
        if os.path.islink(default_config_file):
            default_config_file = os.readlink(default_config_file)

        self.result.add(
            'default_config_file', default_config_file
        )

        for config_file in config_files:
            if config_file and not os.path.islink(config_file):
                setup = AccountSetup(config_file)
                account_info = setup.list()
                if account_info:
                    self.result.add(config_file, account_info)

        self.out.display()
Example #17
0
    def setup(self):
        account = AzureAccount(
            Config(region_name='East US 2', filename='../data/config'))
        self.service = mock.Mock()
        account.get_management_service = mock.Mock(return_value=self.service)
        account.get_blob_service_host_base = mock.Mock(
            return_value='.blob.test.url')
        account.storage_key = mock.Mock()

        MyStruct = namedtuple(
            'MyStruct',
            'name address label state in_use service_name deployment_name \
            location')

        self.list_ips = [
            MyStruct(name='some-name',
                     address='1.2.3.4',
                     label='',
                     state='Created',
                     in_use=True,
                     service_name='foo',
                     deployment_name='bar',
                     location='Region')
        ]

        self.result_list = [{
            'name': 'some-name',
            'address': '1.2.3.4',
            'state': 'Created',
            'in_use': True,
            'cloud_service_name': 'foo',
            'instance_name': 'bar',
            'region': 'Region'
        }]

        MyResult = namedtuple('MyResult', 'request_id')
        self.myrequest = MyResult(request_id=42)

        self.reserved_ip = ReservedIp(account)
 def setup(self):
     MyStruct = namedtuple(
         'MyStruct', 'name label os category description location \
          affinity_group media_link')
     self.list_os_images = [
         MyStruct(name='some-name',
                  label='bob',
                  os='linux',
                  category='cloud',
                  description='nice',
                  location='here',
                  affinity_group='ok',
                  media_link='url')
     ]
     account = AzureAccount(
         Config(region_name='East US 2', filename='../data/config'))
     self.service = mock.Mock()
     account.get_management_service = mock.Mock(return_value=self.service)
     account.get_blob_service_host_base = mock.Mock(return_value='test.url')
     account.storage_key = mock.Mock()
     self.account = account
     self.vm = VirtualMachine(account)
     self.system_config = self.vm.create_linux_configuration(
         'some-user', 'some-host')
Example #19
0
 def setup(self):
     self.config = Config(region_name='East US 2',
                          filename='../data/config')
    def setup(self):
        MyResult = namedtuple(
            'MyResult',
            'request_id'
        )
        self.myrequest = MyResult(request_id=42)
        self.fake_image_name = 'some-name'

        MyStruct = namedtuple(
            'MyStruct',
            'affinity_group category description eula icon_uri image_family \
             is_premium label language location logical_size_in_gb media_link \
             name os os_state pricing_detail_link privacy_uri \
             published_date publisher_name recommended_vm_size show_in_gui \
             small_icon_uri'
        )
        self.list_os_images = [MyStruct(
            affinity_group='group',
            category='cloud',
            description='nice',
            eula='eula',
            icon_uri='url',
            image_family='disks',
            is_premium=False,
            label='bob',
            language='English',
            location='West US',
            logical_size_in_gb=30,
            media_link='url',
            name=self.fake_image_name,
            os='linux',
            os_state='brilliant',
            pricing_detail_link='url',
            privacy_uri='url',
            published_date='date',
            publisher_name='suse',
            recommended_vm_size=10,
            show_in_gui=True,
            small_icon_uri='url'
        )]

        self.os_image = mock.Mock()
        self.os_image.eula = 'eula'
        self.os_image.description = 'description'
        self.os_image.language = 'en_US'
        self.os_image.image_family = 'family'
        self.os_image.icon_uri = 'OpenSuse12_100.png'
        self.os_image.label = 'label'
        self.os_image.small_icon_uri = 'OpenSuse12_45.png'
        self.os_image.published_date = '2016-01-20'
        self.os_image.privacy_uri = 'http://privacy.uri'

        self.os_image_updated = mock.Mock()
        self.os_image_updated.eula = 'eula'
        self.os_image_updated.description = 'description'
        self.os_image_updated.language = 'en_US'
        self.os_image_updated.image_family = 'family'
        self.os_image_updated.icon_uri = 'OpenSuse12_100.png'
        self.os_image_updated.label = 'label'
        self.os_image_updated.small_icon_uri = 'OpenSuse12_45.png'
        self.os_image_updated.published_date = '2016-01-20T00:00:00Z'
        self.os_image_updated.privacy_uri = 'http://privacy.uri/'

        account = AzureAccount(
            Config(
                region_name='East US 2', filename='../data/config'
            )
        )
        self.service = mock.Mock()
        account.get_management_service = mock.Mock(return_value=self.service)
        account.get_blob_service_host_base = mock.Mock(
            return_value='test.url'
        )
        account.storage_key = mock.Mock()
        self.image = Image(account)
Example #21
0
 def test_region_section_not_found(self):
     Config(filename='../data/config.invalid_region'
            ).get_storage_account_name()
Example #22
0
 def test_no_default_section_in_config(self):
     Config(region_name='East US 2', filename='../data/config.no_default')
Example #23
0
class TestConfig:
    def setup(self):
        self.config = Config(region_name='East US 2',
                             filename='../data/config')

    def test_get_account_name(self):
        assert self.config.get_account_name() == 'bob'

    def test_get_region_name(self):
        assert self.config.get_region_name() == 'East US 2'

    def test_get_storage_account_name(self):
        assert self.config.get_storage_account_name() == 'bob'

    def test_get_storage_container_name(self):
        assert self.config.get_storage_container_name() == 'foo'

    @patch('os.path.isfile')
    def test_get_config_file(self, mock_isfile):
        mock_isfile.return_value = True
        with patch.dict('os.environ', {'HOME': 'foo'}):
            assert Config.get_config_file(
                account_name='bob',
                platform='lin') == 'foo/.config/azurectl/bob.config'
        with patch.dict('os.environ', {'HOME': 'foo'}):
            assert Config.get_config_file(filename='bob',
                                          platform='lin') == 'bob'
        with patch.dict('os.environ', {'HOME': 'foo'}):
            assert Config.get_config_file(
                platform='lin') == 'foo/.config/azurectl/config'

    @patch('azurectl.config.parser.ConfigFilePath')
    def test_get_config_file_list(self, mock_config_path):
        paths = mock.Mock()
        paths.default_config.return_value = 'a'
        paths.account_config.return_value = ['b', 'c']
        mock_config_path.return_value = paths
        assert Config.get_config_file_list() == ['a', 'b', 'c']
        paths.default_config.assert_called_once_with()
        paths.account_config.assert_called_once_with()

    @patch('azurectl.config.parser.ConfigFilePath')
    @patch('os.remove')
    @patch('os.symlink')
    @patch('os.path.exists')
    @patch('os.path.islink')
    def test_set_default_config_file(self, mock_islink, mock_exists,
                                     mock_symlink, mock_remove,
                                     mock_config_path):
        paths = mock.Mock()
        paths.default_new_account_config.return_value = 'account-config'
        paths.default_config.return_value = None
        paths.default_new_config.return_value = 'default-config'
        mock_config_path.return_value = paths
        mock_exists.return_value = True
        mock_islink.return_value = True
        Config.set_default_config_file('account-name')
        mock_config_path.assert_called_once_with('account-name', None)
        mock_remove.assert_called_once_with('default-config')
        mock_symlink.assert_called_once_with('account-config',
                                             'default-config')

    @patch('azurectl.config.parser.ConfigFilePath')
    @patch('os.path.exists')
    @raises(AzureConfigAccountFileNotFound)
    def test_set_default_config_file_acount_config_does_not_exist(
            self, mock_exists, mock_config_path):
        paths = mock.Mock()
        paths.default_new_account_config.return_value = 'account-config'
        mock_config_path.return_value = paths
        mock_exists.return_value = False
        Config.set_default_config_file('account-name')

    @patch('azurectl.config.parser.ConfigFilePath')
    @patch('os.path.exists')
    @patch('os.path.islink')
    @raises(AzureConfigDefaultLinkError)
    def test_set_default_config_file_exists_as_file(self, mock_islink,
                                                    mock_exists,
                                                    mock_config_path):
        paths = mock.Mock()
        paths.default_new_account_config.return_value = 'account-config'
        paths.default_config.return_value = 'default-config'
        mock_config_path.return_value = paths
        mock_exists.return_value = True
        mock_islink.return_value = False
        Config.set_default_config_file('account-name')

    @raises(AzureConfigVariableNotFound)
    def test_get_subscription_id_missing(self):
        assert self.config.get_subscription_id()

    @raises(AzureConfigVariableNotFound)
    def test_get_publishsettings_file_name_missing(self):
        config = Config(region_name='East US 2',
                        filename='../data/config.missing_region_data')
        config.get_storage_account_name()

    def test_get_publishsettings_file_name(self):
        assert self.config.get_publishsettings_file_name() == \
            '../data/publishsettings'

    @raises(AzureConfigSectionNotFound)
    def test_account_section_not_found(self):
        Config(filename='../data/config.invalid_account')

    @raises(AzureConfigVariableNotFound)
    def test_region_section_not_found(self):
        Config(filename='../data/config.invalid_region'
               ).get_storage_account_name()

    @raises(AzureConfigVariableNotFound)
    def test_region_not_present(self):
        Config(filename='../data/config.no_region').get_storage_account_name()

    def test_get_region_name_with_region_arg_but_no_config(self):
        expected = 'Foo Test Region'
        result = Config(region_name=expected,
                        filename='../data/config.no_region').get_region_name()
        assert result == expected

    @raises(AzureConfigAccountNotFound)
    def test_account_not_present(self):
        Config(filename='../data/config.no_account')

    @raises(AzureConfigParseError)
    def test_parse_error(self):
        Config(filename='../data/config_parse_error')

    @raises(AzureAccountLoadFailed)
    @patch('os.path.isfile')
    def test_config_account_name_not_found(self, mock_isfile):
        mock_isfile.return_value = False
        Config(account_name='account-name')

    @raises(AzureAccountLoadFailed)
    @patch('os.path.isfile')
    def test_config_file_not_found(self, mock_isfile):
        mock_isfile.return_value = False
        Config(filename="does-not-exist")

    @raises(AzureAccountLoadFailed)
    @patch('os.path.isfile')
    def test_default_config_file_not_found(self, mock_isfile):
        mock_isfile.return_value = False
        Config()

    @raises(AzureAccountDefaultSectionNotFound)
    def test_no_default_section_in_config(self):
        Config(region_name='East US 2', filename='../data/config.no_default')
Example #24
0
 def test_config_file_not_found(self, mock_isfile):
     mock_isfile.return_value = False
     Config(filename="does-not-exist")
Example #25
0
 def test_default_config_file_not_found(self, mock_isfile):
     mock_isfile.return_value = False
     Config()
Example #26
0
 def test_config_account_name_not_found(self, mock_isfile):
     mock_isfile.return_value = False
     Config(account_name='account-name')
Example #27
0
 def test_parse_error(self):
     Config(filename='../data/config_parse_error')
Example #28
0
 def test_account_not_present(self):
     Config(filename='../data/config.no_account')
Example #29
0
 def test_region_not_present(self):
     Config(filename='../data/config.no_region').get_storage_account_name()
Example #30
0
 def test_get_region_name_with_region_arg_but_no_config(self):
     expected = 'Foo Test Region'
     result = Config(region_name=expected,
                     filename='../data/config.no_region').get_region_name()
     assert result == expected