summaryrefslogtreecommitdiff
path: root/test.py
blob: b481a7a6bd2653346b94df679bb569be46979142 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python

from __future__ import print_function
import os
import shutil
import subprocess
import string
import sys
import tempfile

TROVE_ADMIN_USER = 'richardipsum'

# TODO: we need to make sure we remove existing host identities
# in .ssh/known_hosts

def remote_runcmd(url, command):
    #print 'Running: ' + string.join(command)
    subprocess.check_output(['ssh', url] + command)

    # Err find out why this doesn't work when i'm less frazzled
    #subprocess.Popen(['ssh', url] + command, shell=True)

def run_git(args, working_dir, env=None):
    #return cliapp.runcmd(['git'] + args, **kwargs)
    #print 'args', args
    #print ['git'] + args

    p = subprocess.Popen(['git'] + args, cwd=working_dir, env=env)
    return_code = p.wait()

    if return_code != 0:
        print('Git failed', file=sys.stderr)
        raise Exception

def create_test_project(url, project):
    def t(s):
        return project + '-' + s

    remote_runcmd(url, ['group', 'add', t('managers'), 
        'Managers for ' + project])
    remote_runcmd(url, ['group', 'adduser', t('managers'), TROVE_ADMIN_USER])

    remote_runcmd(url, ['group', 'add', t('admins'), 'Admins for ' + project])
    remote_runcmd(url, ['group', 'addgroup', t('admins'), t('managers')])

    remote_runcmd(url, ['group', 'add', t('writers'),
        'Users with write access to ' + project])
    remote_runcmd(url, ['group', 'addgroup', t('writers'), t('admins')])

    remote_runcmd(url, ['group', 'add', t('readers'),
        'Users with read access to ' + project])
    remote_runcmd(url, ['group', 'addgroup', t('readers'), t('writers')])

def delete_repo(repopath):
    output = subprocess.check_output(['ssh', 'git@%s' % trove_id,
        'destroy', repopath],
        stderr=subprocess.STDOUT)

    # The destroy command gives us back a token,
    # we send the same command back with the token,
    # to confirm we really want to delete the repo
    token = output.split()[-1]

    subprocess.check_output(['ssh', 'git@ct-ri-4', 'destroy',
        '%s/%s/%s' % (trove_id, project, repo), token])

def create_repo(repopath):
    remote_runcmd('git@' + trove_id, ['create', repopath])

def can_clone(url, env=None):
    workspace_dir = tempfile.mkdtemp()

    run_git(['clone', url], workspace_dir, env=env)

    shutil.rmtree(workspace_dir)

def can_push(url, repo, env=None):
    workspace_dir = tempfile.mkdtemp()
    repodir = os.path.join(workspace_dir, repo)

    run_git(['clone', url], workspace_dir, env=env)

    with open(os.path.join(workspace_dir, repo, filename), 'w') as f:
        f.write('A test file')

    print('wrote to file', os.path.join(workspace_dir, repo, filename))

    run_git(['add', filename], repodir)
    run_git(['commit', '-m', 'Add a test file'], repodir)

    run_git(['push', 'origin', 'master'], repodir, env=env)

    shutil.rmtree(workspace_dir)

def reconf_lorry(trove_id, workspace_dir):
    lorry_controller_conf = '''[
        {
        "type": "lorries",
        "uuid": "ct-ri-4/open-source-lorries",
        "serial": 1,
        "interval": "6H",
        "create": "always",
        "destroy": "never",
        "stagger": true,
        "prefix": "delta",
        "tarball": "always",
        "globs": [
            "open-source-lorries/*.lorry"
        ]
        }
    ]
    '''

    chunk_lorry = '''{
        "zip": {
            "type": "tarball",
            "compression": "gzip",
            "strip": 1,
            "url": "http://downloads.sourceforge.net/infozip/zip30.tar.gz"
        }
    }
    '''

    controller_filename = 'lorry-controller.conf'
    chunk_filename = 'zip.lorry'
    lorry_dirname = 'open-source-lorries'

    run_git(['clone', 'git@%s:%s/local-config/lorries'] % (trove_id, trove_id))

    repodir = os.path.join(workspace_dir, 'lorries')

    run_git(['rm', '-r', '*'], repodir)
    run_git(['commit', '-m', 'Remove the existing lorry config'], repodir)

    os.mkdir(os.path.join(repodir, lorry_dirname))

    with open(os.path.join(repodir, controller_filename), 'w') as f:
        f.write(lorry_controller_conf)

    with open(os.path.join(repodir, lorry_dirname, chunk_filename), 'w') as f:
        f.write(chunk_lorry)

    run_git(['add', controller_filename], repodir)
    run_git(['add', os.path.join(lorry_dirname, chunk_filename)], repodir)

    run_git(['commit', '-m', 'New lorry config'], repodir)
    run_git(['push', 'origin', 'master'], repodir)

trove_id = 'ct-ri-4'
project = 'test-proj'
repo = 'test-repo'
filename = 'test-file'
repopath = "%s/%s/%s" % (trove_id, project, repo)

# ssh
create_test_project('git@' + trove_id, project)
create_repo(repopath)

can_clone('git@%s:%s/%s/%s' % (trove_id, trove_id, project, repo))
can_push('git@%s:%s/%s/%s' % (trove_id, trove_id, project, repo), repo)

delete_repo(repopath)

# http
can_clone('http://%s/git/delta/zip' % trove_id)

# Test that anonymous push gets 403
# Actually for now we'll just check that git fails
try:
    can_push('http://%s/git/delta/zip' % trove_id, 'zip')
except:
    print("That's great, we're expecting this to fail!")

# https
create_repo(repopath)

username = 'richardipsum'
password = 'quack'

can_clone('https://%s:%s@%s/git/%s/%s/%s' %
    (username, password, trove_id, trove_id, project, repo),
    env={'GIT_SSL_NO_VERIFY': '1'})

can_push('https://%s:%s@%s/git/%s/%s/%s' %
    (username, password, trove_id, trove_id, project, repo), repo,
    env={'GIT_SSL_NO_VERIFY': '1'})

delete_repo(repopath)