File: test_patch.py

package info (click to toggle)
kas 5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,268 kB
  • sloc: python: 5,745; sh: 1,095; makefile: 210
file content (143 lines) | stat: -rw-r--r-- 5,211 bytes parent folder | download | duplicates (2)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
# kas - setup tool for bitbake based projects
#
# Copyright (c) Siemens AG, 2019
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
import stat
import shutil
import pytest
import subprocess
from kas import kas
from kas.repos import PatchApplyError, PatchFileNotFound, PatchMappingError


def git_get_commit(path):
    output = subprocess.check_output(
        ['git', 'rev-parse', 'HEAD'], cwd=path)
    return output.decode('utf-8').strip()


def mercurial_get_commit(path):
    output = subprocess.check_output(
        ['hg', 'log', '-r', '.', '--template', '{node}\n'], cwd=path)
    return output.decode('utf-8').strip()


@pytest.mark.online
def test_patch(monkeykas, tmpdir, mercurial):
    tdir = tmpdir / 'test_patch'
    shutil.copytree('tests/test_patch', tdir)
    monkeykas.chdir(tdir)
    kas_wd = monkeykas.get_kwd()

    repo = mercurial(tmpdir, 'example')
    commit = repo.get_commit()
    subprocess.check_call(
        ['sed', '-i', f's/82e55d328c8c/{commit}/', 'test.yml'])

    kas.kas(['shell', 'test.yml', '-c', 'true'])
    for f in [kas_wd / 'kas/tests/test_patch/hello.sh',
              kas_wd / 'hello/hello.sh']:
        assert os.stat(f)[stat.ST_MODE] & stat.S_IXUSR

    kas_head_ref = git_get_commit(kas_wd / "kas")
    kas_branch_head_ref = git_get_commit(kas_wd / "kas-branch")
    hello_head_ref = mercurial_get_commit(kas_wd / "hello")
    hello_branch_head_ref = mercurial_get_commit(kas_wd / "hello-branch")

    kas.kas(['shell', 'test.yml', '-c', 'true'])

    assert git_get_commit(kas_wd / "kas") == kas_head_ref
    assert git_get_commit(kas_wd / "kas-branch") == kas_branch_head_ref
    assert mercurial_get_commit(kas_wd / "hello") == hello_head_ref
    assert mercurial_get_commit(kas_wd / "hello-branch") == \
        hello_branch_head_ref


@pytest.mark.online
def test_patch_update(monkeykas, tmpdir, mercurial):
    """
        Test that patches are applied correctly after switching a repo from
        a branch to a commit hash and vice-versa with both git and mercurial
        repositories.
    """
    tdir = str(tmpdir / 'test_patch_update')
    shutil.copytree('tests/test_patch', tdir)
    monkeykas.chdir(tdir)
    kas_wd = monkeykas.get_kwd()

    repo = mercurial(tmpdir, 'example')
    commit = repo.get_commit()
    for file, c in [('test.yml', '82e55d328c8c'),
                    ('test2.yml', '0a04b987be5a')]:
        subprocess.check_call(
            ['sed', '-i', f's/{c}/{commit}/', file])

    kas.kas(['shell', 'test.yml', '-c', 'true'])
    kas.kas(['shell', 'test2.yml', '-c', 'true'])
    for f in [kas_wd / 'kas/tests/test_patch/hello.sh',
              kas_wd / 'hello/hello.sh']:
        assert os.stat(f)[stat.ST_MODE] & stat.S_IXUSR


@pytest.mark.online
def test_invalid_patch(monkeykas, tmpdir):
    """
        Test on common errors when applying patches
    """
    tdir = str(tmpdir / 'test_patch_invalid')
    shutil.copytree('tests/test_patch', tdir)
    monkeykas.chdir(tdir)

    with pytest.raises(PatchFileNotFound):
        kas.kas(['shell', 'test-invalid.yml', '-c', 'true'])

    with pytest.raises(PatchMappingError):
        kas.kas(['shell', 'test-invalid2.yml', '-c', 'true'])

    with pytest.raises(PatchApplyError):
        kas.kas(['shell', 'test-invalid3.yml', '-c', 'true'])


@pytest.mark.online
def test_patch_dirty_repo(monkeykas, tmpdir, mercurial):
    """
        Test that kas will not apply patches to a dirty repository
    """
    tdir = str(tmpdir / 'test_patch_dirty')
    shutil.copytree('tests/test_patch', tdir)
    monkeykas.chdir(tdir)

    repo = mercurial(tmpdir, 'example')
    commit = repo.get_commit()
    subprocess.check_call(
        ['sed', '-i', f's/82e55d328c8c/{commit}/', 'test.yml'])

    kas.kas(['checkout', '--skip', 'repos_apply_patches', 'test.yml'])

    kas_branch_dir = monkeykas.get_kwd() / 'kas-branch'
    with open(kas_branch_dir / 'README.rst', 'a') as f:
        f.write('echo "dirty"')
    kas.kas(['checkout', 'test.yml'])
    # the dirty repo must not be patched
    assert not (kas_branch_dir / 'tests/test_patch/hello.sh').exists()
    # the clean repo must be patched
    assert (monkeykas.get_kwd() / 'hello-branch/hello.sh').exists()