commit
78f1e40acc
|
@ -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:
|
Recommended way to run acc tests is to use two distinct credentials:
|
||||||
one for terraform related actions, and one for driftctl scan.
|
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
|
#### AWS
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,9 @@ func TestAcc_AwsInstance_WithBlockDevices(t *testing.T) {
|
||||||
Args: []string{"scan", "--filter", "Type=='aws_instance'"},
|
Args: []string{"scan", "--filter", "Type=='aws_instance'"},
|
||||||
Checks: []acceptance.AccCheck{
|
Checks: []acceptance.AccCheck{
|
||||||
{
|
{
|
||||||
|
Env: map[string]string{
|
||||||
|
"AWS_REGION": "us-east-1",
|
||||||
|
},
|
||||||
Check: func(result *acceptance.ScanResult, stdout string, err error) {
|
Check: func(result *acceptance.ScanResult, stdout string, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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() {
|
PreExec: func() {
|
||||||
client := ec2.New(awsutils.Session())
|
client := ec2.New(awsutils.Session())
|
||||||
response, err := client.DescribeInstances(&ec2.DescribeInstancesInput{
|
response, err := client.DescribeInstances(&ec2.DescribeInstancesInput{
|
||||||
|
@ -49,11 +55,11 @@ func TestAcc_AwsInstance_WithBlockDevices(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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")
|
t.Fatal("Error, unexpected number of instances found, manual check required")
|
||||||
}
|
}
|
||||||
mutatedInstanceId = *response.Reservations[0].Instances[0].InstanceId
|
mutatedInstanceId = *response.Reservations[0].Instances[0].InstanceId
|
||||||
_, _ = client.CreateTags(&ec2.CreateTagsInput{
|
_, err = client.CreateTags(&ec2.CreateTagsInput{
|
||||||
Resources: []*string{&mutatedInstanceId},
|
Resources: []*string{&mutatedInstanceId},
|
||||||
Tags: []*ec2.Tag{
|
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) {
|
Check: func(result *acceptance.ScanResult, stdout string, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -12,6 +12,9 @@ func TestAcc_AwsRoute53Record_WithFQDNAsId(t *testing.T) {
|
||||||
Args: []string{"scan", "--filter", "Type=='aws_route53_record'"},
|
Args: []string{"scan", "--filter", "Type=='aws_route53_record'"},
|
||||||
Checks: []acceptance.AccCheck{
|
Checks: []acceptance.AccCheck{
|
||||||
{
|
{
|
||||||
|
Env: map[string]string{
|
||||||
|
"AWS_REGION": "us-east-1",
|
||||||
|
},
|
||||||
Check: func(result *acceptance.ScanResult, stdout string, err error) {
|
Check: func(result *acceptance.ScanResult, stdout string, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
|
@ -12,6 +12,9 @@ func TestAcc_AwsS3Bucket_BucketInUsEast1(t *testing.T) {
|
||||||
Args: []string{"scan", "--filter", "Type=='aws_s3_bucket'"},
|
Args: []string{"scan", "--filter", "Type=='aws_s3_bucket'"},
|
||||||
Checks: []acceptance.AccCheck{
|
Checks: []acceptance.AccCheck{
|
||||||
{
|
{
|
||||||
|
Env: map[string]string{
|
||||||
|
"AWS_REGION": "us-east-1",
|
||||||
|
},
|
||||||
Check: func(result *acceptance.ScanResult, stdout string, err error) {
|
Check: func(result *acceptance.ScanResult, stdout string, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
provider "aws" {
|
provider "aws" {
|
||||||
region = "eu-west-3"
|
region = "us-east-1"
|
||||||
}
|
}
|
||||||
terraform {
|
terraform {
|
||||||
required_providers {
|
required_providers {
|
||||||
|
@ -7,4 +7,4 @@ terraform {
|
||||||
version = "~> 3.19.0"
|
version = "~> 3.19.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ provider "registry.terraform.io/hashicorp/aws" {
|
||||||
version = "3.19.0"
|
version = "3.19.0"
|
||||||
constraints = "3.19.0"
|
constraints = "3.19.0"
|
||||||
hashes = [
|
hashes = [
|
||||||
|
"h1:+7Vi7p13+cnrxjXbfJiTimGSFR97xCaQwkkvWcreLns=",
|
||||||
"h1:xur9tF49NgsovNnmwmBR8RdpN8Fcg1TD4CKQPJD6n1A=",
|
"h1:xur9tF49NgsovNnmwmBR8RdpN8Fcg1TD4CKQPJD6n1A=",
|
||||||
"zh:185a5259153eb9ee4699d4be43b3d509386b473683392034319beee97d470c3b",
|
"zh:185a5259153eb9ee4699d4be43b3d509386b473683392034319beee97d470c3b",
|
||||||
"zh:2d9a0a01f93e8d16539d835c02b8b6e1927b7685f4076e96cb07f7dd6944bc6c",
|
"zh:2d9a0a01f93e8d16539d835c02b8b6e1927b7685f4076e96cb07f7dd6944bc6c",
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
provider "aws" {
|
provider "aws" {
|
||||||
region = "eu-west-3"
|
region = "us-east-1"
|
||||||
}
|
}
|
||||||
terraform {
|
terraform {
|
||||||
required_providers {
|
required_providers {
|
||||||
|
@ -7,4 +7,4 @@ terraform {
|
||||||
version = "~> 3.19.0"
|
version = "~> 3.19.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ func (r *ScanResult) AssertResourceHasDrift(id, ty string, change analyser.Chang
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found {
|
if !found {
|
||||||
r.Failf("no differences found", "%s(%s)", id, ty)
|
r.Failf("no differences found", "%s (%s)", id, ty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,7 @@ type AccTestCase struct {
|
||||||
OnEnd func()
|
OnEnd func()
|
||||||
Checks []AccCheck
|
Checks []AccCheck
|
||||||
tmpResultFilePath string
|
tmpResultFilePath string
|
||||||
|
originalEnv []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AccTestCase) createResultFile(t *testing.T) error {
|
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
|
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) {
|
func Run(t *testing.T, c AccTestCase) {
|
||||||
|
|
||||||
if os.Getenv("DRIFTCTL_ACC") == "" {
|
if os.Getenv("DRIFTCTL_ACC") == "" {
|
||||||
|
@ -219,6 +242,7 @@ func Run(t *testing.T, c AccTestCase) {
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
|
c.restoreEnv()
|
||||||
err := c.terraformDestroy()
|
err := c.terraformDestroy()
|
||||||
os.Setenv("CHECKPOINT_DISABLE", checkpoint)
|
os.Setenv("CHECKPOINT_DISABLE", checkpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -248,7 +272,9 @@ func Run(t *testing.T, c AccTestCase) {
|
||||||
t.Fatal("Check attribute must be defined")
|
t.Fatal("Check attribute must be defined")
|
||||||
}
|
}
|
||||||
if check.PreExec != nil {
|
if check.PreExec != nil {
|
||||||
|
c.useTerraformEnv()
|
||||||
check.PreExec()
|
check.PreExec()
|
||||||
|
c.restoreEnv()
|
||||||
}
|
}
|
||||||
if len(check.Env) > 0 {
|
if len(check.Env) > 0 {
|
||||||
for key, value := range check.Env {
|
for key, value := range check.Env {
|
||||||
|
|
Loading…
Reference in New Issue