~jocave/checkbox/hybrid-amd-gpu-mods

1614 by Daniel Manrique
[FEATURE] Merged oem_config_test by Chris Wayne
1
#!/usr/bin/python
2
3
import sys
4
import time
5
import os
6
import pwd
7
import subprocess
8
import shutil
9
import argparse
10
11
from stat import ST_MODE
12
from xpresser import Xpresser
13
from xpresser import ImageNotFound
14
15
16
def user_exists(user):
17
    ''' Make sure that the given user exists on the system '''
18
    try:
19
        user_info = pwd.getpwnam(user)
20
        print("User %s found with UID %s" % (user, user_info.pw_uid))
21
        return True
22
    except KeyError:
23
        print("User %s not found or removed" % user)
24
        return False
25
26
27
def home_dir_exists(user):
28
    ''' Checks for a directory in /home/user where user is given '''
29
    home_dir = pwd.getpwnam(user).pw_dir
30
    try:
31
        if os.path.isdir(home_dir):
32
            print("Home directory for %s found" % user)
33
            assert(_check_dir_permissions(home_dir) == '755')
34
            return True
35
        else:
36
            sys.stderr.write("No home directory for %s found" % user)
37
            return False
38
    except AssertionError:
39
        sys.stderr.write("Permissions not set properly to 755")
40
        return False
41
42
43
def _check_dir_permissions(dir):
44
    ''' Checks the permissions of the given directory '''
45
    mode = oct(os.stat(dir)[ST_MODE])[-3:]
46
    return mode
47
48
49
def cleanup_user(user):
50
    ''' Will delete the given user, and delete the user's home directory '''
51
    success = True
52
    print("Deleting user %s" % user)
53
    try:
54
        output = subprocess.Popen(['deluser', '--remove-home', user],
55
                                 stdout=subprocess.PIPE,
56
                                 stderr=subprocess.PIPE)
57
        time.sleep(1)
58
        output.poll()
59
        if output.returncode == 0 and not user_exists(user):
60
            print("User %s has been removed" % user)
61
        else:
62
            error_output = output.stderr.read()
63
            sys.stderr.write("Deleting user %s has failed with error: %s"
64
                 % (user, error_output))
65
            success = False
66
    except Exception as e:
67
        print("Failure during cleanup: %s" % e)
68
        success = False
69
    return success
70
71
72
def run_oem_config(user, passw, images_dir):
73
    ''' Call and run through oem-config using xpresser '''
74
    oem_config_de = subprocess.check_output(['oem-config', '-q'])
75
    if 'gtk_ui' not in oem_config_de:
76
        print("OEM-config is using %s, only gtk is supported" % oem_config_de)
77
    else:
78
        subprocess.Popen(['oem-config'])
79
        time.sleep(5)
80
        xp.find('welcome')
81
        try:
82
            xp.find('english_checked')
83
            print("English  selected, clicking continue")
84
        except ImageNotFound:
85
            xp.click('english_unchecked')
86
            print("English already selected, clicking continue")
87
        finally:
88
            xp.click('continue_button')
89
        try:
90
            xp.wait('wireless')
91
            print("Wireless screen found, skipping")
92
            xp.click('continue_button')
93
        except ImageNotFound:
94
            print("Wireless screen not found, no WLAN setup")
95
        xp.wait('where_are_you')
96
        print("Timezone screen found. Leaving default, clicking continue")
97
        xp.click('continue_button')
98
        xp.wait('keyboard_layout')
99
        print("Keyboard screen found, leaving default. Clicking continue")
100
        xp.click('continue_button')
101
        xp.wait('who_are_you')
102
        print(("User creation screen found.  Adding user %s with password %s"
103
                % (user, passw)))
104
        print("Editing 'your name' field")
105
        time.sleep(1)
106
        xp.type(user)
107
        time.sleep(1)
108
        print("Editing password field")
109
        xp.click('password')
110
        xp.type(passw)
111
        time.sleep(.5)
112
        print("Retyping password")
113
        xp.click('password2')
114
        xp.type(passw)
115
        print('Done editing, clicking continue')
116
        xp.click('continue_button')
117
        print('OEM config user setup completed')
118
        try:
119
            xp.find('choose_your_picture')
120
            print("Picture screen found, clicking continue")
121
            xp.click('continue_button')
122
        except:
123
            print("No picture screen found")
124
125
126
def is_oem_config_done(timeout):
127
    ''' Loops through every 15 seconds to see if oem-config is done '''
128
    is_done = False
129
    how_long = 0
130
    while (not is_done):
131
        try:
132
            xp.find('not_done', 5)
133
            print("OEM config still running")
134
            how_long += 5
135
            if how_long > timeout:
136
                sys.stderr.write('OEM config timeout exceeded.')
137
                sys.exit(1)
138
            time.sleep(5)
139
        except ImageNotFound:
140
            if how_long == 0:
141
                sys.stderr.write('OEM config not found to be running')
142
                sys.exit(1)
143
            else:
144
                print("OEM config completed!  Took %i seconds" % how_long)
145
                is_done = True
146
    return is_done
147
148
149
if __name__ == "__main__":
150
    p = argparse.ArgumentParser(description="Automatically tests oem-config")
151
    p.add_argument('-u', '--user', dest='user', required=True,
152
                  help='User to create')
153
    p.add_argument('-p', '--password', dest='passw',
154
                  required=True, help='Password to create')
155
    p.add_argument('-d', '--image-dir', dest='images_dir', required=True,
156
                  help='Directory where the oem-config Xpresser images live')
157
    p.add_argument('-t', '--timeout', dest='timeout', type=int, default=120,
158
                  help='Timeout before oem-config is considered failed in '
159
                  'seconds. Default is 120')
160
    args = p.parse_args()
161
    xp = Xpresser()
162
    xp.load_images(args.images_dir)
163
    run_oem_config(args.user, args.passw, args.images_dir)
164
    if not is_oem_config_done(args.timeout):
165
        sys.exit(1)
166
    if not user_exists(args.user):
167
        sys.exit(1)
168
    if not home_dir_exists(args.user):
169
        sys.exit(1)
170
    time.sleep(1)
171
    if not cleanup_user(args.user):
172
        sys.exit(1)