summaryrefslogtreecommitdiff
path: root/test.py
blob: a5033fb36b54aee2495954ff7bb5c901dc6314f1 (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
#!/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'

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):
    #return cliapp.runcmd(['git'] + args, **kwargs)
    #print 'args', args
    #print ['git'] + args

    p = subprocess.Popen(['git'] + args, cwd=working_dir)
    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 create_test_repo(remote_url, project, repo):
    trove_id = 'ct-ri-4' #self.vm_nhame

    remote_runcmd(remote_url, 
        ['create', "%s/%s/%s" % (trove_id, project, repo)])

def run_git_clone(vm_name, project, repo, workspace_dir):
    run_git(['clone', 'git@%s:%s/%s/%s'
        % (vm_name, vm_name, project, repo)], workspace_dir)

def run_git_clone_http(vm_name, project, repo, workspace_dir):
    run_git(['clone', 'http://%s/git/%s/%s'
        % (vm_name, vm_name, project, repo)], workspace_dir)

def can_clone(vm_name, project, repo):
    workspace_dir = tempfile.mkdtemp()

    run_git_clone(vm_name, project, repo, workspace_dir)

    shutil.rmtree(workspace_dir)

def can_clone_http(vm_name, project, repo):
    workspace_dir = tempfile.mkdtemp()

    run_git_clone_http(vm_name, project, repo, workspace_dir)

    shutil.rmtree(workspace_dir)

def can_push(vm_name, project, repo):
    workspace_dir = tempfile.mkdtemp()
    repodir = os.path.join(workspace_dir, repo)

    run_git_clone(vm_name, project, repo, workspace_dir)

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

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

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

    shutil.rmtree(workspace_dir)

vm_name = 'ct-ri-4'

project = 'test-proj'
repo = 'test-repo'
filename = 'test-file'
remote_url = 'git@' + vm_name

create_test_project(remote_url, project)
create_test_repo(remote_url, project, repo)

can_clone(vm_name, project, repo)
can_push(vm_name, project, repo)

#can_clone_http(vm_name, project, repo)
#can_push_http(vm)