80 lines
2.5 KiB
Go
80 lines
2.5 KiB
Go
package buildstep
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/delivery"
|
|
)
|
|
|
|
type toolchainKeys map[string]delivery.Toolchain
|
|
|
|
func (k toolchainKeys) put(key string, value any) (uint64, error) {
|
|
tc, err := delivery.DecodeToolchain(value.(json.RawMessage))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
k[key] = tc
|
|
return uint64(len(k)), nil
|
|
}
|
|
|
|
func guardFor(k toolchainKeys, installed string, probeErr error) Guard {
|
|
return Guard{Language: "go", Want: "1.27.0", Recipe: "r7", Node: "ace", Put: k.put,
|
|
Probe: func(context.Context) (string, error) { return installed, probeErr }}
|
|
}
|
|
|
|
// 1473/AC1: the probe answers the recipe's version: nil, and <node>.go holds that version and the recipe's.
|
|
func TestCheckPassesAndWritesTheToolchain(t *testing.T) {
|
|
k := toolchainKeys{}
|
|
if err := guardFor(k, "1.27.0", nil).Check(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tc, ok := k["ace.go"]
|
|
if !ok || tc.Version != "1.27.0" || tc.Recipe != "r7" || tc.Language != "go" || tc.At.IsZero() {
|
|
t.Errorf("keys = %+v", k)
|
|
}
|
|
}
|
|
|
|
// 1473/AC2: another version: a Refusal naming both, and the key holds the installed version.
|
|
func TestCheckRefusesAnotherVersionAndWritesTheInstalledOne(t *testing.T) {
|
|
k := toolchainKeys{}
|
|
err := guardFor(k, "1.27.1", nil).Check(context.Background())
|
|
var ref *Refusal
|
|
if !errors.As(err, &ref) {
|
|
t.Fatalf("Check = %v, want a Refusal", err)
|
|
}
|
|
if ref.Text != "go 1.27.1 installed, the recipe builds with go 1.27.0" || ref.Want != "1.27.0" || ref.Have != "1.27.1" {
|
|
t.Errorf("refusal = %+v", ref)
|
|
}
|
|
if k["ace.go"].Version != "1.27.1" {
|
|
t.Errorf("the key holds %q, want the installed 1.27.1", k["ace.go"].Version)
|
|
}
|
|
}
|
|
|
|
// 1473/AC3: a probe that fails: a Refusal naming the failure, and nothing put.
|
|
func TestCheckRefusesAFailedProbeAndPutsNothing(t *testing.T) {
|
|
k := toolchainKeys{}
|
|
err := guardFor(k, "", errors.New("go: not found")).Check(context.Background())
|
|
var ref *Refusal
|
|
if !errors.As(err, &ref) || !strings.Contains(ref.Text, "go: not found") {
|
|
t.Fatalf("Check = %v, want a Refusal naming the probe's failure", err)
|
|
}
|
|
if len(k) != 0 {
|
|
t.Errorf("put %v", k)
|
|
}
|
|
}
|
|
|
|
// A failed write is an error, not a refusal: the step fails instead of refusing.
|
|
func TestCheckAnswersAFailedWriteAsAnError(t *testing.T) {
|
|
g := guardFor(toolchainKeys{}, "1.27.0", nil)
|
|
g.Put = func(string, any) (uint64, error) { return 0, errors.New("no grant") }
|
|
err := g.Check(context.Background())
|
|
var ref *Refusal
|
|
if err == nil || errors.As(err, &ref) {
|
|
t.Errorf("Check = %v", err)
|
|
}
|
|
}
|