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

Adds postgres implementation for tree_storage #1343

Merged
merged 5 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
52 changes: 13 additions & 39 deletions storage/postgres/admin_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,37 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package postgres_test
package postgres
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it not possible to do black box testing of this? (just curious)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Admin storage is actually currently blackbox tested; however I wanted to bring everything under the postgres package because

A) that seems to be the pattern the rest of the codebase follows
B) It allows us to use a unified TestMain function.

Happy to change it back though, I don't have strong feelings about this


import (
"context"
"database/sql"
"flag"
"fmt"
"os"
"testing"

"github.com/golang/glog"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/empty"
"github.com/google/trillian"
"github.com/google/trillian/storage"
"github.com/google/trillian/storage/postgres"
"github.com/google/trillian/storage/postgres/testdb"
"github.com/google/trillian/storage/testonly"
)

var allTables = []string{"unsequenced", "tree_head", "sequenced_leaf_data", "leaf_data", "subtree", "tree_control", "trees"}
var DB *sql.DB

const selectTreeControlByID = "SELECT signing_enabled, sequencing_enabled, sequence_interval_seconds FROM tree_control WHERE tree_id = $1"

func TestPgAdminStorage(t *testing.T) {
tester := &testonly.AdminStorageTester{NewAdminStorage: func() storage.AdminStorage {
cleanTestDB(DB, t)
return postgres.NewAdminStorage(DB)
cleanTestDB(db, t)
return NewAdminStorage(db)
}}
tester.RunAllTests(t)
}

func TestAdminTX_CreateTree_InitializesStorageStructures(t *testing.T) {
cleanTestDB(DB, t)
s := postgres.NewAdminStorage(DB)
cleanTestDB(db, t)
s := NewAdminStorage(db)
ctx := context.Background()

tree, err := storage.CreateTree(ctx, s, testonly.LogTree)
Expand All @@ -59,7 +53,7 @@ func TestAdminTX_CreateTree_InitializesStorageStructures(t *testing.T) {
// Check if TreeControl is correctly written.
var signingEnabled, sequencingEnabled bool
var sequenceIntervalSeconds int
if err := DB.QueryRowContext(ctx, selectTreeControlByID, tree.TreeId).Scan(&signingEnabled, &sequencingEnabled, &sequenceIntervalSeconds); err != nil {
if err := db.QueryRowContext(ctx, selectTreeControlByID, tree.TreeId).Scan(&signingEnabled, &sequencingEnabled, &sequenceIntervalSeconds); err != nil {
t.Fatalf("Failed to read TreeControl: %v", err)
}
// We don't mind about specific values, defaults change, but let's check
Expand All @@ -70,8 +64,8 @@ func TestAdminTX_CreateTree_InitializesStorageStructures(t *testing.T) {
}

func TestCreateTreeInvalidStates(t *testing.T) {
cleanTestDB(DB, t)
s := postgres.NewAdminStorage(DB)
cleanTestDB(db, t)
s := NewAdminStorage(db)
ctx := context.Background()

states := []trillian.TreeState{trillian.TreeState_DRAINING, trillian.TreeState_FROZEN}
Expand All @@ -86,8 +80,8 @@ func TestCreateTreeInvalidStates(t *testing.T) {
}

func TestAdminTX_TreeWithNulls(t *testing.T) {
cleanTestDB(DB, t)
s := postgres.NewAdminStorage(DB)
cleanTestDB(db, t)
s := NewAdminStorage(db)
ctx := context.Background()

// Setup: create a tree and set all nullable columns to null.
Expand All @@ -99,7 +93,7 @@ func TestAdminTX_TreeWithNulls(t *testing.T) {
}
treeID := tree.TreeId

if err := setNulls(ctx, DB, treeID); err != nil {
if err := setNulls(ctx, db, treeID); err != nil {
t.Fatalf("setNulls() = %v, want = nil", err)
}

Expand Down Expand Up @@ -155,8 +149,8 @@ func TestAdminTX_TreeWithNulls(t *testing.T) {
}

func TestAdminTX_StorageSettingsNotSupported(t *testing.T) {
cleanTestDB(DB, t)
s := postgres.NewAdminStorage(DB)
cleanTestDB(db, t)
s := NewAdminStorage(db)
ctx := context.Background()

settings, err := ptypes.MarshalAny(&empty.Empty{})
Expand Down Expand Up @@ -207,14 +201,6 @@ func cleanTestDB(db *sql.DB, t *testing.T) {
}
}

func openTestDBOrDie() *sql.DB {
db, err := testdb.NewTrillianDB(context.TODO())
if err != nil {
panic(err)
}
return db
}

func setNulls(ctx context.Context, db *sql.DB, treeID int64) error {
stmt, err := db.PrepareContext(ctx, `
UPDATE trees SET
Expand All @@ -229,15 +215,3 @@ func setNulls(ctx context.Context, db *sql.DB, treeID int64) error {
_, err = stmt.ExecContext(ctx, treeID)
return err
}

func TestMain(m *testing.M) {
flag.Parse()
if !testdb.PGAvailable() {
glog.Errorf("PG not available, skipping all PG storage tests")
return
}
DB = openTestDBOrDie()
defer DB.Close()
ec := m.Run()
os.Exit(ec)
}
45 changes: 45 additions & 0 deletions storage/postgres/storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package postgres

import (
"context"
"database/sql"
"flag"
"os"
"testing"
"time"

"github.com/golang/glog"
"github.com/google/trillian/storage/postgres/testdb"
)

// db is shared throughout all postgres tests
var db *sql.DB

func TestMain(m *testing.M) {
flag.Parse()
ec := 0
defer func() { os.Exit(ec) }()
if !testdb.PGAvailable() {
glog.Errorf("PG not available, skipping all PG storage tests")
ec = 1
return
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second*30))
defer cancel()
db = testdb.OpenTestDBOrDie(ctx)
defer db.Close()
ec = m.Run()
}
12 changes: 11 additions & 1 deletion storage/postgres/testdb/testdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func NewTrillianDB(ctx context.Context) (*sql.DB, error) {
}

// sanitize tries to remove empty lines and comments from a sql script
// to prevent them from being executed
// to prevent them from being executed.
func sanitize(script string) string {
buf := &bytes.Buffer{}
for _, line := range strings.Split(string(script), "\n") {
Expand All @@ -120,3 +120,13 @@ func sanitize(script string) string {
func getConnStr(name string) string {
return fmt.Sprintf("database=%s %s", name, *pgOpts)
}

// OpenTestDBOrDie attempts to return a connection to a new postgres
// test database and fails if unable to do so.
func OpenTestDBOrDie(ctx context.Context) *sql.DB {
vishalkuo marked this conversation as resolved.
Show resolved Hide resolved
db, err := NewTrillianDB(ctx)
if err != nil {
panic(err)
}
return db
}
Loading