def t17(executable, sources): pretty = '%s t17' % __file__ print(pretty) if sources: sources = ' -s ' + ','.join(sources) else: sources = '' w = Workspace(uid='vcsjob-cli-t17') # pull the tree into a temporary directory valid = 'git://review.sonyericsson.net/semctools/ave/vcsjob' target = w.make_tempdir() for i in range(5): cmd = '%s %s fetch -s %s -d %s -r master' % ( executable, sources, valid, target ) (s, o, e) = ave.cmd.run(cmd) if s != 0: print(o) print(e) print('FAIL %s: wrong return code: %s' % (pretty, s)) return w.delete()
def t15(executable, sources): pretty = '%s t15' % __file__ print(pretty) if sources: sources = ' -s ' + ','.join(sources) else: sources = '' w = Workspace(uid='vcsjob-cli-t15') dst = w.make_tempdir() (s, o, e) = ave.cmd.run( '%s %s fetch --source=git://no.such.host/tree -d %s -r y' % (executable, sources, dst) ) if s == 0: print('FAIL %s: wrong return code: %s' % (pretty, s)) return if 'failed to clone from git://no.such.host/tree' not in o: print('FAIL %s: error not shown:\n%s' % (pretty, o)) return if e: print('FAIL %s: stderr set:\n%s' % (pretty, e)) return if os.path.exists(dst): print( 'FAIL %s: failing a clone to an empty dir should cause the dir to ' 'be deleted.' ) return w.delete()
def t18(executable, sources): pretty = '%s t18' % __file__ print(pretty) if sources: sources = ' -s ' + ','.join(sources) else: sources = '' w = Workspace(uid='vcsjob-cli-t4') sys.path.insert(0, os.getcwd()) # pull the tree into a temporary directory, check that all remotes are # also locals afterwards valid = 'git://review.sonyericsson.net/semctools/ave/vcsjob' target = w.make_tempdir() cmd = '%s %s fetch -s %s -d %s -r master' % ( executable, sources, valid, target ) (s, o, e) = ave.cmd.run(cmd) if s != 0: print('FAIL %s: could not fetch master:\n%s\n%s' % (pretty, o, e)) return (local, remote) = ave.git.list_branches(target) if local != remote: print( 'FAIL %s: list of remote and local branches not equal: %s != %s' % (pretty, local, remote) ) w.delete()
def t7(): pretty = '%s t7' % __file__ print(pretty) def make_vcsjob_tree(workspace): path = workspace.make_tempdir() f = open(os.path.join(path, '.vcsjob'), 'w') f.write('{ "executables": [ { "path": "job.sh", "tags":["THIS"] } ] }') f.close() f = open(os.path.join(path, 'job.sh'), 'w') f.write('#! /bin/bash\nprintenv\n') f.close() os.chmod(os.path.join(path, 'job.sh'), 0755) return path w = Workspace(uid='vcsjob-execute-t7') jobs = make_vcsjob_tree(w) result = vcsjob.list_jobs(jobs_dir=jobs, job_tags=['THAT']) if result != []: print( 'FAIL %s: vcsjob.list_jobs() returned a nonempty list even though ' 'there were no jobs to list' % pretty) return w.delete()
class factory(object): HOME = None processes = None def __init__(self): pass def __call__(self, fn): def decorated(*args, **kwargs): pretty = '%s %s' % (fn.func_code.co_filename, fn.func_name) print(pretty) self.HOME = Workspace() self.processes = [] os.makedirs(os.path.join(self.HOME.path, '.ave', 'config')) result = fn(pretty, self, *args, **kwargs) for p in self.processes: try: p.terminate() p.join() except Exception, e: print e self.HOME.delete() return result return decorated
def t7(executable, sources): pretty = '%s t7' % __file__ print(pretty) if sources: sources = ' -s ' + ','.join(sources) else: sources = '' w = Workspace(uid='vcsjob-cli-t7') # create a broken .vcsjob file target = w.make_tempdir() f = open(os.path.join(target, '.vcsjob'), 'w') f.write('{ "executables": [ { "path": "does_not_exist" } ] }') f.close() # put in a tags filter that doesn't include any tests cmd = '%s %s execute --jobs=%s --tags=NONSENSE' % ( executable, sources, target ) (s, o, e) = ave.cmd.run(cmd) if s == 0: print('FAIL %s: wrong return code: %s' % (pretty, s)) return if not o.splitlines()[0].startswith('ERROR: no such file: '): print('FAIL %s: help not shown:\n%s' % (pretty, o)) return if e: print('FAIL %s: stderr set:\n%s' % (pretty, e)) return w.delete()
def generate_report(pretty, json_file): ''' Generates a report from a static JSON result file ''' # setup workspace ws = Workspace('test-files-marionette') ws_json_file = os.path.join(ws.make_tempdir(), json_file) # copy static json result file to workspace shutil.copyfile(os.path.join(TEST_DATA_PATH, json_file), ws_json_file) # create a random test name for decibel buff = [] for i in range(10): buff.append(random.randint(0, 9)) name = ''.join(['%d' % i for i in buff]) # create time stamp for report time = datetime.now() start_time = time.strftime("%Y-%m-%d %H:%M:%S") end_time = (time + timedelta(seconds=1)).strftime("%Y-%m-%d %H:%M:%S") # try to generate report. # delete workspace. ws.delete() # check if decibel reported 200 (ok) in response. return name, start_time, end_time
def decorated_fn(): w = Workspace() try: result = fn(w) return result except Exception: raise finally: w.delete()
def decorated_fn(): HOME = Workspace() os.makedirs(os.path.join(HOME.path, '.ave', 'config')) pm_config = { 'logging': False, 'log_path': os.path.join(HOME.path, 'pm.log'), 'map_path': os.path.join(HOME.path, 'pm.json') } write_config(HOME.path, 'powermeter.json', json.dumps(pm_config)) result = fn(HOME) HOME.delete() return result
def t01(): pretty = '%s t1' % __file__ print(pretty) w = Workspace() w.config['jenkins'] = JENKINS; try: path = w.download_jenkins(JOB_ID) except Exception, e: print('FAIL %s: download failed: %s' % (pretty, str(e))) w.delete() return
def t4(): pretty = '%s t4' % __file__ print(pretty) w = Workspace(uid='vcsjob-execute-t4') # create the jobs tree def make_vcsjob_tree(): path = w.make_tempdir() f = open(os.path.join(path, '.vcsjob'), 'w') f.write('{ "executables": [ { "path": "printenv.sh" } ] }') f.close() f = open(os.path.join(path, 'printenv.sh'), 'w') f.write('#! /bin/bash\nprintenv\n') f.close() os.chmod(os.path.join(path, 'printenv.sh'), 0755) return path jobs = make_vcsjob_tree() env = backup_env() # set some environment variables os.environ['FOO'] = 'BAR' os.environ['COW'] = 'MOO' cwd_bak = os.getcwd() (strio, backup) = redirect() result = vcsjob.job.main(['--jobs=' + jobs, '--env=FOO'], ) undirect(backup) os.chdir(cwd_bak) restore_env(env) # check that FOO appeared in the set of environment variables that the # test job could see, but not COW found_foo = False found_cow = False for line in strio.getvalue().splitlines(): if line == 'FOO=BAR': found_foo = True elif line == 'COW=MOO': found_cow = True if not found_foo: print('FAIL %s: FOO was not set: %s' % (pretty, strio.getvalue())) return if found_cow: print('FAIL %s: COW was set: %s' % (pretty, strio.getvalue())) return w.delete()
def t09(): pretty = '%s t9' % __file__ print(pretty) w = Workspace() w.config['jenkins'] = JENKINS; job = JenkinsJob(w.config['jenkins'], JOB_ID) try: job.load() build = job.last_completed() except Exception, e: print('FAIL %s: got exception: %s' % (pretty, str(e))) w.delete() return
def t03(): pretty = '%s t3' % __file__ print(pretty) w = Workspace() w.config['jenkins'] = JENKINS; exc = None try: path = w.download_jenkins(JOB_ID, timeout=1) print('FAIL %s: download did not time out' % pretty) w.delete() return except Exception, e: exc = e pass
def t20(): pretty = '%s t20' % __file__ print(pretty) w = Workspace('vcsjob-fetch-t20') src = 'git://review.sonyericsson.net/platform/prebuilts/misc.git' dst = w.make_tempdir() # fetch the source into the destination try: vcsjob.fetch(src, dst, 'oss/tools_r22.2') except Exception, e: print('FAIL %s: fetch failed: %s' % (pretty, str(e))) w.delete() return False
def t06(): pretty = '%s t6' % __file__ print(pretty) w = Workspace() w.config['jenkins'] = JENKINS; exc = None try: job = JenkinsJob(w.config['jenkins'], JOB_ID) job.load(timeout=0.000001) # let's hope this is small enough to fail print('FAIL %s: JenkinsJob.load() did not time out' % pretty) w.delete() return except Exception, e: exc = e
def t18(): pretty = '%s t18' % __file__ print(pretty) w = Workspace() job = JenkinsJob(base=JENKINS_WITH_BUILD_PARAMETERS, job=JOB_ID_WITH_BUILD_PARAMETERS) try: job.load() build = job.last_successful() build_parameters = build.build_parameters product = build_parameters['PRODUCT'] except Exception, e: print('FAIL %s: got exception: %s' % (pretty, str(e))) w.delete() return
def t9(executable, sources): pretty = '%s t9' % __file__ print(pretty) if sources: sources = ' -s ' + ','.join(sources) else: sources = '' w = Workspace(uid='vcsjob-cli-t9') # fetch something valid valid = 'git://review.sonyericsson.net/semctools/ave/vcsjob' target = w.make_tempdir() cmd = '%s %s fetch -s %s -d %s -r master' % ( executable, sources, valid, target ) (s, o, e) = ave.cmd.run(cmd) if s != 0: print('FAIL %s: could not fetch:\n%s\n%s' % (pretty, o, e)) return # set some environment variables os.environ['FOO'] = 'BAR' os.environ['COW'] = 'MOO' # filter in the FOO environment variable, but not COW cmd = '%s %s execute --jobs=%s -t DEMO -e FOO' % (executable,sources,target) (s, o, e) = ave.cmd.run(cmd) found_foo = False found_cow = False for line in o.splitlines(): if line == 'FOO=BAR': found_foo = True elif line == 'COW=MOO': found_cow = True if not found_foo: print('FAIL %s: FOO was not set: %s' % (pretty, o)) return if found_cow: print('FAIL %s: COW was set: %s' % (pretty, o)) return w.delete()
def t17(): pretty = '%s t17' % __file__ print(pretty) w = Workspace() exc = None try: job = JenkinsJob(base=JENKINS_WITH_BUILD_PARAMETERS, job=JOB_ID_WITH_BUILD_PARAMETERS) build = job.last_successful() build_parameters = build.load_build_parameters(timeout=0.000001) print('FAIL %s: JenkinsBuild.load_build_parameters() did not time ' 'out' % pretty) w.delete() return except Exception, e: exc = e
def t13(): pretty = '%s t13' % __file__ print(pretty) base = 'http://android-ci.cnbj.sonyericsson.net' build = '7' # no artifacts in this build, for sure w = Workspace() try: w.download_jenkins(JOB_ID, build, timeout=300, base=base) print('FAIL %s: did not raise exception' % pretty) w.delete() return False except Exception, e: if not str(e).startswith('job has no artifacts'): print('FAIL %s: wrong error: %s' % (pretty, e)) w.delete() return False
def t14(HOME): pretty = '%s t14' % __file__ print(pretty) cfg_path = os.path.join(HOME.path, '.ave', 'config', 'workspace.json') with open(cfg_path, 'w') as f: json.dump( { 'root': '~/workspaces', 'tools': { 'ls': '/bin/ls' }, 'flocker': FLOCKER }, f) avail = Workspace.list_available(cfg_path, home=HOME.path) if avail != []: print('FAIL %s: list is not empty 1: %s' % (pretty, avail)) return False w1 = Workspace('w1', home=HOME.path) expected = [WorkspaceProfile({'type': 'workspace', 'uid': 'w1'})] avail = Workspace.list_available(cfg_path, home=HOME.path) if len(avail) != 1 or avail != expected: print('FAIL %s: wrong available 1: %s' % (pretty, avail)) return False w2 = Workspace(home=HOME.path) expected.append(WorkspaceProfile({'type': 'workspace', 'uid': w2.uid})) avail = Workspace.list_available(cfg_path, home=HOME.path) if len(avail) != 2 or expected[1] not in avail: print('FAIL %s: wrong available 2: %s' % (pretty, avail)) return False w1.delete() w2.delete() avail = Workspace.list_available(cfg_path, home=HOME.path) if avail != []: print('FAIL %s: list is not empty 2: %s' % (pretty, avail)) return False return True
def t5(executable, sources): pretty = '%s t5' % __file__ print(pretty) if sources: sources = ' -s ' + ','.join(sources) else: sources = '' w = Workspace(uid='vcsjob-cli-t5') # fetch something valid valid = 'git://review.sonyericsson.net/semctools/ave/vcsjob' target = w.make_tempdir() cmd = '%s %s fetch -s %s -d %s -r master' % ( executable, sources, valid, target ) (s, o, e) = ave.cmd.run(cmd) if s != 0: print('FAIL %s: could not fetch:\n%s\n%s' % (pretty, o, e)) return # break the .vcsjob file f = open(os.path.join(target, '.vcsjob'), 'a') f.write('not valid syntax in a .vcsjob file') f.close() # put in a tags filter that doesn't include any tests cmd = '%s %s execute --jobs=%s --tags=NONSENSE' %(executable,sources,target) (s, o, e) = ave.cmd.run(cmd) if s == 0: print('FAIL %s: wrong return code: %s' % (pretty, s)) return if not o.split('\n')[0].startswith('ERROR: could not load'): print('FAIL %s: help not shown:\n%s' % (pretty, o)) return if e: print('FAIL %s: stderr set:\n%s' % (pretty, e)) return w.delete()
def t4(executable, sources): pretty = '%s t4' % __file__ print(pretty) if sources: sources = ' -s ' + ','.join(sources) else: sources = '' w = Workspace(uid='vcsjob-cli-t4') # fetch something valid valid = 'git://review.sonyericsson.net/semctools/ave/vcsjob' target = w.make_tempdir() cmd = '%s %s fetch -s %s -d %s -r master' % ( executable, sources, valid, target ) (s, o, e) = ave.cmd.run(cmd) if s != 0: print('FAIL %s: could not fetch:\n%s\n%s' % (pretty, o, e)) return # put in a tags filter that doesn't include any tests cmd = '%s %s execute --jobs=%s --tags=NONSENSE' % ( executable, sources, target ) (s, o, e) = ave.cmd.run(cmd) if s == 0: print('FAIL %s: wrong return code: %s' % (pretty, s)) return if o.splitlines()[0] != 'WARNING: no jobs were executed': print('FAIL %s: help not shown:\n%s' % (pretty, o)) return if e: print('FAIL %s: stderr set:\n%s' % (pretty, e)) return w.delete()
def t8(executable, sources): pretty = '%s t8' % __file__ print(pretty) if sources: sources = ' -s ' + ','.join(sources) else: sources = '' w = Workspace(uid='vcsjob-cli-t8') # fetch something valid valid = 'git://review.sonyericsson.net/semctools/ave/vcsjob' target = w.make_tempdir() cmd = '%s %s fetch -s %s -d %s -r master' % ( executable, sources, valid, target ) (s, o, e) = ave.cmd.run(cmd) if s != 0: print('FAIL %s: could not fetch:\n%s\n%s' % (pretty, o, e)) return # remove the executable flag on a job os.chmod(os.path.join(target, 'jobs', 'demo_job.sh'), 0644) cmd = '%s %s execute --jobs=%s --tags=EXECUTE' % (executable, sources, target) (s, o, e) = ave.cmd.run(cmd) if s == 0: print('FAIL %s: wrong return code: %s' % (pretty, s)) return if not o.splitlines()[0].startswith('ERROR: file is not executable: '): print('FAIL %s: help not shown:\n%s' % (pretty, o)) return if e: print('FAIL %s: stderr set:\n%s' % (pretty, e)) return w.delete()
def t11(): pretty = '%s t11' % __file__ print(pretty) w = Workspace(uid='vcsjob-execute-t11') jobs = make_vcsjob_tree(w) cwd_bak = os.getcwd() (strio, backup) = redirect() result = vcsjob.execute_tags(jobs_dir=jobs, job_tags=['THAT']) undirect(backup) os.chdir(cwd_bak) if result == vcsjob.OK: print( 'FAIL %s: vcsjob.execute_tags() returned OK even though there were ' 'no jobs to run' % pretty) return result = strio.getvalue().split('\n')[0] if result != 'WARNING: no jobs were executed': print('FAIL %s: error not shown:\n%s' % (pretty, strio.getvalue())) return w.delete()
def t02(): pretty = '%s t2' % __file__ print(pretty) w = Workspace() w.config['jenkins'] = JENKINS; # find a build id, not the latest job = ave.jenkins.JenkinsJob(w.config['jenkins'], JOB_ID) all_builds = job.all_builds() if len(all_builds) < 2: print( 'BLOCKED %s: no enough builds available to support the test: %d' % (pretty, len(all_builds)) ) w.delete() return build_id = None artifacts = None for b in all_builds: if (b.attributes['result'] == 'SUCCESS' and len(b.attributes['artifacts']) > 1): build_id = str(b.build) artifacts = [a['relativePath'] for a in b.attributes['artifacts']] break if not build_id: print('BLOCKED %s: no usable build to support the test' % pretty) w.delete() return # download the id'd build try: path = w.download_jenkins(JOB_ID, build_id, artifacts) except Exception, e: print('FAIL %s: download failed: %s' % (pretty, str(e))) w.delete() return
def t19(): pretty = '%s t19' % __file__ print(pretty) w = Workspace('vcsjob-fetch-t19') src = 'git://review.sonyericsson.net/platform/prebuilts/misc.git' dst = w.make_tempdir() # fetch the source into the destination try: vcsjob.fetch(src, dst, 'oss/tools_r22.2', timeout=1) print('FAIL %s: it should be timeout') % pretty w.delete() return False except Exception, e: if 'command timed out' == str(e): w.delete() return True else: print('FAIL %s: fetch failed: %s' % (pretty, str(e))) w.delete() return False
def decorator(): w = Workspace() result = fn(w) w.delete() return result
def decorated_fn(): w = Workspace() result = fn(w) if result: w.delete() return result
def decorated_fn(so_path): w = Workspace() r = fn(w, so_path) w.delete() return r
class factory(object): HOME = None processes = None def __init__(self): pass def kill(self): for p in self.processes: p.terminate() try: p.join() except Unwaitable: pass def __call__(self, fn): def decorated(*args, **kwargs): pretty = '%s %s' % (fn.func_code.co_filename, fn.func_name) print(pretty) self.HOME = Workspace() self.processes = [] os.makedirs(os.path.join(self.HOME.path, '.ave','config')) try: result = fn(pretty, self, *args, **kwargs) except: raise finally: self.kill() self.HOME.delete() return result return decorated def write_config(self, filename, config): path = os.path.join(self.HOME.path, '.ave','config',filename) with open(path, 'w') as f: json.dump(config, f) def make_control(self, home=None, authkey=None, max_tx=-1, timeout=1): sock,port = find_free_port() c = MockControl(port, authkey, sock, home=home, max_tx=max_tx) r = RemoteControl(('',port), None, timeout, home=home) c.start() self.processes.append(c) return r def make_proxy(self, target): sock,port = find_free_port() c = MockControl(port, None, sock, proxee=target) r = RemoteControl(('',port), None, 1) c.start() self.processes.append(c) return r def make_client(self, fn, *argv): p = Process(target=fn, args=argv) p.start() self.processes.append(p) return p def make_panotti_server(self, home): sock,port = find_free_port() c = PanottiControl(port, sock, home=home) r = RemoteControl(('',port), None, 1) c.start() self.processes.append(c) return r