summaryrefslogtreecommitdiff
path: root/ubuntu/scripts/sata-partition-rw.py
blob: 64ae6b7dbbc42484909bf6692a8ede81b8838ce2 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/env python
#
# SATA Partition, Read and Write test cases for Linux Linaro ubuntu
#
# Copyright (C) 2012 - 2014, Linaro Limited.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# Author: Botao Sun <botao.sun@linaro.org>
#

import sys
import os
import stat
import time
import commands

# Switch to home path of current user to avoid any permission issue
home_path = os.environ['HOME']
os.chdir(home_path)
print os.getcwd()

# List all test cases
test_case_list = ["sata_device_existence", "sata_mklabel_msdos", "sata_mklabel_gpt", "sata_first_ext2_partition", "sata_second_ext2_partition", "sata_ext3_format", "sata_ext4_format", "sata_ext4_mount", "sata_ext4_umount", "sata_ext4_file_fill", "sata_ext4_file_edit", "sata_ext4_dd_write", "sata_ext4_dd_read"]
print "There are " + str(len(test_case_list)) + " test cases in this test suite."


# All skipped - If test case sata_device_existence failed, then skip all the rest.
def all_skipped():
    for element in test_case_list[1:]:
        print element + ": SKIP" + " 0" + " Inapplicable"

# Save partition layout to a local file for reference
commands.getstatusoutput("fdisk -l > partition_layout.txt 2>&1")
device_name = sys.argv[1]


def sata_device_existence():
    testcase_name = "sata_device_existence"
    if device_name == "":
        print testcase_name + ": FAIL" + " 0" + " Inapplicable" + " - Device name is empty"
        all_skipped()
        sys.exit(1)
    else:
        logfile = open("partition_layout.txt", "r")
        logcontent = logfile.readlines()
        positive_counter = 0
        for i in range(0, len(logcontent)):
            print logcontent[i].strip("\n")
            if device_name in logcontent[i]:
                positive_counter = positive_counter + 1
        logfile.close()
        if positive_counter > 0:
            print testcase_name + ": PASS" + " 0" + " Inapplicable"
        else:
            print testcase_name + ": FAIL" + " 0" + " Inapplicable" + " - Could not locate " + device_name + " on target board"
            all_skipped()
            sys.exit(1)


def sata_mklabel_msdos():
    testcase_name = "sata_mklabel_msdos"
    label_name = "msdos"
    run_command = "parted -s " + device_name + " mklabel " + label_name
    print run_command

    mklabel_return = commands.getstatusoutput(run_command)
    if mklabel_return[0] != 0:
        print testcase_name + ": FAIL" + " 0" + " Inapplicable"
    else:
        commands.getstatusoutput("parted -s " + device_name + " print > partition_table_msdos.txt 2>&1")
        logfile = open("partition_table_msdos.txt", "r")
        logcontent = logfile.readlines()
        positive_counter = 0
        for i in range(0, len(logcontent)):
            print logcontent[i].strip("\n")
            if label_name in logcontent[i]:
                positive_counter = positive_counter + 1
        logfile.close()

        if positive_counter > 0:
            print testcase_name + ": PASS" + " 0" + " Inapplicable"
        else:
            print testcase_name + ": FAIL" + " 0" + " Inapplicable" + " - Could not find partition table " + label_name + " on " + device_name


def sata_mklabel_gpt():
    testcase_name = "sata_mklabel_gpt"
    label_name = "gpt"
    run_command = "parted -s " + device_name + " mklabel " + label_name
    print run_command

    mklabel_return = commands.getstatusoutput(run_command)
    if mklabel_return[0] != 0:
        print testcase_name + ": FAIL" + " 0" + " Inapplicable"
    else:
        commands.getstatusoutput("parted -s " + device_name + " print > partition_table_gpt.txt 2>&1")
        logfile = open("partition_table_gpt.txt", "r")
        logcontent = logfile.readlines()
        positive_counter = 0
        for i in range(0, len(logcontent)):
            print logcontent[i].strip("\n")
            if label_name in logcontent[i]:
                positive_counter = positive_counter + 1
        logfile.close()

        if positive_counter > 0:
            print testcase_name + ": PASS" + " 0" + " Inapplicable"
        else:
            print testcase_name + ": FAIL" + " 0" + " Inapplicable" + " - Could not find partition table " + label_name + " on " + device_name


