Browse Source

Tests upkeep (68.9%)

main
Netscape Navigator 2 years ago
parent
commit
6087e221a7
  1. 12
      project/blob.go
  2. 7
      project/cli.go
  3. 29
      project/db.go
  4. 21
      project/db_test.go
  5. 8
      project/filesystem.go
  6. 5
      project/import_bundle.go
  7. 14
      project/migrations.go
  8. 9
      project/parser.go
  9. 19
      project/parser_test.go
  10. 48
      project/peers.go
  11. 5
      project/util.go

12
project/blob.go

@ -6,7 +6,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
)
@ -69,9 +68,10 @@ func addBlobFromPipe() string {
for {
input, err := reader.ReadByte()
if err != nil && err == io.EOF {
if err == io.EOF {
break
}
check(err, "addBlobFromPipe err: %s", err)
output = append(output, input)
}
@ -85,15 +85,11 @@ func write(path string, data []byte) {
os.O_WRONLY|os.O_TRUNC|os.O_CREATE,
0600,
)
if err != nil {
log.Fatal(err)
}
check(err, "Error writing to %s: %s", path, err)
defer file.Close()
_, err2 := file.Write(data)
if err2 != nil {
log.Fatal(err)
}
check(err2, "Write error (II): %s", err2)
}
func getSha256(data []byte) []byte {

7
project/cli.go

@ -181,9 +181,6 @@ func BootstrapCLI() {
rootCmd.AddCommand(bundleCommand)
bundleCommand.AddCommand(bundleIngestCommand)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
err := rootCmd.Execute()
check(err, "Failed to load CLI: %s", err)
}

29
project/db.go

@ -12,15 +12,11 @@ func openDB() *sql.DB {
dbPath := path.Join(pigeonPath, "db.sqlite")
db, err0 := sql.Open("sqlite3", dbPath)
if err0 != nil {
panicf("failed to open db: %s", err0)
}
check(err0, "failed to open db: %s", err0)
err1 := db.Ping()
if err1 != nil {
panicf("failed to ping db: %s", err1)
}
check(err1, "failed to ping db: %s", err1)
migrateUp(db)
@ -43,17 +39,11 @@ func getDB() *sql.DB {
// table
func SetConfig(key string, value []byte) {
tx, err := getDB().Begin()
if err != nil {
panicf("Failed to SetConfig (0): %s", err)
}
check(err, "Failed to SetConfig (0): %s", err)
_, err2 := tx.Exec("INSERT INTO configs(key, value) VALUES(?1, ?2)", key, string(value))
if err2 != nil {
panicf("Failed to SetConfig (1): %s", err2)
}
check(err2, "Failed to SetConfig (1): %s", err2)
err1 := tx.Commit()
if err1 != nil {
panicf("Failed to SetConfig (2): %s", err)
}
check(err1, "Failed to SetConfig (2): %s", err1)
}
// GetConfig retrieves a key/value pair from the database.
@ -61,12 +51,9 @@ func GetConfig(key string) []byte {
var result string
row := getDB().QueryRow("SELECT value FROM configs WHERE key=$1", key)
err := row.Scan(&result)
if err != nil {
if err == sql.ErrNoRows {
panicf("CONFIG MISSING: %s", key)
} else {
panic(err)
}
if err == sql.ErrNoRows {
panicf("CONFIG MISSING: %s", key)
}
check(err, "SOmething else went wrong: %s", err)
return []byte(result)
}

21
project/db_test.go

@ -8,27 +8,20 @@ import (
func resetDB() {
tx, err := getDB().Begin()
if err != nil {
panicf("Failed to start transaction: %s", err)
}
check(err, "Failed to start transaction: %s", err)
for i := len(migrations) - 1; i >= 0; i-- {
_, err := tx.Exec(migrations[i].down)
if err != nil {
panicf("Migration failure: %s", err)
}
check(err, "Migration #%d failure: %s", i, err)
}
for _, migration := range migrations {
_, err := tx.Exec(migration.up)
if err != nil {
panicf("Migration failure: %s", err)
}
check(err, "Migration failure: %s", err)
}
if tx.Commit() != nil {
panic(err)
}
err7 := tx.Commit()
check(err7, "TEST FAILURE db_test.go: %s", err7)
exec.Command("rm", "-rf", pigeonBlobDir())
}
@ -36,7 +29,5 @@ func resetDB() {
func TestSetUpTeardown(t *testing.T) {
resetDB()
err := getDB().Ping()
if err != nil {
t.Fatalf("Test setup failed: %s", err)
}
check(err, "Test setup failed: %s", err)
}

8
project/filesystem.go

@ -13,9 +13,7 @@ func pigeonHomeDir() string {
return customPath
}
home, err := homedir.Dir()
if err != nil {
panicf("Home directory resolution error %s", err)
}
check(err, "Home directory resolution error %s", err)
return path.Join(home, ".pigeon")
}
@ -25,8 +23,6 @@ func pigeonBlobDir() string {
func maybeSetupPigeonDir() string {
err := os.MkdirAll(pigeonHomeDir(), 0700)
if err != nil {
panicf("maybeSetupPigeonDir: %s", err)
}
check(err, "maybeSetupPigeonDir: %s", err)
return pigeonHomeDir()
}

5
project/import_bundle.go

@ -1,7 +1,6 @@
package main
import (
"fmt"
"io/ioutil"
"path"
"path/filepath"
@ -28,9 +27,7 @@ func ingestOneMessage(msg pigeonMessage, blobIndex map[string]bool) {
for rank, pair := range msg.body {
_, err4 := tx.Exec(insertBodyItemQuery, parent, pair.key, pair.value, rank)
if err4 != nil {
fmt.Printf("%s", err4)
}
check(err4, "%s", err4)
rollbackCheck(tx, err4, "Failed to insert body item %d of %s", rank, msg.signature)
}
err5 := tx.Commit()

14
project/migrations.go

@ -57,18 +57,12 @@ var migrations = []migration{
func migrateUp(db *sql.DB) {
tx, err := db.Begin()
if err != nil {
panicf("Failed to start transaction: %s", err)
}
check(err, "Failed to start transaction: %s", err)
for i, migration := range migrations {
_, err := tx.Exec(migration.up)
if err != nil {
panicf("Migration failure(%d): %s", i, err)
}
}
if tx.Commit() != nil {
panic(err)
check(err, "Migration failure(%d): %s", i, err)
}
err3 := tx.Commit()
check(err3, "Transaction commit failure: %s", err3)
}

9
project/parser.go

@ -122,10 +122,7 @@ func parseHeader(state *parserState) {
return
case "depth":
depth, err := strconv.ParseInt(chunks[1], 10, 32)
if err != nil {
tpl := "Parsing bad depth in message %d: %q"
panicf(tpl, len(state.results), chunks[1])
}
check(err, "Parsing bad depth in message %d: %q", len(state.results), chunks[1])
state.buffer.depth = depth
return
default:
@ -151,9 +148,7 @@ func parseFooter(state *parserState) {
state.buffer.signature = chunks[1]
state.mode = parsingDone
err := verifyShallow(&state.buffer)
if err != nil {
panicf("Message verification failed for %s. %s", state.buffer.signature, err)
}
check(err, "Message verification failed for %s. %s", state.buffer.signature, err)
state.results = append(state.results, state.buffer)
state.buffer.body = []pigeonBodyItem{}
state.buffer = pigeonMessage{}

19
project/parser_test.go

@ -2,20 +2,16 @@ package main
import (
"io/ioutil"
"log"
"testing"
)
func TestParser(t *testing.T) {
content, err1 := ioutil.ReadFile("../fixtures/fixture.pgn")
if err1 != nil {
log.Fatal(err1)
}
check(err1, "TEST PARSER ERROR 1: %s", err1)
output, err2 := parseMessage(string(content))
if err2 != nil {
log.Fatal(err2)
}
check(err2, "TEST PARSER ERROR 2: %s", err2)
fixtureSize := 13
length := len(output.messages)
@ -26,14 +22,11 @@ func TestParser(t *testing.T) {
func TestParser2(t *testing.T) {
content, err1 := ioutil.ReadFile("../fixtures/has_blobs/messages.pgn")
if err1 != nil {
log.Fatal(err1)
}
check(err1, "TestParser2 error (1) %s", err1)
parserOutput, err2 := parseMessage(string(content))
output := parserOutput.messages
if err2 != nil {
log.Fatal(err2)
}
check(err2, "TestParser2 error (2) %s", err2)
fixtureSize := 3
length := len(output)

48
project/peers.go

@ -28,54 +28,38 @@ const sqlRemovePeer = "DELETE FROM peers WHERE mhash=$1;"
func getPeerStatus(mHash string) PeerStatus {
var status PeerStatus
row := getDB().QueryRow(sqlFindPeerByStatus, mHash)
switch err := row.Scan(&status); err {
case sql.ErrNoRows:
err := row.Scan(&status)
if err == sql.ErrNoRows {
return "unknown"
case nil:
return status
default:
panicf("getPeerStatus failure: %s", err)
panic(err)
}
check(err, "getPeerStatus error I")
return status
}
func addPeer(mHash string, status PeerStatus) {
tx, err := getDB().Begin()
if err != nil {
panicf("Failed to begin addPeer trx (0): %s", err)
}
check(err, "Failed to begin addPeer trx (0): %s", err)
_, err2 := tx.Exec(sqlCreatePeer, mHash, status)
if err2 != nil {
// This .Commit() call never gets hit:
err1 := tx.Rollback()
if err1 != nil {
panicf("Failed to rollback peer (1): %s", err)
}
check(err1, "Failed to rollback peer (1): %s", err)
panic(fmt.Sprintf("Failure. Possible duplicate peer?: %s", err2))
}
err1 := tx.Commit()
if err1 != nil {
panicf("Failed to commit peer (2): %s", err)
}
check(err1, "Failed to commit peer (2): %s", err1)
}
func removePeer(mHash string) {
tx, err := getDB().Begin()
if err != nil {
panicf("Failed to begin removePeer trx (0): %s", err)
}
check(err, "Failed to begin removePeer trx (0): %s", err)
_, err2 := tx.Exec(sqlRemovePeer, mHash)
if err2 != nil {
err1 := tx.Rollback()
if err1 != nil {
panicf("Failed to rollback removePeer (1): %s", err)
}
check(err1, "Failed to rollback removePeer (1): %s", err)
panic(fmt.Sprintf("Failure. Possible duplicate peer?: %s", err2))
}
err1 := tx.Commit()
if err1 != nil {
panicf("Failed to commit peer removal (2): %s", err)
}
check(err1, "Failed to commit peer removal (2): %s", err1)
}
func listPeers() []peer {
@ -84,22 +68,16 @@ func listPeers() []peer {
mhash string
)
rows, err := getDB().Query(sqlGetAllPeers)
if err != nil {
panicf("showPeers query failure: %s", err)
}
check(err, "showPeers query failure: %s", err)
defer rows.Close()
output := []peer{}
for rows.Next() {
err := rows.Scan(&mhash, &status)
if err != nil {
panicf("Show peers row scan failure: %s", err)
}
check(err, "Show peers row scan failure: %s", err)
output = append(output, peer{mhash: mhash, status: status})
}
err = rows.Err()
if err != nil {
panicf("showPeers row error: %s", err)
}
check(err, "showPeers row error: %s", err)
return output
}

5
project/util.go

@ -31,9 +31,7 @@ func createOrShowIdentity() string {
// as a Base32 encoded string
func CreateIdentity() (ed25519.PublicKey, ed25519.PrivateKey) {
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
panicf("Keypair creation error %s", err)
}
check(err, "Keypair creation error %s", err)
SetConfig("public_key", pub)
SetConfig("private_key", priv)
return pub, priv
@ -52,6 +50,7 @@ func rollbackCheck(tx *sql.Tx, e error, tpl string, args ...interface{}) {
func check(e error, tpl string, args ...interface{}) {
if e != nil {
fmt.Printf("=== NEW ERROR PLEASE REPORT: %s\n", e)
panicf(tpl, args...)
}
}

Loading…
Cancel
Save