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