116 lines
4.4 KiB
Go
116 lines
4.4 KiB
Go
package bankcoverage
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const assessmentFixturePath = "../../../../../testdata/assessment"
|
|
|
|
func TestRunReportsIncompleteInventoryWithoutFailing(t *testing.T) {
|
|
manifestPath, bundleDir := prepareCoverageFixtures(t)
|
|
var output bytes.Buffer
|
|
if err := Run([]string{"--manifest", manifestPath, "--bundles", bundleDir}, &output, &output); err != nil {
|
|
t.Fatalf("run() error = %v, want informational success", err)
|
|
}
|
|
if !strings.Contains(output.String(), "backend: 0/25 planned family keys present") ||
|
|
!strings.Contains(output.String(), "network: 0/15 planned family keys present") {
|
|
t.Fatalf("report omitted incomplete family counts:\n%s", output.String())
|
|
}
|
|
}
|
|
|
|
func TestRunStrictRefusesIncompleteInventory(t *testing.T) {
|
|
manifestPath, bundleDir := prepareCoverageFixtures(t)
|
|
var output bytes.Buffer
|
|
err := Run([]string{"--strict", "--manifest", manifestPath, "--bundles", bundleDir}, &output, &output)
|
|
if err == nil || !strings.Contains(err.Error(), "assessment.BankCoverageReport.StrictError") {
|
|
t.Fatalf("run(--strict) error = %v, want strict incomplete-inventory error", err)
|
|
}
|
|
if !strings.Contains(output.String(), "Missing family keys (40)") {
|
|
t.Fatalf("strict report omitted missing keys:\n%s", output.String())
|
|
}
|
|
}
|
|
|
|
func TestRunReportsMalformedBundleAndStrictModeRejectsIt(t *testing.T) {
|
|
manifestPath, bundleDir := prepareCoverageFixtures(t)
|
|
malformedPath := filepath.Join(bundleDir, "malformed.json")
|
|
if err := os.WriteFile(malformedPath, []byte("not a bundle"), 0o600); err != nil {
|
|
t.Fatalf("write malformed bundle: %v", err)
|
|
}
|
|
var output bytes.Buffer
|
|
if err := Run([]string{"--manifest", manifestPath, "--bundles", bundleDir}, &output, &output); err != nil {
|
|
t.Fatalf("run() error = %v, want informational success", err)
|
|
}
|
|
if !strings.Contains(output.String(), "Invalid bundle records (1)") || !strings.Contains(output.String(), "malformed.json") {
|
|
t.Fatalf("report omitted malformed bundle diagnostic:\n%s", output.String())
|
|
}
|
|
if err := Run([]string{"--strict", "--manifest", manifestPath, "--bundles", bundleDir}, &output, &output); err == nil {
|
|
t.Fatal("run(--strict) error = nil, want malformed bundle refusal")
|
|
}
|
|
}
|
|
|
|
func TestRunSkipsTheCustomManifestInsideBundleDirectory(t *testing.T) {
|
|
_, bundleDir := prepareCoverageFixtures(t)
|
|
manifestPath := filepath.Join(bundleDir, "plan-custom.json")
|
|
content, err := os.ReadFile(filepath.Join(assessmentFixturePath, "bank_manifest.json"))
|
|
if err != nil {
|
|
t.Fatalf("read manifest fixture: %v", err)
|
|
}
|
|
if err := os.WriteFile(manifestPath, content, 0o600); err != nil {
|
|
t.Fatalf("write custom manifest: %v", err)
|
|
}
|
|
var output bytes.Buffer
|
|
if err := Run([]string{"--manifest", manifestPath, "--bundles", bundleDir}, &output, &output); err != nil {
|
|
t.Fatalf("run() error = %v, want report success", err)
|
|
}
|
|
if strings.Contains(output.String(), "plan-custom.json") {
|
|
t.Fatalf("custom manifest was parsed as a bundle:\n%s", output.String())
|
|
}
|
|
}
|
|
|
|
func TestRunPropagatesOutputWriteFailure(t *testing.T) {
|
|
manifestPath, bundleDir := prepareCoverageFixtures(t)
|
|
writeErr := errors.New("closed output")
|
|
err := Run([]string{"--manifest", manifestPath, "--bundles", bundleDir}, failingCoverageWriter{err: writeErr}, &bytes.Buffer{})
|
|
if !errors.Is(err, writeErr) {
|
|
t.Fatalf("run() error = %v, want wrapped output error", err)
|
|
}
|
|
}
|
|
|
|
func prepareCoverageFixtures(t *testing.T) (string, string) {
|
|
t.Helper()
|
|
root := t.TempDir()
|
|
bundleDir := filepath.Join(root, "bundles")
|
|
if err := os.Mkdir(bundleDir, 0o700); err != nil {
|
|
t.Fatalf("create bundle directory: %v", err)
|
|
}
|
|
manifestContent, err := os.ReadFile(filepath.Join(assessmentFixturePath, "bank_manifest.json"))
|
|
if err != nil {
|
|
t.Fatalf("read manifest fixture: %v", err)
|
|
}
|
|
manifestPath := filepath.Join(root, "bank-plan.json")
|
|
if err := os.WriteFile(manifestPath, manifestContent, 0o600); err != nil {
|
|
t.Fatalf("write manifest fixture: %v", err)
|
|
}
|
|
bundleContent, err := os.ReadFile(filepath.Join(assessmentFixturePath, "backend_demo_bundle.json"))
|
|
if err != nil {
|
|
t.Fatalf("read demo bundle fixture: %v", err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(bundleDir, "backend_demo_bundle.json"), bundleContent, 0o600); err != nil {
|
|
t.Fatalf("write demo bundle fixture: %v", err)
|
|
}
|
|
return manifestPath, bundleDir
|
|
}
|
|
|
|
type failingCoverageWriter struct {
|
|
err error
|
|
}
|
|
|
|
func (writer failingCoverageWriter) Write([]byte) (int, error) {
|
|
return 0, writer.err
|
|
}
|