Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalkuo committed Nov 12, 2018
1 parent 5b5a89c commit 8fb8dee
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 32 deletions.
38 changes: 6 additions & 32 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

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)
return NewAdminStorage(DB)
}}
tester.RunAllTests(t)
}

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

tree, err := storage.CreateTree(ctx, s, testonly.LogTree)
Expand All @@ -71,7 +65,7 @@ func TestAdminTX_CreateTree_InitializesStorageStructures(t *testing.T) {

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

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

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

// Setup: create a tree and set all nullable columns to null.
Expand Down Expand Up @@ -156,7 +150,7 @@ func TestAdminTX_TreeWithNulls(t *testing.T) {

func TestAdminTX_StorageSettingsNotSupported(t *testing.T) {
cleanTestDB(DB, t)
s := postgres.NewAdminStorage(DB)
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)
}
38 changes: 38 additions & 0 deletions storage/postgres/storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 (
"database/sql"
"flag"
"os"
"testing"

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

var DB *sql.DB

func TestMain(m *testing.M) {
flag.Parse()
if !testdb.PGAvailable() {
glog.Errorf("PG not available, skipping all PG storage tests")
return
}
DB = testdb.OpenTestDBOrDie()
defer DB.Close()
ec := m.Run()
os.Exit(ec)
}
10 changes: 10 additions & 0 deletions storage/postgres/testdb/testdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() *sql.DB {
db, err := NewTrillianDB(context.TODO())
if err != nil {
panic(err)
}
return db
}
72 changes: 72 additions & 0 deletions storage/postgres/tree_storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 (
"strings"
"testing"
)

type expandTestcase struct {
input *statementSkeleton
expected string
}

func TestInitializes(t *testing.T) {
_ = &statementSkeleton{}
arbitraryStorage := newTreeStorage(nil)
_ = arbitraryStorage.getSubtreeStmt
_ = arbitraryStorage.beginTreeTx
treeTx := &treeTX{}
_ = treeTx.getSubtree
_ = treeTx.getSubtrees
}

func TestExpandPlaceholderSQL(t *testing.T) {
testCases := []*expandTestcase{
{
input: &statementSkeleton{
sql: selectSubtreeSQL,
firstInsertion: "%s",
firstPlaceholders: 1,
restInsertion: "%s",
restPlaceholders: 1,
num: 2,
},
expected: strings.Replace(selectSubtreeSQL, placeholderSQL, "$1,$2", 1),
},
{
input: &statementSkeleton{
sql: insertSubtreeMultiSQL,
firstInsertion: "VALUES(%s, %s, %s, %s)",
firstPlaceholders: 4,
restInsertion: "(%s, %s, %s, %s)",
restPlaceholders: 4,
num: 2,
},
expected: strings.Replace(
insertSubtreeMultiSQL,
placeholderSQL,
"VALUES($1, $2, $3, $4),($5, $6, $7, $8)",
1),
},
}

for _, tc := range testCases {
if res := expandPlaceholderSQL(tc.input); res != tc.expected {
t.Fatalf("Expected %v but got %v", tc.expected, res)
}
}
}

0 comments on commit 8fb8dee

Please sign in to comment.