Files
infra-provisioner/main_test.go

237 lines
6.3 KiB
Go

package main
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"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)
}
}
}
func Test_getDeploymentPortalUrl(t *testing.T) {
testCases := []struct {
name string
manifestFile string
vertical string
expectedURL string
setEnv bool
}{
{
name: "Correct Lending Vertical URL should be generated",
expectedURL: "https://deployment-portal-backend.cmd.navi-tech.in/api/manifest/status/env/dev/name/foo",
vertical: "lending",
setEnv: false,
},
{
name: "Correct Insurance Vertical URL should be generated",
expectedURL: "https://gi-deployment-portal-backend.cmd.navi-tech.in/api/manifest/status/env/dev/name/foo",
vertical: "insurance",
setEnv: false,
},
{
name: "Correct for localhost URL should be generated",
expectedURL: "http://localhost:8080/api/manifest/status/env/dev/name/foo",
vertical: "test",
setEnv: true,
},
{
name: "Correct Navi-pay Vertical URL should be generated",
expectedURL: "https://navi-pay-deployment-portal-backend.cmd.navi-tech.in/api/manifest/status/env/dev/name/foo",
vertical: "navi-pay",
setEnv: false,
},
{
name: "Correct Sa Vertical URL should be generated",
expectedURL: "https://sa-deployment-portal-backend.cmd.navi-tech.in/api/manifest/status/env/dev/name/foo",
vertical: "sa",
setEnv: false,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
if testCase.setEnv {
_ = os.Setenv("TESTING_ENV", "dev")
}
defer func() {
_ = os.Unsetenv("TESTING_ENV")
}()
testActions, err := setActions(true, false, false)
assert.NoError(t, err)
manifestFile := "sample_infra_manifest.json"
manifest, err := parseManifest(filepath.Join(testDataDir, manifestFile), testActions)
assert.NoError(t, err)
manifest.InfraVertical = testCase.vertical
actualURL := getDeploymentPortalUrl(manifest)
assert.Equal(t, testCase.expectedURL, actualURL)
})
}
}