Skip to content

Commit

Permalink
upgrade to Terraform v0.12 (#122)
Browse files Browse the repository at this point in the history
* upgrade to v0.12

* add comment for test

* add comment for test

* remove useless test code

* change bool type

* remove useless import

* add os back
  • Loading branch information
yupwei68 authored Jan 10, 2020
1 parent 97f2ecc commit 9952198
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 212 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:
- docker

env:
- TERRAFORM_VERSION=0.11.7 IMAGE_NAME=azure-compute-module
- TERRAFORM_VERSION=0.12.10 IMAGE_NAME=azure-compute-module

jobs:
include:
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Pull the base image with given version.
ARG BUILD_TERRAFORM_VERSION="0.11.7"
FROM microsoft/terraform-test:${BUILD_TERRAFORM_VERSION}
ARG BUILD_TERRAFORM_VERSION="0.12.10"
FROM mcr.microsoft.com/terraform-test:${BUILD_TERRAFORM_VERSION}

ARG MODULE_NAME="terraform-azurerm-compute"

Expand Down Expand Up @@ -33,5 +33,6 @@ RUN ssh-keygen -q -t rsa -b 4096 -f $HOME/.ssh/id_rsa
ENV GOPATH /go
ENV PATH /usr/local/go/bin:$GOPATH/bin:$PATH
RUN /bin/bash -c "curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh"
RUN terraform init

RUN ["bundle", "install", "--gemfile", "./Gemfile"]
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ source 'https://rubygems.org/'

group :test do
git 'https://github.com/Azure/terramodtest.git' do
gem 'terramodtest', :tag => 'v0.2.0'
gem 'terramodtest', :tag => 'v0.3.0'
end
end
236 changes: 118 additions & 118 deletions main.tf

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
output "vm_ids" {
description = "Virtual machine ids created."
value = "${concat(azurerm_virtual_machine.vm-windows.*.id,azurerm_virtual_machine.vm-windows-with-datadisk.*.id, azurerm_virtual_machine.vm-linux.*.id,azurerm_virtual_machine.vm-linux-with-datadisk.*.id)}"
value = "${concat(azurerm_virtual_machine.vm-windows.*.id, azurerm_virtual_machine.vm-windows-with-datadisk.*.id, azurerm_virtual_machine.vm-linux.*.id, azurerm_virtual_machine.vm-linux-with-datadisk.*.id)}"
}

output "network_security_group_id" {
Expand Down
37 changes: 17 additions & 20 deletions test/fixture/main.tf
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
provider "random" {
version = "~> 1.0"
}

resource "random_id" "ip_dns" {
byte_length = 8
}

module "ubuntuservers" {
source = "../../"
location = "${var.location}"
admin_username = "${var.admin_username}"
admin_password = "${var.admin_password}"
vm_os_simple = "${var.vm_os_simple_1}"
location = var.location
admin_username = var.admin_username
admin_password = var.admin_password
vm_os_simple = var.vm_os_simple_1
public_ip_dns = ["ubuntusimplevmips-${random_id.ip_dns.hex}"]
vnet_subnet_id = "${module.network.vnet_subnets[0]}"
ssh_key = "${var.ssh_key}"
vnet_subnet_id = module.network.vnet_subnets[0]
ssh_key = var.ssh_key
resource_group_name = "${var.resource_group_name}-${random_id.ip_dns.hex}"
public_ip_address_allocation = "static"
allocation_method = "Static"
enable_accelerated_networking = "true"
vm_size = "Standard_DS2_V2"
}

module "debianservers" {
source = "../../"
location = "${var.location}"
location = var.location
vm_hostname = "mylinvm"
admin_username = "${var.admin_username}"
admin_password = "${var.admin_password}"
custom_data = "${var.custom_data}"
vm_os_simple = "${var.vm_os_simple_2}"
admin_username = var.admin_username
admin_password = var.admin_password
custom_data = var.custom_data
vm_os_simple = var.vm_os_simple_2
public_ip_dns = ["debiansimplevmips-${random_id.ip_dns.hex}"] // change to a unique name per datacenter region
vnet_subnet_id = "${module.network.vnet_subnets[0]}"
ssh_key = "${var.ssh_key}"
vnet_subnet_id = module.network.vnet_subnets[0]
ssh_key = var.ssh_key
resource_group_name = "${var.resource_group_name}-${random_id.ip_dns.hex}"
public_ip_address_allocation = "static"
allocation_method = "Static"
}

module "network" {
source = "Azure/network/azurerm"
version = "2.0.0"
location = "westus2"
subnet_names = ["subnet1"]
resource_group_name = "${var.resource_group_name}-${random_id.ip_dns.hex}"
}
}
63 changes: 3 additions & 60 deletions test/terraform_ssh_example_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package test

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"

"github.com/gruntwork-io/terratest/modules/retry"
"github.com/gruntwork-io/terratest/modules/ssh"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/gruntwork-io/terratest/modules/test-structure"
)
Expand All @@ -36,14 +28,9 @@ func TestTerraformSshExample(t *testing.T) {
terraform.InitAndApply(t, terraformOptions)
})

