def test_master_joined_is_leader_and_leader_change(self):
        self.relation_ids.return_value = ['master:1']
        self.get_cluster_id.return_value = 1
        self.is_clustered.return_value = True
        self.leader_get.return_value = {
            'async-rep-password': '******',
            'master-address': '10.0.0.1',
            'master-file': 'file1',
            'master-position': 'position1'
        }
        self.is_leader.return_value = True
        self.configure_master.return_value = True
        self.get_master_status.return_value = '10.0.0.2', 'file2', 'position2'

        hooks.master_joined()
        self.leader_set.assert_called_with({
            'async-rep-password': '******',
            'master-address': '10.0.0.2',
            'master-file': 'file2',
            'master-position': 'position2'
        })
        self.relation_set.assert_called_with(relation_id='master:1',
                                             relation_settings={
                                                 'leader': True,
                                                 'cluster_id': 1,
                                                 'master_address': '10.0.0.2',
                                                 'master_file': 'file2',
                                                 'master_password': '******',
                                                 'master_position': 'position2'
                                             })
    def test_master_joined_is_not_leader(self):
        self.relation_ids.return_value = ['master:1']
        self.get_cluster_id.return_value = 1
        self.is_clustered.return_value = True
        self.leader_get.return_value = {'async-rep-password': '******',
                                        'master-address': '10.0.0.1',
                                        'master-file': 'file',
                                        'master-position': 'position'}
        self.is_leader.return_value = False

        hooks.master_joined()
        self.relation_set.assert_called_with(
            relation_id='master:1', relation_settings={
                'leader': False, 'cluster_id': 1, 'master_address': '10.0.0.1',
                'master_file': 'file', 'master_password': '******',
                'master_position': 'position'})
    def test_master_joined_is_not_leader(self):
        self.relation_ids.return_value = ['master:1']
        self.get_cluster_id.return_value = 1
        self.is_clustered.return_value = True
        self.leader_get.return_value = {'async-rep-password': '******',
                                        'master-address': '10.0.0.1',
                                        'master-file': 'file',
                                        'master-position': 'position'}
        self.is_leader.return_value = False

        hooks.master_joined()
        self.relation_set.assert_called_with(
            relation_id='master:1', relation_settings={
                'leader': False, 'cluster_id': 1, 'master_address': '10.0.0.1',
                'master_file': 'file', 'master_password': '******',
                'master_position': 'position'})
    def test_master_joined_is_leader_and_leader_change(self):
        self.relation_ids.return_value = ['master:1']
        self.get_cluster_id.return_value = 1
        self.is_clustered.return_value = True
        self.leader_get.return_value = {'async-rep-password': '******',
                                        'master-address': '10.0.0.1',
                                        'master-file': 'file1',
                                        'master-position': 'position1'}
        self.is_leader.return_value = True
        self.configure_master.return_value = True
        self.get_master_status.return_value = '10.0.0.2', 'file2', 'position2'

        hooks.master_joined()
        self.leader_set.assert_called_with(
            {'async-rep-password': '******',
             'master-address': '10.0.0.2',
             'master-file': 'file2',
             'master-position': 'position2'})
        self.relation_set.assert_called_with(
            relation_id='master:1', relation_settings={
                'leader': True, 'cluster_id': 1, 'master_address': '10.0.0.2',
                'master_file': 'file2', 'master_password': '******',
                'master_position': 'position2'})