Skip to content

Commit

Permalink
add some bits to the test to reproduce the error
Browse files Browse the repository at this point in the history
  • Loading branch information
FraDiBen committed Aug 14, 2019
1 parent cdc7361 commit fcc823c
Showing 1 changed file with 131 additions and 2 deletions.
133 changes: 131 additions & 2 deletions tests/e2e/gray_box_cdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ definitions:
type: "string"
`

const swaggerContent = `swagger: "2.0"
const swaggerREADONLYpropertyContent = `swagger: "2.0"
host: %s
schemes:
- "http"
Expand Down Expand Up @@ -268,6 +268,27 @@ paths:
schema:
$ref: "#/definitions/ContentDeliveryNetworkV1"
put:
summary: "Updated cdn"
operationId: "ContentDeliveryNetworkUpdateV1"
parameters:
- name: "id"
in: "path"
description: "cdn that needs to be updated"
required: true
type: "string"
- in: "body"
name: "body"
description: "Updated cdn object"
required: true
schema:
$ref: "#/definitions/ContentDeliveryNetworkV1"
responses:
200:
description: "successful operation"
schema:
$ref: "#/definitions/ContentDeliveryNetworkV1"
definitions:
ContentDeliveryNetworkV1:
type: "object"
Expand Down Expand Up @@ -442,7 +463,7 @@ func (a *api) apiResponse(t *testing.T, responseBody string, httpResponseStatusC
}

func TestAccCDN_Create_and_UpdateSubResource(t *testing.T) {
api := initAPI(t, swaggerContent)
api := initAPI(t, cdnSwaggerYAMLTemplate)
tfFileContents := createTerraformFile(expectedCDNLabel, expectedCDNFirewallLabel)

p := openapi.ProviderOpenAPI{ProviderName: providerName}
Expand Down Expand Up @@ -506,6 +527,70 @@ func TestAccCDN_Create_and_UpdateSubResource(t *testing.T) {
//assert.Equal(t, "/v1/cdns/42/v1/firewalls/1337", secondToLastRequest.URL.Path)
}

func TestAccCDN_Create_and_UpdateSubResource_WITH_READONLY(t *testing.T) {
api := initAPI(t, swaggerREADONLYpropertyContent)
tfFileContents := createTerraformFileREADONLY(expectedCDNLabel)

p := openapi.ProviderOpenAPI{ProviderName: providerName}
provider, err := p.CreateSchemaProviderFromServiceConfiguration(&openapi.ServiceConfigStub{SwaggerURL: api.swaggerURL})
assert.NoError(t, err)
assertProviderSchemaForReadONLY(t, provider)

resourceInstancesToCheck := map[string]string{
openAPIResourceNameCDN: fmt.Sprintf("%s/v1/cdns", api.apiHost),
}

var testAccProviders = map[string]terraform.ResourceProvider{providerName: provider}
resource.Test(t, resource.TestCase{
IsUnitTest: true,
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t, api.swaggerURL) },
CheckDestroy: testAccCheckDestroy(resourceInstancesToCheck),
Steps: []resource.TestStep{
{ // STEP 0 : create a Terraform State which has a Value for the readOnly property called `read_only`
Config: tfFileContents,
Check: resource.ComposeTestCheckFunc(
testAccCheckWhetherResourceExist(resourceInstancesToCheck),
resource.TestCheckResourceAttr(
openAPIResourceStateCDN, "label", expectedCDNLabel),
resource.TestCheckResourceAttr(
"openapi_cdn_v1.my_cdn", "object_nested_scheme_property.0.name", "hello"),
resource.TestCheckResourceAttr(
"openapi_cdn_v1.my_cdn", "object_nested_scheme_property.0.object_property.read_only", "bub"),
),
},
{ //STEP 1: Using a new configuration widtout readOnly property keeps `read_only` = "bub"
Config: createTerraformFileREADONLY_UPDATE("updatedCDNLabel"),
Check: resource.ComposeTestCheckFunc(
testAccCheckWhetherResourceExist(resourceInstancesToCheck),
resource.TestCheckResourceAttr(
openAPIResourceStateCDN, "label", "updatedCDNLabel"),
resource.TestCheckResourceAttr(
"openapi_cdn_v1.my_cdn", "object_nested_scheme_property.0.name", "hello"),
//resource.TestCheckResourceAttr(
// "openapi_cdn_v1.my_cdn", "object_nested_scheme_property.0.object_property.read_only", "bub"),
//resource.TestCheckResourceAttr(
// "openapi_cdn_v1.my_cdn", "object_nested_scheme_property.0.object_property.account", "im new here, but you should still see read_only : bub"),
),
},
{ //STEP 2: Using a new configuration widtout readOnly property keeps `read_only` = "bub"
Config: createTerraformFileREADONLY_UPDATE2("updatedCDNLabel"),
Check: resource.ComposeTestCheckFunc(
testAccCheckWhetherResourceExist(resourceInstancesToCheck),
resource.TestCheckResourceAttr(
openAPIResourceStateCDN, "label", "updatedCDNLabel"),
resource.TestCheckResourceAttr(
"openapi_cdn_v1.my_cdn", "object_nested_scheme_property.0.name", "hello"),
resource.TestCheckResourceAttr(
"openapi_cdn_v1.my_cdn", "object_nested_scheme_property.0.object_property.read_only", "bub"),
resource.TestCheckResourceAttr(
"openapi_cdn_v1.my_cdn", "object_nested_scheme_property.0.object_property.account", "im new here, but you should still see read_only : bub"),
),
},
},
})
}

func TestAcc_Create_MissingRequiredParentPropertyInTFConfigurationFile(t *testing.T) {
api := initAPI(t, cdnSwaggerYAMLTemplate)

Expand Down Expand Up @@ -597,6 +682,11 @@ func assertProviderSchema(t *testing.T, provider *schema.Provider) {
assert.Nil(t, provider.ResourcesMap[openAPIResourceNameCDNFirewall].Schema["cdns_v1_id"])
}

func assertProviderSchemaForReadONLY(t *testing.T, provider *schema.Provider) {
assert.Nil(t, provider.ResourcesMap[openAPIResourceNameCDN].Schema["id"])
assert.NotNil(t, provider.ResourcesMap[openAPIResourceNameCDN].Schema["label"])
}

func createTerraformFile(expectedCDNLabel, expectedFirewallLabel string) string {
return fmt.Sprintf(`
# URI /v1/cdns/
Expand All @@ -609,3 +699,42 @@ func createTerraformFile(expectedCDNLabel, expectedFirewallLabel string) string
label = "%s"
}`, openAPIResourceNameCDN, openAPIResourceInstanceNameCDN, expectedCDNLabel, openAPIResourceNameCDNFirewall, openAPIResourceInstanceNameCDNFirewall, openAPIResourceStateCDN, expectedFirewallLabel)
}

func createTerraformFileREADONLY(expectedCDNLabel string) string {
return fmt.Sprintf(`
# URI /v1/cdns/
resource "%s" "%s" {
label = "%s"
object_nested_scheme_property {
name = "hello"
object_property = {
read_only = "bub"
}
}
}`, openAPIResourceNameCDN, openAPIResourceInstanceNameCDN, expectedCDNLabel)
}

func createTerraformFileREADONLY_UPDATE(expectedCDNLabel string) string {
return fmt.Sprintf(`
# URI /v1/cdns/
resource "%s" "%s" {
label = "%s"
object_nested_scheme_property {
object_property = {
account = "im new here, but you should still see read_only : bub"
}
}
}`, openAPIResourceNameCDN, openAPIResourceInstanceNameCDN, expectedCDNLabel)
}

func createTerraformFileREADONLY_UPDATE2(expectedCDNLabel string) string {
return fmt.Sprintf(`
# URI /v1/cdns/
resource "%s" "%s" {
label = "%s"
object_nested_scheme_property {
object_property = {
}
}
}`, openAPIResourceNameCDN, openAPIResourceInstanceNameCDN, expectedCDNLabel)
}

0 comments on commit fcc823c

Please sign in to comment.