Skip to content

Commit

Permalink
Added attributes runscope_step.assertion, runscope_step.header, runsc…
Browse files Browse the repository at this point in the history
…ope_step.variable (#2)
  • Loading branch information
alxrem committed Mar 29, 2021
1 parent 1f696bd commit 1e8ddcf
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 30 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ NOTES:

* Deprecated attributes `remote_agents` and `emails` of `runscope_environment`.
Use `remote_agent` and `email` instead.
* Deprecated attribute `variables`, `assertions` and `headers` of `runscope_step`.
Use `variable` instead.

ENHANCEMENTS:

* Added attributes `remote_agent` and `email` of `runscope_environment`.
* Added attribute `variable`, `assertion` and `header` of `runscope_step`.

## 0.7.0 (March 27, 2021)

Expand Down
133 changes: 122 additions & 11 deletions runscope/resource_runscope_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,30 @@ func resourceRunscopeStep() *schema.Resource {
},
},
},
Optional: true,
Optional: true,
ConflictsWith: []string{"variable"},
Deprecated: "use variable instead",
},
"variable": {
Type: schema.TypeSet,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"property": {
Type: schema.TypeString,
Optional: true,
},
"source": {
Type: schema.TypeString,
Required: true,
},
},
},
Optional: true,
ConflictsWith: []string{"variables"},
},
// TODO: rename to "assertion" for better UX
"assertions": {
Expand All @@ -142,7 +165,34 @@ func resourceRunscopeStep() *schema.Resource {
},
},
},
Optional: true,
Optional: true,
ConflictsWith: []string{"assertion"},
Deprecated: "use assertion instead",
},
"assertion": {
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"source": {
Type: schema.TypeString,
Required: true,
},
"property": {
Type: schema.TypeString,
Optional: true,
},
"comparison": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Optional: true,
},
},
},
Optional: true,
ConflictsWith: []string{"assertions"},
},
// TODO: rename to "header" for better UX
"headers": {
Expand All @@ -160,6 +210,25 @@ func resourceRunscopeStep() *schema.Resource {
},
},
},
ConflictsWith: []string{"header"},
Deprecated: "use header instead",
},
"header": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"header": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
},
ConflictsWith: []string{"headers"},
},
"auth": {
Type: schema.TypeSet,
Expand Down Expand Up @@ -248,9 +317,21 @@ func resourceStepRead(d *schema.ResourceData, meta interface{}) error {
d.Set("method", step.Method)
d.Set("url", step.URL)
d.Set("body", step.Body)
d.Set("variables", readVariables(step.Variables))
d.Set("assertions", readAssertions(step.Assertions))
d.Set("headers", readHeaders(step.Headers))
if _, ok := d.GetOk("variables"); ok {
d.Set("variables", readVariables(step.Variables))
} else {
d.Set("variable", readVariables(step.Variables))
}
if _, ok := d.GetOk("assertions"); ok {
d.Set("assertions", readAssertions(step.Assertions))
} else {
d.Set("assertion", readAssertions(step.Assertions))
}
if _, ok := d.GetOk("headers"); ok {
d.Set("headers", readHeaders(step.Headers))
} else {
d.Set("header", readHeaders(step.Headers))
}
d.Set("scripts", step.Scripts)
d.Set("before_scripts", step.BeforeScripts)
d.Set("note", step.Note)
Expand All @@ -276,7 +357,10 @@ func resourceStepUpdate(d *schema.ResourceData, meta interface{}) error {

if d.HasChange("url") ||
d.HasChange("variables") ||
d.HasChange("variable") ||
d.HasChange("assertion") ||
d.HasChange("assertions") ||
d.HasChange("header") ||
d.HasChange("headers") ||
d.HasChange("body") ||
d.HasChange("note") {
Expand Down Expand Up @@ -323,7 +407,16 @@ func createStepFromResourceData(d *schema.ResourceData) (*runscope.TestStep, str
step.URL = attr.(string)
}

if attr, ok := d.GetOk("variables"); ok {
func() {
var attr interface{}
var ok bool

if attr, ok = d.GetOk("variables"); !ok {
if attr, ok = d.GetOk("variable"); !ok {
return
}
}

variables := []*runscope.Variable{}
items := attr.(*schema.Set)
for _, x := range items.List() {
Expand All @@ -337,7 +430,7 @@ func createStepFromResourceData(d *schema.ResourceData) (*runscope.TestStep, str
variables = append(variables, &variable)
}
step.Variables = variables
}
}()

if v, _ := d.GetOk("auth"); v != nil {
authSet := v.(*schema.Set).List()
Expand All @@ -351,7 +444,16 @@ func createStepFromResourceData(d *schema.ResourceData) (*runscope.TestStep, str
}
}

if attr, ok := d.GetOk("assertions"); ok {
func() {
var attr interface{}
var ok bool

if attr, ok = d.GetOk("assertions"); !ok {
if attr, ok = d.GetOk("assertion"); !ok {
return
}
}

assertions := []*runscope.Assertion{}
items := attr.([]interface{})
for _, x := range items {
Expand All @@ -367,17 +469,26 @@ func createStepFromResourceData(d *schema.ResourceData) (*runscope.TestStep, str
}

step.Assertions = assertions
}
}()

func() {
var attr interface{}
var ok bool

if attr, ok = d.GetOk("headers"); !ok {
if attr, ok = d.GetOk("header"); !ok {
return
}
}

if attr, ok := d.GetOk("headers"); ok {
step.Headers = make(map[string][]string)
items := attr.(*schema.Set)
for _, x := range items.List() {
item := x.(map[string]interface{})
header := item["header"].(string)
step.Headers[header] = append(step.Headers[header], item["value"].(string))
}
}
}()

if attr, ok := d.GetOk("scripts"); ok {
step.Scripts = expandStringList(attr.([]interface{}))
Expand Down
85 changes: 85 additions & 0 deletions runscope/resource_runscope_step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ func TestAccStep_basic(t *testing.T) {
testAccCheckStepMainPageExists("runscope_step.main_page"),
resource.TestCheckResourceAttr(
"runscope_step.main_page", "url", "http://example.com"),
resource.TestCheckResourceAttr("runscope_step.main_page", "variables.#", "2"),
resource.TestCheckResourceAttr("runscope_step.main_page", "assertions.#", "2"),
resource.TestCheckResourceAttr("runscope_step.main_page", "headers.#", "3"),
),
},
{
Config: fmt.Sprintf(testRunscopeStepConfigAWithSingulars, teamID),
Check: resource.ComposeTestCheckFunc(
testAccCheckStepMainPageExists("runscope_step.main_page"),
resource.TestCheckResourceAttr(
"runscope_step.main_page", "url", "http://example.com"),
resource.TestCheckResourceAttr("runscope_step.main_page", "variable.#", "2"),
resource.TestCheckResourceAttr("runscope_step.main_page", "assertion.#", "2"),
resource.TestCheckResourceAttr("runscope_step.main_page", "header.#", "3"),
),
},
},
Expand Down Expand Up @@ -312,6 +326,77 @@ resource "runscope_bucket" "bucket" {
}
`

const testRunscopeStepConfigAWithSingulars = `
resource "runscope_step" "main_page" {
bucket_id = "${runscope_bucket.bucket.id}"
test_id = "${runscope_test.test.id}"
step_type = "request"
note = "Testing step, single step test"
url = "http://example.com"
method = "GET"
variable {
name = "httpStatus"
source = "response_status"
}
variable {
name = "httpContentEncoding"
source = "response_header"
property = "Content-Encoding"
}
assertion {
source = "response_status"
comparison = "equal_number"
value = "200"
}
assertion {
source = "response_json"
comparison = "equal"
value = "c5baeb4a-2379-478a-9cda-1b671de77cf9"
property = "data.id"
}
header {
header = "Accept-Encoding"
value = "application/json"
}
header {
header = "Accept-Encoding"
value = "application/xml"
}
header {
header = "Authorization"
value = "Bearer bb74fe7b-b9f2-48bd-9445-bdc60e1edc6a"
}
auth {
username = "user"
auth_type = "basic"
password = "password1"
}
scripts = [
"log(\"script 1\");",
"log(\"script 2\");",
]
before_scripts = [
"log(\"before script\");",
]
}
resource "runscope_test" "test" {
bucket_id = "${runscope_bucket.bucket.id}"
name = "runscope test"
description = "This is a test test..."
}
resource "runscope_bucket" "bucket" {
name = "terraform-provider-test"
team_uuid = "%s"
}
`

const testRunscopeStepConfigMultipleSteps = `
resource "runscope_step" "step_a" {
bucket_id = "${runscope_bucket.bucket.id}"
Expand Down
Loading

0 comments on commit 1e8ddcf

Please sign in to comment.