def fuck_with_mysql_server(): print "Stopping MySQL" yield AsyncExecCmds(['pkill -9 mysqld; sudo stop mysql'], cmd_prefix='sudo ').getDeferred() # The pkill -9 mysqld causes "Lost connection to MySQL server during query" # or "MySQL server has gone away" if you try to query on a connection which has died while 1: if random.choice([0, 1]) == 0: # Only sometimes will the MySQL server be up for long enough to # successfully return a SELECT print "Starting MySQL" yield AsyncExecCmds(['start mysql'], cmd_prefix='sudo ').getDeferred() wait = random.randrange(30, 31) yield sleep(wait) print "Stopping MySQL" yield AsyncExecCmds(['pkill -9 mysqld; sudo stop mysql'], cmd_prefix='sudo ').getDeferred() wait = random.randrange(1, 5) yield sleep(wait) else: # And sometimes MySQL will be replaced with an evil daemon # which accepts TCP connections for 10-20 seconds but stays silent # This causes the official MySQL client to say: # Lost connection to MySQL server at 'reading initial communication packet', system error: 0 # ... when the connection finally dies (i.e. when evildaemon.py stops) print "Starting evil daemon" yield AsyncExecCmds(['stop mysql; sudo python test/evildaemon.py'], cmd_prefix='sudo ').getDeferred() print "Evil daemon stopped" wait = random.randrange(1, 5) yield sleep(wait)
def _start_evildaemon(self, secs): """ Simulates a MySQL server which accepts connections but has mysteriously stopped returning responses at all, i.e. it's just /dev/null """ return AsyncExecCmds(['python ../test/evildaemon.py %s' % str(secs)], cmd_prefix='sudo ').getDeferred()
def test_0900_autoRepairKeyError(self): """ """ yield AsyncExecCmds(['/opt/HybridCluster/init.d/mysqld stop' ]).getDeferred() sampleBadDataPath = FilePath(__file__).sibling('bad-data') target = FilePath('/var/db/mysql/autorepair') try: target.remove() except OSError, e: if e.errno != ENOENT: raise
def _start_mysql(self): return AsyncExecCmds(['start mysql'], cmd_prefix='sudo ').getDeferred()
def test_0600_error_strings_test(self): """ This test causes MySQL to return what we consider a temporary local error. We do this by starting MySQL, querying a table, then physically removing MySQL's data files. This triggers MySQL to return a certain error code which we want to consider a temporary local error, which should result in a reconnection to MySQL. This is arguably the most application-specific behaviour in the txMySQL client library. """ res = yield AsyncExecCmds([ """sh -c ' cd /var/lib/mysql/foo; chmod 0660 *; chown mysql:mysql * '""" ], cmd_prefix="sudo ").getDeferred() yield self._start_mysql() conn = self._connect_mysql( retry_on_error=True, temporary_error_strings=[ "Can't find file: './foo/foo.frm' (errno: 13)", ]) yield conn.selectDb("foo") yield conn.runOperation("create database if not exists foo") yield conn.runOperation("create database if not exists foo") yield conn.runOperation("drop table if exists foo") yield conn.runOperation("create table foo (id int)") yield conn.runOperation("insert into foo set id=1") result = yield conn.runQuery("select * from foo") self.assertEquals(result, [[1]]) # Now the tricky bit, we have to force MySQL to yield the error message. res = yield AsyncExecCmds([ """sh -c ' cd /var/lib/mysql/foo; chmod 0600 *; chown root:root *' """ ], cmd_prefix="sudo ").getDeferred() print res yield conn.runOperation( "flush tables") # cause the files to get re-opened d = conn.runQuery( "select * from foo" ) # This will spin until we fix the files, so do that pronto yield sleep(1) res = yield AsyncExecCmds([ """sh -c ' cd /var/lib/mysql/foo; chmod 0660 *; chown mysql:mysql * '""" ], cmd_prefix="sudo ").getDeferred() print res result = yield d self.assertEquals(result, [[1]]) conn.disconnect()
""" yield AsyncExecCmds(['/opt/HybridCluster/init.d/mysqld stop' ]).getDeferred() sampleBadDataPath = FilePath(__file__).sibling('bad-data') target = FilePath('/var/db/mysql/autorepair') try: target.remove() except OSError, e: if e.errno != ENOENT: raise sampleBadDataPath.copyTo(target) passwordEntry = pwd.getpwnam('mysql') for path in target.walk(): os.chown(path.path, passwordEntry.pw_uid, passwordEntry.pw_gid) yield AsyncExecCmds(['/opt/HybridCluster/init.d/mysqld start' ]).getDeferred() conn = client.MySQLConnection('127.0.0.1', 'root', secrets.MYSQL_ROOT_PASS, 'autorepair', port=3307, autoRepair=True) yield conn.runQuery( "select id from mailaliases where username='******' and deletedate is null" ) conn.disconnect() FREEBSD_TESTS.append(test_0900_autoRepairKeyError.__name__) # Utility functions: