Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed May 8, 2024
1 parent 82d9c61 commit fd7330e
Show file tree
Hide file tree
Showing 51 changed files with 5,112 additions and 22 deletions.
84 changes: 64 additions & 20 deletions commands/generate_test.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,60 @@
package commands_test

import (
"fmt"
"math/rand"
"os"
"path"
"strings"
"testing"

"github.com/azure/armstrong/commands"
"github.com/azure/armstrong/resource"
"github.com/azure/armstrong/tf"
)

func TestGenerateCommand_multiple(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %+v", err)
}
runGenerateCommand([][]string{
{"-type", "data", "-path", path.Join(wd, "testdata", "case0", "DatabaseSqlVulnerabilityAssessmentGet.json")},
{"-path", path.Join(wd, "testdata", "case0", "DatabaseSqlVulnerabilityAssessmentBaselineAdd.json")},
}, t)
runGenerateCommand(t, [][]string{
{"-type", "data", "-path", "DatabaseSqlVulnerabilityAssessmentGet.json"},
{"-path", "DatabaseSqlVulnerabilityAssessmentBaselineAdd.json"},
})
}

func TestGenerateCommand_identity(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %+v", err)
}
runGenerateCommand([][]string{
{"-path", path.Join(wd, "testdata", "case1", "ConfigurationStoresCreateWithIdentity.json")},
}, t)
runGenerateCommand(t, [][]string{
{"-path", "ConfigurationStoresCreateWithIdentity.json"},
})
}

func TestGenerateCommand_fromSwagger(t *testing.T) {
runGenerateCommand(t, [][]string{
{"-swagger", "purview.json"},
})
}

func runGenerateCommand(args [][]string, t *testing.T) {
func runGenerateCommand(t *testing.T, args [][]string) {
resource.R = rand.New(rand.NewSource(0))

wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %+v", err)
}

wd = path.Join(wd, ".temp", t.Name())
if err := os.MkdirAll(wd, 0755); err != nil {
tfDir := path.Join(wd, ".temp", t.Name())
if err := os.MkdirAll(tfDir, 0755); err != nil {
t.Fatalf("failed to create working directory: %+v", err)
}
defer os.RemoveAll(wd)
defer os.RemoveAll(tfDir)

command := commands.GenerateCommand{}

for _, arg := range args {
res := command.Run(append([]string{"-working-dir", wd}, arg...))
for i, a := range arg {
if a == "-path" || a == "-swagger" {
arg[i+1] = path.Join(wd, "testdata", t.Name(), arg[i+1])
}
}
res := command.Run(append([]string{"-working-dir", tfDir}, arg...))
if res != 0 {
t.Fatalf("failed to generate terraform configuration")
}
Expand All @@ -67,4 +76,39 @@ func runGenerateCommand(args [][]string, t *testing.T) {
if out.ErrorCount != 0 {
t.Fatalf("[Error] terraform configuration is not valid: %+v\n", out)
}

expectDir := path.Join(wd, "testdata", t.Name(), "expect")
if err := requireFoldersEqual(expectDir, tfDir); err != nil {
t.Fatal(err)
}
}

func requireFoldersEqual(path1, path2 string) error {
files, err := os.ReadDir(path1)
if err != nil {
return err
}
for _, f := range files {
if f.IsDir() {
if err := requireFoldersEqual(path.Join(path1, f.Name()), path.Join(path2, f.Name())); err != nil {
return err
}
} else {
if f.Name() == "provider.tf" || !strings.HasSuffix(f.Name(), ".tf") {
continue
}
actual, err := os.ReadFile(path.Join(path2, f.Name()))
if err != nil {
return err
}
expect, err := os.ReadFile(path.Join(path1, f.Name()))
if err != nil {
return err
}
if string(expect) != string(actual) {
return fmt.Errorf("expected %s to be equal to %s", path.Join(path1, f.Name()), path.Join(path2, f.Name()))
}
}
}
return nil
}
106 changes: 106 additions & 0 deletions commands/test_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package commands_test

import (
"os"
"path"
"strings"
"testing"

"github.com/azure/armstrong/commands"
)

func TestTestCommand_AllPass(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("TF_ACC is not set")
}
runTestCommand(t, map[string]string{
"all_passed_report.md": "Microsoft.Automation/automationAccounts@2023-11-01 (azapi_resource.automationAccount)",
})
}

func TestTestCommand_Diff(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("TF_ACC is not set")
}
runTestCommand(t, map[string]string{
"partial_passed_report.md": "Microsoft.Resources/resourceGroups@2020-06-01 (azapi_resource.resourceGroup)",
"Microsoft.Automation_automationAccounts@2023-11-01_automationAccount.md": ".properties.sku.name: expect Free, but got Basic",
})
}

func TestTestCommand_MissingProperties(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("TF_ACC is not set")
}
runTestCommand(t, map[string]string{
"partial_passed_report.md": "Microsoft.Resources/resourceGroups@2020-06-01 (azapi_resource.resourceGroup)",
"Microsoft.Automation_automationAccounts@2023-11-01_automationAccount.md": ".properties.sku.family = bar: not returned from response",
})
}