// Make sure we can SSH to virtual machines directly from the public Internet
test_structure.RunTestStage(t, "validate", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, exampleFolder)

testSSHToPublicHost(t, terraformOptions, "ubuntu_ip_address")
testSSHToPublicHost(t, terraformOptions, "debian_ip_address")
})

// It has ever been planned to test the VM could be accessed from the public through SSH,
// however currently this connection is constrained because the testing VM in CI is within the Microsoft internal environment and the public cannot access it.
// So skip this test.
}

func configureTerraformOptions(t *testing.T, exampleFolder string) *terraform.Options {
Expand All @@ -59,47 +46,3 @@ func configureTerraformOptions(t *testing.T, exampleFolder string) *terraform.Op
return terraformOptions
}

func testSSHToPublicHost(t *testing.T, terraformOptions *terraform.Options, address string) {
// Run `terraform output` to get the value of an output variable
publicIP := terraform.Output(t, terraformOptions, address)

// Read private key from given file
buffer, err := ioutil.ReadFile(os.Args[len(os.Args)-1])
if err != nil {
t.Fatal(err)
}
keyPair := ssh.KeyPair{PrivateKey: string(buffer)}

// We're going to try to SSH to the virtual machine, using our local key pair and specific username
publicHost := ssh.Host{
Hostname: publicIP,
SshKeyPair: &keyPair,
SshUserName: os.Args[len(os.Args)-2],
}

// It can take a minute or so for the virtual machine to boot up, so retry a few times
maxRetries := 15
timeBetweenRetries := 5 * time.Second
description := fmt.Sprintf("SSH to public host %s", publicIP)

// Run a simple echo command on the server
expectedText := "Hello, World"
command := fmt.Sprintf("echo -n '%s'", expectedText)

// Verify that we can SSH to the virtual machine and run commands
retry.DoWithRetry(t, description, maxRetries, timeBetweenRetries, func() (string, error) {
// Run the command and get the output
actualText, err := ssh.CheckSshCommandE(t, publicHost, command)
if err != nil {
return "", err
}

// Check whether the output is correct
if strings.TrimSpace(actualText) != expectedText {
return "", fmt.Errorf("Expected SSH command to return '%s' but got '%s'", expectedText, actualText)
}
fmt.Println(actualText)

return "", nil
})
}
25 changes: 16 additions & 9 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ variable "vm_os_id" {

variable "is_windows_image" {
description = "Boolean flag to notify when the custom image is windows based."
default = "false"
default = false
}

variable "vm_os_publisher" {
Expand All @@ -97,7 +97,7 @@ variable "vm_os_version" {
}

variable "tags" {
type = "map"
type = map(string)
description = "A map of the tags to use on the resources that are deployed with this module."

default = {
Expand All @@ -106,8 +106,13 @@ variable "tags" {
}

variable "public_ip_address_allocation" {
description = "This attribute is deprecated, and to be replaced by 'allocation_method'"
default = ""
}

variable "allocation_method" {
description = "Defines how an IP address is assigned. Options are Static or Dynamic."
default = "dynamic"
default = ""
}

variable "nb_public_ip" {
Expand All @@ -116,8 +121,9 @@ variable "nb_public_ip" {
}

variable "delete_os_disk_on_termination" {
type = bool
description = "Delete datadisk when machine is terminated"
default = "false"
default = false
}

variable "data_sa_type" {
Expand All @@ -131,14 +137,15 @@ variable "data_disk_size_gb" {
}

variable "data_disk" {
type = "string"
type = bool
description = "Set to true to add a datadisk."
default = "false"
default = false
}

variable "boot_diagnostics" {
type = bool
description = "(Optional) Enable or Disable boot diagnostics"
default = "false"
default = false
}

variable "boot_diagnostics_sa_type" {
Expand All @@ -147,7 +154,7 @@ variable "boot_diagnostics_sa_type" {
}

variable "enable_accelerated_networking" {
type = "string"
type = bool
description = "(Optional) Enable accelerated networking on Network interface"
default = "false"
default = false
}

0 comments on commit 9952198

Please sign in to comment.