示例#1
0
class MysqlQueryMultiCheckTest(unittest.TestCase):
    def setUp(self):
        self.f = Fixture()
        self.f.create_database()
        self.f.create_multicolumn_example()

    def tearDown(self):
        self.f.close()

    def testInsertRequired(self):
        args = dict(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='multicolumn_example',
            identifiers=dict(identifier1='*****@*****.**',
                             identifier2='4',
                             identifier3='testInsert'),
            values=dict(value1='8', value2='admin', value3="made up"),
        )

        result = utils.ansible_check(args)
        self.assertTrue(result['changed'], 'a required change is detected')
        self.assertRegexpMatches(result['msg'], 'insert')
        self.assertEquals(self.f.count_multicolumn_example(), 0,
                          'no row has been inserted in check-mode')

    def testNoChangeRequired(self):
        # insert a row that does not need to be updated
        self.f.insert_into_multicolumn_example(
            ['*****@*****.**', 4, 'testNoChangeRequired'],
            [8, 'admin', 'made up'])

        args = dict(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='multicolumn_example',
            identifiers=dict(identifier1='*****@*****.**',
                             identifier2='4',
                             identifier3='testNoChangeRequired'),
            values={
                'value1': '8',
                'value2': 'admin',
                'value3': "made up"
            },
        )

        result = utils.ansible_check(args)
        self.assertIn('changed', result)
        self.assertFalse(result['changed'], 'no changed required is detected')
        self.assertEquals(self.f.count_multicolumn_example(), 1,
                          'no additional row has been inserted in check-mode')
示例#2
0
class MysqlQueryTest(unittest.TestCase):

    def setUp(self):
        self.module = mysql_query

        self.mock_exit_fail = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json)
        self.mock_exit_fail.start()
        self.addCleanup(self.mock_exit_fail.stop)

        self.f = Fixture()
        self.f.create_database()

    def tearDown(self):
        self.f.close()

    def test_fail_for_empty_values(self):
        set_module_args(
            login_user='******',
            name='ansible-mysql-query-test',
            table='key_value_example',
        )
        with self.assertRaises(AnsibleFailJson) as e:
            self.module.main()

        result = e.exception.args[0]
        self.assertTrue(result['failed'])
        self.assertEqual(result['msg'], 'missing required arguments: identifiers')

    def test_fail_for_table_missing(self):
        set_module_args(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='does_not_exist_table',
            identifiers=dict(not_relevant='bogus'),
            values=dict(not_relevant2='bogus'),
        )
        with self.assertRaises(AnsibleFailJson) as e:
            self.module.main()

        result = e.exception.args[0]

        self.assertTrue(result['failed'])
        self.assertEqual(result['msg'], 'No such table `does_not_exist_table`')
class MysqlQueryMultiCheckTest(unittest.TestCase):
    def setUp(self):
        self.f = Fixture()
        self.f.create_database()
        self.f.create_multicolumn_example()

    def tearDown(self):
        self.f.close()

    def testInsertRequired(self):
        args = dict(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='multicolumn_example',
            identifiers=dict(identifier1='*****@*****.**', identifier2='4', identifier3='testInsert'),
            values=dict(value1='8', value2='admin', value3="made up"),
        )

        result = utils.ansible_check(args)
        self.assertTrue(result['changed'], 'a required change is detected')
        self.assertRegexpMatches(result['msg'], 'insert')
        self.assertEquals(self.f.count_multicolumn_example(), 0, 'no row has been inserted in check-mode')

    def testNoChangeRequired(self):
        # insert a row that does not need to be updated
        self.f.insert_into_multicolumn_example(['*****@*****.**', 4, 'testNoChangeRequired'], [8, 'admin', 'made up'])

        args = dict(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='multicolumn_example',
            identifiers=dict(identifier1='*****@*****.**', identifier2='4', identifier3='testNoChangeRequired'),
            values={'value1': '8', 'value2': 'admin', 'value3': "made up"},
        )

        result = utils.ansible_check(args)
        self.assertIn('changed', result)
        self.assertFalse(result['changed'], 'no changed required is detected')
        self.assertEquals(self.f.count_multicolumn_example(), 1, 'no additional row has been inserted in check-mode')
