Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add win user data test #377

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to have to run this command in terminal when I get terraform fmt error. I should be able to run it via make.

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"},
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to run this on all platforms.
The latest "win-2022" has both a test for ssm and non ssm.
Meaning this will be tested the same 2 times.
This test will only use ssm since it makes the ps1 command simpler.

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"
adam-mateen marked this conversation as resolved.
Show resolved Hide resolved
$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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can upload the value to ssm for all feature tests since it will be deleted by terraform destroy anyway. This will allow us to use ssm for userdata test.

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are using EC2 "userdata" to launch the instance and install the agent, then do we still need winrm access to the instance?

In other words, after launching a Windows EC2 instance with the agent installed with user-data, what additional steps occur?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This runs the validator, it does not reinstall / restart the agent. The validator runs on the ec2 instance.

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[:]))
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This come from running make fmt

}
Loading