Skip to content

Commit

Permalink
ci: add barebone e2e tests with basic awslogs scenario
Browse files Browse the repository at this point in the history
Signed-off-by: Ziwen Ning <[email protected]>
  • Loading branch information
ningziwen committed Sep 14, 2023
1 parent 289fb02 commit d653eaa
Show file tree
Hide file tree
Showing 10 changed files with 469 additions and 36 deletions.
51 changes: 45 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version-file: go.mod
go-version: 'stable'
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Pin the version in case all the builds start to fail at the same time.
# There may not be an automatic way (e.g., dependabot) to update a specific parameter of a GitHub Action,
# so we will just update it manually whenever it makes sense (e.g., a feature that we want is added).
version: v1.54.0
args: --fix=false --timeout=5m
build:
args: --fix=false --timeout=5m --out-format=colored-line-number
unit-tests:
strategy:
fail-fast: false
matrix:
go: [ '1.17', '1.18', '1.19', '1.20', '1.21' ]
os: [ 'ubuntu-22.04', 'windows-2022' ]

name: ${{ matrix.os }} / Go ${{ matrix.go }}
name: Unit Tests / ${{ matrix.os }} / Go ${{ matrix.go }}
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand All @@ -43,8 +43,47 @@ jobs:
cache: false
- name: build
run: make build
- name: test
run: make test
- name: test-unit
run: make test-unit
e2e-tests-for-awslogs:
strategy:
fail-fast: false
matrix:
go: [ '1.17', '1.18', '1.19', '1.20', '1.21' ]
os: [ 'ubuntu-22.04' ] # TODO: Add Windows
name: E2E tests / awslogs / ${{ 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: start ecs local endpoint
shell: bash
run: | # ecs local endpoint can only use credentials in aws credential file instead of env vars
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID --profile default
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY --profile default
aws configure set aws_session_token $AWS_SESSION_TOKEN --profile default
docker run -d --name ecs-local-endpoint -p 51679:51679 -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
- name: ip forwarding # awslogs driver hardcodes "169.254.170.2" as the aws credential endpoint ip so need to forward to local endpoint
shell: bash
run: sudo scripts/ip-forwarding
- name: build
run: make build
- name: test-e2e
run: sudo -E make test-e2e-for-awslogs # containerd interaction requires sudo and aws cloudwatch interaction requires passing env vars
go-mod-tidy-check:
runs-on: ubuntu-latest
steps:
Expand Down
23 changes: 20 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,29 @@ 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 --binary "$(AWS_CONTAINERD_LOGGERS_BINARY)"

.PHONY: test-e2e-for-awslogs
test-e2e:
go test -timeout 30m ./e2e -test.v -ginkgo.v --binary "$(AWS_CONTAINERD_LOGGERS_BINARY)" --log-driver "awslogs"

.PHONY: test-e2e-for-fluentd
test-e2e:
go test -timeout 30m ./e2e -test.v -ginkgo.v --binary "$(AWS_CONTAINERD_LOGGERS_BINARY)" --log-driver "fluentd"

.PHONY: test-e2e-for-splunk
test-e2e:
go test -timeout 30m ./e2e -test.v -ginkgo.v --binary "$(AWS_CONTAINERD_LOGGERS_BINARY)" --log-driver "splunk"

.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
91 changes: 91 additions & 0 deletions e2e/awslogs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/oci"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

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"
testAwsLogsMessage = "test-e2e-log"
)

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() { //nolint:typecheck
ginkgo.It("should send logs to awslogs log driver", func() { //nolint:typecheck
args := map[string]string{
logDriverTypeKey: awslogsDriverName,
containerIdKey: testContainerId,
containerNameKey: testContainerName,
awslogsCredentialsEndpointKey: testAwslogsCredentialEndpoint,
awslogsRegionKey: testAwslogsRegion,
awslogsGroupKey: testAwsLogsGroup,
awslogsStreamKey: testAwsLogsStream,
}
creator := cio.BinaryIO(*Binary, 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("test-snapshot", image), containerd.WithNewSpec(oci.WithImageConfig(image), oci.WithProcessArgs("/bin/sh", "-c", fmt.Sprintf("echo '%s'", testAwsLogsMessage))))
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
defer container.Delete(ctx, containerd.WithSnapshotCleanup) //nolint:errcheck // testing only
// 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) //nolint:errcheck // testing only

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

statusC, err := task.Wait(ctx)
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(uint32(0)))

// Validating in AWS logs
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(testAwslogsRegion))
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
cwClient := cloudwatchlogs.NewFromConfig(cfg)
cwOutput, err := cwClient.GetLogEvents(ctx, &cloudwatchlogs.GetLogEventsInput{
LogStreamName: aws.String(testAwsLogsStream),
LogGroupName: aws.String(testAwsLogsGroup),
Limit: aws.Int32(1),
})
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(*cwOutput.Events[0].Message).Should(gomega.Equal(testAwsLogsMessage))
})
})
}
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() { //nolint:typecheck
// TODO: add test cases
})
}
51 changes: 51 additions & 0 deletions e2e/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
"flag"
"testing"

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

const (
// 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"
)

var (
// Binary is the path the binary of the shim loggers for containerd
Binary = flag.String("binary", "", "the binary of shim loggers for containerd")
LogDriver = flag.String("log-driver", "", "the log driver to test")
)

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

ginkgo.Describe("", func() {
if *LogDriver == awslogsDriverName || *LogDriver == "" {
testAwslogs()
}
if *LogDriver == fluentdDriverName || *LogDriver == "" {
testFluentd()
}
if *LogDriver == splunkDriverName || *LogDriver == "" {
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
})
}
Loading

0 comments on commit d653eaa

Please sign in to comment.