示例#4
0
class MysqlQueryCheckTest(unittest.TestCase):
    def setUp(self):
        self.f = Fixture()
        self.f.create_database()
        self.f.create_key_value_example()

    def tearDown(self):
        self.f.close()

    def testInsertRequired(self):
        args = dict(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='key_value_example',
            identifiers=dict(name='testInsertRequired_myKey'),
            values=dict(value='42'),
        )

        result = utils.ansible_check(args)
        print(result)
        self.assertTrue(result['changed'], 'a required change is detected')
        self.assertRegexpMatches(result['msg'], 'insert')
        self.assertEquals(self.f.count_key_value_example(), 0,
                          'no row has been inserted in check-mode')

    def testNoChangeRequired(self):
        # insert a row that does not need to be updated
        self.f.insert_into_key_value_example('testNoChangeRequired_myKey', 42)

        args = dict(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='key_value_example',
            identifiers=dict(name='testNoChangeRequired_myKey'),
            values=dict(value='42'),
        )

        result = utils.ansible_check(args)
        self.assertIn('changed', result)
        self.assertFalse(result['changed'], 'no changed required is detected')
        self.assertEquals(self.f.count_key_value_example(), 1,
                          'no additional row has been inserted in check-mode')

    def testUpdateRequired(self):
        # insert a row that does need to be updated (4 vs 8)
        self.f.insert_into_key_value_example('testUpdateRequired_myKey', 4)

        args = dict(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='key_value_example',
            identifiers=dict(name='testUpdateRequired_myKey'),
            values=dict(value='8'),
        )

        result = utils.ansible_check(args)
        self.assertIn('changed', result)
        self.assertTrue(result['changed'],
                        'a change (update) required is detected')
        self.assertRegexpMatches(result['msg'], 'update')
        self.assertEquals(self.f.count_key_value_example(), 1,
                          'no additional row has been inserted in check-mode')
示例#5
0
class MysqlQueryCheckTest(unittest.TestCase):
    def setUp(self):
        self.module = mysql_query

        self.mock_exit_fail = patch.multiple(basic.AnsibleModule,
                                             exit_json=exit_json,
                                             fail_json=fail_json)
        self.mock_exit_fail.start()
        self.addCleanup(self.mock_exit_fail.stop)

        self.f = Fixture()
        self.f.create_database()
        self.f.create_key_value_example()

    def tearDown(self):
        self.f.close()

    def test_insert_required(self):
        set_module_args(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='key_value_example',
            identifiers=dict(name='testInsertRequired_myKey'),
            values=dict(value='42'),
            _ansible_check_mode=True,
        )

        with self.assertRaises(AnsibleExitJson) as e:
            self.module.main()

        result = e.exception.args[0]
        self.assertTrue(result['changed'], 'a required change is detected')
        self.assertRegexpMatches(result['msg'], 'insert')
        self.assertEquals(self.f.count_key_value_example(), 0,
                          'no row has been inserted in check-mode')

    def test_no_change_required(self):
        # insert a row that does not need to be updated
        self.f.insert_into_key_value_example('testNoChangeRequired_myKey', 42)

        set_module_args(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='key_value_example',
            identifiers=dict(name='testNoChangeRequired_myKey'),
            values=dict(value='42'),
        )

        with self.assertRaises(AnsibleExitJson) as e:
            self.module.main()

        result = e.exception.args[0]
        self.assertIn('changed', result)
        self.assertFalse(result['changed'], 'no changed required is detected')
        self.assertEquals(self.f.count_key_value_example(), 1,
                          'no additional row has been inserted in check-mode')

    def test_update_required(self):
        # insert a row that does need to be updated (4 vs 8)
        self.f.insert_into_key_value_example('testUpdateRequired_myKey', 4)

        set_module_args(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='key_value_example',
            identifiers=dict(name='testUpdateRequired_myKey'),
            values=dict(value='8'),
        )

        with self.assertRaises(AnsibleExitJson) as e:
            self.module.main()

        result = e.exception.args[0]
        self.assertIn('changed', result)
        self.assertTrue(result['changed'],
                        'a change (update) required is detected')
        self.assertRegexpMatches(result['msg'], 'update')
        self.assertEquals(self.f.count_key_value_example(), 1,
                          'no additional row has been inserted in check-mode')

    def test_delete_required(self):
        # insert a row that need to deleted.
        self.f.insert_into_key_value_example('testDeleteRequired_myKey', 6)

        set_module_args(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='key_value_example',
            identifiers=dict(name='testDeleteRequired_myKey'),
            state='absent',
            _ansible_check_mode=True,
        )

        with self.assertRaises(AnsibleExitJson) as e:
            self.module.main()

        result = e.exception.args[0]
        self.assertTrue(result['changed'], 'a required change is detected')
        self.assertRegexpMatches(result['msg'], 'delete')
        self.assertEquals(self.f.count_key_value_example(), 1,
                          'no row has been deleted in check-mode')
