Skip to content

Commit

Permalink
Add integration tests and support for pushing static file/RPM in a st…
Browse files Browse the repository at this point in the history
…ream fashion. Fix beskar registry transport issue.
  • Loading branch information
cclerget committed Dec 15, 2023
1 parent 1ba1557 commit 5d78fdd
Show file tree
Hide file tree
Showing 36 changed files with 1,968 additions and 125 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,31 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.21'
- uses: actions/checkout@v3
- name: Run linters
run: ./scripts/mage lint:all

tests:
name: tests
runs-on: ubuntu-22.04
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.21'
- uses: actions/checkout@v3
- name: Run unit tests
run: ./scripts/mage test:unit
- name: Run integration tests
run: ./scripts/mage test:integration

build:
name: build
runs-on: ubuntu-22.04
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.21'
- uses: actions/checkout@v3
- name: Build binaries
run: ./scripts/mage build:all
27 changes: 20 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,32 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.21'
- uses: actions/checkout@v3
- name: Run linters
run: ./scripts/mage lint:all

tests:
name: tests
runs-on: ubuntu-22.04
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.21'
- uses: actions/checkout@v3
- name: Run unit tests
run: ./scripts/mage test:unit
- name: Run integration tests
run: ./scripts/mage test:integration

release-beskar:
name: release beskar
needs: lint
needs: [lint, tests]
runs-on: ubuntu-22.04
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.21'
- uses: actions/checkout@v3
- name: Release beskar image
run: ./scripts/mage ci:image ghcr.io/ctrliq/beskar:${{ github.ref_name }} "${{ github.actor }}" "${{ secrets.GITHUB_TOKEN }}"
Expand All @@ -32,12 +45,12 @@ jobs:

release-beskar-yum:
name: release beskar-yum
needs: lint
needs: [lint, tests]
runs-on: ubuntu-22.04
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.21'
- uses: actions/checkout@v3
- name: Release beskar-yum image
run: ./scripts/mage ci:image ghcr.io/ctrliq/beskar-yum:${{ github.ref_name }} "${{ github.actor }}" "${{ secrets.GITHUB_TOKEN }}"
Expand All @@ -46,12 +59,12 @@ jobs:

release-beskar-static:
name: release beskar-static
needs: lint
needs: [lint, tests]
runs-on: ubuntu-22.04
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.21'
- uses: actions/checkout@v3
- name: Release beskar-static image
run: ./scripts/mage ci:image ghcr.io/ctrliq/beskar-static:${{ github.ref_name }} "${{ github.actor }}" "${{ secrets.GITHUB_TOKEN }}"
Expand Down
77 changes: 56 additions & 21 deletions build/mage/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,44 +54,61 @@ type binaryConfig struct {
genAPI *genAPI
buildTags []string
baseImage string
integrationTest *integrationTest
}

const (
beskarBinary = "beskar"
beskarctlBinary = "beskarctl"
beskarYUMBinary = "beskar-yum"
beskarStaticBinary = "beskar-static"
BeskarBinary = "beskar"
BeskarctlBinary = "beskarctl"
BeskarYUMBinary = "beskar-yum"
BeskarStaticBinary = "beskar-static"
)