func TestTestCommand_BadRequest(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("TF_ACC is not set")
}
runTestCommand(t, map[string]string{
"partial_passed_report.md": "Microsoft.Resources/resourceGroups@2020-06-01 (azapi_resource.resourceGroup)",
"Microsoft.Automation_automationAccounts@2023-11-01_automationAccount.md": "RESPONSE 400: 400 Bad Request",
})
}

func runTestCommand(t *testing.T, fileContentMap map[string]string) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %+v", err)
}

tfDir := path.Join(wd, ".temp", t.Name())
if err := os.MkdirAll(tfDir, 0755); err != nil {
t.Fatalf("failed to create working directory: %+v", err)
}
defer os.RemoveAll(tfDir)
defer commands.CleanupCommand{}.Run([]string{"-working-dir", tfDir})

if err := copyFile(path.Join(wd, "testdata", t.Name(), "main.tf"), path.Join(tfDir, "main.tf")); err != nil {
t.Fatalf("failed to copy file: %+v", err)
}

command := commands.TestCommand{}
command.Run([]string{"-working-dir", tfDir})

reportDir := tfDir
dirs, err := os.ReadDir(reportDir)
if err != nil {
t.Fatalf("failed to read directory %s: %+v", reportDir, err)
}
for _, dir := range dirs {
if dir.IsDir() && strings.HasPrefix(dir.Name(), "armstrong_reports_") {
reportDir = path.Join(reportDir, dir.Name())
break
}
}

for file, content := range fileContentMap {
filePath := path.Join(reportDir, file)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
t.Fatalf("file %s not found", filePath)
}
if content != "" {
data, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("failed to read file %s: %+v", filePath, err)
}
if !strings.Contains(string(data), content) {
t.Fatalf("file %s does not contain expected content", filePath)
}
}
}
}

