Skip to content

Commit

Permalink
tests: Log network interfaces before and after destroy
Browse files Browse the repository at this point in the history
For better upstream Terraform debugging we would like to log the network
interfaces related to the VPC. For further details see:
hashicorp/terraform-provider-aws#1051
  • Loading branch information
mxinden committed Oct 30, 2017
1 parent dc5ae9e commit 6a8ab83
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
27 changes: 27 additions & 0 deletions tests/rspec/lib/aws_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'env_var'
require 'aws_iam'
require 'aws_support'
require 'tfstate_file'

# AWSCluster represents a k8s cluster on AWS cloud provider
class AwsCluster < Cluster
Expand Down Expand Up @@ -110,4 +111,30 @@ def tectonic_console_url
ingress_ext
end
end

private

def destroy
# For debugging purposes (see: https://github.com/terraform-providers/terraform-provider-aws/pull/1051)
describe_network_interfaces

super

# For debugging purposes (see: https://github.com/terraform-providers/terraform-provider-aws/pull/1051)
describe_network_interfaces
end

def describe_network_interfaces
puts 'describing network interfaces for debugging purposes'
vpc_id = @tfstate_file.value('module.vpc.aws_vpc.cluster_vpc', 'id')
filter = "--filters=Name=vpc-id,Values=#{vpc_id}"
region = "--region #{@aws_region}"

success = system("aws ec2 describe-network-interfaces #{filter} #{region}")
raise 'failed to describe network interfaces by vpc' unless success

# Do not fail build. This is only for debugging purposes
rescue => e
puts e
end
end
5 changes: 4 additions & 1 deletion tests/rspec/lib/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'securerandom'
require 'jenkins'
require 'tfvars_file'
require 'tfstate_file'
require 'fileutils'
require 'name_generator'
require 'password_generator'
Expand All @@ -12,7 +13,7 @@
# Cluster represents a k8s cluster
class Cluster
attr_reader :tfvars_file, :kubeconfig, :manifest_path, :build_path,
:tectonic_admin_email, :tectonic_admin_password
:tectonic_admin_email, :tectonic_admin_password, :tfstate_file

def initialize(tfvars_file)
@tfvars_file = tfvars_file
Expand All @@ -26,6 +27,8 @@ def initialize(tfvars_file)
@build_path = File.join(File.realpath('../../'), "build/#{@name}")
@manifest_path = File.join(@build_path, 'generated')
@kubeconfig = File.join(manifest_path, 'auth/kubeconfig')
@tfstate_file = TFStateFile.new(@build_path)

check_prerequisites
localconfig
prepare_assets
Expand Down
32 changes: 32 additions & 0 deletions tests/rspec/lib/tfstate_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

# TFStateFile represents a Terraform state file
class TFStateFile
def initialize(build_path)
@build_path = build_path
end

def value(address, wanted_key)
file_exists?

Dir.chdir(@build_path) do
state = `terraform state show #{address}`.chomp.split("\n")
state.each do |value|
key, value = value.split('=')
key = key.strip.chomp
value = value.strip.chomp
return value if key == wanted_key
end
end

msg = "could not find value for key \"#{wanted_key}\" in tfstate file #{@build_path}"
raise TFStateFileValueForKeyDoesNotExist, msg
end

def file_exists?
tfstate_file = File.join(@build_path, 'terraform.tfstate')
raise "tfstate file #{tfstate_file} does not exist" unless File.exist? tfstate_file
end
end

class TFStateFileValueForKeyDoesNotExist < StandardError; end

0 comments on commit 6a8ab83

Please sign in to comment.