class MysqlQueryChangeTest(unittest.TestCase):
    def setUp(self):
        self.module = mysql_query

        self.mock_exit_fail = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json)
        self.mock_exit_fail.start()
        self.addCleanup(self.mock_exit_fail.stop)

        self.f = Fixture()
        self.f.create_database()
        self.f.create_change_example()

    def tearDown(self):
        self.f.close()

    def test_insert_required(self):
        set_module_args(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='change_example',
            identifiers=dict(setting_name='do_backups', setting_group_id='4', ),
            values=dict(value1=8, value2='fifteen'),
            defaults=dict(default1=16, default3='thirty-two'),
        )

        self.assertEquals(self.f.count_change_example(), 0, 'no row in table before running the module')

        with self.assertRaises(AnsibleExitJson) as e:
            self.module.main()

        result = e.exception.args[0]

        self.assertIn('changed', result)
        self.assertTrue(result['changed'], 'the database has been changed')
        self.assertEquals(self.f.count_change_example(), 1, 'a row has been inserted')
        row = self.f.query_change_example('do_backups', 4)
        six.assertCountEqual(self, (8, 'fifteen'), row[3:5], 'values were inserted')
        six.assertCountEqual(self, (16, 42, 'thirty-two'), row[5:8], 'row has been inserted with default values')

    def test_update_required(self):
        self.f.insert_into_change_example(['do_syncs', '5'], [42, 'four'], [8, 16, 'bar'])

        set_module_args(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='change_example',
            identifiers=dict(setting_name='do_syncs', setting_group_id='5'),
            values=dict(value1='43', value2='five'),
            defaults=dict(default1=9, default2='miow', bogus='foo')
        )

        with self.assertRaises(AnsibleExitJson) as e:
            self.module.main()

        result = e.exception.args[0]
        self.assertIn('changed', result)
        self.assertRegexpMatches(result['msg'], 'Successfully updated')
        self.assertEquals(self.f.count_change_example(), 1, 'no additional row has been inserted')
        row = self.f.query_change_example('do_syncs', '5')
        six.assertCountEqual(self, (43, 'five'), row[3:5], 'values have been updated')
        six.assertCountEqual(self, (8, 16, 'bar'), row[5:8], 'defaults have not been changed')

    def test_delete_required(self):
        self.f.insert_into_change_example(['do_syncs', '5'], [42, 'four'], [8, 16, 'bar'])

        set_module_args(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='change_example',
            identifiers=dict(setting_name='do_syncs', setting_group_id='5'),
            state='absent'
        )

        with self.assertRaises(AnsibleExitJson) as e:
            self.module.main()

        result = e.exception.args[0]
        self.assertIn('changed', result)
        self.assertRegexpMatches(result['msg'], 'Successfully deleted')
        self.assertEquals(self.f.count_change_example(), 0, 'all rows have been deleted')

    def test_no_change_required_in_no_check_mode(self):
        """
        this is the case if no change is required, but we're not in check mode.
        :return:
        """
        self.f.insert_into_change_example(['no change required', 3], [1, 'one'], [1, 2, 'three'])

        set_module_args(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='change_example',
            identifiers=dict(setting_name='no change required', setting_group_id=3),
            values=dict(value1=1, value2='one')
        )

        with self.assertRaises(AnsibleExitJson) as e:
            self.module.main()

        result = e.exception.args[0]
        self.assertIn('changed', result)
        self.assertFalse(result['changed'])