func copyFile(src, dst string) error {
data, err := os.ReadFile(src)
if err != nil {
return err
}
return os.WriteFile(dst, data, 0644)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"parameters": {
"subscriptionId": "34adfa4f-cedf-4dc0-ba29-b6d1a69ab345",
"resourceGroupName": "SampleResourceGroup",
"accountName": "account1",
"api-version": "2021-12-01",
"collectionAdminUpdate": {
"objectId": "7e8de0e7-2bfc-4e1f-9659-2a5785e4356f"
}
},
"responses": {
"200": {
"headers": {
"Date": "Wed, 13 Sep 2017 18:04:32 GMT",
"x-ms-request-id": "d5496da4-9c52-402f-b067-83cc9ddea888",
"X-Content-Type-Options": "nosniff",
"x-ms-ratelimit-remaining-tenant-reads": "14999",
"x-ms-correlation-request-id": "25c78f97-0b0a-4fe9-ad39-883a482265cd"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"parameters": {
"checkNameAvailabilityRequest": {
"name": "account1",
"type": "Microsoft.Purview/accounts"
},
"api-version": "2021-12-01",
"subscriptionId": "34adfa4f-cedf-4dc0-ba29-b6d1a69ab345"
},
"responses": {
"200": {
"headers": {
"Date": "Wed, 13 Sep 2017 18:04:32 GMT",
"x-ms-request-id": "d5496da4-9c52-402f-b067-83cc9ddea888",
"X-Content-Type-Options": "nosniff",
"x-ms-ratelimit-remaining-tenant-reads": "14999",
"x-ms-correlation-request-id": "25c78f97-0b0a-4fe9-ad39-883a482265cd"
},
"body": {
"nameAvailable": true
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"parameters": {
"subscriptionId": "34adfa4f-cedf-4dc0-ba29-b6d1a69ab345",
"resourceGroupName": "SampleResourceGroup",
"accountName": "account1",
"api-version": "2021-12-01",
"account": {
"location": "West US 2",
"properties": {
"managedResourceGroupName": "custom-rgname",
"managedResourcesPublicNetworkAccess": "Enabled"
}
}
},
"responses": {
"201": {
"headers": {
"Date": "Wed, 13 Sep 2017 18:04:32 GMT",
"x-ms-request-id": "d5496da4-9c52-402f-b067-83cc9ddea888",
"X-Content-Type-Options": "nosniff",
"x-ms-ratelimit-remaining-tenant-reads": "14999",
"x-ms-correlation-request-id": "25c78f97-0b0a-4fe9-ad39-883a482265cd"
},
"body": {
"location": "West US 2",
"id": "/subscriptions/34adfa4f-cedf-4dc0-ba29-b6d1a69ab345/resourceGroups/SampleResourceGroup/providers/Microsoft.Purview/accounts/account1",
"name": "account1",
"type": "Microsoft.Purview/accounts",
"sku": {
"name": "Standard",
"capacity": 1
},
"properties": {
"friendlyName": "friendly-account1",
"provisioningState": "Creating",
"accountStatus": {
"accountProvisioningState": "Succeeded",
"errorDetails": {}
},
"endpoints": {
"catalog": "https://account1.catalog.purview.azure-test.com",
"scan": "https://account1.scan.purview.azure-test.com",
"guardian": "https://account1.guardian.purview.azure-test.com"
},
"publicNetworkAccess": "Enabled"
}
}
},
"200": {
"headers": {
"Date": "Wed, 13 Sep 2017 18:04:32 GMT",
"x-ms-request-id": "d5496da4-9c52-402f-b067-83cc9ddea888",
"X-Content-Type-Options": "nosniff",
"x-ms-ratelimit-remaining-tenant-reads": "14999",
"x-ms-correlation-request-id": "25c78f97-0b0a-4fe9-ad39-883a482265cd"
},
"body": {
"location": "West US 2",
"id": "/subscriptions/34adfa4f-cedf-4dc0-ba29-b6d1a69ab345/resourceGroups/SampleResourceGroup/providers/Microsoft.Purview/accounts/account1",
"name": "account1",
"type": "Microsoft.Purview/accounts",
"sku": {
"name": "Standard",
"capacity": 1
},
"systemData": {
"createdBy": "client-name",
"createdByType": "User",
"createdAt": "2019-11-22T18:39:58.6929344Z",
"lastModifiedBy": "client-name",
"lastModifiedByType": "User",
"lastModifiedAt": "2021-03-16T23:24:34.3430059Z"
},
"properties": {
"friendlyName": "friendly-account1",
"provisioningState": "Succeeded",
"accountStatus": {
"accountProvisioningState": "Succeeded",
"errorDetails": {}
},
"endpoints": {
"catalog": "https://account1.catalog.purview.azure-test.com",
"scan": "https://account1.scan.purview.azure-test.com",
"guardian": "https://account1.guardian.purview.azure-test.com"
},
"publicNetworkAccess": "Enabled",
"managedResourceGroupName": "custom-rgname",
"managedResourcesPublicNetworkAccess": "Enabled",
"managedResources": {
"resourceGroup": "/subscriptions/34adfa4f-cedf-4dc0-ba29-b6d1a69ab345/resourceGroups/custom-rgname",
"storageAccount": "/subscriptions/34adfa4f-cedf-4dc0-ba29-b6d1a69ab345/resourceGroups/custom-rgname/providers/Microsoft.Storage/storageAccounts/scanwestustzaagzr",
"eventHubNamespace": "/subscriptions/34adfa4f-cedf-4dc0-ba29-b6d1a69ab345/resourceGroups/custom-rgname/providers/Microsoft.EventHub/namespaces/atlas-westusdddnbtp"
}
}
}
}
}
}
Loading

0 comments on commit fd7330e

Please sign in to comment.