go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/tq/txn/spanner/main_test.go (about) 1 // Copyright 2021 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package spanner 16 17 import ( 18 "context" 19 "os" 20 "path/filepath" 21 "testing" 22 23 "cloud.google.com/go/spanner" 24 25 "go.chromium.org/luci/common/errors" 26 "go.chromium.org/luci/common/spantest" 27 ) 28 29 func TestMain(m *testing.M) { 30 spantest.SpannerTestMain(m, findInitScript) 31 } 32 33 // findInitScript returns path //tq/txn/spanner/init_db.sql. 34 func findInitScript() (string, error) { 35 ancestor, err := filepath.Abs(".") 36 if err != nil { 37 return "", err 38 } 39 40 for { 41 scriptPath := filepath.Join(ancestor, "init_db.sql") 42 _, err := os.Stat(scriptPath) 43 if os.IsNotExist(err) { 44 parent := filepath.Dir(ancestor) 45 if parent == ancestor { 46 return "", errors.Reason("init_db.sql not found").Err() 47 } 48 ancestor = parent 49 continue 50 } 51 52 return scriptPath, err 53 } 54 } 55 56 // cleanupDatabase deletes all data from all tables. 57 func cleanupDatabase(ctx context.Context, client *spanner.Client) error { 58 _, err := client.Apply(ctx, []*spanner.Mutation{ 59 spanner.Delete("TQReminders", spanner.AllKeys()), 60 spanner.Delete("TQLeases", spanner.AllKeys()), 61 }) 62 return err 63 }