Projet

Général

Profil

0001-Jenkinsfile-use-workspace-as-temporary-directory-311.patch

Benjamin Dauvergne, 04 juin 2019 12:46

Télécharger (7,96 ko)

Voir les différences:

Subject: [PATCH] Jenkinsfile: use workspace as temporary directory (#31192)

- git repository is checked-out in the repository/ sub-directory.
- tox is launched using the common step from jenkins-lib.
- pg_virtualenv temporary database is used for running tests.
 Jenkinsfile           | 35 ++++++++++++++++++++++---------
 jenkins.sh            |  9 --------
 tests/settings.py     |  9 +++++---
 tests/wcs/conftest.py | 49 ++++++++++++++++++++++++++-----------------
 tox.ini               | 24 +++++++++++++++------
 5 files changed, 79 insertions(+), 47 deletions(-)
 delete mode 100755 jenkins.sh
Jenkinsfile
2 2

  
3 3
pipeline {
4 4
    agent any
5
    options {
6
       disableConcurrentBuilds()
7
       skipDefaultCheckout()
8
    }
9
    environment {
10
       TMPDIR = "$WORKSPACE"
11
    }
5 12
    stages {
6 13
        stage('Unit Tests') {
7 14
            steps {
8
                sh 'tox -rv'
15
                cleanWs()
16
                dir ("repository") {
17
                    checkout scm
18
                    tox()
19
                }
9 20
            }
10 21
            post {
11 22
                always {
12
                    script {
13
                        utils = new Utils()
14
                        utils.publish_coverage('coverage.xml')
15
                        utils.publish_coverage_native('index.html')
16
                        utils.publish_pylint('pylint.out')
23
                    dir("repository") {
24
                        script {
25
                            utils = new Utils()
26
                            utils.publish_coverage('coverage.xml')
27
                            utils.publish_coverage_native('index.html')
28
                            utils.publish_pylint('pylint.out')
29
                        }
30
                        mergeJunitResults()
17 31
                    }
18
                    junit '*_results.xml'
19 32
                }
20 33
            }
21 34
        }
......
31 44
    }
32 45
    post {
33 46
        always {
34
            script {
35
                utils = new Utils()
36
                utils.mail_notify(currentBuild, env, 'admin+jenkins-passerelle@entrouvert.com')
47
            dir("repository") {
48
                script {
49
                    utils = new Utils()
50
                    utils.mail_notify(currentBuild, env, 'admin+jenkins-passerelle@entrouvert.com')
51
                }
37 52
            }
38 53
        }
39 54
        success {
jenkins.sh
1
#!/bin/sh
2

  
3
set -e
4

  
5
rm -f coverage.xml
6
rm -f test_results.xml
7

  
8
pip install --upgrade tox
9
tox -r
tests/settings.py
62 62
DATABASES = {
63 63
    'default': {
64 64
        'ENGINE': os.environ.get('DB_ENGINE', 'django.db.backends.sqlite3'),
65
        'TEST': {
66
            'NAME': 'passerelle-test-%s' % os.environ.get("BRANCH_NAME", "").replace('/', '-')[:63],
67
        },
65
        'NAME': 'passerelle',
68 66
    }
69 67
}
68

  
69
if 'postgres' in DATABASES['default']['ENGINE']:
70
    for key in ('PGPORT', 'PGHOST', 'PGUSER', 'PGPASSWORD'):
71
        if key in os.environ:
72
            DATABASES['default'][key[2:]] = os.environ[key]
tests/wcs/conftest.py
41 41

  
42 42
@contextlib.contextmanager
43 43
def postgres_db_factory():
44
    database = 'db%s' % random.getrandbits(20)
45

  
46
    with contextlib.closing(psycopg2.connect('')) as conn:
44
    dsn = {
45
        'dbname': 'postgres',
46
    }
47
    for key, env in [
48
            ('host', 'PGHOST'),
49
            ('port', 'PGPORT'),
50
            ('user', 'PGUSER'),
51
            ('password', 'PGPASSWORD')]:
52
        if env in os.environ:
53
            dsn[key] = os.environ[env]
54

  
55
    dbname = 'db%s' % random.getrandbits(20)
56

  
57
    with contextlib.closing(psycopg2.connect(**dsn)) as conn:
47 58
        conn.set_isolation_level(0)
48 59
        with conn.cursor() as cursor:
49
            cursor.execute('CREATE DATABASE %s' % database)
60
            cursor.execute('CREATE DATABASE %s' % dbname)
50 61
    try:
51
        yield PostgresDB(database)
62
        db_dsn = dsn.copy()
63
        db_dsn['dbname'] = dbname
64
        yield PostgresDB(db_dsn)
52 65
    finally:
53
        with contextlib.closing(psycopg2.connect('')) as conn:
66
        with contextlib.closing(psycopg2.connect(**dsn)) as conn:
54 67
            conn.set_isolation_level(0)
55 68
            with conn.cursor() as cursor:
56
                cursor.execute('DROP DATABASE IF EXISTS %s' % database)
69
                cursor.execute('DROP DATABASE IF EXISTS %s' % dbname)
57 70

  
58 71

  
59 72
class PostgresDB(object):
60
    def __init__(self, database):
61
        self.database = database
62

  
63
    @property
64
    def dsn(self):
65
        return 'dbname={self.database}'.format(self=self)
73
    def __init__(self, dsn):
74
        self.dsn = dsn
66 75

  
67 76
    @contextlib.contextmanager
68 77
    def conn(self):
69
        with contextlib.closing(psycopg2.connect(self.dsn)) as conn:
78
        with contextlib.closing(psycopg2.connect(**self.dsn)) as conn:
70 79
            yield conn
71 80

  
72 81
    def __repr__(self):
73
        return '<Postgres Database %r>' % self.database
82
        return '<Postgres Database %r>' % self.dsn
74 83

  
75 84

  
76 85
@pytest.fixture
......
193 202
            config.set('options', 'postgresql', 'true')
194 203

  
195 204
        with self.config_pck as config:
196
            config['postgresql'] = {
197
                'database': database,
198
            }
205
            wcs_dsn = database.dsn.copy()
206
            # w.c.s. only support the deprecated "database" key in a PostgreSQL DSN
207
            if 'dbname' in wcs_dsn:
208
                wcs_dsn['database'] = wcs_dsn['dbname']
209
            config['postgresql'] = wcs_dsn
199 210
        self.run_in_context(self._wcs_init_sql)
200 211

  
201 212
    def _wcs_init_sql(self):
......
446 457

  
447 458
@pytest.fixture
448 459
def wcs_host(wcs, postgres_db, datasource):
449
        with wcs.host('127.0.0.1', database=postgres_db.database) as wcs_host:
460
        with wcs.host('127.0.0.1', database=postgres_db) as wcs_host:
450 461
            yield wcs_host
tox.ini
1 1
[tox]
2
toxworkdir = {env:TMPDIR:/tmp}/tox-{env:USER}/passerelle/{env:BRANCH_NAME:}
3
envlist = django{18,111}-{sqlite,pg}
2
envlist = py27-django{18,111}-{sqlite,pg},pylint
3
toxworkdir = {env:TMPDIR:/tmp}/tox-{env:USER}/passerelle/
4 4

  
5 5
[testenv]
6 6
usedevelop = True
7
basepython = python2
8 7
setenv =
9 8
  DJANGO_SETTINGS_MODULE=passerelle.settings
10 9
  PASSERELLE_SETTINGS_FILE=tests/settings.py
......
13 12
  fast: FAST=--nomigrations
14 13
  sqlite: DB_ENGINE=django.db.backends.sqlite3
15 14
  pg: DB_ENGINE=django.db.backends.postgresql_psycopg2
15
  PGPORT={env:PGPORT:}
16
  PGHOST={env:PGHOST:}
17
  PGUSER={env:PGUSER:}
18
  PGPASSWORD={env:PGPASSWORD:}
19
  JUNIT={tty::-o junit_suite_name={envname} --junit-xml=junit-{envname}.xml}
20
  COVERAGE=--cov=passerelle/ --cov-branch --cov-append --cov-report xml --cov-report html --cov-config .coveragerc
21
  BASETEMP=--basetemp={envtmpdir}
16 22
deps =
17 23
  django18: django>=1.8,<1.9
18 24
  django111: django>=1.11,<1.12
......
37 43
  vobject
38 44
commands =
39 45
  ./get_wcs.sh
40
  django18: py.test {posargs: {env:FAST:} --junitxml=test_{envname}_results.xml --cov-report xml --cov-report html --cov=passerelle/ --cov-config .coveragerc tests/}
41
  django18: ./pylint.sh passerelle/
42
  django111: py.test {posargs: --junitxml=test_{envname}_results.xml tests/}
46
  py.test {posargs: {env:BASETEMP:} {env:FAST:} {env:JUNIT:} {env:COVERAGE:} tests/}
47

  
48
[testenv:pylint]
49
basepython = python2.7
50
deps =
51
    pylint<1.8
52
    pylint-django<0.8.1
53
commands =
54
    /bin/bash -c "./pylint.sh passerelle/"
43
-