Skip to content

Commit

Permalink
Merge branch 'main' into module_support
Browse files Browse the repository at this point in the history
  • Loading branch information
eliecharra authored Jan 27, 2021
2 parents aeafb86 + 97f992a commit e301c0a
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
- run:
name: "Bump driftctl formula"
command: |
brew bump-formula-pr --dry-run driftctl --url https://github.com/cloudskiff/driftctl/archive/$CIRCLE_TAG.tar.gz
brew bump-formula-pr driftctl --url https://github.com/cloudskiff/driftctl/archive/$CIRCLE_TAG.tar.gz
workflows:
pullrequest:
jobs:
Expand Down
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ To record a bug report, enhancement proposal, or give any other product feedback

For new contributors we've labeled a few issues with Good First Issue as a nod to issues which will help get you familiar with driftctl development, while also providing an onramp to the codebase itself.

Read the documentation, and don't be afraid to [ask questions](https://discord.gg/eYGHUa75Q2).
Read the documentation, and don't be afraid to [ask questions](https://discord.gg/NMCBxtD7Nd).

## Proposing a Change

Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
blank_issues_enabled: false
contact_links:
- name: '👾 Support chat'
url: https://discord.gg/eYGHUa75Q2
url: https://discord.gg/NMCBxtD7Nd
about: Find support with community chat on discord
2 changes: 1 addition & 1 deletion .github/SUPPORT.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Support