def sata_first_ext2_partition():
    testcase_name = "sata_first_ext2_partition"
    label_name = "msdos"
    partition_table_creation = "parted -s " + device_name + " mklabel " + label_name
    first_partition_creation = "parted -s " + device_name + " mkpart primary ext2 0 10%"
    print partition_table_creation
    print first_partition_creation

    partition_table_return = commands.getstatusoutput(partition_table_creation)
    if partition_table_return[0] != 0:
        print testcase_name + ": FAIL" + " 0" + " Inapplicable" + " Failures on " + partition_table_creation
    else:
        first_partition_return = commands.getstatusoutput(first_partition_creation)
        if first_partition_return[0] != 0:
            print testcase_name + ": FAIL" + " 0" + " Inapplicable" + " Failures on " + first_partition_creation
        else:
            commands.getstatusoutput("fdisk -l " + device_name + " > ext2_msdos_first.txt 2>&1")
            logfile = open("ext2_msdos_first.txt", "r")
            logcontent = logfile.readlines()
            positive_counter = 0
            partition_name_first = device_name + "1"
            for i in range(0, len(logcontent)):
                print logcontent[i].strip("\n")
                if partition_name_first in logcontent[i]:
                    positive_counter = positive_counter + 1
            logfile.close()

            if positive_counter > 0:
                print testcase_name + ": PASS" + " 0" + " Inapplicable"
            else:
                print testcase_name + ": FAIL" + " 0" + " Inapplicable" + " - Could not find partition " + partition_name_first + " on " + device_name


def sata_second_ext2_partition():
    testcase_name = "sata_second_ext2_partition"
    second_partition_creation = "parted -s " + device_name + " mkpart primary ext2 11% 20%"
    print second_partition_creation

    second_partition_return = commands.getstatusoutput(second_partition_creation)
    if second_partition_return[0] != 0:
        print testcase_name + ": FAIL" + " 0" + " Inapplicable" + " Failures on " + second_partition_creation
    else:
        commands.getstatusoutput("fdisk -l " + device_name + " > ext2_msdos_second.txt 2>&1")
        logfile = open("ext2_msdos_second.txt", "r")
        logcontent = logfile.readlines()
        positive_counter = 0
        partition_name_second = device_name + "2"
        for i in range(0, len(logcontent)):
            print logcontent[i].strip("\n")
            if partition_name_second in logcontent[i]:
                positive_counter = positive_counter + 1
        logfile.close()

        if positive_counter > 0:
            print testcase_name + ": PASS" + " 0" + " Inapplicable"
        else:
            print testcase_name + ": FAIL" + " 0" + " Inapplicable" + " - Could not find partition " + partition_name_second + " on " + device_name


def sata_ext3_format():
    testcase_name = "sata_ext3_format"
    target_partition_name = device_name + "1"
    ext3_format = "mkfs.ext3 " + target_partition_name
    print ext3_format

    ext3_format_return = commands.getstatusoutput(ext3_format)
    if ext3_format_return[0] != 0:
        print testcase_name + ": FAIL" + " 0" + " Inapplicable" + " Failures on ext3 format command"
    else:
        commands.getstatusoutput("parted -s " + device_name + " print > ext3_format_first.txt 2>&1")
        logfile = open("ext3_format_first.txt", "r")
        logcontent = logfile.readlines()
        positive_counter = 0
        for i in range(0, len(logcontent)):
            print logcontent[i].strip("\n")
            if "ext3" in logcontent[i]:
                positive_counter = positive_counter + 1
        logfile.close()

        if positive_counter > 0:
            print testcase_name + ": PASS" + " 0" + " Inapplicable"
        else:
            print testcase_name + ": FAIL" + " 0" + " Inapplicable" + " - Could not find ext3 partition on " + device_name


def sata_ext4_format():
    testcase_name = "sata_ext4_format"
    target_partition_name = device_name + "2"
    ext4_format = "mkfs.ext4 " + target_partition_name
    print ext4_format

    ext4_format_return = commands.getstatusoutput(ext4_format)
    if ext4_format_return[0] != 0:
        print testcase_name + ": FAIL" + " 0" + " Inapplicable" + " Failures on ext4 format command"
    else:
        commands.getstatusoutput("parted -s " + device_name + " print > ext4_format_second.txt 2>&1")
        logfile = open("ext4_format_second.txt", "r")
        logcontent = logfile.readlines()
        positive_counter = 0
        for i in range(0, len(logcontent)):
            print logcontent[i].strip("\n")
            if "ext4" in logcontent[i]:
                positive_counter = positive_counter + 1
        logfile.close()

        if positive_counter > 0:
            print testcase_name + ": PASS" + " 0" + " Inapplicable"
        else:
            print testcase_name + ": FAIL" + " 0" + " Inapplicable" + " - Could not find ext4 partition on " + device_name


def sata_ext4_mount_umount():
    testcase_mount = "sata_ext4_mount"
    testcase_umount = "sata_ext4_umount"
    target_partition_name = device_name + "2"
    mount_point = "/media"
    ext4_mount = "mount " + target_partition_name + " " + mount_point
    ext4_umount = "umount " + mount_point

    # umount everything from mount point - cleaning
    umount_clean = "umount " + mount_point
    commands.getstatusoutput(umount_clean)
    time.sleep(5)
    print "mount point cleaned!"

    # Test mount command
    ext4_mount_return = commands.getstatusoutput(ext4_mount)
    time.sleep(5)
    if ext4_mount_return[0] != 0:
        print testcase_mount + ": FAIL" + " 0" + " Inapplicable" + " Failures on ext4 partition mount!"
    else:
        commands.getstatusoutput("mount > mount_log.txt 2>&1")
        logfile = open("mount_log.txt", "r")
        logcontent = logfile.readlines()
        positive_counter = 0
        for i in range(0, len(logcontent)):
            if target_partition_name in logcontent[i]:
                positive_counter = positive_counter + 1
        logfile.close()
        if positive_counter > 0:
            print testcase_mount + ": PASS" + " 0" + " Inapplicable"
        else:
            print testcase_mount + ": FAIL" + " 0" + " Inapplicable" + " - Could not find ext4 partition in mount log!"

    # Test umount command
    ext4_umount_return = commands.getstatusoutput(ext4_umount)
    time.sleep(5)
    if ext4_umount_return[0] != 0:
        print testcase_umount + ": FAIL" + " 0" + " Inapplicable" + " Failures on ext4 partition umount!"
    else:
        commands.getstatusoutput("mount > umount_log.txt 2>&1")
        logfile = open("umount_log.txt", "r")
        logcontent = logfile.readlines()
        positive_counter = 0
        for i in range(0, len(logcontent)):
            if target_partition_name in logcontent[i]:
                positive_counter = positive_counter + 1
        logfile.close()
        if positive_counter > 0:
            print testcase_umount + ": FAIL" + " 0" + " Inapplicable" + " - ext4 partition umount failed from " + mount_point
        else:
            print testcase_umount + ": PASS" + " 0" + " Inapplicable"


def sata_ext4_file_fill_edit():
    testcase_filefill = "sata_ext4_file_fill"
    testcase_fileedit = "sata_ext4_file_edit"
    target_partition_name = device_name + "2"
    mount_point = "/media"
    ext4_mount = "mount " + target_partition_name + " " + mount_point
    ext4_umount = "umount " + mount_point

    # umount everything from mount point - cleaning
    umount_clean = "umount " + mount_point
    commands.getstatusoutput(umount_clean)
    time.sleep(5)
    print "mount point cleaned!"
    commands.getstatusoutput(ext4_mount)
    time.sleep(5)

    # Create a 1MB file with 0x00 filled in
    file_creation_1M = "dd if=/dev/zero of=/media/file_1MB bs=4k count=256"
    file_creation_1M_return = commands.getstatusoutput(file_creation_1M)
    if file_creation_1M_return[0] != 0:
        print testcase_filefill + ": FAIL" + " 0" + " Inapplicable" + " failed to create a 1MB file filled with 0x00!"
    elif os.path.isfile("/media/file_1MB") == False:
        print testcase_filefill + ": FAIL" + " 0" + " Inapplicable" + " file_1MB can not be found!"
    elif os.path.getsize("/media/file_1MB") != 1048576:
        print testcase_filefill + ": FAIL" + " 0" + " Inapplicable" + " file_1MB size incorrect!"
    else:
        print testcase_filefill + ": PASS" + " 0" + " Inapplicable"

    # Create an empty file then write something into it
    testfile_name = "/media/test_file.txt"
    if os.path.isfile(testfile_name) == True:
        os.unlink(testfile_name)
    file_creation_empty = "touch " + testfile_name
    file_creation_empty_return = commands.getstatusoutput(file_creation_empty)
    if file_creation_empty_return[0] != 0:
        print testcase_fileedit + ": FAIL" + " 0" + " Inapplicable" + " failed to create an empty file!"
    elif os.path.isfile(testfile_name) == False:
        print testcase_fileedit + ": FAIL" + " 0" + " Inapplicable" + " " + testfile_name + " can not be found!"
    else:
        os.chmod(testfile_name, stat.S_IRWXU)
        test_string = "This is a test file"
        testfile = open(testfile_name, "a")
        testfile.write(test_string)
        testfile.close()
        # Validating
        testfile = open(testfile_name, "r")
        return_string = testfile.read()
        if return_string != test_string:
            print testcase_fileedit + ": FAIL" + " 0" + " Inapplicable" + " file content doesn't match!"
        else:
            print testcase_fileedit + ": PASS" + " 0" + " Inapplicable"
        testfile.close()

    # umount everything from mount point - cleaning
    umount_clean = "umount " + mount_point
    commands.getstatusoutput(umount_clean)
    time.sleep(5)
    print "mount point cleaned!"


