Harden Harness runtime, persistence and execution for Hand
This commit is contained in:
1 parent
09c30fe2d5
commit
0972eed3ea
164 files changed
+15912
-506
No files matched your search
@@ -0,0 +1,54 @@
|
||||
package execution
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWorkerSnapshotPinsBytesAcrossSourceReplacement(t *testing.T) {
|
||||
source := filepath.Join(t.TempDir(), "worker")
|
||||
original := []byte("#!/bin/sh\nprintf original\n")
|
||||
if err := os.WriteFile(source, original, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hash := sha256.Sum256(original)
|
||||
digest := hex.EncodeToString(hash[:])
|
||||
frozen, err := snapshotWorker(context.Background(), source, digest, t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = os.WriteFile(source, []byte("#!/bin/sh\nprintf replaced\n"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw, err := os.ReadFile(frozen)
|
||||
if err != nil || string(raw) != string(original) {
|
||||
t.Fatal("snapshot changed with source")
|
||||
}
|
||||
dir := t.TempDir()
|
||||
if _, err = snapshotWorker(context.Background(), source, digest, dir); err == nil {
|
||||
t.Fatal("changed worker accepted")
|
||||
}
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil || len(entries) != 0 {
|
||||
t.Fatal("failed snapshot retained")
|
||||
}
|
||||
}
|
||||
func TestWorkerSnapshotRejectsCancellationAndInvalidDigest(t *testing.T) {
|
||||
source := filepath.Join(t.TempDir(), "worker")
|
||||
if err := os.WriteFile(source, []byte("worker"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := snapshotWorker(context.Background(), source, "invalid", t.TempDir()); err == nil {
|
||||
t.Fatal("invalid digest accepted")
|
||||
}
|
||||
hash := sha256.Sum256([]byte("worker"))
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if _, err := snapshotWorker(ctx, source, hex.EncodeToString(hash[:]), t.TempDir()); err != context.Canceled {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user