gitlab.py 8.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2014 Vifib SARL and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from .slaprunner import SlaprunnerTestSuite

import json
import random
import string
import time
import requests

class NotHttpOkException(Exception):
  pass

class GitlabTestSuite(SlaprunnerTestSuite):
  """
  Run Gitlab inside Slaprunner Resiliency Test.
  Note: requires specific kernel allowing long shebang paths.
  """

  def _setGitlabInstanceParameter(self):
    """
    Set inside of slaprunner the instance parameter to use to deploy gitlab instance.
    """
    self.logger.info('Updating instance parameter to use to deploy gitlab instance...')
    self._connectToSlaprunner(
        resource='saveParameterXml',
        data='software_type=gitlab-test&parameter=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%3E%0A%3Cinstance%3E%0A%3C%2Finstance%3E'
    )

56
  def _connectToGitlab(self, path='', post_data=None, url='', parameter_dict={}):
57 58 59 60 61 62 63
    request_url = self.backend_url
    if url:
      request_url = url
    if path:
      request_url += '/' + path
    headers = {"PRIVATE-TOKEN" : self.private_token}
    if post_data is None:
64
      response = requests.get(request_url, params=parameter_dict,
65 66
                              headers=headers, verify=False)
    elif post_data == {}:
67 68
      response = requests.post(request_url, params=parameter_dict,
                                headers=headers, verify=False)
69
    else:
70 71
      response = requests.post(request_url, params=parameter_dict,
                                headers=headers, data=post_data, verify=False)
72 73 74 75 76 77 78
    if not response.ok:
      raise Exception("ERROR connecting to Gitlab: %s: %s \n%s" % (
        response.status_code, response.reason, response.text))
    return response.text

  def _createNewProject(self, name, namespace='open'):
    uri = 'api/v3/projects'
79 80
    parameter_dict = {'name': name, 'namespace': namespace}
    return self._connectToGitlab(uri, post_data={}, parameter_dict=parameter_dict)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

  def _listProjects(self):
    path = 'api/v3/projects'
    return json.loads(self._connectToGitlab(path=path))

  def _setGitlabConnectionParameter(self):
    """
    set parameters of gitlab instance.
    Note: it is not connection parameter of slaprunner,
    but connection parameter of what is inside of webrunner.
    """
    data = self._connectToSlaprunner(
        resource='getConnectionParameter/slappart0'
    )
    parameter_dict = json.loads(data)
    self.backend_url = parameter_dict['backend_url']
    self.password = parameter_dict['password']
    self.private_token = parameter_dict['private-token']
    self.file_uri = parameter_dict['latest-file-uri']

  def _getRootPassword(self):
    data = self._connectToSlaprunner(
        resource='getConnectionParameter/slappart0'
    )
    password = json.loads(json.loads(data)['_'])['password']
    self.logger.info('Retrieved gitlab root password is:\n%s' % password)
    return password

  def generateData(self):
    self.slaprunner_password = ''.join(
        random.SystemRandom().sample(string.ascii_lowercase, 8)
    )
    self.slaprunner_user = 'slapos'
    self.logger.info('Generated slaprunner user is: %s' % self.slaprunner_user)
    self.logger.info('Generated slaprunner password is: %s' % self.slaprunner_password)

  def pushDataOnMainInstance(self):
    """
    Create a dummy Software Release,
    Build it,
    Wait for build to be successful,
    Deploy instance,
    Wait for instance to be started.
    Store the main IP of the slaprunner for future use.
    """

    self.logger.debug('Getting the backend URL...')
    parameter_dict = self._getPartitionParameterDict()
    self.slaprunner_backend_url = parameter_dict['backend-url']
    self.logger.info('backend_url is %s.' % self.slaprunner_backend_url)
    self.slaprunner_user = parameter_dict['init-user']
    self.slaprunner_password = parameter_dict['init-password']

    self._login()
    time.sleep(10)

    self._gitClone()
    self._openSoftwareRelease('gitlab')
    self._setGitlabInstanceParameter()

    self.logger.info('Waiting for 1 minutes...')
    # Debug, remove me.
    time.sleep(60)

    self._buildSoftwareRelease()
    self._deployInstance()
    # wait for unicorn to start then run instance again to install gitlab backup
    self.logger.info('wait 1 minute for unicorn to start then run instance...')
    time.sleep(60)
    self._deployInstance()
    self._deployInstance()
    # Stop all services because we want to restart gitlab (safe)
    self.logger.info('Stop all services because we want to restart gitlab..')
    self._connectToSlaprunner('/stopAllPartition')
    self._deployInstance()

    self._setGitlabConnectionParameter()
    self.logger.info('Retrieved gitlab url is:\n%s' % self.backend_url)
    self.logger.info('Gitlab root password is:\n%s' % self.password)
    self.logger.info('Gitlab private token is:\n%s' % self.private_token)

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    self.logger.info('Waiting 90 seconds so that gitlab can be started...')
    time.sleep(90)

    self.logger.info('Trying to connect to gitlab backend URL...')
    loop = 0
    while loop < 3:
      try:
        self._connectToGitlab(url=self.backend_url)
      except Exception, e:
        if loop == 2:
          raise
        self.logger.warning(str(e))
        self.logger.info('Retry connection in 60 seconds...')
        loop += 1
        time.sleep(60)
      else:
        self.logger.info('success!')
        break

    self.logger.info(self._createNewProject('sample.test'))
182 183 184 185 186 187 188 189 190 191
    project_list = self._listProjects()
    self.default_project_list = []
    for project in project_list:
      self.default_project_list.append(project['name_with_namespace'])

    self.logger.info('Gitlab project list is:\n%s' % self.default_project_list)
    self.logger.info('Getting test file at url: %s' % self.file_uri)
    self.sample_file = self._connectToGitlab(url=self.file_uri)

    self.logger.info('Wait 10 minutes for main instance to have backup of gitlab...')
192
    time.sleep(600)
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

    # in erp5testnode, we have only one IP, so we can't run at the same time
    # gitlab in webrunner of main instance, and other services in import script of clone instance
    # So we stop main instance processes.
    self._connectToSlaprunner('/stopAllPartition')

    self.logger.info('Wait half an hour for clone to have compiled Gitlab SR...')
    #time.sleep(3600 / 2)
    time.sleep(600)

  def checkDataOnCloneInstance(self):
    """
    Check that:
      * backend_url is different
      * Software Release profile is the same,
      * Software Release is built and is the same,
      * Instance is deployed and is the same (contains same new data).
    """
    old_slaprunner_backend_url = self.slaprunner_backend_url
    self.slaprunner_backend_url = self._returnNewInstanceParameter(
213
        parameter_key='backend-url',
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
        old_parameter_value=old_slaprunner_backend_url,
        force_new=True,
    )
    self._login()
    self._waitForSoftwareBuild()
    self._deployInstance()
    time.sleep(60)

    project_list = self._listProjects()
    success = True
    for project in project_list:
      success = success and (project['name_with_namespace'] in self.default_project_list)

    if success:
      file_content = self._connectToGitlab(url=self.file_uri)
      success = success and (file_content == self.sample_file)

    if success:
      self.logger.info('Data are the same: success.')
      return True
    else:
      self.logger.info('Data are different: failure.')
      return False


def runTestSuite(*args, **kwargs):
  """
  Run Slaprunner Resiliency Test.
  """
  return GitlabTestSuite(*args, **kwargs).runTestSuite()