Skip to content

Commit

Permalink
Merge branch 'ThrowsException-variables'
Browse files Browse the repository at this point in the history
  • Loading branch information
landro committed May 24, 2018
2 parents 5a539ca + 4c7be77 commit a5c289d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
38 changes: 38 additions & 0 deletions examples/example_irule_variables.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package require -exact testcl 1.0.10
namespace import ::testcl::*

##
# Example demonstrating how to use TesTcl
# This is how you should write your tests
#
# To run example
#
# export TCLLIBPATH=/parent/dir/of/this/file
#
# and run the following command from /parent/dir/of/this/file
#
# jtcl examples/example_irule_variables.tcl
#
##

# Comment out to suppress logging
#log::lvSuppressLE info 0

before {
event HTTP_REQUEST
}

it "should set pool to foo when status is 1" {
endstate pool foo
array set variables { status 1 }
run irules/variables_irule.tcl simple variables
}

it "should set pool to bar when status is 0" {
endstate pool bar
array set variables { status 0 }
run irules/variables_irule.tcl simple variables
}


stats
11 changes: 11 additions & 0 deletions irules/variables_irule.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
rule simple {

when HTTP_REQUEST priority 100 {
# set variable outside irule
if { $status eq 1 } {
pool foo
} else {
pool bar
}
}
}
13 changes: 12 additions & 1 deletion src/onirule.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package require log

namespace eval ::testcl {
variable expectedEvent
variable variables
namespace export rule
namespace export when
namespace export event
Expand Down Expand Up @@ -65,6 +66,13 @@ proc ::testcl::when args {
set body [lindex $args end]
}

variable variables
if [ info exists variables ] {
foreach { key value } [ array get variables ] {
uplevel 0 {set $key $value}
}
}

variable expectedEvent

if {[info exists expectedEvent] && $event eq $expectedEvent} {
Expand Down Expand Up @@ -150,8 +158,11 @@ proc ::testcl::event {event_type} {
#
# Results:
# none
proc ::testcl::run {irule rulename} {
proc ::testcl::run {irule rulename {vars {}}} {
log::log info "Running irule $irule"
variable variables
upvar 1 $vars a
array set variables [array get a]
set rc [catch {source $irule} result]
if { 0 != $rc } {
log::log error "Running irule $irule failed: $result"
Expand Down
16 changes: 16 additions & 0 deletions test/test_irule_variables.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
source src/on.tcl
source src/assert.tcl
source src/onirule.tcl
namespace import ::testcl::*

# Comment out to suppress logging
#log::lvSuppressLE info 0

event HTTP_REQUEST

on pool bar return ""

endstate pool foo

array set variables { status 1 }
run irules/variables_irule.tcl simple variables

1 comment on commit a5c289d

@snorrebrandstadmoen
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Please sign in to comment.