Skip to content
Snippets Groups Projects
Commit f8b585ac authored by Cameron Swords's avatar Cameron Swords :ghost:
Browse files

Use a builder for OCI artifact in tests

Also separate artifact and artifacts into different files
parent 48a11ce9
No related branches found
No related tags found
1 merge request!183Create initial publish OCI image step
......@@ -5,71 +5,8 @@ import (
"os"
v1 "github.com/google/go-containerregistry/pkg/v1"
"gitlab.com/gitlab-org/step-runner/pkg/cache/oci/internal"
)
type Artifacts struct {
values []*Artifact
}
func NewArtifacts(artifacts ...*Artifact) *Artifacts {
return &Artifacts{
values: artifacts,
}
}
func (a *Artifacts) ForPlatform(platform *v1.Platform) *Artifacts {
values := make([]*Artifact, 0)
for _, artifact := range a.values {
if artifact.platform.Equals(*platform) {
values = append(values, artifact)
}
}
return NewArtifacts(values...)
}
func (a *Artifacts) Generic() *Artifacts {
return a.ForPlatform(internal.PlatformGeneric)
}
// Platforms returns a unique list of platforms represented by the artifacts.
// The generic platform is excluded from the result set.
func (a *Artifacts) Platforms() []*v1.Platform {
unique := make([]*v1.Platform, 0)
// O(n^2) approach due to a lack reliable platform hash function
for _, artifact := range a.values {
seen := false
for _, platform := range unique {
if artifact.platform.Equals(*platform) {
seen = true
continue
}
}
if !seen && !artifact.platform.Equals(*internal.PlatformGeneric) {
unique = append(unique, artifact.platform)
}
}
return unique
}
func (a *Artifacts) Add(artifacts *Artifacts) *Artifacts {
combined := make([]*Artifact, 0, len(a.values)+len(artifacts.values))
combined = append(combined, a.values...)
combined = append(combined, artifacts.values...)
return NewArtifacts(combined...)
}
func (a *Artifacts) Values() []*Artifact {
return a.values
}
type Artifact struct {
dir string
platform *v1.Platform
......
package oci
import (
v1 "github.com/google/go-containerregistry/pkg/v1"
"gitlab.com/gitlab-org/step-runner/pkg/cache/oci/internal"
)
type Artifacts struct {
values []*Artifact
}
func NewArtifacts(artifacts ...*Artifact) *Artifacts {
return &Artifacts{
values: artifacts,
}
}
func (a *Artifacts) ForPlatform(platform *v1.Platform) *Artifacts {
values := make([]*Artifact, 0)
for _, artifact := range a.values {
if artifact.platform.Equals(*platform) {
values = append(values, artifact)
}
}
return NewArtifacts(values...)
}
func (a *Artifacts) Generic() *Artifacts {
return a.ForPlatform(internal.PlatformGeneric)
}
// Platforms returns a unique list of platforms represented by the artifacts.
// The generic platform is excluded from the result set.
func (a *Artifacts) Platforms() []*v1.Platform {
unique := make([]*v1.Platform, 0)
// O(n^2) approach due to a lack reliable platform hash function
for _, artifact := range a.values {
seen := false
for _, platform := range unique {
if artifact.platform.Equals(*platform) {
seen = true
continue
}
}
if !seen && !artifact.platform.Equals(*internal.PlatformGeneric) {
unique = append(unique, artifact.platform)
}
}
return unique
}
func (a *Artifacts) Add(artifacts *Artifacts) *Artifacts {
combined := make([]*Artifact, 0, len(a.values)+len(artifacts.values))
combined = append(combined, a.values...)
combined = append(combined, artifacts.values...)
return NewArtifacts(combined...)
}
func (a *Artifacts) Values() []*Artifact {
return a.values
}
......@@ -11,8 +11,8 @@ import (
func TestArtifacts_ForPlatform(t *testing.T) {
t.Run("filters by platform", func(t *testing.T) {
generic := oci.NewArtifact("/common", bldr.OCIPlatform.Generic)
linuxAmd64 := oci.NewArtifact("/linux/amd64", bldr.OCIPlatform.LinuxAMD64)
generic := bldr.OCIArtifact(t).Generic().Build()
linuxAmd64 := bldr.OCIArtifact(t).LinuxAMD64().Build()
artifacts := oci.NewArtifacts(generic, linuxAmd64).ForPlatform(bldr.OCIPlatform.Generic)
require.Len(t, artifacts.Values(), 1)
......@@ -20,7 +20,7 @@ func TestArtifacts_ForPlatform(t *testing.T) {
})
t.Run("returns zero artifacts when none match", func(t *testing.T) {
generic := oci.NewArtifact("/common", bldr.OCIPlatform.Generic)
generic := bldr.OCIArtifact(t).Generic().Build()
artifacts := oci.NewArtifacts(generic).ForPlatform(bldr.OCIPlatform.LinuxARM64)
require.Len(t, artifacts.Values(), 0)
......@@ -29,9 +29,9 @@ func TestArtifacts_ForPlatform(t *testing.T) {
func TestArtifacts_Platforms(t *testing.T) {
t.Run("returns unique platforms", func(t *testing.T) {
linuxAmd64A := oci.NewArtifact("/linux/amd64", bldr.OCIPlatform.LinuxAMD64)
linuxArm64A := oci.NewArtifact("/linux/arm64/a", bldr.OCIPlatform.LinuxARM64)
linuxAmd64B := oci.NewArtifact("/linux/arm64/b", bldr.OCIPlatform.LinuxARM64)
linuxAmd64A := bldr.OCIArtifact(t).LinuxAMD64().Build()
linuxArm64A := bldr.OCIArtifact(t).LinuxARM64().Build()
linuxAmd64B := bldr.OCIArtifact(t).LinuxAMD64().Build()
platforms := oci.NewArtifacts(linuxAmd64A, linuxArm64A, linuxAmd64B).Platforms()
require.Len(t, platforms, 2)
......@@ -40,8 +40,8 @@ func TestArtifacts_Platforms(t *testing.T) {
})
t.Run("excludes generic as a platform", func(t *testing.T) {
generic := oci.NewArtifact("/common", bldr.OCIPlatform.Generic)
linuxAmd64 := oci.NewArtifact("/linux/amd64", bldr.OCIPlatform.LinuxAMD64)
generic := bldr.OCIArtifact(t).Generic().Build()
linuxAmd64 := bldr.OCIArtifact(t).LinuxAMD64().Build()
platforms := oci.NewArtifacts(generic, linuxAmd64).Platforms()
require.Len(t, platforms, 1)
......@@ -50,9 +50,9 @@ func TestArtifacts_Platforms(t *testing.T) {
}
func TestArtifacts_Generic(t *testing.T) {
genericA := oci.NewArtifact("/common/a", bldr.OCIPlatform.Generic)
genericB := oci.NewArtifact("/common/b", bldr.OCIPlatform.Generic)
linuxArm64 := oci.NewArtifact("/linux/arm64", bldr.OCIPlatform.LinuxARM64)
genericA := bldr.OCIArtifact(t).Generic().Build()
genericB := bldr.OCIArtifact(t).Generic().Build()
linuxArm64 := bldr.OCIArtifact(t).LinuxARM64().Build()
artifacts := oci.NewArtifacts(genericA, genericB, linuxArm64).Generic()
require.Len(t, artifacts.Values(), 2)
......@@ -61,9 +61,9 @@ func TestArtifacts_Generic(t *testing.T) {
}
func TestArtifacts_Add(t *testing.T) {
a := oci.NewArtifact("/common/a", bldr.OCIPlatform.Generic)
b := oci.NewArtifact("/common/b", bldr.OCIPlatform.Generic)
c := oci.NewArtifact("/linux/arm64", bldr.OCIPlatform.LinuxARM64)
a := bldr.OCIArtifact(t).Generic().Build()
b := bldr.OCIArtifact(t).Generic().Build()
c := bldr.OCIArtifact(t).LinuxARM64().Build()
artifactsA := oci.NewArtifacts(a, b)
artifactsB := oci.NewArtifacts(c)
......
......@@ -28,8 +28,8 @@ func TestReleaser_Release(t *testing.T) {
Build()
artifacts := oci.NewArtifacts(
oci.NewArtifact(filepath.Join(baseDir, "dist/common"), bldr.OCIPlatform.Generic),
oci.NewArtifact(filepath.Join(baseDir, "dist/linux/amd64"), bldr.OCIPlatform.LinuxAMD64))
bldr.OCIArtifact(t).WithDir(filepath.Join(baseDir, "dist/common")).Generic().Build(),
bldr.OCIArtifact(t).WithDir(filepath.Join(baseDir, "dist/linux/amd64")).LinuxAMD64().Build())
downloadDir := t.TempDir()
err := oci.NewReleaser(downloadDir).Release(ctx, remoteImgRef, artifacts)
......@@ -56,9 +56,9 @@ func TestReleaser_Release(t *testing.T) {
Build()
artifacts := oci.NewArtifacts(
oci.NewArtifact(filepath.Join(baseDir, "dist/common"), bldr.OCIPlatform.Generic),
oci.NewArtifact(filepath.Join(baseDir, "dist/linux/amd64"), bldr.OCIPlatform.LinuxAMD64),
oci.NewArtifact(filepath.Join(baseDir, "dist/linux/arm64"), bldr.OCIPlatform.LinuxARM64))
bldr.OCIArtifact(t).WithDir(filepath.Join(baseDir, "dist/common")).Generic().Build(),
bldr.OCIArtifact(t).WithDir(filepath.Join(baseDir, "dist/linux/amd64")).LinuxAMD64().Build(),
bldr.OCIArtifact(t).WithDir(filepath.Join(baseDir, "dist/linux/arm64")).LinuxARM64().Build())
downloadDir := t.TempDir()
err := oci.NewReleaser(downloadDir).Release(ctx, remoteImgRef, artifacts)
......
package bldr
import (
"testing"
v1 "github.com/google/go-containerregistry/pkg/v1"
"gitlab.com/gitlab-org/step-runner/pkg/cache/oci"
)
type OCIArtifactBuilder struct {
dir string
platform *v1.Platform
}
func OCIArtifact(t *testing.T) *OCIArtifactBuilder {
return &OCIArtifactBuilder{
dir: t.TempDir(),
platform: OCIPlatform.LinuxARM64,
}
}
func (bldr *OCIArtifactBuilder) Generic() *OCIArtifactBuilder {
return bldr.WithPlatform(OCIPlatform.Generic)
}
func (bldr *OCIArtifactBuilder) LinuxAMD64() *OCIArtifactBuilder {
return bldr.WithPlatform(OCIPlatform.LinuxAMD64)
}
func (bldr *OCIArtifactBuilder) LinuxARM64() *OCIArtifactBuilder {
return bldr.WithPlatform(OCIPlatform.LinuxARM64)
}
func (bldr *OCIArtifactBuilder) WithPlatform(platform *v1.Platform) *OCIArtifactBuilder {
bldr.platform = platform
return bldr
}
func (bldr *OCIArtifactBuilder) WithDir(dir string) *OCIArtifactBuilder {
bldr.dir = dir
return bldr
}
func (bldr *OCIArtifactBuilder) Build() *oci.Artifact {
return oci.NewArtifact(bldr.dir, bldr.platform)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment