-
Notifications
You must be signed in to change notification settings - Fork 43
Test ECS Servicediscovery on ecs ec2 #538
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9f60497
Add ECS Servicediscovery test against ECS_EC2
agarakan 2eddd10
implement testRunner for ecs_servicediscovery
agarakan 454b0dc
Update validation logic
agarakan 98001c6
testing locally with updated test flags
agarakan 8ccca5e
Format imports for go lint
agarakan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| //go:build !windows | ||
|
|
||
| package ecs_sd | ||
|
|
||
| import ( | ||
| _ "embed" | ||
| "fmt" | ||
| "log" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types" | ||
|
|
||
| "github.com/aws/amazon-cloudwatch-agent-test/environment" | ||
| "github.com/aws/amazon-cloudwatch-agent-test/test/status" | ||
| "github.com/aws/amazon-cloudwatch-agent-test/test/test_runner" | ||
| "github.com/aws/amazon-cloudwatch-agent-test/util/awsservice" | ||
| ) | ||
|
|
||
| const ( | ||
| MaxRetryCount = 15 | ||
| // Log group format: https://github.com/aws/amazon-cloudwatch-agent/blob/5ef3dba446cb56a4c2306878592b5d14300ae82f/translator/translate/otel/exporter/awsemf/prometheus.go#L38 | ||
| ECSLogGroupNameFormat = "/aws/ecs/containerinsights/%s/prometheus" | ||
| // Log stream based on job name in extra_apps.tpl:https://github.com/aws/amazon-cloudwatch-agent-test/blob/main/test/ecs/ecs_sd/resources/extra_apps.tpl#L41 | ||
| LogStreamName = "prometheus-redis" | ||
| ) | ||
|
|
||
| //go:embed resources/emf_prometheus_redis_schema.json | ||
| var schema string | ||
|
|
||
| type ECSServiceDiscoveryTestRunner struct { | ||
| test_runner.BaseTestRunner | ||
| } | ||
|
|
||
| func (t ECSServiceDiscoveryTestRunner) GetTestName() string { | ||
| return "ecs_servicediscovery" | ||
| } | ||
|
|
||
| func (t ECSServiceDiscoveryTestRunner) GetAgentConfigFileName() string { | ||
| return "" | ||
| } | ||
|
|
||
| func (t ECSServiceDiscoveryTestRunner) GetMeasuredMetrics() []string { | ||
| // dummy function to satisfy the interface | ||
| return []string{} | ||
| } | ||
|
|
||
| func (t ECSServiceDiscoveryTestRunner) Validate() status.TestGroupResult { | ||
| var testResults []status.TestResult | ||
| testResults = append(testResults, t.ValidateCloudWatchLogs()) | ||
|
|
||
| return status.TestGroupResult{ | ||
| Name: t.GetTestName(), | ||
| TestResults: testResults, | ||
| } | ||
| } | ||
|
|
||
| func (t ECSServiceDiscoveryTestRunner) ValidateCloudWatchLogs() status.TestResult { | ||
| env := environment.GetEnvironmentMetaData() | ||
| logGroupName := fmt.Sprintf(ECSLogGroupNameFormat, env.EcsClusterName) | ||
|
|
||
| testResult := status.TestResult{ | ||
| Name: fmt.Sprintf("Retrieve Test LogGroup: %s", logGroupName), | ||
| Status: status.FAILED, | ||
| } | ||
|
|
||
| logGroupFound, err := t.ValidateLogGroupFormat(logGroupName) | ||
|
|
||
| if logGroupFound { | ||
| if err != nil { | ||
| log.Printf("ECS ServiceDiscovery Test LogGroups invalid\n") | ||
| testResult.Name = err.Error() | ||
| testResult.Status = status.FAILED | ||
| } else { | ||
| testResult.Status = status.SUCCESSFUL | ||
| } | ||
| awsservice.DeleteLogGroupAndStream(logGroupName, LogStreamName) | ||
| } | ||
| return testResult | ||
| } | ||
|
|
||
| func (t ECSServiceDiscoveryTestRunner) ValidateLogGroupFormat(logGroupName string) (bool, error) { | ||
| start := time.Now() | ||
|
|
||
| for retries := 0; retries < MaxRetryCount; retries++ { | ||
| if awsservice.IsLogGroupExists(logGroupName) { | ||
| end := time.Now() | ||
| return true, t.ValidateLogsContent(logGroupName, start, end) | ||
| } | ||
|
|
||
| log.Printf("Retry %d/%d: Log group not found. Waiting 20 seconds...\n", retries+1, MaxRetryCount) | ||
| time.Sleep(20 * time.Second) | ||
| } | ||
|
|
||
| log.Printf("ECS ServiceDiscovery Test has exhausted %v retry times", MaxRetryCount) | ||
| return false, fmt.Errorf("Test Retries Exhausted: %d", MaxRetryCount) | ||
| } | ||
|
|
||
| func (t ECSServiceDiscoveryTestRunner) ValidateLogsContent(logGroupName string, start time.Time, end time.Time) error { | ||
| return awsservice.ValidateLogs( | ||
| logGroupName, | ||
| LogStreamName, | ||
| &start, | ||
| &end, | ||
| awsservice.AssertLogsNotEmpty(), | ||
| awsservice.AssertPerLog( | ||
| awsservice.AssertLogSchema(awsservice.WithSchema(schema)), | ||
| func(event types.OutputLogEvent) error { | ||
| if strings.Contains(*event.Message, "CloudWatchMetrics") && | ||
| !strings.Contains(*event.Message, "\"Namespace\":\"ECS/ContainerInsights/Prometheus\"") { | ||
| return fmt.Errorf("emf log found for non ECS/ContainerInsights/Prometheus namespace: %s", *event.Message) | ||
| } | ||
| return nil | ||
| }, | ||
| awsservice.AssertLogContainsSubstring("\"job\":\"prometheus-redis\""), | ||
| ), | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.