def test_dump_load(): with capture() as out: dump(['', test_config]) with open('/tmp/k', 'w') as f: f.write(out[0]) with capture() as out: load(['', test_config, '/tmp/k']) stdout, stderr = out assert stderr == '' assert 'Done' in stdout
def test_start_time(): app = get_app() # bar34234 or default # gets picked because foo23542 has not started yet path = '/1/firefox/39/beta/fr-BE/BE/default/default' res = app.get(path).json assert res['settings']['searchDefault'] != 'Google2' # also, any attempt to get foo23542 directly should # fallback to the defaults because it's not active yet res = app.get(path + '/foo23542').json assert res['settings']['searchDefault'] != 'Google2' # let's change the data config = app.app._config datadir = os.path.join(os.path.dirname(__file__), '..', '..', 'data') datafile = os.path.join(datadir, config['absearch']['config']) # save a copy shutil.copyfile(datafile, datafile + '.saved') with open(datafile) as f: data = json.loads(f.read()) # change the start time so it's activated now filters = data['locales']['fr-BE']['BE']['tests']['foo23542']['filters'] filters['startTime'] = time.time() - 10 try: # save the new data with open(datafile, 'w') as f: f.write(json.dumps(data)) with capture(): # reload S3 populate_S3() # reload the app reload() # now it has to be foo23542 res = app.get(path).json assert res['settings']['searchDefault'] == 'Google2', res finally: # back to original os.rename(datafile + '.saved', datafile) with capture(): # reload S3 populate_S3() # reload the app reload()
def test_main(): with capture(): greenlet = gevent.spawn(main, [test_config]) gevent.sleep(0.1) assert greenlet.started greenlet.kill() gevent.wait([greenlet]) assert not greenlet.started
def test_main_no_args(): with capture() as f: greenlet = gevent.spawn(main, []) gevent.sleep(0.1) assert greenlet.started, f greenlet.kill() gevent.wait([greenlet]) assert not greenlet.started
def test_check(): with capture() as out: res = main([]) stdout, stderr = out assert stderr == '', stderr assert 'OK' in stdout assert res == 0
def test_redis_dies(): # life is good, we get some cohorts app = get_app() path = '/1/firefox/39/beta/en-US/US/default/default' res = app.get(path) # now redis goes down because life is hard redis_process = _P[1] try: os.killpg(redis_process.pid, signal.SIGTERM) redis_process.kill() except OSError: pass redis_process.wait() # what happens to our app ? class MySentry(object): exceptions = 0 def get_ident(self, something): self.exceptions += 1 return 'id' def captureException(self): return 'yeah' old_sentry = app.app._sentry app.app._sentry = MySentry() app.app.catchall = True with capture(): try: res = app.get(path) except AppError as e: # that's what we want assert '500' in str(e) else: raise Exception(res) finally: app.app._sentry = old_sentry app.app.catchall = False # and... redis is back! _P.append(run_redis()) time.sleep(.1) # our app should be ok. app.get(path)
def test_check_fails(): conf = os.path.join(os.path.dirname(__file__), '..', '..', 'data', 'config.json') os.rename(conf, conf + '.saved') try: with open(conf, 'w') as f: f.write("{'valid': 'json but crap'}") with capture(): res = main([]) finally: os.rename(conf + '.saved', conf) # we should fail assert res == 1
def test_main_sys_args(): old_argv = list(sys.argv) config = os.path.join(os.path.dirname(__file__), '..', '..', 'config', 'absearch.ini') sys.argv = ['', config] try: with capture() as f: greenlet = gevent.spawn(main) gevent.sleep(0.1) assert greenlet.started, f greenlet.kill() gevent.wait([greenlet]) assert not greenlet.started finally: sys.argv[:] = old_argv
def test_check_enum_fails(): conf = os.path.join(os.path.dirname(__file__), '..', '..', 'data', 'config.json') os.rename(conf, conf + '.saved') broken_conf = os.path.join(os.path.dirname(__file__), 'broken-conf.json') e = None try: shutil.copy(broken_conf, conf) with capture(): main([]) except ValidationError as err: # that's what we want e = err finally: os.rename(conf + '.saved', conf) # we should fail assert e is not None
def test_check_enum_fails(): conf = os.path.join(os.path.dirname(__file__), '..', '..', 'data', 'config.json') os.rename(conf, conf + '.saved') broken_conf = os.path.join(os.path.dirname(__file__), 'broken-conf.json') e = None try: shutil.copy(broken_conf, conf) with capture(): main([]) except ValidationError as e: # that's what we want pass finally: os.rename(conf + '.saved', conf) # we should fail assert e is not None
def test_reload(): # change some things in the config app = get_app() config_file = app.app._config_file config = app.app._config # save current config with open(config_file + '.saved', 'w') as f: config.write(f) # change the configuration to alternatives # so we actually test them config['statsd']['prefix'] = 'meh' config['sentry']['enabled'] = '1' config['absearch']['backend'] = 'directory' config['absearch']['counter'] = 'memory' datadir = os.path.join(os.path.dirname(__file__), '..', '..', 'data') config.add_section('directory') config['directory']['path'] = datadir # save new config with open(config_file, 'w') as f: config.write(f) try: # make sure that reload grabs the config with capture(): reload() assert app.app._config['statsd']['prefix'] == 'meh' # doing a call with sentry disabled path = '/1/firefox/39/beta/hh-FR/uz/default/default' res = app.get(path).json assert res.keys() == ['interval'] finally: # restore old config os.rename(config_file + '.saved', config_file)