GlennRC | b3202c5 | 2015-08-24 14:43:30 -0700 | [diff] [blame] | 1 | ''' |
| 2 | Wrapper functions for maxIntent |
| 3 | ''' |
| 4 | |
| 5 | import json |
| 6 | import time |
GlennRC | d0e5ed2 | 2015-09-03 11:54:27 -0700 | [diff] [blame^] | 7 | import pexpect |
GlennRC | b3202c5 | 2015-08-24 14:43:30 -0700 | [diff] [blame] | 8 | |
| 9 | def __init__( self ): |
| 10 | self.default = "" |
| 11 | |
| 12 | def getIntents( main, state="INSTALLED", sleep=1, timeout=120 ): |
GlennRC | d0e5ed2 | 2015-09-03 11:54:27 -0700 | [diff] [blame^] | 13 | intents = 0 |
| 14 | try: |
| 15 | cmd = "intents | grep " + state + " | wc -l" |
| 16 | main.log.info("Sending: " + cmd) |
| 17 | main.CLIs[0].handle.sendline(cmd) |
GlennRC | b3202c5 | 2015-08-24 14:43:30 -0700 | [diff] [blame] | 18 | |
GlennRC | d0e5ed2 | 2015-09-03 11:54:27 -0700 | [diff] [blame^] | 19 | time.sleep(sleep) |
GlennRC | b3202c5 | 2015-08-24 14:43:30 -0700 | [diff] [blame] | 20 | |
GlennRC | d0e5ed2 | 2015-09-03 11:54:27 -0700 | [diff] [blame^] | 21 | main.CLIs[0].handle.expect("onos>", timeout=timeout) |
| 22 | raw = main.CLIs[0].handle.before |
| 23 | intents = int(main.CLIs[0].handle.before.split()[7]) |
| 24 | main.log.info(state + "intents: " + str(intents)) |
| 25 | except pexpect.TIMEOUT: |
| 26 | main.log.exception("Timeout exception found") |
| 27 | except pexpect.EOF: |
| 28 | main.log.error("EOF exception found") |
GlennRC | b3202c5 | 2015-08-24 14:43:30 -0700 | [diff] [blame] | 29 | return intents |
| 30 | |
GlennRC | b3202c5 | 2015-08-24 14:43:30 -0700 | [diff] [blame] | 31 | def getFlows( main, state="ADDED", sleep=1, timeout=120 ): |
GlennRC | d0e5ed2 | 2015-09-03 11:54:27 -0700 | [diff] [blame^] | 32 | flows = 0 |
| 33 | try: |
| 34 | cmd = "flows | grep " + state + " | wc -l" |
| 35 | main.log.info("Sending: " + cmd) |
| 36 | main.CLIs[0].handle.sendline(cmd) |
GlennRC | b3202c5 | 2015-08-24 14:43:30 -0700 | [diff] [blame] | 37 | |
GlennRC | d0e5ed2 | 2015-09-03 11:54:27 -0700 | [diff] [blame^] | 38 | time.sleep(sleep) |
GlennRC | b3202c5 | 2015-08-24 14:43:30 -0700 | [diff] [blame] | 39 | |
GlennRC | d0e5ed2 | 2015-09-03 11:54:27 -0700 | [diff] [blame^] | 40 | main.CLIs[0].handle.expect("onos>", timeout=timeout) |
| 41 | raw = main.CLIs[0].handle.before |
| 42 | flows = int(main.CLIs[0].handle.before.split()[7]) |
| 43 | main.log.info(state + "flows: " + str(flows)) |
| 44 | except pexpect.TIMEOUT: |
| 45 | main.log.exception("Timeout exception found") |
| 46 | except pexpect.EOF: |
| 47 | main.log.error("EOF exception found") |
GlennRC | b3202c5 | 2015-08-24 14:43:30 -0700 | [diff] [blame] | 48 | return flows |
| 49 | |
| 50 | |
| 51 | def pushIntents( main, |
| 52 | switch, |
| 53 | ingress, |
| 54 | egress, |
| 55 | batch, |
| 56 | offset, |
| 57 | sleep=1, |
| 58 | options="", |
| 59 | timeout=120): |
| 60 | ''' |
GlennRC | d0e5ed2 | 2015-09-03 11:54:27 -0700 | [diff] [blame^] | 61 | Pushes intents using the push-test-intents cli command. |
GlennRC | b3202c5 | 2015-08-24 14:43:30 -0700 | [diff] [blame] | 62 | ''' |
GlennRC | d0e5ed2 | 2015-09-03 11:54:27 -0700 | [diff] [blame^] | 63 | try: |
| 64 | cmd = "push-test-intents " + options + " " + switch + ingress + " " +\ |
| 65 | switch + egress + " " + str(batch) + " " + str(offset) |
| 66 | main.log.info("Installing " + str(offset+batch) + " intents") |
| 67 | main.log.debug("Sending: " + cmd) |
| 68 | main.CLIs[0].handle.sendline(cmd) |
| 69 | time.sleep(sleep) |
| 70 | main.CLIs[0].handle.expect("onos>", timeout=timeout) |
GlennRC | b3202c5 | 2015-08-24 14:43:30 -0700 | [diff] [blame] | 71 | |
GlennRC | d0e5ed2 | 2015-09-03 11:54:27 -0700 | [diff] [blame^] | 72 | raw = main.CLIs[0].handle.before |
| 73 | if "Failure:" not in raw and "GC" not in raw: |
| 74 | return main.TRUE |
| 75 | except pexpect.TIMEOUT: |
| 76 | main.log.exception("Timeout exception found") |
| 77 | except pexpect.EOF: |
| 78 | main.log.error("EOF exception found") |
| 79 | return main.FALSE |
GlennRC | b3202c5 | 2015-08-24 14:43:30 -0700 | [diff] [blame] | 80 | |
| 81 | def verifyFlows( main, expectedFlows, state="ADDED", sleep=1, timeout=120): |
| 82 | ''' |
| 83 | This function returns main.TRUE if the number of expected flows are in |
| 84 | the specified state |
| 85 | |
| 86 | @params |
| 87 | expectedFlows: the flows you expect to see in the specified state |
| 88 | state: the state of the flow to check for |
| 89 | timeout: the timeout for pexpect |
| 90 | ''' |
| 91 | cmd = "flows | grep " + state + " | wc -l" |
| 92 | for i in range(10): |
| 93 | flows = getFlows( main, state, sleep, timeout ) |
| 94 | if expectedFlows == flows: |
| 95 | return main.TRUE |
| 96 | |
| 97 | return main.FALSE |
| 98 | |
| 99 | def verifyIntents( main, expectedIntents, state="INSTALLED", sleep=1, timeout=120): |
| 100 | ''' |
| 101 | This function returns main.TRUE if the number of expected intents are in |
| 102 | the specified state |
| 103 | |
| 104 | @params |
| 105 | expectedFlows: the intents you expect to see in the specified state |
| 106 | state: the state of the intent to check for |
| 107 | timeout: the timeout for pexpect |
| 108 | ''' |
| 109 | cmd = "intents | grep " + state + " | wc -l" |
| 110 | for i in range(10): |
| 111 | intents = getIntents( main, state, sleep, timeout ) |
| 112 | if expectedIntents == intents: |
| 113 | return main.TRUE |
| 114 | |
| 115 | return main.FALSE |