Skip to content

Commit

Permalink
test: add barebone e2e tests for awslogs
Browse files Browse the repository at this point in the history
Signed-off-by: Ziwen Ning <[email protected]>
  • Loading branch information
ningziwen committed Sep 13, 2023
1 parent 289fb02 commit eb0d349
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 26 deletions.
38 changes: 36 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,42 @@ jobs:
cache: false
- name: build
run: make build
- name: test
run: make test
- name: test-unit
run: make test-unit

e2e-tests:
strategy:
fail-fast: false
matrix:
go: [ '1.20' ]
os: [ 'ubuntu-22.04' ]

name: ${{ matrix.os }} / Go ${{ matrix.go }}
runs-on: ${{ matrix.os }}
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
cache: false
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.ROLE }}
aws-region: ${{ secrets.REGION }}
- name: install and start containerd
shell: bash
run: sudo scripts/install-containerd
- name: build
run: make build
- name: set up ip forwarding
shell: bash
run: sudo scripts/ip-forwarding
- name: test-e2e
run: make test-e2e
go-mod-tidy-check:
runs-on: ubuntu-latest
steps:
Expand Down
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ build: $(AWS_CONTAINERD_LOGGERS_BINARY)
$(AWS_CONTAINERD_LOGGERS_BINARY):
go build -o $(AWS_CONTAINERD_LOGGERS_BINARY) $(AWS_CONTAINERD_LOGGERS_DIR)

test: $(SOURCES)
go test -tags unit -race -timeout 30s -cover $(shell go list ./...) --count=1
.PHONY: test-unit
test-unit: $(SOURCES)
go test -tags unit -race -timeout 30s -cover $(shell go list ./... | grep -v e2e) --count=1

.PHONY: test-e2e
test-e2e:
go test -timeout 30m ./e2e -test.v -ginkgo.v

.PHONY: coverage
coverage:
go test -tags unit $(shell go list ./...) -coverprofile=test-coverage.out
go test -tags unit $(shell go list ./... | grep -v e2e) -coverprofile=test-coverage.out
go tool cover -html=test-coverage.out

.PHONY: lint
Expand Down
86 changes: 86 additions & 0 deletions e2e/awslogs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
"context"
"fmt"
"os/exec"

"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/namespaces"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
specs "github.com/opencontainers/runtime-spec/specs-go"
)

const (
awslogsCredentialsEndpointKey = "awslogs-credentials-endpoint"
awslogsRegionKey = "awslogs-region"
awslogsStreamKey = "awslogs-stream"
awslogsGroupKey = "awslogs-group"
testEcsLocalEndpointPort = "51679"
testAwslogsCredentialEndpoint = ":" + testEcsLocalEndpointPort + "/creds"
testAwslogsRegion = "us-west-2"
testAwsLogsStream = "test-stream"
testAwsLogsGroup = "test-shim-logger"
)

var testAwslogs = func() {
// These tests are run in serial because we only define one log driver instance.
ginkgo.Describe("awslogs shim logger", ginkgo.Serial, func() {
ginkgo.BeforeAll(func() {
cmd := exec.Command("docker", "run", "-d", "--name", "ecs-local-endpoint", "-p", fmt.Sprintf("%s:51679", testEcsLocalEndpointPort), "-v", "$HOME/.aws/:/home/.aws/", "-e", "AWS_REGION=us-west-2", "-e", "HOME=\"/home\"", "-e", "AWS_PROFILE=default", "-e", "ECS_LOCAL_METADATA_PORT=51679", "amazon/amazon-ecs-local-container-endpoints")
err := cmd.Run()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
})
ginkgo.It("should send logs to awslogs log driver", func() {
args := map[string]string{
logDriverTypeKey: awslogsDriverName,
containerIdKey: testContainerId,
containerNameKey: testContainerName,
awslogsCredentialsEndpointKey: testAwslogsCredentialEndpoint,
awslogsRegionKey: testAwslogsRegion,
awslogsGroupKey: testAwsLogsGroup,
awslogsStreamKey: testAwsLogsStream,
}
creator := cio.BinaryIO(shimLoggersBinary, args)
// Create a new client connected to the containerd daemon
client, err := containerd.New(containerdAddress)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
defer client.Close()
// Create a new context with a customized namespace
ctx := namespaces.WithNamespace(context.Background(), "testAwslogs")
// Pull an image
image, err := client.Pull(ctx, testImage, containerd.WithPullUnpack)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
// Create a new container with the pulled image
container, err := client.NewContainer(ctx, testContainerId, containerd.WithImage(image),
containerd.WithNewSnapshot("hello-snapshot", image))
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
defer container.Delete(ctx, containerd.WithSnapshotCleanup)
// Create a new task from the container and start it
task, err := container.NewTask(ctx, creator)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
defer task.Delete(ctx)

err = task.Start(ctx)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())

statusC, err := task.Wait(ctx)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
// Run the "echo" command in the container to print "test-e2e-log" to the log
_, err = task.Exec(ctx, testExecId, &specs.Process{
Args: []string{"/bin/sh", "-c", "echo 'test-e2e-log'"},
}, creator)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
// Waiting for the task to finish
status := <-statusC
code, _, err := status.Result()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(code).Should(gomega.Equal(0))
})
})
}
13 changes: 13 additions & 0 deletions e2e/fluentd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package e2e

