Skip to content

Commit

Permalink
chore: added test coverage and upload coveralls
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Case <[email protected]>
  • Loading branch information
richardcase committed Sep 8, 2021
1 parent a1aff98 commit 531f08a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 118 deletions.
18 changes: 13 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
name: test

on: [pull_request]
on: [pull_request, push]

jobs:
test:
name: test
runs-on: ubuntu-latest
steps:
- name: Set up containerd
uses: crazy-max/ghaction-setup-containerd@v1
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '1.16'
- run: go version
- name: Test
run: make test
go-version: '1.16'
- name: Test with coverage
run: sudo --preserve-env=CTR_SOCK_PATH make test-with-cov
env:
CTR_SOCK_PATH: /run/containerd/containerd.sock
- uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: out/coverage.out
env:
COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
9 changes: 3 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,9 @@ lint: $(GOLANGCI_LINT) $(BUF) ## Lint
test: ## Run unit tests
go test -v -race ./...

.PHONY: test-int
test-int: $(OUT_DIR) ## Run tests (including intengration tests)
CTR_ROOT_DIR=$(OUT_DIR)/containerd
mkdir -p $(CTR_ROOT_DIR)
sudo go test -v -race -count=1 ./...
sudo rm -rf $(CTR_ROOT_DIR)
.PHONY: test-with-cov
test-with-cov: $(OUT_DIR) ## Run unit tests with coverage
go test -v -covermode=count -coverprofile=$(OUT_DIR)/coverage.out ./...

.PHONY: test-e2e
test-e2e: ## Run e2e tests
Expand Down
104 changes: 2 additions & 102 deletions infrastructure/containerd/image_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package containerd_test

import (
"context"
_ "embed"
"fmt"
"os"
"os/exec"
"syscall"
"testing"

. "github.com/onsi/gomega"
Expand All @@ -25,9 +22,6 @@ var (
testSnapshotter = "native"
testOwnerNamespace = "int_ns"
testOwnerName = "imageservice-get-test"

//go:embed testdata/config.toml
containerdConfig string
)

func TestImageService_Integration(t *testing.T) {
Expand Down Expand Up @@ -89,32 +83,8 @@ func TestImageService_Integration(t *testing.T) {
Expect(err).NotTo(HaveOccurred())
}

func TestMain(m *testing.M) {
if !runContainerDTests() {
os.Exit(m.Run())
}

rootDir := os.Getenv("CTR_ROOT_DIR")
if err := os.RemoveAll(rootDir); err != nil {
fmt.Fprintf(os.Stderr, "could not empty test folder %s: %s\n", rootDir, err)
os.Exit(1)
}
fmt.Printf("the containerd root folder is %s\n", rootDir)

cleanup, err := startContainerd(rootDir)
if err != nil {
fmt.Fprintf(os.Stderr, "could not start containerd: %s\n", err)
os.Exit(1)
}

status := m.Run()

cleanup()
os.Exit(status)
}

func testCreateClient(t *testing.T) (*ctr.Client, context.Context) {
addr := containerDTestSocketPath()
addr := os.Getenv("CTR_SOCK_PATH")
client, err := ctr.New(addr)
Expect(err).NotTo(HaveOccurred())

Expand All @@ -128,76 +98,6 @@ func testCreateClient(t *testing.T) (*ctr.Client, context.Context) {
}

func runContainerDTests() bool {
testCtr := os.Getenv("CTR_ROOT_DIR")
testCtr := os.Getenv("CTR_SOCK_PATH")
return testCtr != ""
}

func containerDTestSocketPath() string {
rootDir := os.Getenv("CTR_ROOT_DIR")
return fmt.Sprintf("%s/containerd.sock", rootDir)
}

func startContainerd(rootDir string) (func(), error) {
root := fmt.Sprintf("%s/root", rootDir)
os.MkdirAll(root, os.ModePerm)
state := fmt.Sprintf("%s/state", rootDir)
os.MkdirAll(state, os.ModePerm)
addr := containerDTestSocketPath()
cfg := fmt.Sprintf("%s/containerd.config", rootDir)

stdOutFile, err := os.Create(fmt.Sprintf("%s/stdout.txt", rootDir))
if err != nil {
return nil, fmt.Errorf("could not open containerd stdout file file %s: %w", stdOutFile.Name(), err)
}
stdErrFile, err := os.Create(fmt.Sprintf("%s/stderr.txt", rootDir))
if err != nil {
return nil, fmt.Errorf("could not open containerd stderr file file %s: %w", stdErrFile.Name(), err)
}

if err := writeContainerdConfig(cfg); err != nil {
return nil, fmt.Errorf("writing containerd config file: %w", err)
}

args := []string{
"--address",
addr,
"--root",
root,
"--state",
state,
"--log-level",
"debug",
"--config",
cfg,
}
cmd := exec.Command("containerd", args...)
cmd.Stdout = stdOutFile
cmd.Stderr = stdErrFile
if err := cmd.Start(); err != nil {
cmd.Wait()
return nil, fmt.Errorf("failed to start containerd: %w", err)
}

cleanup := func() {
stdOutFile.Close()
stdErrFile.Close()
cmd.Process.Signal(syscall.SIGTERM)
cmd.Process.Wait()
}

return cleanup, nil
}

func writeContainerdConfig(configPath string) error {
cfgFile, err := os.Create(configPath)
defer cfgFile.Close()

if err != nil {
return fmt.Errorf("could not open containerd config file %s: %w", configPath, err)
}
if _, err := cfgFile.WriteString(containerdConfig); err != nil {
return fmt.Errorf("Failed to write to config file %s: %w", configPath, err)
}

return nil
}
5 changes: 0 additions & 5 deletions infrastructure/containerd/testdata/config.toml

This file was deleted.

0 comments on commit 531f08a

Please sign in to comment.