示例#7
0
class MysqlQueryChangeTest(unittest.TestCase):
    def setUp(self):
        self.f = Fixture()
        self.f.create_database()
        self.f.create_change_example()

    def tearDown(self):
        self.f.close()

    def testInsertRequired(self):
        args = dict(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='change_example',
            identifiers=dict(
                setting_name='do_backups',
                setting_group_id='4',
            ),
            values=dict(value1=8, value2='fifteen'),
            defaults=dict(default1=16, default3='thirty-two'),
        )

        self.assertEquals(self.f.count_change_example(), 0,
                          'no row in table before running the module')
        result = utils.ansible_run(args)
        self.assertIn('changed', result)
        self.assertTrue(result['changed'], 'the database has been changed')
        self.assertEquals(self.f.count_change_example(), 1,
                          'a row has been inserted')
        row = self.f.query_change_example('do_backups', 4)
        self.assertItemsEqual((8, 'fifteen'), row[3:5], 'values were inserted')
        self.assertItemsEqual((16, 42, 'thirty-two'), row[5:8],
                              'row has been inserted with default values')

    def testUpdateRequired(self):
        self.f.insert_into_change_example(['do_syncs', '5'], [42, 'four'],
                                          [8, 16, 'bar'])

        args = dict(login_user=MYSQL_CONNECTION_PARAMS['user'],
                    name=MYSQL_CONNECTION_PARAMS['db'],
                    login_password=MYSQL_CONNECTION_PARAMS['passwd'],
                    login_host=MYSQL_CONNECTION_PARAMS['host'],
                    table='change_example',
                    identifiers=dict(setting_name='do_syncs',
                                     setting_group_id='5'),
                    values=dict(value1='43', value2='five'),
                    defaults=dict(default1=9, default2='miow', bogus='foo'))

        result = utils.ansible_run(args)
        self.assertIn('changed', result)
        self.assertRegexpMatches(result['msg'], 'Successfully updated')
        self.assertEquals(self.f.count_change_example(), 1,
                          'no additional row has been inserted')
        row = self.f.query_change_example('do_syncs', '5')
        self.assertItemsEqual((43, 'five'), row[3:5],
                              'values have been updated')
        self.assertItemsEqual((8, 16, 'bar'), row[5:8],
                              'defaults have not been changed')

    def testNoChangeRequiredInNoCheckMode(self):
        """
        this is the case if no change is required, but we're not in check mode.
        :return:
        """
        self.f.insert_into_change_example(['no change required', 3],
                                          [1, 'one'], [1, 2, 'three'])

        args = dict(login_user=MYSQL_CONNECTION_PARAMS['user'],
                    name=MYSQL_CONNECTION_PARAMS['db'],
                    login_password=MYSQL_CONNECTION_PARAMS['passwd'],
                    login_host=MYSQL_CONNECTION_PARAMS['host'],
                    table='change_example',
                    identifiers=dict(setting_name='no change required',
                                     setting_group_id=3),
                    values=dict(value1=1, value2='one'))

        result = utils.ansible_run(args)
        self.assertIn('changed', result)
        self.assertFalse(result['changed'])
class MysqlQueryCheckTest(unittest.TestCase):
    def setUp(self):
        self.f = Fixture()
        self.f.create_database()
        self.f.create_key_value_example()

    def tearDown(self):
        self.f.close()

    def testInsertRequired(self):
        args = dict(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='key_value_example',
            identifiers=dict(name='testInsertRequired_myKey'),
            values=dict(value='42'),
        )

        result = utils.ansible_check(args)
        print(result)
        self.assertTrue(result['changed'], 'a required change is detected')
        self.assertRegexpMatches(result['msg'], 'insert')
        self.assertEquals(self.f.count_key_value_example(), 0, 'no row has been inserted in check-mode')

    def testNoChangeRequired(self):
        # insert a row that does not need to be updated
        self.f.insert_into_key_value_example('testNoChangeRequired_myKey', 42)

        args = dict(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='key_value_example',
            identifiers=dict(name='testNoChangeRequired_myKey'),
            values=dict(value='42'),
        )

        result = utils.ansible_check(args)
        self.assertIn('changed', result)
        self.assertFalse(result['changed'], 'no changed required is detected')
        self.assertEquals(self.f.count_key_value_example(), 1, 'no additional row has been inserted in check-mode')

    def testUpdateRequired(self):
        # insert a row that does need to be updated (4 vs 8)
        self.f.insert_into_key_value_example('testUpdateRequired_myKey', 4)

        args = dict(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='key_value_example',
            identifiers=dict(name='testUpdateRequired_myKey'),
            values=dict(value='8'),
        )

        result = utils.ansible_check(args)
        self.assertIn('changed', result)
        self.assertTrue(result['changed'], 'a change (update) required is detected')
        self.assertRegexpMatches(result['msg'], 'update')
        self.assertEquals(self.f.count_key_value_example(), 1, 'no additional row has been inserted in check-mode')
