Merge pull request #166 from cloudskiff/acc_test_fixes

Fix acceptance test issues
main
William BEUIL 2021-01-29 18:40:36 +01:00 committed by GitHub
commit 78f1e40acc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 52 additions and 8 deletions

View File

@ -133,7 +133,9 @@ Acceptance tests need credentials to perform real world action on cloud provider
Recommended way to run acc tests is to use two distinct credentials:
one for terraform related actions, and one for driftctl scan.
You can override environment variables passed to terraform operations by adding `ACC_` prefix on env variables.
In our acceptance tests, we may need read/write permissions during specific contexts
(e.g. terraform init, apply, destroy)or lifecycle (PreExec and PostExec).
If needed, you can override environment variables in those contexts by adding `ACC_` prefix on env variables.
#### AWS

View File

@ -20,6 +20,9 @@ func TestAcc_AwsInstance_WithBlockDevices(t *testing.T) {
Args: []string{"scan", "--filter", "Type=='aws_instance'"},
Checks: []acceptance.AccCheck{
{
Env: map[string]string{
"AWS_REGION": "us-east-1",
},
Check: func(result *acceptance.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)
@ -28,6 +31,9 @@ func TestAcc_AwsInstance_WithBlockDevices(t *testing.T) {
},
},
{
Env: map[string]string{
"AWS_REGION": "us-east-1",
},
PreExec: func() {
client := ec2.New(awsutils.Session())
response, err := client.DescribeInstances(&ec2.DescribeInstancesInput{
@ -49,11 +55,11 @@ func TestAcc_AwsInstance_WithBlockDevices(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(response.Reservations[0].Instances) != 1 {
if len(response.Reservations) != 1 || len(response.Reservations[0].Instances) != 1 {
t.Fatal("Error, unexpected number of instances found, manual check required")
}
mutatedInstanceId = *response.Reservations[0].Instances[0].InstanceId
_, _ = client.CreateTags(&ec2.CreateTagsInput{
_, err = client.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{&mutatedInstanceId},
Tags: []*ec2.Tag{
{
@ -62,6 +68,9 @@ func TestAcc_AwsInstance_WithBlockDevices(t *testing.T) {
},
},
})
if err != nil {
t.Fatal(err)
}
},
Check: func(result *acceptance.ScanResult, stdout string, err error) {
if err != nil {

View File

@ -12,6 +12,9 @@ func TestAcc_AwsRoute53Record_WithFQDNAsId(t *testing.T) {
Args: []string{"scan", "--filter", "Type=='aws_route53_record'"},
Checks: []acceptance.AccCheck{
{
Env: map[string]string{
"AWS_REGION": "us-east-1",
},
Check: func(result *acceptance.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)

View File

@ -12,6 +12,9 @@ func TestAcc_AwsS3Bucket_BucketInUsEast1(t *testing.T) {
Args: []string{"scan", "--filter", "Type=='aws_s3_bucket'"},
Checks: []acceptance.AccCheck{
{
Env: map[string]string{
"AWS_REGION": "us-east-1",
},
Check: func(result *acceptance.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)

View File

@ -1,5 +1,5 @@
provider "aws" {
region = "eu-west-3"
region = "us-east-1"
}
terraform {
required_providers {
@ -7,4 +7,4 @@ terraform {
version = "~> 3.19.0"
}
}
}
}

View File

@ -5,6 +5,7 @@ provider "registry.terraform.io/hashicorp/aws" {
version = "3.19.0"
constraints = "3.19.0"
hashes = [
"h1:+7Vi7p13+cnrxjXbfJiTimGSFR97xCaQwkkvWcreLns=",
"h1:xur9tF49NgsovNnmwmBR8RdpN8Fcg1TD4CKQPJD6n1A=",
"zh:185a5259153eb9ee4699d4be43b3d509386b473683392034319beee97d470c3b",
"zh:2d9a0a01f93e8d16539d835c02b8b6e1927b7685f4076e96cb07f7dd6944bc6c",

View File

@ -1,5 +1,5 @@
provider "aws" {
region = "eu-west-3"
region = "us-east-1"
}
terraform {
required_providers {
@ -7,4 +7,4 @@ terraform {
version = "~> 3.19.0"
}
}
}
}

View File

@ -60,7 +60,7 @@ func (r *ScanResult) AssertResourceHasDrift(id, ty string, change analyser.Chang
}
}
if !found {
r.Failf("no differences found", "%s(%s)", id, ty)
r.Failf("no differences found", "%s (%s)", id, ty)
}
}

View File

@ -40,6 +40,7 @@ type AccTestCase struct {
OnEnd func()
Checks []AccCheck
tmpResultFilePath string
originalEnv []string
}
func (c *AccTestCase) createResultFile(t *testing.T) error {
@ -188,6 +189,28 @@ func runDriftCtlCmd(driftctlCmd *cmd.DriftctlCmd) (*cobra.Command, string, error
return cmd, out, cmdErr
}
func (c *AccTestCase) useTerraformEnv() {
c.originalEnv = os.Environ()
c.setEnv(c.resolveTerraformEnv())
}
func (c *AccTestCase) restoreEnv() {
if c.originalEnv != nil {
logrus.Debug("Restoring original environment ...")
os.Clearenv()
c.setEnv(c.originalEnv)
c.originalEnv = nil
}
}
func (c *AccTestCase) setEnv(env []string) {
os.Clearenv()
for _, e := range env {
envKeyValue := strings.SplitN(e, "=", 2)
os.Setenv(envKeyValue[0], envKeyValue[1])
}
}
func Run(t *testing.T, c AccTestCase) {
if os.Getenv("DRIFTCTL_ACC") == "" {
@ -219,6 +242,7 @@ func Run(t *testing.T, c AccTestCase) {
}
defer func() {
c.restoreEnv()
err := c.terraformDestroy()
os.Setenv("CHECKPOINT_DISABLE", checkpoint)
if err != nil {
@ -248,7 +272,9 @@ func Run(t *testing.T, c AccTestCase) {
t.Fatal("Check attribute must be defined")
}
if check.PreExec != nil {
c.useTerraformEnv()
check.PreExec()
c.restoreEnv()
}
if len(check.Env) > 0 {
for key, value := range check.Env {