Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ctlog: test recover behavior with errors on overwrite #12

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/ctlog/ctlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,11 @@ func (l *Log) sequencePool(ctx context.Context, p *pool) (err error) {
return fmtErrorf("couldn't sign checkpoint: %w", err)
}
l.c.Log.DebugContext(ctx, "uploading checkpoint", "size", len(checkpoint))

if testingOnlyFailCommit {
return errors.Join(errFatal, errors.New("failing commit checkpoint for test"))
FiloSottile marked this conversation as resolved.
Show resolved Hide resolved
}

newLock, err := l.c.Lock.Replace(ctx, l.lockCheckpoint, checkpoint)
if err != nil {
// This is a critical error, since we don't know the state of the
Expand Down Expand Up @@ -844,6 +849,8 @@ func (l *Log) sequencePool(ctx context.Context, p *pool) (err error) {

var testingOnlyPauseSequencing func()

var testingOnlyFailCommit bool

// signTreeHead signs the tree and returns a checkpoint according to
// c2sp.org/checkpoint.
func signTreeHead(name string, logID [sha256.Size]byte, privKey *ecdsa.PrivateKey, tree treeWithTimestamp) (checkpoint []byte, err error) {
Expand Down
27 changes: 27 additions & 0 deletions internal/ctlog/ctlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,33 @@ func testReloadLog(t *testing.T, add func(*testing.T, *TestLog) func(context.Con
}
}

func TestRecoverLogFailCommit(t *testing.T) {
tl := NewEmptyTestLog(t)
n := int64(tileWidth + 2)
if testing.Short() {
n = 3
} else {
tl.Quiet()
}

// set to false to make test pass
tl.Config.Backend.(*MemoryBackend).errorOnOverwrite = true

for i := int64(0); i < n; i++ {
addCertificateFast(t, tl) // won't complain on error
ctlog.SetFailCommit(true)
if err := tl.Log.Sequence(); err == nil {
t.Fatal("expected sequencer failure")
}
ctlog.SetFailCommit(false)

tl = ReloadLog(t, tl)
addCertificate(t, tl) // will complain on error
fatalIfErr(t, tl.Log.Sequence())
tl.CheckLog()
}
}

func TestSubmit(t *testing.T) {
t.Run("Certificates", func(t *testing.T) {
testSubmit(t, false)
Expand Down
4 changes: 4 additions & 0 deletions internal/ctlog/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ func PauseSequencer() {
func ResumeSequencer() {
close(seqRunning)
}

func SetFailCommit(b bool) {
testingOnlyFailCommit = b
}
8 changes: 7 additions & 1 deletion internal/ctlog/testlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ type MemoryBackend struct {
mu sync.Mutex
m map[string][]byte

uploads uint64
uploads uint64
errorOnOverwrite bool
}

func NewMemoryBackend(t testing.TB) *MemoryBackend {
Expand All @@ -342,6 +343,11 @@ func (b *MemoryBackend) Upload(ctx context.Context, key string, data []byte, opt
}
b.mu.Lock()
defer b.mu.Unlock()
if opts.Immutable && b.errorOnOverwrite {
if _, ok := b.m[key]; ok {
return fmt.Errorf("key %q already exists", key)
}
}
b.m[key] = data
return nil
}
Expand Down
Loading