class MysqlQueryChangeTest(unittest.TestCase):
    def setUp(self):
        self.f = Fixture()
        self.f.create_database()
        self.f.create_change_example()

    def tearDown(self):
        self.f.close()

    def testInsertRequired(self):
        args = dict(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='change_example',
            identifiers=dict(setting_name='do_backups', setting_group_id='4', ),
            values=dict(value1=8, value2='fifteen'),
            defaults=dict(default1=16, default3='thirty-two'),
        )

        self.assertEquals(self.f.count_change_example(), 0, 'no row in table before running the module')
        result = utils.ansible_run(args)
        self.assertIn('changed', result)
        self.assertTrue(result['changed'], 'the database has been changed')
        self.assertEquals(self.f.count_change_example(), 1, 'a row has been inserted')
        row = self.f.query_change_example('do_backups', 4)
        self.assertItemsEqual((8, 'fifteen'), row[3:5], 'values were inserted')
        self.assertItemsEqual((16, 42, 'thirty-two'), row[5:8], 'row has been inserted with default values')

    def testUpdateRequired(self):
        self.f.insert_into_change_example(['do_syncs', '5'], [42, 'four'], [8, 16, 'bar'])

        args = dict(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='change_example',
            identifiers=dict(setting_name='do_syncs', setting_group_id='5'),
            values=dict(value1='43', value2='five'),
            defaults=dict(default1=9, default2='miow', bogus='foo')
        )

        result = utils.ansible_run(args)
        self.assertIn('changed', result)
        self.assertRegexpMatches(result['msg'], 'Successfully updated')
        self.assertEquals(self.f.count_change_example(), 1, 'no additional row has been inserted')
        row = self.f.query_change_example('do_syncs', '5')
        self.assertItemsEqual((43, 'five'), row[3:5], 'values have been updated')
        self.assertItemsEqual((8, 16, 'bar'), row[5:8], 'defaults have not been changed')

    def testNoChangeRequiredInNoCheckMode(self):
        """
        this is the case if no change is required, but we're not in check mode.
        :return:
        """
        self.f.insert_into_change_example(['no change required', 3], [1, 'one'], [1, 2, 'three'])

        args = dict(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='change_example',
            identifiers=dict(setting_name='no change required', setting_group_id=3),
            values=dict(value1=1, value2='one')
        )

        result = utils.ansible_run(args)
        self.assertIn('changed', result)
        self.assertFalse(result['changed'])
示例#10
0
class MysqlQueryMultiCheckTest(unittest.TestCase):
    def setUp(self):
        self.module = mysql_query

        self.mock_exit_fail = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json)
        self.mock_exit_fail.start()
        self.addCleanup(self.mock_exit_fail.stop)

        self.f = Fixture()
        self.f.create_database()
        self.f.create_multicolumn_example()

    def tearDown(self):
        self.f.close()

    def test_insert_required(self):
        set_module_args(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='multicolumn_example',
            identifiers=dict(identifier1='*****@*****.**', identifier2='4', identifier3='testInsert'),
            values=dict(value1='8', value2='admin', value3="made up"),
            _ansible_check_mode=True,
        )

        with self.assertRaises(AnsibleExitJson) as e:
            self.module.main()

        result = e.exception.args[0]
        self.assertTrue(result['changed'], 'a required change is detected')
        self.assertRegexpMatches(result['msg'], 'insert')
        self.assertEquals(self.f.count_multicolumn_example(), 0, 'no row has been inserted in check-mode')

    def test_no_change_required(self):
        # insert a row that does not need to be updated
        self.f.insert_into_multicolumn_example(['*****@*****.**', 4, 'testNoChangeRequired'], [8, 'admin', 'made up'])

        set_module_args(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='multicolumn_example',
            identifiers=dict(identifier1='*****@*****.**', identifier2='4', identifier3='testNoChangeRequired'),
            values={'value1': '8', 'value2': 'admin', 'value3': "made up"},
        )

        with self.assertRaises(AnsibleExitJson) as e:
            self.module.main()

        result = e.exception.args[0]

        self.assertIn('changed', result)
        self.assertFalse(result['changed'], 'no changed required is detected')
        self.assertEquals(self.f.count_multicolumn_example(), 1, 'no additional row has been inserted in check-mode')

    def test_change_detection_for_digits_in_strings(self):
        # insert a row that does not need to be updated
        self.f.insert_into_multicolumn_example(['*****@*****.**', 4, '5'], [8, '15', '16'])

        set_module_args(
            login_user=MYSQL_CONNECTION_PARAMS['user'],
            name=MYSQL_CONNECTION_PARAMS['db'],
            login_password=MYSQL_CONNECTION_PARAMS['passwd'],
            login_host=MYSQL_CONNECTION_PARAMS['host'],
            table='multicolumn_example',
            identifiers=dict(identifier1='*****@*****.**', identifier2='4', identifier3='5'),
            values={'value1': '8', 'value2': '15', 'value3': "16"},
        )

        with self.assertRaises(AnsibleExitJson) as e:
            self.module.main()

        result = e.exception.args[0]

        self.assertIn('changed', result)
        self.assertFalse(result['changed'], 'no changed required is detected')
        self.assertEquals(self.f.count_multicolumn_example(), 1, 'no additional row has been inserted in check-mode')