If you have questions about driftctl usage, please feel free to get in touch with us on [Discord](https://discord.gg/eYGHUa75Q2).
If you have questions about driftctl usage, please feel free to get in touch with us on [Discord](https://discord.gg/NMCBxtD7Nd).

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<img src="https://img.shields.io/docker/pulls/cloudskiff/driftctl"/>
<img src="https://img.shields.io/microbadger/layers/cloudskiff/driftctl"/>
<img src="https://img.shields.io/docker/image-size/cloudskiff/driftctl"/>
<a href="https://discord.gg/NMCBxtD7Nd">
<img src="https://img.shields.io/discord/783720783469871124?color=%237289da&label=discord&logo=discord"/>
</a>
</p>

<p align="center">
Expand Down Expand Up @@ -44,7 +47,7 @@ driftctl tracks how well your IaC codebase covers your cloud configuration. drif
## Documentation & support

- [User guide](doc/README.md)
- [Discord](https://discord.gg/eYGHUa75Q2)
- [Discord](https://discord.gg/NMCBxtD7Nd)

## Getting started

Expand Down
2 changes: 1 addition & 1 deletion doc/cmd/completion/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ By default, this command will print on the standard output the content of the co
### Bash
```shell
# Linux:
$ driftctl completion bash > /etc/bash_completion.d/driftctl
$ driftctl completion bash | sudo tee /etc/bash_completion.d/driftctl

# MacOS:
$ driftctl completion bash > /usr/local/etc/bash_completion.d/driftctl
Expand Down
28 changes: 28 additions & 0 deletions doc/contributing/tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,34 @@ Each acceptance test should be prefixed by `TestAcc_` and should be run using en
DRIFTCTL_ACC=true go test -run=TestAcc_ ./pkg/resource/aws/aws_instance_test.go
```

### Credentials

Acceptance tests need credentials to perform real world action on cloud providers:
- Read/write access are required to perform terraform action
- Read only access is required for driftctl execution

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.

#### AWS

You can use `ACC_AWS_PROFILE` to override AWS named profile used for terraform operations.

```shell script
ACC_AWS_PROFILE=read-write-profile AWS_PROFILE=read-only-profile DRIFTCTL_ACC=true go test -run=TestAcc_ ./pkg/resource/aws/aws_instance_test.go
```

In the example below, the `driftctl` AWS profile must have read/write permissions and will be used
for both terraform operations and driftctl run.

This is **not** the recommended way to run tests as it may hide permissions issues.

```shell script
AWS_PROFILE=driftctl DRIFTCTL_ACC=true go test -run=TestAcc_ ./pkg/resource/aws/aws_instance_test.go
```

### Workflow

- **`OnStart`** You may run some code before everything
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ GIT_COMMIT=$(git rev-parse HEAD)
GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "-dev" || true)

# Retrieve
VERSION=$(git name-rev --name-only --tags --no-undefined HEAD 2>/dev/null || git rev-parse --short HEAD)
VERSION=$(git describe --exact-match 2>/dev/null || git rev-parse --short HEAD)

# Inject version number
LD_FLAGS="-X github.com/cloudskiff/driftctl/pkg/version.version=${VERSION}"
Expand Down
34 changes: 34 additions & 0 deletions test/acceptance/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"path"
"strings"
"testing"

"github.com/cloudskiff/driftctl/pkg/analyser"
Expand Down Expand Up @@ -87,12 +88,43 @@ func (c *AccTestCase) getResult(t *testing.T) *ScanResult {
return NewScanResult(t, analysis)
}

/**
* Retrieve env from os.Environ() but override every variable prefixed with ACC_
* e.g. ACC_AWS_PROFILE will override AWS_PROFILE
*/
func (c *AccTestCase) resolveTerraformEnv() []string {

environMap := make(map[string]string, len(os.Environ()))

const PREFIX string = "ACC_"

for _, e := range os.Environ() {
envKeyValue := strings.SplitN(e, "=", 2)
if strings.HasPrefix(envKeyValue[0], PREFIX) {
varName := strings.TrimPrefix(envKeyValue[0], PREFIX)
environMap[varName] = envKeyValue[1]
continue
}
if _, exist := environMap[envKeyValue[0]]; !exist {
environMap[envKeyValue[0]] = envKeyValue[1]
}
}

results := make([]string, 0, len(environMap))
for k, v := range environMap {
results = append(results, fmt.Sprintf("%s=%s", k, v))
}

return results
}

func (c *AccTestCase) terraformInit() error {
_, err := os.Stat(path.Join(c.Path, ".terraform"))
if os.IsNotExist(err) {
logrus.Debug("Running terraform init ...")
cmd := exec.Command("terraform", "init", "-input=false")
cmd.Dir = c.Path
cmd.Env = c.resolveTerraformEnv()
out, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrap(err, string(out))
Expand All @@ -107,6 +139,7 @@ func (c *AccTestCase) terraformApply() error {
logrus.Debug("Running terraform apply ...")
cmd := exec.Command("terraform", "apply", "-auto-approve")
cmd.Dir = c.Path
cmd.Env = c.resolveTerraformEnv()
out, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrap(err, string(out))
Expand All @@ -120,6 +153,7 @@ func (c *AccTestCase) terraformDestroy() error {
logrus.Debug("Running terraform destroy ...")
cmd := exec.Command("terraform", "destroy", "-auto-approve")
cmd.Dir = c.Path
cmd.Env = c.resolveTerraformEnv()
out, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrap(err, string(out))
Expand Down
31 changes: 31 additions & 0 deletions test/acceptance/testing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package acceptance

import (
"os"
"reflect"
"sort"
"testing"
)

func TestAccTestCase_resolveTerraformEnv(t *testing.T) {

os.Clearenv()
os.Setenv("ACC_TEST_VAR", "foobar")
os.Setenv("TEST_VAR", "barfoo")
os.Setenv("TEST_VAR_2", "barfoo")
os.Setenv("ACC_TEST_VAR_3", "")
os.Setenv("TEST_VAR_3", "barfoo")
os.Setenv("TEST_VAR_4", "barfoo")
os.Setenv("ACC_TEST_VAR_4", "")

testCase := AccTestCase{}
env := testCase.resolveTerraformEnv()
expected := []string{"TEST_VAR=foobar", "TEST_VAR_2=barfoo", "TEST_VAR_3=", "TEST_VAR_4="}
sort.Strings(env)
sort.Strings(expected)

if !reflect.DeepEqual(expected, env) {
t.Fatalf("Variable env override not working, got: %+v, expected %+v", env, expected)
}

}

0 comments on commit e301c0a

Please sign in to comment.