Skip to content

Commit

Permalink
Adding functional test for fluentd logging driver
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpen committed Apr 12, 2017
1 parent 5da2ac5 commit 3054281
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 13 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ benchmark-test:
. ./scripts/shared_env && go test -run=XX -bench=. $(shell go list ./agent/... | grep -v /vendor/)

# Run our 'test' registry needed for integ and functional tests
test-registry: netkitten volumes-test squid awscli image-cleanup-test-images
test-registry: netkitten volumes-test squid awscli image-cleanup-test-images fluentd
@./scripts/setup-test-registry

test-in-docker:
Expand All @@ -94,7 +94,7 @@ volumes-test:

# TODO, replace this with a build on dockerhub or a mechanism for the
# functional tests themselves to build this
.PHONY: squid awscli
.PHONY: squid awscli fluentd
squid:
cd misc/squid; $(MAKE) $(MFLAGS)

Expand All @@ -104,6 +104,9 @@ gremlin:
awscli:
cd misc/awscli; $(MAKE) $(MFLAGS)

fluentd:
cd misc/fluentd; $(MAKE) $(MFLAGS)

image-cleanup-test-images:
cd misc/image-cleanup-test-images; $(MAKE) $(MFLAGS)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"family":"ecs-fts-fluentd-driver",
"containerDefinitions":[
{
"memory":100,
"name":"fluentd-driver",
"cpu":10,
"image":"127.0.0.1:51670/amazon/fluentd:latest",
"portMappings":[
{
"containerPort":24224,
"hostPort":24224
}
],
"mountPoints":[
{
"sourceVolume":"logs",
"containerPath":"/fluentd/log"
}
]
}
],
"volumes":[
{
"name":"logs",
"host":{
"sourcePath":"/tmp"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"family":"ecs-fluentd-test",
"containerDefinitions":[
{
"name":"fluentd-test",
"image":"127.0.0.1:51670/ubuntu:latest",
"essential":true,
"cpu":10,
"memory":10,
"command":[
"sh",
"-c",
"echo hello, this is fluentd functional test; sleep 30s;"
],
"logConfiguration":{
"logDriver":"fluentd",
"options":{
"fluentd-address":"localhost:24224",
"fluentd-tag":"ecs.{{.Name}}.{{.FullID}}"
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"family":"ecs-fluentd-test",
"containerDefinitions":[
{
"name":"fluentd-test",
"image":"127.0.0.1:51670/ubuntu:latest",
"essential":true,
"cpu":10,
"memory":10,
"command":[
"sh",
"-c",
"echo hello, this is fluentd functional test; sleep 30s;"
],
"logConfiguration":{
"logDriver":"fluentd",
"options":{
"fluentd-address":"localhost:24224",
"tag":"ecs.{{.Name}}.{{.FullID}}"
}
}
}
]
}
67 changes: 67 additions & 0 deletions agent/functional_tests/tests/functionaltests_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
logDriverTaskDefinition = "logdriver-jsonfile"
cleanupTaskDefinition = "nginx"
networkModeTaskDefinition = "network-mode"
fluentdLogPath = "/tmp/ftslog"
)

// TestRunManyTasks runs several tasks in short succession and expects them to
Expand Down Expand Up @@ -569,3 +570,69 @@ func TestNetworkModeBridge(t *testing.T) {
err := networkModeTest(t, agent, "bridge")
require.NoError(t, err, "Networking mode bridge testing failed")
}

// TestFluentdTag tests the fluentd logging driver option "tag"
func TestFluentdTag(t *testing.T) {
// tag was added in docker 1.9.0
RequireDockerVersion(t, ">=1.9.0")

fluentdDriverTest("fluentd-tag", t)
}

// TestFluentdLogTag tests the fluentd logging driver option "log-tag"
func TestFluentdLogTag(t *testing.T) {
// fluentd was added in docker 1.8.0
// and deprecated in 1.12.0
RequireDockerVersion(t, ">=1.8.0")
RequireDockerVersion(t, "<1.12.0")

fluentdDriverTest("fluentd-log-tag", t)
}

func fluentdDriverTest(taskDefinition string, t *testing.T) {
agentOptions := AgentOptions{
ExtraEnvironment: map[string]string{
"ECS_AVAILABLE_LOGGING_DRIVERS": `["fluentd"]`,
},
}
agent := RunAgent(t, &agentOptions)
defer agent.Cleanup()

// fluentd is supported in agnet >=1.5.0
agent.RequireVersion(">=1.5.0")

driverTask, err := agent.StartTask(t, "fluentd-driver")
require.NoError(t, err)

err = driverTask.WaitRunning(2 * time.Minute)
require.NoError(t, err)

testTask, err := agent.StartTask(t, taskDefinition)
require.NoError(t, err)

err = testTask.WaitRunning(2 * time.Minute)
assert.NoError(t, err)

dockerID, err := agent.ResolveTaskDockerID(testTask, "fluentd-test")
assert.NoError(t, err, "failed to resolve the container id from agent state")

container, err := agent.DockerClient.InspectContainer(dockerID)
assert.NoError(t, err, "failed to inspect the container")

logTag := fmt.Sprintf("ecs.%v.%v", strings.Replace(container.Name, "/", "", 1), dockerID)

// clean up
err = testTask.WaitStopped(1 * time.Minute)
assert.NoError(t, err, "task failed to be stopped")

driverTask.Stop()
err = driverTask.WaitStopped(1 * time.Minute)
assert.NoError(t, err, "task failed to be stopped")

// Verify the log file existed and also the content contains the expected format
err = SearchStrInDir(fluentdLogPath, "ecsfts", "hello, this is fluentd functional test")
assert.NoError(t, err, "failed to find the content in the fluent log file")

err = SearchStrInDir(fluentdLogPath, "ecsfts", logTag)
assert.NoError(t, err, "failed to find the log tag specified in the task definition")
}
25 changes: 15 additions & 10 deletions agent/functional_tests/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,23 +579,28 @@ func SearchStrInDir(dir, filePrefix, content string) error {
}

var desiredFile string
found := false

for _, file := range logfiles {
if strings.HasPrefix(file.Name(), filePrefix) {
desiredFile = file.Name()
break
}
}
if utils.ZeroOrNil(desiredFile) {
return fmt.Errorf("File with prefix: %v does not exist", filePrefix)
}

if utils.ZeroOrNil(desiredFile) {
return fmt.Errorf("File with prefix: %v does not exist", filePrefix)
}
data, err := ioutil.ReadFile(filepath.Join(dir, desiredFile))
if err != nil {
return fmt.Errorf("Failed to read file, err: %v", err)
}

data, err := ioutil.ReadFile(filepath.Join(dir, desiredFile))
if err != nil {
return fmt.Errorf("Failed to read file, err: %v", err)
if strings.Contains(string(data), content) {
found = true
break
}
}
}

if !strings.Contains(string(data), content) {
if !found {
return fmt.Errorf("Could not find the content: %v in the file: %v", content, desiredFile)
}

Expand Down
3 changes: 3 additions & 0 deletions misc/fluentd/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM fluent/fluentd@sha256:db333f72095e3b982e3f6b105d9c593840ab354beee6486d75536945d372453f

COPY fluent.conf /fluentd/etc/
3 changes: 3 additions & 0 deletions misc/fluentd/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.PHONY: all
all:
docker build -t amazon/fluentd:make .
10 changes: 10 additions & 0 deletions misc/fluentd/fluent.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<source>
@type forward
port 24224
</source>

<match **>
@type file
path /fluentd/log/ftslog/ecsfts.*.log
flush_interval 1s
</match>
2 changes: 1 addition & 1 deletion scripts/setup-test-registry
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ mirror_local_image() {
docker rmi $2
}

for image in "amazon/amazon-ecs-netkitten" "amazon/amazon-ecs-volumes-test" "amazon/squid" "amazon/awscli" "amazon/image-cleanup-test-image1" "amazon/image-cleanup-test-image2" "amazon/image-cleanup-test-image3"; do
for image in "amazon/amazon-ecs-netkitten" "amazon/amazon-ecs-volumes-test" "amazon/squid" "amazon/awscli" "amazon/image-cleanup-test-image1" "amazon/image-cleanup-test-image2" "amazon/image-cleanup-test-image3" "amazon/fluentd"; do
mirror_local_image "${image}:make" "127.0.0.1:51670/${image}:latest"
done

Expand Down

0 comments on commit 3054281

Please sign in to comment.