Skip to content

Commit

Permalink
fix the test
Browse files Browse the repository at this point in the history
  • Loading branch information
lonegunmanb committed Sep 18, 2023
1 parent e6a670c commit a52c2d7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 38 deletions.
47 changes: 38 additions & 9 deletions quickstart/201-vmss-packer-jumpbox/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ terraform {
source = "hashicorp/azurerm"
version = "~>2.0"
}
azapi = {
source = "Azure/azapi"
version = "~> 1.0"
}
local = {
source = "hashicorp/local"
version = "2.4.0"
}
random = {
source = "hashicorp/random"
version = "3.5.1"
Expand All @@ -22,8 +30,10 @@ provider "azurerm" {
features {}
}

resource "random_pet" "id" {}

resource "azurerm_resource_group" "vmss" {
name = var.resource_group_name
name = coalesce(var.resource_group_name, "201-vmss-packer-jumpbox-${random_pet.id.id}")
location = var.location
tags = var.tags
}
Expand Down Expand Up @@ -105,10 +115,29 @@ data "azurerm_image" "image" {
resource_group_name = data.azurerm_resource_group.image.name
}

# RSA key of size 4096 bits
resource "tls_private_key" "rsa-4096" {
algorithm = "RSA"
rsa_bits = 4096
resource "azapi_resource" "ssh_public_key" {
type = "Microsoft.Compute/sshPublicKeys@2022-11-01"
name = random_pet.id.id
location = azurerm_resource_group.vmss.location
parent_id = azurerm_resource_group.vmss.id
}

resource "azapi_resource_action" "ssh_public_key_gen" {
type = "Microsoft.Compute/sshPublicKeys@2022-11-01"
resource_id = azapi_resource.ssh_public_key.id
action = "generateKeyPair"
method = "POST"

response_export_values = ["publicKey", "privateKey"]
}

resource "random_password" "password" {
count = var.admin_password == null ? 1 : 0
length = 20
}

locals {
admin_password = try(random_password.password[0].result, var.admin_password)
}

resource "azurerm_virtual_machine_scale_set" "vmss" {
Expand Down Expand Up @@ -144,15 +173,15 @@ resource "azurerm_virtual_machine_scale_set" "vmss" {
os_profile {
computer_name_prefix = "vmlab"
admin_username = var.admin_user
admin_password = var.admin_password
admin_password = local.admin_password
}

os_profile_linux_config {
disable_password_authentication = true

ssh_keys {
path = "/home/azureuser/.ssh/authorized_keys"
key_data = tls_private_key.rsa-4096.public_key_openssh
key_data = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}
}

Expand Down Expand Up @@ -219,15 +248,15 @@ resource "azurerm_virtual_machine" "jumpbox" {
os_profile {
computer_name = "jumpbox"
admin_username = var.admin_user
admin_password = var.admin_password
admin_password = local.admin_password
}

os_profile_linux_config {
disable_password_authentication = true

ssh_keys {
path = "/home/azureuser/.ssh/authorized_keys"
key_data = file("~/.ssh/id_rsa.pub")
key_data = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}
}

Expand Down
10 changes: 3 additions & 7 deletions quickstart/201-vmss-packer-jumpbox/output.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
output "vmss_public_ip_fqdn" {
value = azurerm_public_ip.vmss.fqdn
value = azurerm_public_ip.vmss.fqdn
}

output "jumpbox_public_ip_fqdn" {
value = azurerm_public_ip.jumpbox.fqdn
value = azurerm_public_ip.jumpbox.fqdn
}

output "jumpbox_public_ip" {
value = azurerm_public_ip.jumpbox.ip_address
}

output "vm_private_key" {
value = tls_private_key.rsa-4096.private_key_pem
value = azurerm_public_ip.jumpbox.ip_address
}
37 changes: 19 additions & 18 deletions quickstart/201-vmss-packer-jumpbox/variables.tf
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
variable "packer_resource_group_name" {
description = "Name of the resource group in which the Packer image will be created"
default = "myPackerImages"
description = "Name of the resource group in which the Packer image will be created"
default = "myPackerImages"
}

variable "packer_image_name" {
description = "Name of the Packer image"
default = "myPackerImage"
description = "Name of the Packer image"
default = "myPackerImage"
}

variable "resource_group_name" {
description = "Name of the resource group in which the Packer image will be created"
default = "myPackerImages"
description = "Name of the resource group in which the Packer image will be created"
default = null
}

variable "location" {
default = "eastus"
description = "Location where resources will be created"
default = "eastus"
description = "Location where resources will be created"
}

variable "tags" {
description = "Map of the tags to use for the resources that are deployed"
type = map(string)
default = {
environment = "codelab"
}
description = "Map of the tags to use for the resources that are deployed"
type = map(string)
default = {
environment = "codelab"
}
}

variable "application_port" {
description = "Port that you want to expose to the external load balancer"
default = 80
description = "Port that you want to expose to the external load balancer"
default = 80
}

variable "admin_user" {
description = "User name to use as the admin account on the VMs that will be part of the VM scale set"
default = "azureuser"
description = "User name to use as the admin account on the VMs that will be part of the VM scale set"
default = "azureuser"
}

variable "admin_password" {
description = "Default password for admin account"
description = "Default password for admin account"
default = null
}
14 changes: 11 additions & 3 deletions test/e2e/quickstart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package e2e
import (
"github.com/gruntwork-io/terratest/modules/files"
"github.com/gruntwork-io/terratest/modules/packer"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
"github.com/stretchr/testify/require"
"os"
"path/filepath"
Expand All @@ -14,7 +15,7 @@ import (
)

var speicalTests = map[string]func(*testing.T){
"quickstart/201-vmss-packer-jumpbox": test201VmssPackerJumpbox,
"quickstart/201-vmss-packer-jumpbox": Test201VmssPackerJumpbox,
}

func Test_Quickstarts(t *testing.T) {
Expand Down Expand Up @@ -69,8 +70,12 @@ func allExamples() ([]string, error) {
return r, nil
}

func test201VmssPackerJumpbox(t *testing.T) {
func Test201VmssPackerJumpbox(t *testing.T) {
examplePath := filepath.Join("..", "..", "quickstart", "201-vmss-packer-jumpbox")
examplePath = test_structure.CopyTerraformFolderToTemp(t, examplePath, "")
defer func() {
_ = os.RemoveAll(examplePath)
}()
harnessPath := filepath.Join(examplePath, "test_harness")
harnessOptions := &terraform.Options{
TerraformDir: harnessPath,
Expand All @@ -79,7 +84,7 @@ func test201VmssPackerJumpbox(t *testing.T) {
terraform.InitAndApply(t, harnessOptions)
harnessOutput := terraform.OutputAll(t, harnessOptions)
imageResourceGroupName := harnessOutput["resource_group_name"].(string)
pkrCfg := filepath.Join("..", "..", "quickstart", "201-vmss-packer-jumpbox", "ubuntu.pkr.hcl")
pkrCfg := filepath.Join(examplePath, "ubuntu.pkr.hcl")
packerVars := map[string]string{
"image_resource_group_name": imageResourceGroupName,
}
Expand All @@ -98,5 +103,8 @@ func test201VmssPackerJumpbox(t *testing.T) {
require.NoError(t, err)
helper.RunE2ETest(t, examplePath, "", terraform.Options{
Upgrade: true,
Vars: map[string]interface{}{
"packer_resource_group_name": imageResourceGroupName,
},
}, nil)
}
2 changes: 1 addition & 1 deletion test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/Azure/terraform-module-test-helper v0.8.0
github.com/gruntwork-io/terratest v0.41.9
github.com/stretchr/testify v1.8.1
)

require (
Expand Down Expand Up @@ -66,7 +67,6 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/tmccombs/hcl2json v0.3.3 // indirect
github.com/ulikunitz/xz v0.5.8 // indirect
github.com/urfave/cli v1.22.2 // indirect
Expand Down

0 comments on commit a52c2d7

Please sign in to comment.