Skip to content

Commit 7f6ca96

Browse files
Add command line tool to run tests (#1)
* Add command line tool to run tests * Fix errors & warnings in tests * Support filter option by test names * ops: setup pipeline to analyze code * ops: fix branch name * ops: fix pipeline * Terraform fmt * ops: added missing step * ops: addedstep to download modules * Revert "ops: addedstep to download modules"
1 parent ca12038 commit 7f6ca96

File tree

131 files changed

+1587
-1579
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+1587
-1579
lines changed

.github/workflows/PolicyTesting.yml

Lines changed: 0 additions & 91 deletions
This file was deleted.

.github/workflows/analysis.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: 'Analysis'
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
lint:
13+
name: Check linter
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Setup Terraform
17+
uses: hashicorp/setup-terraform@v1
18+
- name: Checkout
19+
uses: actions/checkout@v2
20+
- name: Terraform fmt
21+
run: terraform fmt -check -recursive
22+
- name: Golang lint
23+
uses: golangci/golangci-lint-action@v2
24+
codeql:
25+
name: CodeQL
26+
runs-on: ubuntu-latest
27+
permissions:
28+
actions: read
29+
contents: read
30+
security-events: write
31+
steps:
32+
- name: Checkout repository
33+
uses: actions/checkout@v2
34+
- name: Initialize CodeQL
35+
uses: github/codeql-action/init@v1
36+
with:
37+
languages: go
38+
- name: Autobuild
39+
uses: github/codeql-action/autobuild@v1
40+
- name: Perform CodeQL Analysis
41+
uses: github/codeql-action/analyze@v1

README.md

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,46 @@
22

33
This repository is a command line tool for testing Azure Policies.
44

5-
This repository is organized as follow:
5+
## Usage
66

7-
- custom_policies : contains 1 folder (1 [terraform Module](https://www.terraform.io/docs/configuration/modules.html)) per custom Azure Policy.
8-
- prod_policies : this folder contains the terraform configuration applied on Azure environments to match security requirements.
9-
- test : for each custom Azure Policy, automated tests are located here.
7+
For each test case, the following directory structure must be created:
8+
9+
```txt
10+
+-- your_test_name
11+
| +-- setup
12+
| | +-- terraform files to setup the policy
13+
| +-- terraform files for test cases
14+
```
15+
16+
Additionally, a `.yaml` configuration file must describe the test as following:
17+
18+
```yaml
19+
name: Name of the test
20+
cases:
21+
- variables:
22+
- key: variable name in TF
23+
value: val
24+
errorExpected: true
25+
- variables:
26+
- key: variable name in TF
27+
value: val2
28+
errorExpected: false
29+
terraformDir: relative path to the folder structure described above
30+
errorMessage: Error message from Azure (ex 'Error creating Network Interface')
31+
errorCode: Error code from Azure (ex 'Error Code=\"RequestDisallowedByPolicy\"')
32+
```
33+
34+
To run from the source code:
35+
36+
```bash
37+
go run ./cmd/policyTester/ -config ./test/ -test.v
38+
```
39+
40+
Note: any args from the `go test` command are available for use. For instance:
41+
42+
```bash
43+
go run ./cmd/policyTester/ -config ./test/ -test.v -test.parallel=10 -test.run Location
44+
```
1045

1146
## About Azure Policies
1247

cmd/policyTester/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"log"
6+
"os"
7+
"testing"
8+
9+
"github.com/microsoft/AzurePolicyTestFramework/pkg/tester"
10+
)
11+
12+
var (
13+
cases *string
14+
)
15+
16+
func init() {
17+
cases = flag.String("config", "", "path to the test cases")
18+
}
19+
20+
func main() {
21+
testing.Init()
22+
flag.Parse()
23+
24+
code, err := tester.RunGoTest(*cases)
25+
if err != nil {
26+
log.Fatal(err)
27+
}
28+
29+
os.Exit(code)
30+
}

go.mod

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module github.com/microsoft/AzurePolicyTestFramework
2+
3+
go 1.17
4+
5+
require (
6+
github.com/gruntwork-io/terratest v0.31.0
7+
github.com/hashicorp/terraform-exec v0.15.0
8+
github.com/stretchr/testify v1.7.0
9+
gopkg.in/yaml.v2 v2.3.0
10+
)
11+
12+
require (
13+
cloud.google.com/go v0.51.0 // indirect
14+
cloud.google.com/go/storage v1.0.0 // indirect
15+
github.com/BurntSushi/toml v0.3.1 // indirect
16+
github.com/aws/aws-sdk-go v1.27.1 // indirect
17+
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
18+
github.com/davecgh/go-spew v1.1.1 // indirect
19+
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 // indirect
20+
github.com/golang/protobuf v1.4.2 // indirect
21+
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
22+
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
23+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
24+
github.com/hashicorp/go-getter v1.5.3 // indirect
25+
github.com/hashicorp/go-safetemp v1.0.0 // indirect
26+
github.com/hashicorp/go-uuid v1.0.0 // indirect
27+
github.com/hashicorp/go-version v1.3.0 // indirect
28+
github.com/hashicorp/terraform-json v0.13.0 // indirect
29+
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
30+
github.com/jstemmer/go-junit-report v0.9.1 // indirect
31+
github.com/klauspost/compress v1.11.2 // indirect
32+
github.com/mitchellh/go-homedir v1.1.0 // indirect
33+
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
34+
github.com/pmezard/go-difflib v1.0.0 // indirect
35+
github.com/ulikunitz/xz v0.5.8 // indirect
36+
github.com/zclconf/go-cty v1.9.1 // indirect
37+
go.opencensus.io v0.22.2 // indirect
38+
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
39+
golang.org/x/exp v0.0.0-20191227195350-da58074b4299 // indirect
40+
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f // indirect
41+
golang.org/x/net v0.0.0-20210326060303-6b1517762897 // indirect
42+
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
43+
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79 // indirect
44+
golang.org/x/text v0.3.5 // indirect
45+
golang.org/x/tools v0.0.0-20200113040837-eac381796e91 // indirect
46+
google.golang.org/api v0.15.0 // indirect
47+
google.golang.org/appengine v1.6.5 // indirect
48+
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
49+
google.golang.org/grpc v1.27.0 // indirect
50+
google.golang.org/protobuf v1.24.0 // indirect
51+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
52+
honnef.co/go/tools v0.0.1-2019.2.3 // indirect
53+
)

0 commit comments

Comments
 (0)