Skip to content

Commit

Permalink
Add win user data test (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethAmazon authored Nov 13, 2023
1 parent cc44437 commit d06c821
Show file tree
Hide file tree
Showing 7 changed files with 485 additions and 7 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ install-golang-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(TOOLS_BIN_DIR) v1.50.1

fmt: install-goimports addlicense
terraform fmt -recursive
go fmt ./...
@echo $(ALL_SRC) | xargs -n 10 $(GOIMPORTS) $(GOIMPORTS_OPT)

Expand Down
4 changes: 4 additions & 0 deletions generator/test_case_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ var testTypeToTestConfig = map[string][]testConfig{
{testDir: "../../../test/restart"},
{testDir: "../../../test/acceptance"},
{testDir: "../../../test/feature/windows/event_logs"},
{
testDir: "../../../test/feature/windows/userdata",
targets: map[string]map[string]struct{}{"os": {"win-2019": {}}},
},
// assume role test doesn't add much value, and it already being tested with linux
//{testDir: "../../../test/assume_role"},
},
Expand Down
24 changes: 24 additions & 0 deletions terraform/ec2/win/install_and_start_agent.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<powershell>
$installDirectory = "c:\temp\cw"
$downloadDirectory = $installDirectory
$logsDirectory = $installDirectory
$cwAgentInstaller = "$downloadDirectory\amazon-cloudwatch-agent.msi"
$cwAgentInstallPath = "C:\\Program Files\\Amazon\\AmazonCloudWatchAgent"

New-Item -ItemType "directory" -Path $installDirectory

Set-Location -Path $installDirectory

Write-host "Installing Powershell S3 CLI"
Install-PackageProvider NuGet -Force;
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module -Name AWS.Tools.S3 -AllowClobber

Write-host "Installing Cloudwatch Agent"
${copy_object}
Start-Process -FilePath msiexec -Args "/i $cwAgentInstaller /l*v $logsDirectory\installCWAgentLog.log /qn" -Verb RunAs -Wait

Write-host "Load config"

& "$cwAgentInstallPath\amazon-cloudwatch-agent-ctl.ps1" -a fetch-config -m ec2 -s -c ssm:${agent_json_config}
</powershell>
88 changes: 82 additions & 6 deletions terraform/ec2/win/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ resource "aws_instance" "cwagent" {
vpc_security_group_ids = [module.basic_components.security_group]
associate_public_ip_address = true
instance_initiated_shutdown_behavior = "terminate"
user_data = length(regexall("/feature/windows/userdata", var.test_dir)) > 0 ? data.template_file.user_data.rendered : ""
get_password_data = true

metadata_options {
Expand All @@ -72,18 +73,20 @@ resource "aws_instance" "cwagent" {
tags = {
Name = "cwagent-integ-test-ec2-windows-${var.test_name}-${module.common.testing_id}"
}
depends_on = [aws_ssm_parameter.upload_ssm]
}

# Size of windows json is too large thus can't use standard tier
resource "aws_ssm_parameter" "upload_ssm" {
count = var.use_ssm == true && length(regexall("/feature/windows", var.test_dir)) > 0 ? 1 : 0
count = length(regexall("/feature/windows", var.test_dir)) > 0 ? 1 : 0
name = local.ssm_parameter_name
type = "String"
tier = "Advanced"
value = file(module.validator.agent_config)
}

resource "null_resource" "integration_test_setup" {
resource "null_resource" "integration_test_setup_agent" {
count = length(regexall("/feature/windows/userdata", var.test_dir)) > 0 ? 0 : 1
depends_on = [aws_instance.cwagent, module.validator, aws_ssm_parameter.upload_ssm]

# Install software
Expand All @@ -99,6 +102,24 @@ resource "null_resource" "integration_test_setup" {
inline = [
"aws s3 cp s3://${var.s3_bucket}/integration-test/packaging/${var.cwa_github_sha}/amazon-cloudwatch-agent.msi .",
"start /wait msiexec /i amazon-cloudwatch-agent.msi /norestart /qb-",
]
}
}

resource "null_resource" "integration_test_setup_validator" {
depends_on = [aws_instance.cwagent, module.validator, aws_ssm_parameter.upload_ssm]

# Install software
connection {
type = "winrm"
user = "Administrator"
password = rsadecrypt(aws_instance.cwagent.password_data, local.private_key_content)
host = aws_instance.cwagent.public_dns
}

# Install agent binaries
provisioner "remote-exec" {
inline = [
"aws s3 cp s3://${var.s3_bucket}/integration-test/validator/${var.cwa_github_sha}/windows/${var.arc}/validator.exe .",
]
}
Expand All @@ -123,7 +144,8 @@ resource "null_resource" "integration_test_reboot" {
}

depends_on = [
null_resource.integration_test_setup,
null_resource.integration_test_setup_agent,
null_resource.integration_test_setup_validator,
]
}

Expand All @@ -144,7 +166,8 @@ resource "null_resource" "integration_test_run" {
# run go test when it's not feature test
count = length(regexall("/feature/windows", var.test_dir)) < 1 ? 1 : 0
depends_on = [
null_resource.integration_test_setup,
null_resource.integration_test_setup_agent,
null_resource.integration_test_setup_agent,
null_resource.integration_test_wait,
]

Expand All @@ -170,9 +193,10 @@ resource "null_resource" "integration_test_run" {

resource "null_resource" "integration_test_run_validator" {
# run validator only when test_dir is not passed e.g. the default from variable.tf
count = length(regexall("/feature/windows", var.test_dir)) > 0 ? 1 : 0
count = length(regexall("/feature/windows", var.test_dir)) > 0 && length(regexall("/feature/windows/userdata", var.test_dir)) < 1 ? 1 : 0
depends_on = [
null_resource.integration_test_setup,
null_resource.integration_test_setup_agent,
null_resource.integration_test_setup_validator,
null_resource.integration_test_wait,
]

Expand Down Expand Up @@ -213,6 +237,42 @@ resource "null_resource" "integration_test_run_validator" {
}
}

resource "null_resource" "integration_test_run_validator_userdata" {
# run validator only when test_dir is not passed e.g. the default from variable.tf
count = length(regexall("/feature/windows/userdata", var.test_dir)) > 0 ? 1 : 0
depends_on = [
null_resource.integration_test_setup_validator,
null_resource.integration_test_wait,
]

connection {
type = "winrm"
user = "Administrator"
password = rsadecrypt(aws_instance.cwagent.password_data, local.private_key_content)
host = aws_instance.cwagent.public_dns
}

provisioner "file" {
source = module.validator.agent_config
destination = module.validator.instance_agent_config
}

provisioner "file" {
source = module.validator.validator_config
destination = module.validator.instance_validator_config
}

//runs validator and sets up prometheus java agent
provisioner "remote-exec" {
inline = [
"set AWS_REGION=${var.region}",
"validator.exe --validator-config=${module.validator.instance_validator_config} --preparation-mode=true",
"powershell.exe \"& \"C:ProgramFiles\\Amazon\\AmazonCloudWatchAgent\\amazon-cloudwatch-agent-ctl.ps1\" -m ec2 -a status\"",
"validator.exe --validator-config=${module.validator.instance_validator_config} --preparation-mode=false"
]
}
}

data "aws_ami" "latest" {
most_recent = true

Expand All @@ -221,3 +281,19 @@ data "aws_ami" "latest" {
values = [var.ami]
}
}

#####################################################################
# Generate template file for EC2 userdata script
#####################################################################
data "template_file" "user_data" {
template = file("install_and_start_agent.tpl")

vars = {
copy_object = "Copy-S3Object -BucketName ${var.s3_bucket} -Key integration-test/packaging/${var.cwa_github_sha}/amazon-cloudwatch-agent.msi -region ${var.region} -LocalFile $cwAgentInstaller"
agent_json_config = local.ssm_parameter_name
}
}

output "userdata" {
value = data.template_file.user_data.rendered
}
2 changes: 1 addition & 1 deletion test/app_signals/resources/traceid_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ func main() {
binary.BigEndian.PutUint32(r[0:4], uint32(epochNow))
rand.Read(r[4:])
fmt.Printf("%s", hex.EncodeToString(r[:]))
}
}
Loading

0 comments on commit d06c821

Please sign in to comment.