Files
infra-provisioner/main_test.go
2023-11-05 15:49:40 +05:30

177 lines
4.4 KiB
Go

package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"testing"
"github.com/navi-infra/infra-provisioner/v2/bindata"
"github.com/sergi/go-diff/diffmatchpatch"
)
const actualOutputDir = "actual_output"
const testDataDir = "testdata"
const expectedOutputDir = "expected_output"
const s3TfPath = "aws-s3-bucket-tf"
const auroraTfPath = "aurora-db-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 manifestFiles = [3]string{"sample_infra_manifest", "sample_infra_manifest_2", "sample_infra_manifest_flink"}
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) error {
expectedOutPath := resouceDir
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, manifestFileName+".json"), testActions)
if err != nil {
return err
}
err = templateResourceTf(resource, resouceDir, manifest, filepath.Join(testDataDir, actualOutputDir, manifestFileName, expectedOutPath))
if err != nil {
return err
}
err = filepath.Walk(filepath.Join(testDataDir, expectedOutputDir, manifestFileName, 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, actualOutputDir, manifestFileName, expectedOutPath, filepath.Base(path)))
if err != nil {
return err
}
if !bytes.Equal(expectedOutput, actualOutput) {
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.Equal(actualTemplate, binDataTemplate) {
t.Errorf("Found outdated bindata for %s", path)
}
return nil
})
if err != nil {
t.Error(err)
}
}
func TestTemplates_Rds_CompareWithOutput(t *testing.T) {
for _, manifestFile := range manifestFiles {
err := compareResourceWithOutput(rdsTfPath, "rds", manifestFile)
if err != nil {
t.Error(err)
}
}
}
func TestTemplates_S3_CompareWithOutput(t *testing.T) {
for _, manifestFile := range manifestFiles {
err := compareResourceWithOutput(s3TfPath, "s3-bucket", manifestFile)
if err != nil {
t.Error(err)
}
}
}
func TestTemplates_AwsRole_CompareWithOutput(t *testing.T) {
for _, manifestFile := range manifestFiles {
err := compareResourceWithOutput(awsRoleTfPath, "iam-role", manifestFile)
if err != nil {
t.Error(err)
}
}
}
func TestTemplates_ElasticCache_CompareWithOutput(t *testing.T) {
for _, manifestFile := range manifestFiles {
err := compareResourceWithOutput(elasticCacheTfPath, "elasticCache", manifestFile)
if err != nil {
t.Error(err)
}
}
}
func TestTemplates_DocumentDb_CompareWithOutput(t *testing.T) {
for _, manifestFile := range manifestFiles {
err := compareResourceWithOutput(documentDbTfPath, "docdb", manifestFile)
if err != nil {
t.Error(err)
}
}
}
func TestTemplates_DynamoDb_CompareWithOutput(t *testing.T) {
for _, manifestFile := range manifestFiles {
err := compareResourceWithOutput(dynamoDbTfPath, "dynamodb", manifestFile)
if err != nil {
t.Error(err)
}
}
}
func TestTemplates_AuroraDb_CompareWithOutput(t *testing.T) {
for _, manifestFile := range manifestFiles {
fmt.Println(manifestFile)
err := compareResourceWithOutput(auroraTfPath, "auroraDB", manifestFile)
if err != nil {
t.Error(err)
}
}
}