func NewTFBlueprintTest()

in infra/blueprint-test/pkg/tft/terraform.go [184:273]


func NewTFBlueprintTest(t testing.TB, opts ...tftOption) *TFBlueprintTest {
	var err error
	tft := &TFBlueprintTest{
		name:      fmt.Sprintf("%s TF Blueprint", t.Name()),
		tfEnvVars: make(map[string]string),
		t:         t,
	}
	// initiate tft cache file mutex
	tft.tftCacheMutex, err = filemutex.New(filepath.Join(os.TempDir(), tftCacheMutexFilename))
	if err != nil {
		t.Fatalf("tft lock file <%s> could not created: %v", filepath.Join(os.TempDir(), tftCacheMutexFilename), err)
	}
	// default TF blueprint methods
	tft.init = tft.DefaultInit
	// No default plan function, plan is skipped if no custom func provided.
	tft.apply = tft.DefaultApply
	tft.verify = tft.DefaultVerify
	tft.teardown = tft.DefaultTeardown
	// apply options
	for _, opt := range opts {
		opt(tft)
	}
	// if no custom logger, set default based on test verbosity
	if tft.logger == nil {
		tft.logger = utils.GetLoggerFromT()
	}
	// If no custom sensitive logger, use discard logger.
	if tft.sensitiveLogger == nil {
		tft.sensitiveLogger = logger.Discard
	}
	// if explicit tfDir is provided, validate it else try auto discovery
	if tft.tfDir != "" {
		_, err := os.Stat(tft.tfDir)
		if os.IsNotExist(err) {
			t.Fatalf("TFDir path %s does not exist", tft.tfDir)
		}
	} else {
		tfdir, err := discovery.GetConfigDirFromTestDir(utils.GetWD(t))
		if err != nil {
			t.Fatalf("unable to detect TFDir :%v", err)
		}
		tft.tfDir = tfdir
	}

	// discover test config
	tft.BlueprintTestConfig, err = discovery.GetTestConfig(path.Join(tft.tfDir, discovery.DefaultTestConfigFilename))
	if err != nil {
		t.Fatal(err)
	}
	// setupDir is empty, try known setupDir paths
	if tft.setupDir == "" {
		setupDir, err := discovery.GetKnownDirInParents(discovery.SetupDir, 2)
		if err != nil {
			t.Logf("Setup dir not found, skipping loading setup outputs as fixture inputs: %v", err)
		} else {
			tft.setupDir = setupDir
		}
	}
	// load setup sa Key
	if tft.saKey != "" {
		gcloud.ActivateCredsAndEnvVars(tft.t, tft.saKey)
	}
	// load TFEnvVars from setup outputs
	if tft.setupDir != "" {
		tft.logger.Logf(tft.t, "Loading env vars from setup %s", tft.setupDir)
		outputs := tft.getOutputs(tft.sensitiveOutputs(tft.setupDir))
		loadTFEnvVar(tft.tfEnvVars, tft.getTFOutputsAsInputs(outputs))
		if credsEnc, exists := tft.tfEnvVars[fmt.Sprintf("TF_VAR_%s", setupKeyOutputName)]; tft.saKey == "" && exists {
			if credDec, err := b64.StdEncoding.DecodeString(credsEnc); err == nil {
				gcloud.ActivateCredsAndEnvVars(tft.t, string(credDec))
			} else {
				tft.t.Fatalf("Unable to decode setup sa key: %v", err)
			}
		} else {
			tft.logger.Logf(tft.t, "Skipping credential activation %s output from setup", setupKeyOutputName)
		}
	}
	// Load env vars to supplement/override setup
	tft.logger.Logf(tft.t, "Loading setup from environment")
	if tft.setupOutputOverrides == nil {
		tft.setupOutputOverrides = make(map[string]interface{})
	}
	for k, v := range extractFromEnv("CFT_SETUP_") {
		tft.setupOutputOverrides[k] = v
	}

	tftVersion := gjson.Get(terraform.RunTerraformCommand(tft.t, tft.GetTFOptions(), "version", "-json"), "terraform_version")
	tft.logger.Logf(tft.t, "Running tests TF configs in %s with version %s", tft.tfDir, tftVersion)
	return tft
}