162 lines
4.1 KiB
Go
162 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.cmd.navi-tech.in/navi-infra/infra-provisioner/v2/bindata"
|
|
"github.com/sergi/go-diff/diffmatchpatch"
|
|
)
|
|
|
|
const ActualOutputDir = "actual_output"
|
|
const TestDataDir = "testdata"
|
|
const ExpectedOutputDir = "expected_output"
|
|
const ManifestFile = "sample_infra_manifest.json"
|
|
const ManifestFile2 = "sample_infra_manifest_2.json"
|
|
const S3TfPath = "aws-s3-bucket-tf"
|
|
const RdsTfPath = "rds-tf"
|
|
const AwsRoleTfPath = "aws-roles-tf"
|
|
const ElasticCacheTfPath = "elastic-cache-tf"
|
|
const DynamoDbTfPath = "dynamo-db-tf"
|
|
const DocumentDbTfPath = "document-db-tf"
|
|
|
|
var testActions Actions
|
|
|
|
func textDiff(text1, text2 string) string {
|
|
dmp := diffmatchpatch.New()
|
|
diffs := dmp.DiffMain(text1, text2, false)
|
|
return dmp.DiffPrettyText(diffs)
|
|
}
|
|
|
|
func CompareResourceWithOutput(resouceDir string, resource string, manifestFileName string, expectedOutPath string) error {
|
|
dirs, err := ioutil.ReadDir(TestDataDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
testActions, err := setActions(true, false, false)
|
|
if err != nil {
|
|
log.Fatalf("\nErr: %v", err)
|
|
return err
|
|
}
|
|
for _, dir := range dirs {
|
|
fmt.Print(dir.Name())
|
|
manifest, err := parseManifest(filepath.Join(TestDataDir, dir.Name(), manifestFileName), testActions)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = templateResourceTf(resource, resouceDir, manifest, filepath.Join(TestDataDir, dir.Name(), ActualOutputDir, expectedOutPath))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = filepath.Walk(filepath.Join(TestDataDir, dir.Name(), ExpectedOutputDir, expectedOutPath),
|
|
func(path string, fileInfo os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if fileInfo.IsDir() {
|
|
return nil
|
|
}
|
|
expectedOutput, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
actualOutput, err := ioutil.ReadFile(filepath.Join(TestDataDir, dir.Name(), ActualOutputDir, expectedOutPath, filepath.Base(path)))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if bytes.Compare(expectedOutput, actualOutput) != 0 {
|
|
return fmt.Errorf("Mismatch for %s, diff: %s\n", path, textDiff(string(actualOutput), string(expectedOutput)))
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestBinData_CompareWithTemplates(t *testing.T) {
|
|
err := filepath.Walk(TemplatesDir,
|
|
func(path string, fileInfo os.FileInfo, err error) error {
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if fileInfo.IsDir() {
|
|
return nil
|
|
}
|
|
actualTemplate, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
binDataTemplate, err := bindata.Asset(path)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if bytes.Compare(actualTemplate, binDataTemplate) != 0 {
|
|
t.Errorf("Found outdated bindata for %s", path)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestTemplates_Rds_CompareWithOutput(t *testing.T) {
|
|
err := CompareResourceWithOutput(RdsTfPath, "rds", ManifestFile, RdsTfPath)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestTemplates_Rds_CompareWithOutputV2(t *testing.T) {
|
|
err := CompareResourceWithOutput(RdsTfPath, "rds", ManifestFile2, "rds2-tf")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestTemplates_S3_CompareWithOutput(t *testing.T) {
|
|
err := CompareResourceWithOutput(S3TfPath, "s3-bucket", ManifestFile, S3TfPath)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestTemplates_AwsRole_CompareWithOutput(t *testing.T) {
|
|
err := CompareResourceWithOutput(AwsRoleTfPath, "iam-role", ManifestFile, AwsRoleTfPath)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestTemplates_ElasticCache_CompareWithOutput(t *testing.T) {
|
|
err := CompareResourceWithOutput(ElasticCacheTfPath, "elasticCache", ManifestFile, ElasticCacheTfPath)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestTemplates_DocumentDb_CompareWithOutput(t *testing.T) {
|
|
err := CompareResourceWithOutput(DocumentDbTfPath, "docdb", ManifestFile, DocumentDbTfPath)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestTemplates_DynamoDb_CompareWithOutput(t *testing.T) {
|
|
err := CompareResourceWithOutput(DynamoDbTfPath, "dynamodb", ManifestFile, DynamoDbTfPath)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|