Exemplo n.º 1
0
    def test_sr_primary_enabled_basic(self):
        '''
        Test to check sr_primary_enabled when hana is already set as primary
        node with basic setup
        '''
        name = 'SITE1'

        ret = {
            'name': name,
            'changes': {
                'primary': name
            },
            'result': True,
            'comment': 'HANA node set as {}'.format('PRIMARY')
        }

        mock_installed = MagicMock(return_value=True)
        mock_running = MagicMock(return_value=True)
        state = MagicMock()
        state_primary = MagicMock()
        state_primary.name = 'PRIMARY'
        mock_state = MagicMock(side_effect=[state, state_primary])
        mock_enable = MagicMock()
        with patch.dict(
                hanamod.__salt__, {
                    'hana.is_installed': mock_installed,
                    'hana.is_running': mock_running,
                    'hana.get_sr_state': mock_state,
                    'hana.sr_enable_primary': mock_enable
                }):
            assert hanamod.sr_primary_enabled(name, 'pdr', '00', 'pass') == ret
            mock_enable.assert_called_once_with(name=name,
                                                sid='pdr',
                                                inst='00',
                                                password='******')
Exemplo n.º 2
0
 def setUp(self):
     self.mock_res_type = MagicMock()
     self.mock_policy_ids = MagicMock()
     self.mock_prof_mgr = MagicMock(QueryProfile=MagicMock(
         return_value=self.mock_policy_ids))
     # Policies
     self.mock_policies = []
     for i in range(4):
         mock_obj = MagicMock(resourceType=MagicMock(
             resourceType=pbm.profile.ResourceTypeEnum.STORAGE))
         mock_obj.name = "fake_policy{}".format(i)
         self.mock_policies.append(mock_obj)
     patches = (
         (
             "salt.utils.pbm.pbm.profile.ResourceType",
             MagicMock(return_value=self.mock_res_type),
         ),
         (
             "salt.utils.pbm.get_policies_by_id",
             MagicMock(return_value=self.mock_policies),
         ),
     )
     for mod, mock in patches:
         patcher = patch(mod, mock)
         patcher.start()
         self.addCleanup(patcher.stop)
Exemplo n.º 3
0
def test_list_nodes():
    """Test the list_nodes function by using a mock"""
    with patch("salt.cloud.clouds.hetzner._connect_client",
               return_value=MagicMock()) as connect:
        with pytest.raises(SaltCloudSystemExit):
            hetzner.list_nodes("action")

        mock = MagicMock()
        mock.id = 123456
        mock.name = "abc"
        mock.public_net.ipv4.ip = "127.0.0.1/32"
        mock.public_net.ipv6.ip = "::1/64"

        private_net_mock = MagicMock()
        private_net_mock.ip = "10.0.0.1/16"
        mock.private_net = []
        mock.private_net.append(private_net_mock)

        mock.labels = "abc"
        connect.return_value.servers.get_all.return_value = [mock]

        nodes = hetzner.list_nodes()
        assert nodes[mock.name]["id"], mock.id
        # Labels should be filtered
        assert "labels" not in nodes[mock.name]

        assert nodes[mock.name]["public_ips"]["ipv4"] == "127.0.0.1/32"
        assert nodes[mock.name]["public_ips"]["ipv6"] == "::1/64"

        assert nodes[mock.name]["private_ips"][0]["ip"] == "10.0.0.1/16"
Exemplo n.º 4
0
def test_create_address_should_fire_creating_and_created_events_with_expected_args(
    conn, ):
    region = MagicMock()
    region.name = "antarctica"
    kwargs = {"name": "bob", "region": region, "address": "123 Easy Street"}
    expected_args = {
        "name": "bob",
        "region": {
            "name": "antarctica"
        },
        "address": "123 Easy Street",
    }
    expected_creating_call = call(
        "event",
        "create address",
        "salt/cloud/address/creating",
        args=expected_args,
        sock_dir=gce.__opts__["sock_dir"],
        transport=gce.__opts__["transport"],
    )
    expected_created_call = call(
        "event",
        "created address",
        "salt/cloud/address/created",
        args=expected_args,
        sock_dir=gce.__opts__["sock_dir"],
        transport=gce.__opts__["transport"],
    )

    gce.create_address(kwargs, "function")

    gce.__utils__["cloud.fire_event"].assert_has_calls(
        [expected_creating_call, expected_created_call])
