You Wang | 782ff33 | 2017-02-21 17:13:04 -0800 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | ''' |
| 3 | This script uses pexpect to talk to DELTA manager and triggers all CONTROL_PLANE_OF and |
| 4 | ADVANCED test cases. It also reads the DELTA log and prints the results for each case |
| 5 | ''' |
| 6 | |
| 7 | import sys |
| 8 | import pexpect |
| 9 | import time |
| 10 | import datetime |
| 11 | |
| 12 | DELTA_DIR = '/home/sdn/DELTA' |
| 13 | DELTA_LOG = 'delta.log' |
| 14 | RESULT_FILE = 'summary.txt' |
| 15 | LOG_CHECK_INTERVAL = 10 |
| 16 | # TODO: get attack codes from DELTA CLI |
| 17 | CODES = ['2.1.010','2.1.020','2.1.030','2.1.040','2.1.050','2.1.060','2.1.070','2.1.071','2.1.072','2.1.073','2.1.080','3.1.010','3.1.020','3.1.030','3.1.040','3.1.050','3.1.060','3.1.070','3.1.080','3.1.090','3.1.100','3.1.110','3.1.120','3.1.130','3.1.140','3.1.150','3.1.160','3.1.170','3.1.180','3.1.190','3.1.200'] |
| 18 | CODE_TO_SKIP = ['3.1.090','3.1.160'] |
| 19 | # Timeout for each test case |
| 20 | TIMEOUT = 1800 |
| 21 | |
| 22 | def triggerTest( handle, code ): |
| 23 | testAvailable = True |
| 24 | print datetime.datetime.now(), "Starting test", code |
| 25 | # TODO: expect Exceptions thrown by DELTA |
| 26 | i = handle.expect( ['Command>', pexpect.EOF, pexpect.TIMEOUT], 60 ) |
| 27 | if i != 0: |
| 28 | print "pexpect EOF or TIMEOUT, exiting..." |
| 29 | return -1 |
| 30 | time.sleep(0.5) |
| 31 | |
| 32 | handle.sendline( 'k' ) |
| 33 | i = handle.expect( ['Select the attack code>', pexpect.EOF, pexpect.TIMEOUT], 60 ) |
| 34 | if i != 0: |
| 35 | print "pexpect EOF or TIMEOUT, exiting..." |
| 36 | return -1 |
| 37 | time.sleep(0.5) |
| 38 | |
| 39 | handle.sendline( code ) |
| 40 | i = handle.expect( ['not available', 'Press ENTER key to continue..', pexpect.EOF, pexpect.TIMEOUT], 60 ) |
| 41 | if i == 0: |
| 42 | testAvailable = False |
| 43 | elif i == 1: |
| 44 | testAvailable = True |
| 45 | else: |
| 46 | print "pexpect EOF or TIMEOUT, exiting..." |
| 47 | return -1 |
| 48 | time.sleep(0.5) |
| 49 | |
| 50 | handle.sendline( '' ) |
| 51 | if not testAvailable: |
| 52 | print "Test", code, "is not available" |
| 53 | return 0 |
| 54 | |
| 55 | return 1 |
| 56 | |
| 57 | def waitForTest( code ): |
| 58 | startTime = time.time() |
| 59 | while True: |
| 60 | if time.time() - startTime > TIMEOUT: |
| 61 | print "Test timeout, exiting..." |
| 62 | return -1 |
| 63 | time.sleep( LOG_CHECK_INTERVAL ) |
| 64 | log = open( DELTA_LOG ).read() |
| 65 | log = log.split( code ) |
| 66 | if len( log ) == 1: |
| 67 | pass |
| 68 | elif "done" in log[-1]: |
| 69 | try: |
| 70 | testName = log[1].split( ' - ' )[1] |
| 71 | except IndexError: |
| 72 | print "Error getting test name" |
| 73 | testName = "Unknown Test Name" |
| 74 | result = "UNKNOWN" |
| 75 | if "FAIL" in log[-1]: |
| 76 | result = "FAIL" |
| 77 | elif "PASS" in log[-1]: |
| 78 | result = "PASS" |
| 79 | print datetime.datetime.now(), "Test result:", result, "Time taken:", time.time() - startTime, "seconds" |
| 80 | resultFile = open( RESULT_FILE, 'a' ) |
| 81 | resultFile.write( code + " " + testName + ": " + result + "\n" ) |
| 82 | resultFile.close() |
| 83 | return 1 |
| 84 | else: |
| 85 | pass |
| 86 | |
| 87 | def runTests(): |
| 88 | resultFile = open( RESULT_FILE, 'w' ) |
| 89 | resultFile.write( "Test started on " + str(datetime.datetime.now())+"\n" ) |
| 90 | resultFile.close() |
| 91 | handle=pexpect.spawn( 'java -jar ' + DELTA_DIR + '/manager/target/delta-manager-1.0-SNAPSHOT-jar-with-dependencies.jar ' + DELTA_DIR + '/tools/config/manager.cfg' ) |
| 92 | for code in CODES: |
| 93 | # Skip some broken cases |
| 94 | if code in CODE_TO_SKIP: |
| 95 | continue |
| 96 | triggerResult = triggerTest( handle, code ) |
| 97 | # pexpect failures |
| 98 | if triggerResult == -1: |
| 99 | return |
| 100 | # Test not available |
| 101 | elif triggerResult == 0: |
| 102 | continue |
| 103 | testResult = waitForTest( code ) |
| 104 | # Test timed out |
| 105 | if testResult == -1: |
| 106 | break |
| 107 | # Exit DELTA |
| 108 | print "All tests done, exiting DELTA" |
| 109 | i = handle.expect( ['Command>', pexpect.EOF, pexpect.TIMEOUT], 60 ) |
| 110 | handle.sendline( 'q' ) |
| 111 | |
| 112 | if __name__ == '__main__': |
| 113 | if len( sys.argv ) >= 2: |
| 114 | DELTA_DIR = sys.argv[1] |
| 115 | print 'DELTA directory is', DELTA_DIR |
| 116 | runTests() |