mirror of
https://github.com/dsx-ai-factory/nvke.git
synced 2026-12-07 04:35:58 +08:00
* feat(release): export the qualification library for reuse by other repositories Move the qualification and resolve packages from release/internal to release/pkg so another module can import them. Add BuildResolvers, which takes the strategies a caller adds to the built-in ones. An addition that shadows a built-in strategy, is named in both halves, has no factory or builds nothing is refused. qualify passes none, so its behaviour is unchanged. Add Planner.SubjectFile and the --subject-spec flag, so a subject's spec can live apart from the tree --root points at. --root now names only the tree the resolvers read. Closes #2366 Signed-off-by: Harish Sampath <hasampath@nvidia.com> * docs(release): point the layout table at release/pkg The layout table linked the qualification and resolve packages at their old release/internal paths, which docs-check rejects now that they live under release/pkg. Update both links, and say that these two packages are importable from other modules. Signed-off-by: Harish Sampath <hasampath@nvidia.com> * fix(release): keep a Planner from reusing the last plan's commit Plan cached the commit it resolved on the Planner and never cleared it, so a Planner used again with ResolveAtCommit off still recorded the old commit and offered it to the resolvers. Clear it at the start of each Plan, and say that a Planner is not safe for concurrent use. Review feedback on the exported library: - Name the cause when the subject's resolver returns no ref to describe at, instead of an empty ref inside a git error. - Document that a strategy resolving the subject must return a ref naming a commit in the tree at Root when planning at a commit. - Reject an empty strategy name in either map of Strategies. - Cover the commit path of Plan, a Planner used twice, custom strategies planned through the exported API, and a successful plan with the subject's spec outside the tree --root names. Signed-off-by: Harish Sampath <hasampath@nvidia.com> --------- Signed-off-by: Harish Sampath <hasampath@nvidia.com>
158 lines
5.0 KiB
Go
158 lines
5.0 KiB
Go
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package qualification
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/dsx-ai-factory/nvke/release/pkg/resolve"
|
|
)
|
|
|
|
// gitIn runs git in dir and returns what it printed, failing the test if it
|
|
// fails. Signing is off so a developer's own git configuration cannot reach it.
|
|
func gitIn(t *testing.T, dir string, args ...string) string {
|
|
t.Helper()
|
|
cmd := exec.CommandContext(t.Context(), "git", append([]string{
|
|
"-c", "user.name=test", "-c", "user.email=test@example.com",
|
|
"-c", "commit.gpgsign=false", "-c", "tag.gpgsign=false",
|
|
}, args...)...)
|
|
cmd.Dir = dir
|
|
var stderr bytes.Buffer
|
|
cmd.Stderr = &stderr
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, stderr.String())
|
|
}
|
|
return strings.TrimSpace(string(out))
|
|
}
|
|
|
|
// newTaggedRepo makes a repository with one commit that the tag names, and
|
|
// returns its directory and that commit's full hash.
|
|
func newTaggedRepo(t *testing.T, tag string) (dir, commit string) {
|
|
t.Helper()
|
|
dir = t.TempDir()
|
|
gitIn(t, dir, "init", "-q")
|
|
gitIn(t, dir, "commit", "-q", "--allow-empty", "-m", "init")
|
|
gitIn(t, dir, "tag", tag)
|
|
return dir, gitIn(t, dir, "rev-parse", "HEAD")
|
|
}
|
|
|
|
// writeSubjectApart writes a subject spec outside any tree a planner resolves
|
|
// against, and returns its path.
|
|
func writeSubjectApart(t *testing.T, content string) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "qualification.yaml")
|
|
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
// subjectAt resolves the subject to a version and, when ref is not empty, the
|
|
// ref that version describes components at.
|
|
func subjectAt(ref string) *fake {
|
|
return &fake{answer: func(map[string]string) (map[string]string, error) {
|
|
out := map[string]string{resolve.ResultVersionKey: "v0.22.0-beta.1"}
|
|
if ref != "" {
|
|
out[resolve.ResultRefKey] = ref
|
|
}
|
|
return out, nil
|
|
}}
|
|
}
|
|
|
|
// Planning at a commit takes it from the ref the subject's resolver returns,
|
|
// with the subject's spec kept apart from the tree that ref is looked up in.
|
|
// What a resolver that returns no ref, or a ref outside that tree, causes is
|
|
// named, and a plan that does not describe at a commit needs neither.
|
|
func TestPlanAtCommit(t *testing.T) {
|
|
root, head := newTaggedRepo(t, "v0.22.0-beta.1")
|
|
|
|
tests := []struct {
|
|
name string
|
|
atCommit bool
|
|
ref string
|
|
wantCommit string
|
|
wantErr string
|
|
}{
|
|
{"records the commit the subject's ref names", true, "v0.22.0-beta.1", head, ""},
|
|
{"names a resolver that returns no ref", true, "", "", "returned no ref to describe it at"},
|
|
{"names a ref that is not in the tree at Root", true, "v9.9.9", "", "v9.9.9 is not a commit in this repository"},
|
|
{"plans without a commit when not describing at one", false, "", "", ""},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
r := newResolvers()
|
|
r["nvke"] = subjectAt(tc.ref)
|
|
p := newPlanner(t, testSubject, r)
|
|
p.Root = root
|
|
p.SubjectFile = writeSubjectApart(t, testSubject)
|
|
p.ResolveAtCommit = tc.atCommit
|
|
|
|
got, err := p.Plan("nvke", "", "full")
|
|
if tc.wantErr != "" {
|
|
if err == nil || !strings.Contains(err.Error(), tc.wantErr) {
|
|
t.Fatalf("err = %v, want it to contain %q", err, tc.wantErr)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Commit != tc.wantCommit {
|
|
t.Errorf("the plan records commit %q, want %q", got.Commit, tc.wantCommit)
|
|
}
|
|
if tc.wantCommit != "" {
|
|
// The commit is offered to the resolvers, which is how a
|
|
// component is described at it.
|
|
if len(r["cli"].calls) == 0 || r["cli"].calls[0][resolve.InputCommitKey] != tc.wantCommit {
|
|
t.Errorf("the cli resolver was not offered commit %q: %v", tc.wantCommit, r["cli"].calls)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A Planner used again starts from nothing. The commit one plan resolved at is
|
|
// neither recorded in the next nor offered to its resolvers.
|
|
func TestPlanDoesNotCarryACommitToTheNextPlan(t *testing.T) {
|
|
root, head := newTaggedRepo(t, "v0.22.0-beta.1")
|
|
r := newResolvers()
|
|
r["nvke"] = subjectAt("v0.22.0-beta.1")
|
|
p := newPlanner(t, testSubject, r)
|
|
p.Root = root
|
|
p.SubjectFile = writeSubjectApart(t, testSubject)
|
|
|
|
p.ResolveAtCommit = true
|
|
first, err := p.Plan("nvke", "", "full")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if first.Commit != head {
|
|
t.Fatalf("the first plan records commit %q, want %q", first.Commit, head)
|
|
}
|
|
|
|
r["cli"].calls = nil
|
|
p.ResolveAtCommit = false
|
|
second, err := p.Plan("nvke", "", "full")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if second.Commit != "" {
|
|
t.Errorf("the second plan records commit %q, want none", second.Commit)
|
|
}
|
|
if len(r["cli"].calls) == 0 {
|
|
t.Fatal("the cli resolver was never asked, so nothing was checked")
|
|
}
|
|
for _, in := range r["cli"].calls {
|
|
if c := in[resolve.InputCommitKey]; c != "" {
|
|
t.Errorf("the cli resolver was offered the last plan's commit %q", c)
|
|
}
|
|
}
|
|
}
|