Skip to content

Commit

Permalink
fix message size in a message in gentestdata
Browse files Browse the repository at this point in the history
and work around the message in test-upgrade.sh.
and add subcommand to open an account, triggering data upgrades.
  • Loading branch information
mjl- committed Aug 16, 2023
1 parent ddf3cb3 commit 1ccc5d0
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
2 changes: 2 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ possibly making them potentially no longer readable by the previous version.
usage: mox verifydata data-dir
-fix
fix fixable problems, such as moving away message files not referenced by their database
-skip-size-check
skip the check for message size
# mox config test
Expand Down
2 changes: 1 addition & 1 deletion gentestdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ Accounts:
MailboxDestinedID: sent.ID,
Flags: store.Flags{Seen: true, Junk: true},
Size: int64(len(prefix1) + len(msg1)),
MsgPrefix: []byte(prefix),
MsgPrefix: []byte(prefix1),
}
mf1 := tempfile()
xcheckf(err, "creating temp file for delivery")
Expand Down
25 changes: 25 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ var commands = []struct {
{"gentestdata", cmdGentestdata},
{"ximport maildir", cmdXImportMaildir},
{"ximport mbox", cmdXImportMbox},
{"openaccounts", cmdOpenaccounts},
}

var cmds []cmd
Expand Down Expand Up @@ -2378,3 +2379,27 @@ func cmdMessageParse(c *cmd) {
err = enc.Encode(part)
xcheckf(err, "write")
}

func cmdOpenaccounts(c *cmd) {
c.unlisted = true
c.params = "datadir account ..."
c.help = `Open and close accounts, for triggering data upgrades, for tests.
Opens database files directly, not going through a running mox instance.
`

args := c.Parse()
if len(args) <= 1 {
c.Usage()
}

dataDir := filepath.Clean(args[0])
for _, accName := range args[1:] {
accDir := filepath.Join(dataDir, "accounts", accName)
log.Printf("opening account %s...", filepath.Join(accDir, accName))
a, err := store.OpenAccountDB(accDir, accName)
xcheckf(err, "open account %s", accName)
err = a.Close()
xcheckf(err, "close account %s", accName)
}
}
19 changes: 13 additions & 6 deletions store/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,21 +677,27 @@ func OpenAccount(name string) (*Account, error) {
if err != nil {
return nil, err
}
acc.nused++
openAccounts.names[name] = acc
return acc, nil
}

// openAccount opens an existing account, or creates it if it is missing.
func openAccount(name string) (a *Account, rerr error) {
dir := filepath.Join(mox.DataDirPath("accounts"), name)
dbpath := filepath.Join(dir, "index.db")
return OpenAccountDB(dir, name)
}

// OpenAccountDB opens an account database file and returns an initialized account
// or error. Only exported for use by subcommands that verify the database file.
// Almost all account opens must go through OpenAccount/OpenEmail/OpenEmailAuth.
func OpenAccountDB(accountDir, accountName string) (a *Account, rerr error) {
dbpath := filepath.Join(accountDir, "index.db")

// Create account if it doesn't exist yet.
isNew := false
if _, err := os.Stat(dbpath); err != nil && os.IsNotExist(err) {
isNew = true
os.MkdirAll(dir, 0770)
os.MkdirAll(accountDir, 0770)
}

db, err := bstore.Open(context.TODO(), dbpath, &bstore.Options{Timeout: 5 * time.Second, Perm: 0660}, DBTypes...)
Expand Down Expand Up @@ -719,7 +725,7 @@ func openAccount(name string) (a *Account, rerr error) {
return bstore.QueryTx[Mailbox](tx).FilterEqual("HaveCounts", false).ForEach(func(mb Mailbox) error {
if !mentioned {
mentioned = true
xlog.Info("first calculation of mailbox counts for account", mlog.Field("account", name))
xlog.Info("first calculation of mailbox counts for account", mlog.Field("account", accountName))
}
mc, err := mb.CalculateCounts(tx)
if err != nil {
Expand All @@ -736,10 +742,11 @@ func openAccount(name string) (a *Account, rerr error) {
}

return &Account{
Name: name,
Dir: dir,
Name: accountName,
Dir: accountDir,
DBPath: dbpath,
DB: db,
nused: 1,
}, nil
}

Expand Down
20 changes: 16 additions & 4 deletions test-upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cd testdata/upgrade
# Check that we can upgrade what we currently generate.
../../mox gentestdata data
../../mox verifydata data
../../mox openaccounts data test0 test1 test2
rm -r data
echo

Expand All @@ -38,8 +39,13 @@ for tag in $tagsrev; do
(CGO_ENABLED=0 GOBIN=$PWD/$tag go install github.com/mjl-/mox@$tag)
# Generate with historic release.
./$tag/mox gentestdata $tag/data
# Verify with current code.
../../mox verifydata $tag/data
# Verify with current code. v0.0.[45] had a message with wrong Size. We don't
# want to abort the upgrade check because of it.
if test $tag = v0.0.4 -o $tag = v0.0.5; then
../../mox verifydata -skip-size-check $tag/data
else
../../mox verifydata $tag/data
fi
echo
rm -r $tag/data
done
Expand All @@ -65,12 +71,18 @@ for tag in $tags; do
fi

echo "Upgrade data to $tag."
time ./$tag/mox verifydata stepdata
if test $tag = v0.0.4 -o $tag = v0.0.5; then
time ./$tag/mox verifydata stepdata
else
time ./$tag/mox verifydata -skip-size-check stepdata
time ./$tag/mox openaccounts stepdata test0 test1 test2
fi
echo
fi
done
echo "Testing final upgrade to current."
time ../../mox verifydata stepdata
time ../../mox verifydata -skip-size-check stepdata
time ../../mox openaccounts stepdata test0 test1 test2
rm -r stepdata
rm */mox
cd ../..
Expand Down
8 changes: 7 additions & 1 deletion verifydata.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ possibly making them potentially no longer readable by the previous version.
`
var fix bool
c.flag.BoolVar(&fix, "fix", false, "fix fixable problems, such as moving away message files not referenced by their database")

// To prevent aborting the upgrade test with v0.0.[45] that had a message with
// incorrect Size.
var skipSizeCheck bool
c.flag.BoolVar(&skipSizeCheck, "skip-size-check", false, "skip the check for message size")

args := c.Parse()
if len(args) != 1 {
c.Usage()
Expand Down Expand Up @@ -140,7 +146,7 @@ possibly making them potentially no longer readable by the previous version.
checkFile := func(dbpath, path string, prefixSize int, size int64) {
st, err := os.Stat(path)
checkf(err, path, "checking if file exists")
if err == nil && int64(prefixSize)+st.Size() != size {
if !skipSizeCheck && err == nil && int64(prefixSize)+st.Size() != size {
filesize := st.Size()
checkf(fmt.Errorf("%s: message size is %d, should be %d (length of MsgPrefix %d + file size %d), see \"mox fixmsgsize\"", path, size, int64(prefixSize)+st.Size(), prefixSize, filesize), dbpath, "checking message size")
}
Expand Down

0 comments on commit 1ccc5d0

Please sign in to comment.