import "github.com/onsi/ginkgo/v2"

var testFluentd = func() {
// These tests are run in serial because we only define one log driver instance.
ginkgo.Describe("fluentd shim logger", ginkgo.Serial, func() {
// TODO: add test cases
})
}
40 changes: 40 additions & 0 deletions e2e/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
"testing"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

const (
shimLoggersBinary = "/Users/ningziwe/shim-loggers-for-containerd/shim-loggers-for-containerd"
// LogDriver options
logDriverTypeKey = "log-driver"
awslogsDriverName = "awslogs"
fluentdDriverName = "fluentd"
splunkDriverName = "splunk"
containerIdKey = "container-id"
containerNameKey = "container-name"
testContainerId = "test-container-id"
testContainerName = "test-container-name"
containerdAddress = "/run/containerd/containerd.sock"
testImage = "public.ecr.aws/docker/library/ubuntu:latest"
testExecId = "test-exec-id"
)

func TestShimLoggers(t *testing.T) {
const description = "Shim loggers for containerd E2E Tests"

ginkgo.Describe("", func() {
testAwslogs()
testFluentd()
testSplunk()
})

gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, description)
}
15 changes: 15 additions & 0 deletions e2e/splunk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
"github.com/onsi/ginkgo/v2"
)

var testSplunk = func() {
// These tests are run in serial because we only define one log driver instance.
ginkgo.Describe("splunk shim logger", ginkgo.Serial, func() {
// TODO: add test cases
})
}
56 changes: 40 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,89 @@ require (
github.com/docker/docker v20.10.13+incompatible
github.com/docker/go-units v0.4.0
github.com/golang/mock v1.6.0
github.com/onsi/ginkgo/v2 v2.11.0
github.com/opencontainers/runtime-spec v1.1.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.8.1
golang.org/x/sync v0.1.0
golang.org/x/sync v0.2.0
gotest.tools v2.2.0+incompatible
)

require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/aws/aws-sdk-go v1.34.0 // indirect
github.com/Microsoft/hcsshim v0.8.25 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.2.0 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/cgroups v1.0.3 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/containerd/fifo v1.0.0 // indirect
github.com/containerd/ttrpc v1.1.0 // indirect
github.com/containerd/typeurl v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/fluent/fluent-logger-golang v1.9.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/gogo/googleapis v1.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.3.0 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/klauspost/compress v1.11.13 // indirect
github.com/magiconair/properties v1.8.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/sys/mountinfo v0.4.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.0.2 // indirect
github.com/opencontainers/selinux v1.8.2 // indirect
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/philhofer/fwd v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.3.0 // indirect
github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/tinylib/msgp v1.1.1 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.1.0 // indirect
golang.org/x/tools v0.9.3 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/aws/aws-sdk-go v1.34.0 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/fluent/fluent-logger-golang v1.9.0 // indirect
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/onsi/gomega v1.27.8
github.com/philhofer/fwd v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.1 // indirect
github.com/tinylib/msgp v1.1.1 // indirect
google.golang.org/grpc v1.53.0 // indirect
)

replace github.com/docker/docker v20.10.13+incompatible => github.com/dharmadheeraj/moby v20.10.14-0.20220615184823-6b50baca60ea+incompatible

go 1.17
Loading

0 comments on commit eb0d349

Please sign in to comment.