// Test the Vault enterprise cluster example by:
//
// 1. Copy the code in this repo to a temp folder so tests on the Terraform code can run in parallel without the
//    state files overwriting each other.
// 2. Build the Cloud Image in the vault-consul-image example with the given build name and the enterprise packages
// 3. Deploy that Image using the example Terraform code
// 4. TODO - SSH to a Vault node and check if Vault enterprise is installed properly
// 5. SSH into a Vault node and initialize the Vault cluster
// 6. SSH to each other Vault node, restart vault and test that it is unsealed
// 7.  SSH to a Vault node and make sure you can communicate with the nodes via Consul-managed DNS
Test the Vault enterprise cluster example by.

1. Copy the code in this repo to a temp folder so tests on the Terraform code can run in parallel without the
state files overwriting each other.
2. Build the Cloud Image in the vault-consul-image example with the given build name and the enterprise packages
3. Deploy that Image using the example Terraform code
4. TODO - SSH to a Vault node and check if Vault enterprise is installed properly
5. SSH into a Vault node and initialize the Vault cluster
6. SSH to each other Vault node, restart vault and test that it is unsealed
7.  SSH to a Vault node and make sure you can communicate with the nodes via Consul-managed DNS
// Test the Vault enterprise cluster example by : // // 1 . Copy the code in this repo to a temp folder so tests on the Terraform code can run in parallel without the // state files overwriting each other . // 2 . Build the Cloud Image in the vault - consul - image example with the given build name and the enterprise packages // 3 . Deploy that Image using the example Terraform code // 4 . TODO - SSH to a Vault node and check if Vault enterprise is installed properly // 5 . SSH into a Vault node and initialize the Vault cluster // 6 . SSH to each other Vault node restart vault and test that it is unsealed // 7 . SSH to a Vault node and make sure you can communicate with the nodes via Consul - managed DNS
func runVaultEnterpriseClusterTest(t *testing.T, packerBuildSaveName string) {
	exampleDir := test_structure.CopyTerraformFolderToTemp(t, "../", "examples/vault-cluster-enterprise")
	defer test_structure.RunTestStage(t, "teardown", func() {
		terraformOptions := test_structure.LoadTerraformOptions(t, exampleDir)
		terraform.Destroy(t, terraformOptions)
	})
	defer test_structure.RunTestStage(t, "log", func() {
	})
	test_structure.RunTestStage(t, "deploy", func() {
		projectId := test_structure.LoadString(t, WORK_DIR, SAVED_GCP_PROJECT_ID)
		region := test_structure.LoadString(t, WORK_DIR, SAVED_GCP_REGION_NAME)
		imageID := test_structure.LoadString(t, WORK_DIR, packerBuildSaveName)
		uniqueID := strings.ToLower(random.UniqueId())
		consulClusterName := fmt.Sprintf("consul-test-%s", uniqueID)
		vaultClusterName := fmt.Sprintf("vault-test-%s", uniqueID)
		terraformOptions := &terraform.Options{
			TerraformDir: exampleDir,
			Vars: map[string]interface{}{
				TFVAR_NAME_GCP_PROJECT_ID:                     projectId,
				TFVAR_NAME_GCP_REGION:                         region,
				TFVAR_NAME_CONSUL_SERVER_CLUSTER_NAME:         consulClusterName,
				TFVAR_NAME_CONSUL_SOURCE_IMAGE:                imageID,
				TFVAR_NAME_CONSUL_SERVER_CLUSTER_MACHINE_TYPE: "g1-small",
				TFVAR_NAME_VAULT_CLUSTER_NAME:                 vaultClusterName,
				TFVAR_NAME_VAULT_SOURCE_IMAGE:                 imageID,
				TFVAR_NAME_VAULT_CLUSTER_MACHINE_TYPE:         "g1-small",
				TFVAR_NAME_BASTION_SERVER_NAME:                fmt.Sprintf("bastion-test-%s", uniqueID),
				TFVAR_NAME_AUTOUNSEAL_KEY_PROJECT:             projectId,
				TFVAR_NAME_AUTOUNSEAL_KEY_REGION:              AUTOUNSEAL_KEY_REGION,
				TFVAR_NAME_AUTOUNSEAL_KEY_RING_NAME:           AUTOUNSEAL_KEY_RING_NAME,
				TFVAR_NAME_AUTOUNSEAL_CRYPTO_KEY_NAME:         AUTOUNSEAL_CRYPTO_KEY_NAME,
				TFVAR_NAME_SUBNET_CIDR:                        getRandomCidr(),
			},
		}
		test_structure.SaveTerraformOptions(t, exampleDir, terraformOptions)
		terraform.InitAndApply(t, terraformOptions)
	})
	test_structure.RunTestStage(t, "validate", func() {
		terraformOptions := test_structure.LoadTerraformOptions(t, exampleDir)
		projectId := test_structure.LoadString(t, WORK_DIR, SAVED_GCP_PROJECT_ID)
		region := test_structure.LoadString(t, WORK_DIR, SAVED_GCP_REGION_NAME)
		instanceGroupName := terraform.OutputRequired(t, terraformOptions, TFOUT_INSTANCE_GROUP_NAME)
		sshUserName := "terratest"
		keyPair := ssh.GenerateRSAKeyPair(t, 2048)
		saveKeyPair(t, exampleDir, keyPair)
		addKeyPairToInstancesInGroup(t, projectId, region, instanceGroupName, keyPair, sshUserName, 3)
		bastionName := terraform.OutputRequired(t, terraformOptions, TFVAR_NAME_BASTION_SERVER_NAME)
		bastionInstance := gcp.FetchInstance(t, projectId, bastionName)
		bastionInstance.AddSshKey(t, sshUserName, keyPair.PublicKey)
		bastionHost := ssh.Host{
			Hostname:    bastionInstance.GetPublicIp(t),
			SshUserName: sshUserName,
			SshKeyPair:  keyPair,
		}
		cluster := testVaultInitializeAutoUnseal(t, projectId, region, instanceGroupName, sshUserName, keyPair, &bastionHost)
		testVaultUsesConsulForDns(t, cluster, &bastionHost)
	})
}
func runVaultEnterpriseClusterTest ( t * testing . T , packerBuildSaveName string ) { exampleDir := test_structure . CopyTerraformFolderToTemp ( t , " " , " " ) defer test_structure . RunTestStage ( t , " " , func ( ) { terraformOptions := test_structure . LoadTerraformOptions ( t , exampleDir ) terraform . Destroy ( t , terraformOptions ) } ) defer test_structure . RunTestStage ( t , " " , func ( ) { } ) test_structure . RunTestStage ( t , " " , func ( ) { projectId := test_structure . LoadString ( t , WORK_DIR , SAVED_GCP_PROJECT_ID ) region := test_structure . LoadString ( t , WORK_DIR , SAVED_GCP_REGION_NAME ) imageID := test_structure . LoadString ( t , WORK_DIR , packerBuildSaveName ) uniqueID := strings . ToLower ( random . UniqueId ( ) ) consulClusterName := fmt . Sprintf ( " " , uniqueID ) vaultClusterName := fmt . Sprintf ( " " , uniqueID ) terraformOptions := & terraform . Options { TerraformDir : exampleDir , Vars : map [ string ] interface { } { TFVAR_NAME_GCP_PROJECT_ID : projectId , TFVAR_NAME_GCP_REGION : region , TFVAR_NAME_CONSUL_SERVER_CLUSTER_NAME : consulClusterName , TFVAR_NAME_CONSUL_SOURCE_IMAGE : imageID , TFVAR_NAME_CONSUL_SERVER_CLUSTER_MACHINE_TYPE : " " , TFVAR_NAME_VAULT_CLUSTER_NAME : vaultClusterName , TFVAR_NAME_VAULT_SOURCE_IMAGE : imageID , TFVAR_NAME_VAULT_CLUSTER_MACHINE_TYPE : " " , TFVAR_NAME_BASTION_SERVER_NAME : fmt . Sprintf ( " " , uniqueID ) , TFVAR_NAME_AUTOUNSEAL_KEY_PROJECT : projectId , TFVAR_NAME_AUTOUNSEAL_KEY_REGION : AUTOUNSEAL_KEY_REGION , TFVAR_NAME_AUTOUNSEAL_KEY_RING_NAME : AUTOUNSEAL_KEY_RING_NAME , TFVAR_NAME_AUTOUNSEAL_CRYPTO_KEY_NAME : AUTOUNSEAL_CRYPTO_KEY_NAME , TFVAR_NAME_SUBNET_CIDR : getRandomCidr ( ) , } , } test_structure . SaveTerraformOptions ( t , exampleDir , terraformOptions ) terraform . InitAndApply ( t , terraformOptions ) } ) test_structure . RunTestStage ( t , " " , func ( ) { terraformOptions := test_structure . LoadTerraformOptions ( t , exampleDir ) projectId := test_structure . LoadString ( t , WORK_DIR , SAVED_GCP_PROJECT_ID ) region := test_structure . LoadString ( t , WORK_DIR , SAVED_GCP_REGION_NAME ) instanceGroupName := terraform . OutputRequired ( t , terraformOptions , TFOUT_INSTANCE_GROUP_NAME ) sshUserName := " " keyPair := ssh . GenerateRSAKeyPair ( t , 2048 ) saveKeyPair ( t , exampleDir , keyPair ) addKeyPairToInstancesInGroup ( t , projectId , region , instanceGroupName , keyPair , sshUserName , 3 ) bastionName := terraform . OutputRequired ( t , terraformOptions , TFVAR_NAME_BASTION_SERVER_NAME ) bastionInstance := gcp . FetchInstance ( t , projectId , bastionName ) bastionInstance . AddSshKey ( t , sshUserName , keyPair . PublicKey ) bastionHost := ssh . Host { Hostname : bastionInstance . GetPublicIp ( t ) , SshUserName : sshUserName , SshKeyPair : keyPair , } cluster := testVaultInitializeAutoUnseal ( t , projectId , region , instanceGroupName , sshUserName , keyPair , & bastionHost ) testVaultUsesConsulForDns ( t , cluster , & bastionHost ) } ) }
Test the Vault enterprise cluster example by.
Test the Vault enterprise cluster example by .
//ToDo: Modify log retrieval to go through bastion host //      Requires adding feature to terratest //writeVaultLogs(t, "vaultEnterpriseCluster", exampleDir) // GCP only supports lowercase names for some resources