def sata_ext4_dd_write_read():
    testcase_dd_write = "sata_ext4_dd_write"
    testcase_dd_read = "sata_ext4_dd_read"
    target_partition_name = device_name + "2"
    mount_point = "/media"
    ext4_mount = "mount " + target_partition_name + " " + mount_point
    ext4_umount = "umount " + mount_point

    # umount everything from mount point - cleaning
    umount_clean = "umount " + mount_point
    commands.getstatusoutput(umount_clean)
    time.sleep(5)
    print "mount point cleaned!"
    commands.getstatusoutput(ext4_mount)
    time.sleep(5)

    # Test write speed by using dd, direct write
    dd_write_1G = "dd if=/dev/zero of=/media/file_1GB oflag=direct bs=4k count=262144 > dd_write_1GB_stdout.txt 2>&1"
    dd_write_1G_return = commands.getstatusoutput(dd_write_1G)
    if dd_write_1G_return[0] != 0:
        print testcase_dd_write + ": FAIL" + " 0" + " Inapplicable" + " failed to create a 1GB file using dd!"
    elif os.path.isfile("/media/file_1GB") == False:
        print testcase_dd_write + ": FAIL" + " 0" + " Inapplicable" + " file_1GB can not be found!"
    elif os.path.getsize("/media/file_1GB") != 1073741824:
        print testcase_dd_write + ": FAIL" + " 0" + " Inapplicable" + " file_1GB size incorrect!"
    else:
        target_list = []
        writelog = open("dd_write_1GB_stdout.txt", "r")
        writelog_content = writelog.readlines()
        for item in writelog_content:
            if str(1073741824) in item:
                target_list = item.split(",")[-1].strip("\n").split(" ")
                break
        if target_list == []:
            print testcase_dd_write + ": FAIL" + " 0" + " Inapplicable" + " can not find write performance result."
        elif len(target_list) < 2:
            print testcase_dd_write + ": FAIL" + " 0" + " Inapplicable" + " write test result parsing failed, please check the log output!"
        else:
            print testcase_dd_write + ": PASS" + " " + str(target_list[-2]) + " " + target_list[-1]
        writelog.close()

    # Test read speed by using dd, direct read
    if os.path.isfile("/media/file_1GB") == False:
        print testcase_dd_read + ": FAIL" + " 0" + " Inapplicable" + " can not find the target file to read."
    else:
        dd_read_1G = "dd if=/media/file_1GB iflag=direct of=/dev/null bs=4k count=262144 > dd_read_1GB_stdout.txt 2>&1"
        dd_read_1G_return = commands.getstatusoutput(dd_read_1G)
        if dd_read_1G_return[0] != 0:
            print testcase_dd_read + ": FAIL" + " 0" + " Inapplicable" + " failed to read the target file!"
        else:
            target_list = []
            readlog = open("dd_read_1GB_stdout.txt", "r")
            readlog_content = readlog.readlines()
            for item in readlog_content:
                if str(1073741824) in item:
                    target_list = item.split(",")[-1].strip("\n").split(" ")
                    break
            if target_list == []:
                print testcase_dd_read + ": FAIL" + " 0" + " Inapplicable" + " can not find read performance result."
            elif len(target_list) < 2:
                print testcase_dd_read + ": FAIL" + " 0" + " Inapplicable" + " read test result parsing failed, please check the log output!"
            else:
                print testcase_dd_read + ": PASS" + " " + str(target_list[-2]) + " " + target_list[-1]
            readlog.close()

    # umount and clean
    os.unlink("/media/file_1GB")
    umount_clean = "umount " + mount_point
    commands.getstatusoutput(umount_clean)
    time.sleep(5)
    print "mount point cleaned!"

# Run all test
sata_device_existence()
sata_mklabel_msdos()
sata_mklabel_gpt()
sata_first_ext2_partition()
sata_second_ext2_partition()
sata_ext3_format()
sata_ext4_format()
sata_ext4_mount_umount()
sata_ext4_file_fill_edit()
sata_ext4_dd_write_read()