Files
ThothII/tools/tht/internal/preflight/plan_test.go
T

98 lines
3.4 KiB
Go

package preflight
import (
"crypto/sha256"
"fmt"
"github.com/aritmolab/thothii/tools/tht/internal/config"
"github.com/aritmolab/thothii/tools/tht/internal/safeio"
"os"
"path/filepath"
"strings"
"testing"
)
func TestExecutionPlanAcceptsOnlyExactReleaseOwnedMaterialization(t *testing.T) {
root, _ := filepath.EvalSymlinks(t.TempDir())
_ = os.Chmod(root, 0o700)
_ = os.Mkdir(filepath.Join(root, "deploy"), 0o700)
input := filepath.Join(root, "input")
_ = os.WriteFile(input, []byte("prepared"), 0o600)
overlay := filepath.Join(root, "deploy", "compose.git-https.yaml")
data := []byte("services: {}\n")
plan := Plan{SchemaVersion: 1, ValidatorProtocol: Protocol, Installation: config.Installation{ProjectDirectory: root}, Inputs: []string{input}, AbsentInputs: []string{overlay}, Report: NewReport(), Release: Manifest{Files: map[string]string{"deploy/compose.git-https.yaml": fmt.Sprintf("%x", sha256.Sum256(data))}}}
path := filepath.Join(root, "plan.json")
if err := WritePlan(path, &plan); err != nil {
t.Fatal(err)
}
_ = os.WriteFile(overlay, data, 0o600)
if _, err := ReadExecutionPlan(path); err != nil {
t.Fatal(err)
}
if VerifyPlanInputs(path) == nil {
t.Fatal("ordinary validation must still detect new files")
}
_ = os.WriteFile(overlay, []byte("changed"), 0o600)
if _, err := ReadExecutionPlan(path); err == nil {
t.Fatal("modified materialization accepted")
}
}
func TestPlanBindsInputsAndDetectsCredentialRotationWithoutPublicSecretHashes(t *testing.T) {
root, _ := filepath.EvalSymlinks(t.TempDir())
if err := safeio.ProtectPrivateDirectory(root); err != nil {
t.Fatal(err)
}
input := filepath.Join(root, "descriptor.yaml")
secret := filepath.Join(root, "credential")
for path, data := range map[string]string{input: "schemaVersion: 2\n", secret: "PRIVATE_SENTINEL"} {
if err := safeio.WriteCanonicalNewPrivateFile(path, []byte(data), 0o600); err != nil {
t.Fatal(err)
}
}
plan := Plan{SchemaVersion: 1, ValidatorProtocol: Protocol, Inputs: []string{input, secret}, WorkspaceRevision: "content-snapshot", Report: NewReport()}
output := filepath.Join(root, "plan.json")
if err := WritePlan(output, &plan); err != nil {
t.Fatal(err)
}
data, _ := os.ReadFile(output)
if strings.Contains(string(data), "PRIVATE_SENTINEL") {
t.Fatal("secret in plan")
}
if err := VerifyPlanInputs(output); err != nil {
t.Fatal(err)
}
if err := WritePlan(output, &plan); err == nil {
t.Fatal("existing plan replaced")
}
if err := os.WriteFile(secret, []byte("rotated"), 0o600); err != nil {
t.Fatal(err)
}
if err := VerifyPlanInputs(output); err == nil {
t.Fatal("credential rotation did not invalidate plan")
}
if err := os.WriteFile(secret, []byte("PRIVATE_SENTINEL"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(input, []byte("schemaVersion: 3\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := VerifyPlanInputs(output); err == nil {
t.Fatal("document change did not invalidate plan")
}
if err := os.WriteFile(input, []byte("schemaVersion: 2\n"), 0o600); err != nil {
t.Fatal(err)
}
absent := filepath.Join(root, "transport-override.yaml")
plan.AbsentInputs = []string{absent}
second := filepath.Join(root, "second-plan.json")
if err := WritePlan(second, &plan); err != nil {
t.Fatal(err)
}
if err := safeio.WriteCanonicalNewPrivateFile(absent, []byte("services: {}\n"), 0o600); err != nil {
t.Fatal(err)
}
if VerifyPlanInputs(second) == nil {
t.Fatal("newly appearing override did not invalidate plan")
}
}