github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/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  BRANCH_VERSION = {
    30      '1.2': '1.4',
    31      '1.3': '1.4',
    32      'master': '1.8',
    33  }
    34  
    35  VERSION_TAG = {
    36      '1.4': '1.4-v20161130-8958f82',
    37      '1.5': '1.5-v20161205-d664d14',
    38      '1.6': '1.6-v20161205-ad918bc',
    39      '1.7': '1.7-v20170713-c28e0556',
    40      '1.8': '1.8-v20170906-3a1c5ae5',
    41  }
    42  
    43  
    44  def check_output(*cmd):
    45      """Log and run the command, return output, raising on errors."""
    46      print >>sys.stderr, 'Run:', cmd
    47      return subprocess.check_output(cmd)
    48  
    49  
    50  def check(*cmd):
    51      """Log and run the command, raising on errors."""
    52      print >>sys.stderr, 'Run:', cmd
    53      subprocess.check_call(cmd)
    54  
    55  
    56  def main(branch, script, force):
    57      """Test branch using script, optionally forcing verify checks."""
    58      # If branch has 3-part version, only take first 2 parts.
    59      verify_branch = re.match(r'master|release-(\d+\.\d+)', branch)
    60      if not verify_branch:
    61          raise ValueError(branch)
    62      # Extract version if any.
    63      ver = verify_branch.group(1) or verify_branch.group(0)
    64      tag = VERSION_TAG[BRANCH_VERSION.get(ver, ver)]
    65      force = 'y' if force else 'n'
    66      artifacts = '%s/_artifacts' % os.environ['WORKSPACE']
    67      k8s = os.getcwd()
    68      if not os.path.basename(k8s) == 'kubernetes':
    69          raise ValueError(k8s)
    70  
    71      check('rm', '-rf', '.gsutil')
    72      remote = 'bootstrap-upstream'
    73      uri = 'https://github.com/kubernetes/kubernetes.git'
    74  
    75      current_remotes = check_output('git', 'remote')
    76      if re.search('^%s$' % remote, current_remotes, flags=re.MULTILINE):
    77          check('git', 'remote', 'remove', remote)
    78      check('git', 'remote', 'add', remote, uri)
    79      check('git', 'remote', 'set-url', '--push', remote, 'no_push')
    80      # If .git is cached between runs this data may be stale
    81      check('git', 'fetch', remote)
    82  
    83      if not os.path.isdir(artifacts):
    84          os.makedirs(artifacts)
    85      check(
    86          'docker', 'run', '--rm=true', '--privileged=true',
    87          '-v', '/var/run/docker.sock:/var/run/docker.sock',
    88          '-v', '/etc/localtime:/etc/localtime:ro',
    89          '-v', '%s:/go/src/k8s.io/kubernetes' % k8s,
    90          '-v', '%s:/workspace/artifacts' % artifacts,
    91          '-e', 'KUBE_FORCE_VERIFY_CHECKS=%s' % force,
    92          '-e', 'KUBE_VERIFY_GIT_BRANCH=%s' % branch,
    93          '-e', 'REPO_DIR=%s' % k8s,  # hack/lib/swagger.sh depends on this
    94          'gcr.io/k8s-testimages/kubekins-test:%s' % tag,
    95          'bash', '-c', 'cd kubernetes && %s' % script,
    96      )
    97  
    98  
    99  if __name__ == '__main__':
   100      PARSER = argparse.ArgumentParser(
   101          'Runs verification checks on the kubernetes repo')
   102      PARSER.add_argument(
   103          '--branch', default='master', help='Upstream target repo')
   104      PARSER.add_argument(
   105          '--force', action='store_true', help='Force all verify checks')
   106      PARSER.add_argument(
   107          '--script',
   108          default='./hack/jenkins/test-dockerized.sh',
   109          help='Script in kubernetes/kubernetes that runs checks')
   110      ARGS = PARSER.parse_args()
   111      main(ARGS.branch, ARGS.script, ARGS.force)