var binaries = map[string]binaryConfig{
beskarBinary: {
BeskarBinary: {
configFiles: map[string]string{
"internal/pkg/config/default/beskar.yaml": "/etc/beskar/beskar.yaml",
},
useProto: true,
buildTags: []string{"include_gcs"},
integrationTest: &integrationTest{
envs: map[string]string{
"BESKAR_REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY": "/tmp/integration/registry",
"BESKAR_REGISTRY_LOG_ACCESSLOG_DISABLED": "true",
},
},
},
beskarctlBinary: {},
beskarYUMBinary: {
BeskarctlBinary: {},
BeskarYUMBinary: {
configFiles: map[string]string{
"internal/plugins/yum/pkg/config/default/beskar-yum.yaml": "/etc/beskar/beskar-yum.yaml",
},
execStmts: [][]string{
{
//"apk", "add", "-U", "bash", "--repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/",
"sh", "-c", "apt-get update -y && apt-get install -y --no-install-recommends ca-certificates createrepo-c && " +
"rm -rf /var/lib/apt/lists/* && rm -Rf /usr/share/doc && rm -Rf /usr/share/man && apt-get clean",
"apk", "add", "createrepo_c", "--repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/",
// NOTE: restore in case alpine createrepo_c package is broken again
//"sh", "-c", "apt-get update -y && apt-get install -y --no-install-recommends ca-certificates createrepo-c && " +
// "rm -rf /var/lib/apt/lists/* && rm -Rf /usr/share/doc && rm -Rf /usr/share/man && apt-get clean",
},
},
genAPI: &genAPI{
path: "pkg/plugins/yum/api/v1",
filename: "api.go",
interfaceName: "YUM",
},
useProto: true,
baseImage: "debian:bullseye-slim",
useProto: true,
// NOTE: restore in case alpine createrepo_c package is broken again
//baseImage: "debian:bullseye-slim",
integrationTest: &integrationTest{
isPlugin: true,
envs: map[string]string{
"BESKARYUM_ADDR": "127.0.0.1:5200",
"BESKARYUM_GOSSIP_ADDR": "127.0.0.1:5201",
"BESKARYUM_STORAGE_FILESYSTEM_DIRECTORY": "/tmp/integration/beskar-yum",
},
},
},
beskarStaticBinary: {
BeskarStaticBinary: {
configFiles: map[string]string{
"internal/plugins/static/pkg/config/default/beskar-static.yaml": "/etc/beskar/beskar-static.yaml",
},
Expand All @@ -100,8 +117,15 @@ var binaries = map[string]binaryConfig{
filename: "api.go",
interfaceName: "Static",
},
useProto: true,
baseImage: BaseImage,
useProto: true,
integrationTest: &integrationTest{
isPlugin: true,
envs: map[string]string{
"BESKARSTATIC_ADDR": "127.0.0.1:5300",
"BESKARSTATIC_GOSSIP_ADDR": "127.0.0.1:5301",
"BESKARSTATIC_STORAGE_FILESYSTEM_DIRECTORY": "/tmp/integration/beskar-static",
},
},
},
}

Expand Down Expand Up @@ -131,18 +155,18 @@ func (b Build) Proto(ctx context.Context) error {
}

func (b Build) Beskar(ctx context.Context) error {
return b.build(ctx, beskarBinary)
return b.build(ctx, BeskarBinary)
}

func (b Build) Beskarctl(ctx context.Context) error {
return b.build(ctx, beskarctlBinary)
return b.build(ctx, BeskarctlBinary)
}

func (b Build) Plugins(ctx context.Context) {
mg.CtxDeps(
ctx,
mg.F(b.Plugin, beskarYUMBinary),
mg.F(b.Plugin, beskarStaticBinary),
mg.F(b.Plugin, BeskarYUMBinary),
mg.F(b.Plugin, BeskarStaticBinary),
)
}

Expand Down Expand Up @@ -172,13 +196,13 @@ func (b Build) build(ctx context.Context, name string) error {
}
}

buildOpts, ok := getBuildOptions(ctx)

currentPlatform, err := getCurrentPlatform()
if err != nil {
return err
}

buildOpts, ok := getBuildOptions(ctx)

if !ok {
buildOpts = &buildOptions{
platforms: []dagger.Platform{
Expand All @@ -191,6 +215,17 @@ func (b Build) build(ctx context.Context, name string) error {
}
}

files, err := getGoFiles(filepath.Join("cmd", name))
if err != nil {
return err
}
changed, err := target.Path(filepath.Join("build/output", name), files...)
if err != nil {
return err
} else if !changed && !ok {
return nil
}

client := buildOpts.client

if client == nil {
Expand Down
68 changes: 66 additions & 2 deletions build/mage/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (

"dagger.io/dagger"
"golang.org/x/sys/cpu"
"golang.org/x/tools/go/packages"
)

const (
GoImage = "golang:1.21.4-alpine"
GoImage = "golang:1.21.5-alpine3.19"
GolangCILintImage = "golangci/golangci-lint:v1.54.2-alpine"
HelmImage = "alpine/helm:3.12.2"
ProtolintImage = "yoheimuta/protolint:0.45.0"
BaseImage = "alpine:3.17"
BaseImage = "alpine:3.19"

ProtocVersion = "v23.4"
ProtocFileFormat = "protoc-23.4-linux-%s.zip"
Expand Down Expand Up @@ -65,10 +66,28 @@ func getSource(client *dagger.Client) *dagger.Directory {
"README.md",
"go.work",
"go.work.sum",
"integration",
},
})
}

func getIntegrationSource(client *dagger.Client) *dagger.Directory {
return client.Host().Directory(".", dagger.HostDirectoryOpts{
Include: []string{
"go.mod",
"go.sum",
"integration",
"pkg",
},
})
}

func getBuildBinaries(client *dagger.Client, binaries ...string) *dagger.Directory {
return client.Host().Directory("build/output", dagger.HostDirectoryOpts{
Include: binaries,
})
}

func getProtoSource(client *dagger.Client) *dagger.Directory {
return client.Host().Directory("api")
}
Expand Down Expand Up @@ -122,3 +141,48 @@ func getPlatformBinarySuffix(platform string) string {
platform = strings.TrimPrefix(platform, "linux/")
return strings.ReplaceAll(platform, "/", "-")
}

func getGoFiles(dir string) ([]string, error) {
cfg := packages.Config{
Mode: packages.NeedFiles |
packages.NeedEmbedFiles |
packages.NeedImports |
packages.NeedName |
packages.NeedDeps |
packages.NeedModule,
Dir: dir,
}

initial, err := packages.Load(&cfg, ".")
if err != nil {
return nil, fmt.Errorf("while loading directory %s: %s", dir, err)
}
for _, pkg := range initial {
if len(pkg.Errors) > 0 {
return nil, fmt.Errorf("package error: %s", pkg.Errors[0])
}
}

fileMap := make(map[string]struct{})
files := make([]string, 0)

add := func(sourceFiles []string) {
for _, file := range sourceFiles {
if _, ok := fileMap[file]; !ok {
fileMap[file] = struct{}{}
files = append(files, file)
}
}
}

packages.Visit(initial, nil, func(pkg *packages.Package) {
if pkg.Module == nil {
return
}
add(pkg.GoFiles)
add(pkg.OtherFiles)
add(pkg.EmbedFiles)
})

return files, nil
}
8 changes: 4 additions & 4 deletions build/mage/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ go 1.20
require (
dagger.io/dagger v0.7.4
github.com/magefile/mage v1.15.0
golang.org/x/sys v0.12.0
golang.org/x/sys v0.14.0
golang.org/x/tools v0.14.0
)

require (
Expand All @@ -15,7 +16,6 @@ require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/vektah/gqlparser/v2 v2.5.6 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/tools v0.13.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/sync v0.4.0 // indirect
)
15 changes: 9 additions & 6 deletions build/mage/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/vektah/gqlparser/v2 v2.5.6 h1:Ou14T0N1s191eRMZ1gARVqohcbe1e8FrcONScsq8cRU=
github.com/vektah/gqlparser/v2 v2.5.6/go.mod h1:z8xXUff237NntSuH8mLFijZ+1tjV1swDbpDqjJmk6ME=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
Loading

0 comments on commit 5d78fdd

Please sign in to comment.