Files
archie/internal/releaseupdate/promote_test.go
T
sam 2181b5fd08
Quality / check (push) Successful in 3m47s
deploy / gate (push) Successful in 6m29s
deploy / archied-image (push) Successful in 2m59s
deploy / agent-image (push) Successful in 10s
deploy / dist-zip (push) Skipped
ci(deploy): move the images' :latest only when a release is promoted (#1716)
2026-10-31 21:48:10 +11:00

168 lines
5.9 KiB
Go

package releaseupdate
import (
"crypto/ed25519"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync"
"testing"
"time"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/samcharles93/archie-core/internal/domain/distribution"
"github.com/samcharles93/archie-core/internal/infrastructure/signedoci"
)
// stable moves only to a signed, non-prerelease release issued no earlier
// than the one it names, and it moves to the very manifest that was verified.
func TestPromote(t *testing.T) {
trusted, signer, _ := ed25519.GenerateKey(nil)
_, stranger, _ := ed25519.GenerateKey(nil)
jan1, jan2 := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC), time.Date(2026, 1, 2, 0, 0, 0, 0, time.UTC)
type published struct {
version string
issued time.Time
prerelease bool
signer ed25519.PrivateKey
tags []string
}
cases := []struct {
name string
release []published
promote string
want string // error substring, or "" when stable must name promote
}{
{name: "a newer release", release: []published{{"1.0.0", jan1, false, signer, []string{"stable"}}, {"1.1.0", jan2, false, signer, []string{"next"}}}, promote: "1.1.0"},
{name: "the first release", release: []published{{"1.0.0", jan1, false, signer, []string{"next"}}}, promote: "1.0.0"},
{name: "a release issued before stable", release: []published{{"1.1.0", jan2, false, signer, []string{"stable"}}, {"1.0.0", jan1, false, signer, nil}}, promote: "1.0.0", want: "issued before"},
{name: "a prerelease", release: []published{{"1.1.0-rc.1", jan2, true, signer, []string{"next"}}}, promote: "1.1.0-rc.1", want: "prerelease"},
{name: "a release another key signed", release: []published{{"1.1.0", jan2, false, stranger, []string{"next"}}}, promote: "1.1.0", want: "signature"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("XDG_STATE_HOME", t.TempDir())
t.Setenv("DOCKER_CONFIG", t.TempDir())
registry := newMemoryRegistry()
server := httptest.NewServer(registry)
defer server.Close()
for _, r := range tc.release {
registry.publish(t, r.version, r.issued, r.prerelease, r.signer, r.tags...)
}
repository := strings.TrimPrefix(server.URL, "http://") + "/archie"
_, err := Promote(t.Context(), repository, trusted, tc.promote, nil)
switch {
case tc.want == "" && err != nil:
t.Fatalf("Promote: %v", err)
case tc.want != "" && (err == nil || !strings.Contains(err.Error(), tc.want)):
t.Fatalf("Promote error = %v, want %q", err, tc.want)
}
stable, ok := registry.statement("stable")
promoted := ok && stable.Version == tc.promote
if promoted != (tc.want == "") {
t.Fatalf("stable names %q (found %v) after promoting %s", stable.Version, ok, tc.promote)
}
if tc.want == "" {
if _, err := Fetch(t.Context(), distribution.Source{Reference: repository, Key: trusted}, "stable"); err != nil {
t.Fatalf("hosts refuse the promoted stable: %v", err)
}
}
})
}
}
// memoryRegistry serves manifests by tag or digest and accepts manifest
// pushes, which is all fetching and retagging a release needs.
type memoryRegistry struct {
mu sync.Mutex
manifests map[string][]byte
blobs map[string][]byte
}
func newMemoryRegistry() *memoryRegistry {
return &memoryRegistry{manifests: map[string][]byte{}, blobs: map[string][]byte{}}
}
func (m *memoryRegistry) publish(t *testing.T, version string, issued time.Time, prerelease bool, signer ed25519.PrivateKey, tags ...string) {
t.Helper()
archive := []byte("zip " + version)
statement, err := json.Marshal(Statement{
Version: version, Prerelease: prerelease, IssuedAt: issued, Platform: Platform, AgentImage: newImage,
Zip: Blob{Name: ZipName(version), Digest: signedoci.Digest(archive), Size: int64(len(archive))},
})
if err != nil {
t.Fatal(err)
}
manifest, err := json.Marshal(ocispec.Manifest{
SchemaVersion: 2, MediaType: ocispec.MediaTypeImageManifest, ArtifactType: ArtifactType, Config: ocispec.DescriptorEmptyJSON,
Layers: []ocispec.Descriptor{
{MediaType: StatementMediaType, Digest: digest.FromBytes(statement), Size: int64(len(statement))},
{MediaType: "application/zip", Digest: digest.FromBytes(archive), Size: int64(len(archive))},
},
Annotations: map[string]string{Annotation: base64.StdEncoding.EncodeToString(ed25519.Sign(signer, statement))},
})
if err != nil {
t.Fatal(err)
}
m.mu.Lock()
defer m.mu.Unlock()
m.blobs[signedoci.Digest(statement)], m.blobs[signedoci.Digest(archive)] = statement, archive
m.manifests[signedoci.Digest(manifest)] = manifest
for _, tag := range append(tags, "v"+version) {
m.manifests[tag] = manifest
}
}
func (m *memoryRegistry) statement(tag string) (Statement, bool) {
m.mu.Lock()
defer m.mu.Unlock()
var manifest ocispec.Manifest
if json.Unmarshal(m.manifests[tag], &manifest) != nil || len(manifest.Layers) == 0 {
return Statement{}, false
}
var s Statement
return s, json.Unmarshal(m.blobs[manifest.Layers[0].Digest.String()], &s) == nil
}
func (m *memoryRegistry) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.mu.Lock()
defer m.mu.Unlock()
if ref, ok := strings.CutPrefix(r.URL.Path, "/v2/archie/blobs/"); ok {
blob, found := m.blobs[ref]
if !found {
http.NotFound(w, r)
return
}
_, _ = w.Write(blob)
return
}
ref, ok := strings.CutPrefix(r.URL.Path, "/v2/archie/manifests/")
if !ok {
http.NotFound(w, r)
return
}
if r.Method == http.MethodPut {
body, _ := io.ReadAll(r.Body)
m.manifests[ref], m.manifests[signedoci.Digest(body)] = body, body
w.WriteHeader(http.StatusCreated)
return
}
manifest, found := m.manifests[ref]
if !found {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", ocispec.MediaTypeImageManifest)
w.Header().Set("Docker-Content-Digest", signedoci.Digest(manifest))
w.Header().Set("Content-Length", strconv.Itoa(len(manifest)))
if r.Method != http.MethodHead {
_, _ = w.Write(manifest)
}
}