Exemplo n.º 5
0
 def test_change_state_up_state(self):
     """
     Tests _change_state when connected
     and new state is up
     """
     iface_mock = MagicMock()
     iface_mock.name = "test_interface"
     with patch("pyiface.getIfaces", return_value=[iface_mock]):
         with patch("salt.modules.nilrt_ip._change_dhcp_config",
                    return_value=True) as change_dhcp_config_mock:
             assert nilrt_ip._change_state("test_interface", "up")
             assert change_dhcp_config_mock.called_with("test_interface")
Exemplo n.º 6
0
    def test_sr_secondary_registered_basic(self):
        '''
        Test to check sr_secondary_registered when hana is already set as secondary
        node with basic setup
        '''
        name = 'SITE2'

        ret = {
            'name': name,
            'changes': {
                'secondary': name
            },
            'result': True,
            'comment': 'HANA node set as {}'.format('SECONDARY')
        }

        mock_installed = MagicMock(return_value=True)
        mock_running = MagicMock(return_value=True)
        state = MagicMock()
        state_secondary = MagicMock()
        state_secondary.name = 'SECONDARY'
        mock_state = MagicMock(side_effect=[state, state_secondary])
        mock_stop = MagicMock()
        mock_start = MagicMock()
        mock_register = MagicMock()
        with patch.dict(
                hanamod.__salt__, {
                    'hana.is_installed': mock_installed,
                    'hana.is_running': mock_running,
                    'hana.get_sr_state': mock_state,
                    'hana.stop': mock_stop,
                    'hana.start': mock_start,
                    'hana.sr_register_secondary': mock_register
                }):
            assert hanamod.sr_secondary_registered(name, 'hana01', '00',
                                                   'sync', 'logreplay', 'pdr',
                                                   '00', 'pass') == ret
            mock_stop.assert_called_once_with(sid='pdr',
                                              inst='00',
                                              password='******')
            mock_start.assert_called_once_with(sid='pdr',
                                               inst='00',
                                               password='******')
            mock_register.assert_called_once_with(name=name,
                                                  remote_host='hana01',
                                                  remote_instance='00',
                                                  replication_mode='sync',
                                                  operation_mode='logreplay',
                                                  sid='pdr',
                                                  inst='00',
                                                  password='******')
Exemplo n.º 7
0
    def test_image_list(self):
        '''
        test salt.modles.glance
        '''
        name = 'test'
        image = MagicMock()
        image.name = name
        attrs = {'images.list.return_value': [image], }
        mock_auth = MagicMock(**attrs)
        patch_auth = patch('salt.modules.glance._auth', return_value=mock_auth)

        with patch_auth:
            ret = glance.image_list(id='test_id', name=name)
            assert ret[0]['name'] == name
Exemplo n.º 8
0
    def test_image_list(self):
        """
        test salt.modles.glance
        """
        name = "test"
        image = MagicMock()
        image.name = name
        attrs = {
            "images.list.return_value": [image],
        }
        mock_auth = MagicMock(**attrs)
        patch_auth = patch("salt.modules.glance._auth", return_value=mock_auth)

        with patch_auth:
            ret = glance.image_list(id="test_id", name=name)
            assert ret[0]["name"] == name
Exemplo n.º 9
0
    def test_sr_clean_basic(self, mock_disabled):
        '''
        Test to check sr_clean when hana is already set as secondary
        node with basic setup
        '''
        name = 'SITE1'

        ret = {
            'name': name,
            'changes': {
                'disabled': name
            },
            'result': True,
            'comment': 'HANA node set as {}'.format('DISABLED')
        }

        mock_disabled.DISABLED.name = 'DISABLED'
        mock_installed = MagicMock(return_value=True)
        mock_running = MagicMock(return_value=True)
        state = MagicMock()
        state_disabled = MagicMock()
        state_disabled.name = 'DISABLED'
        mock_state = MagicMock(side_effect=[state, state_disabled])
        mock_stop = MagicMock()
        mock_clean = MagicMock()
        with patch.dict(
                hanamod.__salt__, {
                    'hana.is_installed': mock_installed,
                    'hana.is_running': mock_running,
                    'hana.get_sr_state': mock_state,
                    'hana.stop': mock_stop,
                    'hana.sr_cleanup': mock_clean
                }):
            assert hanamod.sr_clean(name, True, 'pdr', '00', 'pass') == ret
            mock_stop.assert_called_once_with(sid='pdr',
                                              inst='00',
                                              password='******')
            mock_clean.assert_called_once_with(force=True,
                                               sid='pdr',
                                               inst='00',
                                               password='******')
Exemplo n.º 10
0
def test_show_instance():
    """ Test the show_instance function by using a mock """
    with patch("salt.cloud.clouds.hetzner._connect_client",
               return_value=MagicMock()) as connect:
        with pytest.raises(SaltCloudSystemExit):
            hetzner.show_instance("myvm", "action")

        mock = MagicMock()
        mock.id = 123456
        mock.name = "abc"
        mock.public_net.ipv4.ip = "127.0.0.1/32"
        mock.public_net.ipv6.ip = "::1/64"
        mock.private_net = []
        mock.labels = "abc"
        connect.return_value.servers.get_all.return_value = [mock]

        nodes = hetzner.show_instance(mock.name)
        assert nodes["id"] == mock.id

        nodes = hetzner.show_instance("not-existing")
        assert nodes == {}
Exemplo n.º 11
0
    def test_sr_primary_enabled_error(self):
        '''
        Test to check sr_primary_enabled when hana is already set as primary
        node and some hana command fail
        '''
        name = 'SITE1'

        ret = {
            'name': name,
            'changes': {},
            'result': False,
            'comment': 'hana command error'
        }

        mock_installed = MagicMock(return_value=True)
        mock_running = MagicMock(return_value=False)
        state = MagicMock()
        state_primary = MagicMock()
        state_primary.name = 'PRIMARY'
        mock_state = MagicMock(side_effect=[state, state_primary])
        mock_start = MagicMock()
        mock_enable = MagicMock(
            side_effect=exceptions.CommandExecutionError('hana command error'))
        with patch.dict(
                hanamod.__salt__, {
                    'hana.is_installed': mock_installed,
                    'hana.is_running': mock_running,
                    'hana.get_sr_state': mock_state,
                    'hana.start': mock_start,
                    'hana.sr_enable_primary': mock_enable
                }):
            assert hanamod.sr_primary_enabled(name, 'pdr', '00', 'pass') == ret
            mock_start.assert_called_once_with(sid='pdr',
                                               inst='00',
                                               password='******')
            mock_enable.assert_called_once_with(name=name,
                                                sid='pdr',
                                                inst='00',
                                                password='******')
Exemplo n.º 12
0
    def test_sr_primary_enabled_complex(self):
        '''
        Test to check sr_primary_enabled when hana is already set as primary
        node with complex setup (backup and userkey created)
        '''
        name = 'SITE1'

        ret = {
            'name': name,
            'changes': {
                'primary': name,
                'userkey': 'key',
                'backup': 'file'
            },
            'result': True,
            'comment': 'HANA node set as {}'.format('PRIMARY')
        }

        userkey = [{
            'key': 'key'
        }, {
            'environment': 'env'
        }, {
            'user': '******'
        }, {
            'password': '******'
        }, {
            'database': 'database'
        }]

        backup = [{
            'user': '******'
        }, {
            'password': '******'
        }, {
            'database': 'database'
        }, {
            'file': 'file'
        }]

        mock_installed = MagicMock(return_value=True)
        mock_running = MagicMock(return_value=False)
        state = MagicMock()
        state_primary = MagicMock()
        state_primary.name = 'PRIMARY'
        mock_state = MagicMock(side_effect=[state, state_primary])
        mock_start = MagicMock()
        mock_enable = MagicMock()
        mock_userkey = MagicMock()
        mock_backup = MagicMock()
        with patch.dict(
                hanamod.__salt__, {
                    'hana.is_installed': mock_installed,
                    'hana.is_running': mock_running,
                    'hana.get_sr_state': mock_state,
                    'hana.start': mock_start,
                    'hana.sr_enable_primary': mock_enable,
                    'hana.create_user_key': mock_userkey,
                    'hana.create_backup': mock_backup
                }):
            assert hanamod.sr_primary_enabled(name,
                                              'pdr',
                                              '00',
                                              'pass',
                                              userkey=userkey,
                                              backup=backup) == ret
            mock_start.assert_called_once_with(sid='pdr',
                                               inst='00',
                                               password='******')
            mock_enable.assert_called_once_with(name=name,
                                                sid='pdr',
                                                inst='00',
                                                password='******')
            mock_userkey.assert_called_once_with(key='key',
                                                 environment='env',
                                                 user='******',
                                                 user_password='******',
                                                 database='database',
                                                 sid='pdr',
                                                 inst='00',
                                                 password='******')
            mock_backup.assert_called_once_with(user_key='user',
                                                user_password='******',
                                                database='database',
                                                backup_name='file',
                                                sid='pdr',
                                                inst='00',
                                                password='******')