Skip to content

Commit

Permalink
Merge pull request #3313 from terraform-providers/t-aws_sns_platform_…
Browse files Browse the repository at this point in the history
…application-support-file-contents

test/resource/aws_sns_platform_application: Support APNS private key and certificate file contents
  • Loading branch information
bflad authored Feb 12, 2018
2 parents e928290 + bf818b5 commit c574356
Showing 1 changed file with 34 additions and 48 deletions.
82 changes: 34 additions & 48 deletions aws/resource_aws_sns_platform_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package aws

import (
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strconv"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
homedir "github.com/mitchellh/go-homedir"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sns"
Expand All @@ -27,44 +27,42 @@ import (
**/

type testAccAwsSnsPlatformApplicationPlatform struct {
Name string
Credential string
CredentialHash string
Principal string
PrincipalHash string
Name string
Credential string
Principal string
}

func testAccAwsSnsPlatformApplicationPlatformFromEnv(t *testing.T) []*testAccAwsSnsPlatformApplicationPlatform {
platforms := make([]*testAccAwsSnsPlatformApplicationPlatform, 0, 2)

if os.Getenv("APNS_SANDBOX_CREDENTIAL_PATH") != "" {
if os.Getenv("APNS_SANDBOX_PRINCIPAL_PATH") == "" {
t.Fatalf("APNS_SANDBOX_CREDENTIAL_PATH set but missing APNS_SANDBOX_PRINCIPAL_PATH")
if os.Getenv("APNS_SANDBOX_CREDENTIAL") != "" {
if os.Getenv("APNS_SANDBOX_PRINCIPAL") == "" {
t.Fatalf("APNS_SANDBOX_CREDENTIAL set but missing APNS_SANDBOX_PRINCIPAL")
}
credentialHash, err := testAccHashSumPath(os.Getenv("APNS_SANDBOX_CREDENTIAL_PATH"))
if err != nil {
t.Fatal(err)

platform := &testAccAwsSnsPlatformApplicationPlatform{
Name: "APNS_SANDBOX",
Credential: fmt.Sprintf("<<EOF\n%s\nEOF\n", strings.TrimSpace(os.Getenv("APNS_SANDBOX_CREDENTIAL"))),
Principal: fmt.Sprintf("<<EOF\n%s\nEOF\n", strings.TrimSpace(os.Getenv("APNS_SANDBOX_PRINCIPAL"))),
}
principalHash, err := testAccHashSumPath(os.Getenv("APNS_SANDBOX_PRINCIPAL_PATH"))
if err != nil {
t.Fatal(err)
platforms = append(platforms, platform)
} else if os.Getenv("APNS_SANDBOX_CREDENTIAL_PATH") != "" {
if os.Getenv("APNS_SANDBOX_PRINCIPAL_PATH") == "" {
t.Fatalf("APNS_SANDBOX_CREDENTIAL_PATH set but missing APNS_SANDBOX_PRINCIPAL_PATH")
}

platform := &testAccAwsSnsPlatformApplicationPlatform{
Name: "APNS_SANDBOX",
Credential: fmt.Sprintf("${file(pathexpand(%q))}", os.Getenv("APNS_SANDBOX_CREDENTIAL_PATH")),
CredentialHash: credentialHash,
Principal: fmt.Sprintf("${file(pathexpand(%q))}", os.Getenv("APNS_SANDBOX_PRINCIPAL_PATH")),
PrincipalHash: principalHash,
Name: "APNS_SANDBOX",
Credential: strconv.Quote(fmt.Sprintf("${file(pathexpand(%q))}", os.Getenv("APNS_SANDBOX_CREDENTIAL_PATH"))),
Principal: strconv.Quote(fmt.Sprintf("${file(pathexpand(%q))}", os.Getenv("APNS_SANDBOX_PRINCIPAL_PATH"))),
}
platforms = append(platforms, platform)
}

if os.Getenv("GCM_API_KEY") != "" {
platform := &testAccAwsSnsPlatformApplicationPlatform{
Name: "GCM",
Credential: os.Getenv("GCM_API_KEY"),
CredentialHash: hashSum(os.Getenv("GCM_API_KEY")),
Name: "GCM",
Credential: strconv.Quote(os.Getenv("GCM_API_KEY")),
}
platforms = append(platforms, platform)
}
Expand All @@ -75,18 +73,6 @@ func testAccAwsSnsPlatformApplicationPlatformFromEnv(t *testing.T) []*testAccAws
return platforms
}

func testAccHashSumPath(path string) (string, error) {
path, err := homedir.Expand(path)
if err != nil {
return "", err
}
data, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return hashSum(string(data)), nil
}

func TestDecodeResourceAwsSnsPlatformApplicationID(t *testing.T) {

var testCases = []struct {
Expand Down Expand Up @@ -168,7 +154,7 @@ func TestAccAwsSnsPlatformApplication_basic(t *testing.T) {
name := fmt.Sprintf("tf-acc-%d", acctest.RandInt())
platformPrincipalCheck := resource.TestCheckNoResourceAttr(resourceName, "platform_principal")
if platform.Principal != "" {
platformPrincipalCheck = resource.TestCheckResourceAttr(resourceName, "platform_principal", platform.PrincipalHash)
platformPrincipalCheck = resource.TestCheckResourceAttrSet(resourceName, "platform_principal")
}

t.Run(platform.Name, func(*testing.T) {
Expand All @@ -180,16 +166,16 @@ func TestAccAwsSnsPlatformApplication_basic(t *testing.T) {
{
Config: testAccAwsSnsPlatformApplicationConfig_basic(name, &testAccAwsSnsPlatformApplicationPlatform{
Name: "APNS",
Credential: "NOTEMPTY",
Principal: "",
Credential: strconv.Quote("NOTEMPTY"),
Principal: strconv.Quote(""),
}),
ExpectError: regexp.MustCompile(`platform_principal is required when platform =`),
},
{
Config: testAccAwsSnsPlatformApplicationConfig_basic(name, &testAccAwsSnsPlatformApplicationPlatform{
Name: "APNS_SANDBOX",
Credential: "NOTEMPTY",
Principal: "",
Credential: strconv.Quote("NOTEMPTY"),
Principal: strconv.Quote(""),
}),
ExpectError: regexp.MustCompile(`platform_principal is required when platform =`),
},
Expand All @@ -200,7 +186,7 @@ func TestAccAwsSnsPlatformApplication_basic(t *testing.T) {
resource.TestMatchResourceAttr(resourceName, "arn", regexp.MustCompile(fmt.Sprintf("^arn:[^:]+:sns:[^:]+:[^:]+:app/%s/%s$", platform.Name, name))),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "platform", platform.Name),
resource.TestCheckResourceAttr(resourceName, "platform_credential", platform.CredentialHash),
resource.TestCheckResourceAttrSet(resourceName, "platform_credential"),
platformPrincipalCheck,
),
},
Expand Down Expand Up @@ -430,16 +416,16 @@ func testAccAwsSnsPlatformApplicationConfig_basic(name string, platform *testAcc
resource "aws_sns_platform_application" "test" {
name = "%s"
platform = "%s"
platform_credential = "%s"
platform_credential = %s
}
`, name, platform.Name, platform.Credential)
}
return fmt.Sprintf(`
resource "aws_sns_platform_application" "test" {
name = "%s"
platform = "%s"
platform_credential = "%s"
platform_principal = "%s"
platform_credential = %s
platform_principal = %s
}
`, name, platform.Name, platform.Credential, platform.Principal)
}
Expand All @@ -450,7 +436,7 @@ func testAccAwsSnsPlatformApplicationConfig_basicAttribute(name string, platform
resource "aws_sns_platform_application" "test" {
name = "%s"
platform = "%s"
platform_credential = "%s"
platform_credential = %s
%s = "%s"
}
`, name, platform.Name, platform.Credential, attributeKey, attributeValue)
Expand All @@ -459,8 +445,8 @@ resource "aws_sns_platform_application" "test" {
resource "aws_sns_platform_application" "test" {
name = "%s"
platform = "%s"
platform_credential = "%s"
platform_principal = "%s"
platform_credential = %s
platform_principal = %s
%s = "%s"
}
`, name, platform.Name, platform.Credential, platform.Principal, attributeKey, attributeValue)
Expand Down

0 comments on commit c574356

Please sign in to comment.