github.com/jenkins-x/test-infra@v0.0.7/scenarios/kubernetes_verify.py (about) 1 #!/usr/bin/env python 2 3 # Copyright 2016 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 # Need to figure out why this only fails on travis 18 # pylint: disable=bad-continuation 19 20 """Runs verify/test-go checks for kubernetes/kubernetes.""" 21 22 import argparse 23 import os 24 import re 25 import subprocess 26 import sys 27 28 29 VERSION_TAG = { 30 '1.10': '1.10-v20181218-db74ab3f4', 31 '1.11': '1.11-v20181218-db74ab3f4', 32 '1.12': '1.12-v20181218-db74ab3f4', 33 '1.13': '1.13-v20181218-db74ab3f4', 34 # this is master, feature branches... 35 'default': '1.13-v20181218-db74ab3f4' 36 } 37 38 39 def check_output(*cmd): 40 """Log and run the command, return output, raising on errors.""" 41 print >>sys.stderr, 'Run:', cmd 42 return subprocess.check_output(cmd) 43 44 45 def check(*cmd): 46 """Log and run the command, raising on errors.""" 47 print >>sys.stderr, 'Run:', cmd 48 subprocess.check_call(cmd) 49 50 51 def retry(func, times=5): 52 """call func until it returns true at most times times""" 53 success = False 54 for _ in range(0, times): 55 success = func() 56 if success: 57 return success 58 return success 59 60 61 def try_call(cmds): 62 """returns true if check(cmd) does not throw an exception 63 over all cmds where cmds = [[cmd, arg, arg2], [cmd2, arg]]""" 64 try: 65 for cmd in cmds: 66 check(*cmd) 67 return True 68 # pylint: disable=bare-except 69 except: 70 return False 71 72 73 def get_git_cache(k8s): 74 git = os.path.join(k8s, ".git") 75 if not os.path.isfile(git): 76 return None 77 with open(git) as git_file: 78 return git_file.read().replace("gitdir: ", "").rstrip("\n") 79 80 81 def branch_to_tag(branch): 82 verify_branch = re.match(r'release-(\d+\.\d+)', branch) 83 key = 'default' 84 if verify_branch and verify_branch.group(1) in VERSION_TAG: 85 key = verify_branch.group(1) 86 return VERSION_TAG[key] 87 88 89 def main(branch, script, force, on_prow): 90 """Test branch using script, optionally forcing verify checks.""" 91 tag = branch_to_tag(branch) 92 93 force = 'y' if force else 'n' 94 artifacts = '%s/_artifacts' % os.environ['WORKSPACE'] 95 k8s = os.getcwd() 96 if not os.path.basename(k8s) == 'kubernetes': 97 raise ValueError(k8s) 98 99 check('rm', '-rf', '.gsutil') 100 remote = 'bootstrap-upstream' 101 uri = 'https://github.com/kubernetes/kubernetes.git' 102 103 current_remotes = check_output('git', 'remote') 104 if re.search('^%s$' % remote, current_remotes, flags=re.MULTILINE): 105 check('git', 'remote', 'remove', remote) 106 check('git', 'remote', 'add', remote, uri) 107 check('git', 'remote', 'set-url', '--push', remote, 'no_push') 108 # If .git is cached between runs this data may be stale 109 check('git', 'fetch', remote) 110 111 if not os.path.isdir(artifacts): 112 os.makedirs(artifacts) 113 114 if on_prow: 115 # TODO(bentheelder): on prow REPO_DIR should be /go/src/k8s.io/kubernetes 116 # however these paths are brittle enough as is... 117 git_cache = get_git_cache(k8s) 118 cmd = [ 119 'docker', 'run', '--rm=true', '--privileged=true', 120 '-v', '/var/run/docker.sock:/var/run/docker.sock', 121 '-v', '/etc/localtime:/etc/localtime:ro', 122 '-v', '%s:/go/src/k8s.io/kubernetes' % k8s, 123 ] 124 if git_cache is not None: 125 cmd.extend(['-v', '%s:%s' % (git_cache, git_cache)]) 126 cmd.extend([ 127 '-v', '/workspace/k8s.io/:/workspace/k8s.io/', 128 '-v', '%s:/workspace/artifacts' % artifacts, 129 '-e', 'KUBE_FORCE_VERIFY_CHECKS=%s' % force, 130 '-e', 'KUBE_VERIFY_GIT_BRANCH=%s' % branch, 131 '-e', 'REPO_DIR=%s' % k8s, # hack/lib/swagger.sh depends on this 132 '--tmpfs', '/tmp:exec,mode=1777', 133 'gcr.io/k8s-testimages/kubekins-test:%s' % tag, 134 'bash', '-c', 'cd kubernetes && %s' % script, 135 ]) 136 check(*cmd) 137 else: 138 check( 139 'docker', 'run', '--rm=true', '--privileged=true', 140 '-v', '/var/run/docker.sock:/var/run/docker.sock', 141 '-v', '/etc/localtime:/etc/localtime:ro', 142 '-v', '%s:/go/src/k8s.io/kubernetes' % k8s, 143 '-v', '%s:/workspace/artifacts' % artifacts, 144 '-e', 'KUBE_FORCE_VERIFY_CHECKS=%s' % force, 145 '-e', 'KUBE_VERIFY_GIT_BRANCH=%s' % branch, 146 '-e', 'REPO_DIR=%s' % k8s, # hack/lib/swagger.sh depends on this 147 'gcr.io/k8s-testimages/kubekins-test:%s' % tag, 148 'bash', '-c', 'cd kubernetes && %s' % script, 149 ) 150 151 152 if __name__ == '__main__': 153 PARSER = argparse.ArgumentParser( 154 'Runs verification checks on the kubernetes repo') 155 PARSER.add_argument( 156 '--branch', default='master', help='Upstream target repo') 157 PARSER.add_argument( 158 '--force', action='store_true', help='Force all verify checks') 159 PARSER.add_argument( 160 '--script', 161 default='./hack/jenkins/test-dockerized.sh', 162 help='Script in kubernetes/kubernetes that runs checks') 163 PARSER.add_argument( 164 '--prow', action='store_true', help='Force Prow mode' 165 ) 166 ARGS = PARSER.parse_args() 167 main(ARGS.branch, ARGS.script, ARGS.force, ARGS.prow)