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 support for feature validation with MacOS and Windows OS #165

Merged
merged 15 commits into from
Apr 12, 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
2 changes: 1 addition & 1 deletion generator/test_case_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var testTypeToTestDirMap = map[string][]string{
"../../../test/feature/mac",
},
"ec2_windows": {
"../../../test/feature/win",
"../../../test/feature/windows",
},
"ec2_performance": {
"../../test/performance/logs",
Expand Down
3 changes: 1 addition & 2 deletions internal/awsservice/cloudwatchlogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func DeleteLogGroupAndStream(logGroupName, logStreamName string) {

// DeleteLogStream cleans up log stream by name
func DeleteLogStream(logGroupName, logStreamName string) {

_, err := CwlClient.DeleteLogStream(ctx, &cloudwatchlogs.DeleteLogStreamInput{
LogGroupName: aws.String(logGroupName),
LogStreamName: aws.String(logStreamName),
Expand Down Expand Up @@ -97,7 +96,7 @@ func getLogsSince(logGroup, logStream string, since, until *time.Time) ([]string
if err != nil {
if errors.As(err, &rnf) && attempts <= StandardRetries {
// The log group/stream hasn't been created yet, so wait and retry
time.Sleep(time.Minute)
time.Sleep(30 * time.Second)
SaxyPandaBear marked this conversation as resolved.
Show resolved Hide resolved
continue
}

Expand Down
2 changes: 1 addition & 1 deletion internal/awsservice/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

const (
StandardRetries = 5
StandardRetries = 3
)

var (
Expand Down
32 changes: 25 additions & 7 deletions internal/common/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import (
"io"
"log"
"os"
"runtime"
"time"

"go.uber.org/multierr"
)

const logLine = "# %d - This is a log line. \n"

// StartLogWrite starts go routines to write logs to each of the logs that are monitored by CW Agent according to
// the config provided
func StartLogWrite(configFilePath string, duration time.Duration, sendingInterval time.Duration, logLinesPerMinute int) error {
Expand Down Expand Up @@ -50,15 +53,19 @@ func writeToLogs(filePath string, duration, sendingInterval time.Duration, logLi
defer ticker.Stop()
endTimeout := time.After(duration)

//loop until the test duration is reached
// Sending the logs within the first minute before the ticker kicks in the next minute
for i := 0; i < logLinesPerMinute; i++ {
_, err := f.WriteString(fmt.Sprintf(logLine, i))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The first sending would be more prioritized in catching error. If it fails during the first time, then we would return error immediately without worrying about the second write. However, if the first write success, then it would guarantee to have no errors in the second write.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand. What second write are you referring to? The loop below only has one WriteString call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So we have two writes:

  • First write will be in the first minute
for i := 0; i < logLinesPerMinute; i++ {
		_, err := f.WriteString(fmt.Sprintf(logLine, i))
  • Second write would be after 60 second
for {
		select {
		case <-ticker.C:
			for i := 0; i < logLinesPerMinute; i++ {
				_, err := f.WriteString(fmt.Sprintf("# %d - This is a log line.", i))
				if err != nil {
					log.Printf("Error in writing a string to the file %s: %v ", filePath, err)
				}

So we only need to catch the first error instead of catching the second write errors

if err != nil {
return err
}
}

for {
select {
case <-ticker.C:
for i := 0; i < logLinesPerMinute; i++ {
_, err := f.WriteString(fmt.Sprintf("# %d - This is a log line.", i))
if err != nil {
log.Printf("Error in writing a string to the file %s: %v ", filePath, err)
}
f.WriteString(fmt.Sprintf(logLine, i))
adam-mateen marked this conversation as resolved.
Show resolved Hide resolved
}
case <-endTimeout:
return nil
Expand Down Expand Up @@ -99,6 +106,7 @@ func GenerateLogConfig(numberMonitoredLogs int, filePath string) error {
if numberMonitoredLogs == 0 || filePath == "" {
return errors.New("number of monitored logs or file path is empty")
}

type LogInfo struct {
FilePath string `json:"file_path"`
LogGroupName string `json:"log_group_name"`
Expand Down Expand Up @@ -126,12 +134,13 @@ func GenerateLogConfig(numberMonitoredLogs int, filePath string) error {
}

var logFiles []LogInfo
tempFolder := getTempFolder()

for i := 0; i < numberMonitoredLogs; i++ {
logFiles = append(logFiles, LogInfo{
FilePath: fmt.Sprintf("/tmp/test%d.log", i+1),
FilePath: fmt.Sprintf("%s/test%d.log", tempFolder, i+1),
LogGroupName: "{instance_id}",
LogStreamName: fmt.Sprintf("{instance_id}/tmp%d", i+1),
LogStreamName: fmt.Sprintf("test%d.log", i+1),
RetentionInDays: 1,
Timezone: "UTC",
})
Expand All @@ -153,3 +162,12 @@ func GenerateLogConfig(numberMonitoredLogs int, filePath string) error {

return nil
}

// getTempFolder gets the temp folder for generate logs
// depends on the operating system
func getTempFolder() string {
if runtime.GOOS == "windows" {
return "C:/Users/Administrator/AppData/Local/Temp"
}
return "/tmp"
}
45 changes: 44 additions & 1 deletion internal/common/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,39 @@ func SendCollectDMetrics(metricPerMinute int, sendingInterval, duration time.Dur
defer ticker.Stop()
endTimeout := time.After(duration)

// Sending the collectd metric within the first minute before the ticker kicks in the next minute
for t := 1; t <= metricPerMinute/2; t++ {
_ = client.Write(ctx, &api.ValueList{
Identifier: api.Identifier{
Host: exec.Hostname(),
Plugin: fmt.Sprint("gauge_", t),
Type: "gauge",
},
Time: time.Now(),
Interval: time.Minute,
Values: []api.Value{api.Gauge(t)},
})
adam-mateen marked this conversation as resolved.
Show resolved Hide resolved

err = client.Write(ctx, &api.ValueList{
Identifier: api.Identifier{
Host: exec.Hostname(),
Plugin: fmt.Sprint("counter_", t),
Type: "counter",
},
Time: time.Now(),
Interval: time.Minute,
Values: []api.Value{api.Counter(t)},
})

if err != nil && !errors.Is(err, network.ErrNotEnoughSpace) {
return err
}
}

if err := client.Flush(); err != nil {
return err
}

for {
select {
case <-ticker.C:
Expand Down Expand Up @@ -96,7 +129,7 @@ func SendCollectDMetrics(metricPerMinute int, sendingInterval, duration time.Dur

func SendStatsdMetrics(metricPerMinute int, metricDimension []string, sendingInterval, duration time.Duration) error {
// https://github.com/DataDog/datadog-go#metrics
client, err := statsd.New("127.0.0.1:8125", statsd.WithMaxMessagesPerPayload(100), statsd.WithNamespace("statsd"))
client, err := statsd.New("127.0.0.1:8125", statsd.WithMaxMessagesPerPayload(100), statsd.WithNamespace("statsd"), statsd.WithoutTelemetry())
adam-mateen marked this conversation as resolved.
Show resolved Hide resolved

if err != nil {
return err
Expand All @@ -108,6 +141,16 @@ func SendStatsdMetrics(metricPerMinute int, metricDimension []string, sendingInt
defer ticker.Stop()
endTimeout := time.After(duration)

// Sending the statsd metric within the first minute before the ticker kicks in the next minute
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whenever we call sendStatsdMetrics, we create a ticker for every 60 second, however; the first call (e.g from 0 to 1 minutes) won't be sent; therefore, start sending statsd metrics here during the first time for and after every 60s, we will send statsd metrics regualarly

case <-ticker.C:
			for t := 0; t < metricPerMinute; t++ {
				client.Inc(fmt.Sprint(t), int64(t), 1.0)
				client.Count(fmt.Sprint(t), int64(t), []string{}, 1.0)
			}

for t := 1; t <= metricPerMinute/2; t++ {
if err := client.Count(fmt.Sprint("counter_", t), int64(t), metricDimension, 1.0); err != nil {
return err
}
if err := client.Gauge(fmt.Sprint("gauge_", t), float64(t), metricDimension, 1.0); err != nil {
return err
}
}

for {
select {
case <-ticker.C:
Expand Down
2 changes: 1 addition & 1 deletion terraform/ec2/mac/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ resource "null_resource" "integration_test" {

connection {
type = "ssh"
user = var.user
user = "ec2-user"
private_key = local.private_key_content
host = aws_instance.cwagent.public_ip
timeout = "10m"
Expand Down
5 changes: 0 additions & 5 deletions terraform/ec2/mac/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ variable "ssh_key_value" {
default = ""
}

variable "user" {
type = string
default = "ec2-user"
}

variable "arc" {
type = string
default = "arm64"
Expand Down
3 changes: 1 addition & 2 deletions terraform/ec2/win/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,5 @@ variable "test_name" {

variable "test_dir" {
type = string
default = "../../../test/feature/win"
default = "../../../test/feature/windows"
}

3 changes: 2 additions & 1 deletion test/cloudwatchlogs/publish_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ package cloudwatchlogs

import (
"fmt"
"github.com/stretchr/testify/assert"
"log"
"os"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/aws/amazon-cloudwatch-agent-test/environment"
"github.com/aws/amazon-cloudwatch-agent-test/internal/awsservice"
"github.com/aws/amazon-cloudwatch-agent-test/internal/common"
Expand Down
8 changes: 4 additions & 4 deletions test/ecs/ecs_metadata/ecs_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
_ "embed"
"flag"
"fmt"
"github.com/stretchr/testify/assert"
"log"
"strings"
"testing"
"time"

"github.com/aws/amazon-cloudwatch-agent-test/internal/awsservice"
"github.com/qri-io/jsonschema"
"github.com/stretchr/testify/assert"

"github.com/aws/amazon-cloudwatch-agent-test/internal/awsservice"
)

// Purpose: Detect the changes in metadata endpoint for ECS Container Agent https://github.com/aws/amazon-cloudwatch-agent/blob/main/translator/util/ecsutil/ecsutil.go#L67-L75
Expand Down Expand Up @@ -48,8 +49,7 @@ func TestValidatingCloudWatchLogs(t *testing.T) {
t.Fatalf("Test metadata has exhausted %v retry time", RetryTime)
}

logGroupFound = awsservice.IsLogGroupExists(logGroupName)
if !logGroupFound {
if !awsservice.IsLogGroupExists(logGroupName) {
log.Printf("Current retry: %v/%v and begin to sleep for 20s \n", currentRetry, RetryTime)
time.Sleep(20 * time.Second)
continue
Expand Down
50 changes: 22 additions & 28 deletions test/feature/mac/agent_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
"namespace": "CloudWatchAgentMacFeature",
"metrics_collected": {
"statsd": {
"service_address": ":8125",
"metrics_collection_interval": 60
"metrics_aggregation_interval": 60,
"metrics_collection_interval": 60,
"service_address": ":8125"
},
"cpu": {
"measurement": [
"time_active",
"usage_idle"
"time_guest"
],
"metrics_collection_interval": 1
},
"swap": {
"measurement": [
"free",
"used_percent"
]
],
"metrics_collection_interval": 1
},
"processes": {
"measurement": [
Expand All @@ -36,43 +38,35 @@
],
"metrics_collection_interval": 1
},
"mem": {
"mem": {
"measurement": [
"available_percent",
"used_percent"
],
"metrics_collection_interval": 1
},
"diskio": {
},
"diskio": {
"resources": [
"devfs"
"*"
],
"measurement": [
"iops_in_progress",
"io_time"
],
"metrics_collection_interval": 1
},
"disk": {
},
"disk": {
"resources": [
"devfs"
"*"
],
"measurement": [
"free",
"used_percent"
],
"drop_device": true
},
"ethtool": {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ethtool is not supported in MacOS and only supported in Linux since we are using ethtool from telegraf. Even though CWA rules supports Darwin Rules, it is not possible for collecting ethtool metrics in MacOS

"interface_include": [
"en0"
],
"metrics_include": [
"queue_0_tx_cnt",
"queue_0_rx_cnt"
]
},
"net": {
"drop_device": true,
"metrics_collection_interval": 1
},
"net": {
"resources": [
"en0"
],
Expand All @@ -81,8 +75,8 @@
"bytes_recv"
],
"metrics_collection_interval": 1
},
"procstat": [
},
"procstat": [
{
"exe": "amazon-cloudwatch-agent",
"measurement": [
Expand All @@ -91,12 +85,12 @@
],
"metrics_collection_interval": 1
}
]
]
},
"append_dimensions": {
"InstanceId": "${aws:InstanceId}"
},
"force_flush_interval": 60
"force_flush_interval": 30
},
"logs": {
"logs_collected": {
Expand All @@ -105,7 +99,7 @@
{
"file_path": "/tmp/test1.log",
"log_group_name": "{instance_id}",
"log_stream_name": "{instance_id}",
"log_stream_name": "test1.log",
"timezone": "UTC"
}
]
Expand Down
Loading