lntest: fix linter stylecheck and containedctx

This commit is contained in:
yyforyongyu 2023-02-01 00:32:03 +08:00
parent 0c50d4379f
commit 28744d89c7
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
5 changed files with 30 additions and 29 deletions

View File

@ -42,7 +42,8 @@ type HarnessMiner struct {
// runCtx is a context with cancel method. It's used to signal when the // runCtx is a context with cancel method. It's used to signal when the
// node needs to quit, and used as the parent context when spawning // node needs to quit, and used as the parent context when spawning
runCtx context.Context // children contexts for RPC requests.
runCtx context.Context //nolint:containedctx
cancel context.CancelFunc cancel context.CancelFunc
// logPath is the directory path of the miner's logs. // logPath is the directory path of the miner's logs.

View File

@ -27,7 +27,7 @@ func SetupHarness(t *testing.T, binaryPath, dbBackendName string,
require.NoError(t, os.MkdirAll(logDir, 0700), "create log dir failed") require.NoError(t, os.MkdirAll(logDir, 0700), "create log dir failed")
// Parse database backend // Parse database backend
dbBackend := prepareDbBackend(t, dbBackendName) dbBackend := prepareDBBackend(t, dbBackendName)
// Create a new HarnessTest. // Create a new HarnessTest.
ht := NewHarnessTest(t, binaryPath, feeService, dbBackend) ht := NewHarnessTest(t, binaryPath, feeService, dbBackend)
@ -94,8 +94,8 @@ func prepareChainBackend(t *testing.T,
} }
} }
// prepareDbBackend parses a DatabaseBackend based on the name given. // prepareDBBackend parses a DatabaseBackend based on the name given.
func prepareDbBackend(t *testing.T, func prepareDBBackend(t *testing.T,
dbBackendName string) node.DatabaseBackend { dbBackendName string) node.DatabaseBackend {
var dbBackend node.DatabaseBackend var dbBackend node.DatabaseBackend

View File

@ -126,12 +126,12 @@ type BaseNodeConfig struct {
// compiled with all required itest flags. // compiled with all required itest flags.
LndBinary string LndBinary string
// backupDbDir is the path where a database backup is stored, if any. // backupDBDir is the path where a database backup is stored, if any.
backupDbDir string backupDBDir string
// postgresDbName is the name of the postgres database where lnd data // postgresDBName is the name of the postgres database where lnd data
// is stored in. // is stored in.
postgresDbName string postgresDBName string
} }
func (cfg BaseNodeConfig) P2PAddr() string { func (cfg BaseNodeConfig) P2PAddr() string {

View File

@ -80,7 +80,7 @@ type HarnessNode struct {
// runCtx is a context with cancel method. It's used to signal when the // runCtx is a context with cancel method. It's used to signal when the
// node needs to quit, and used as the parent context when spawning // node needs to quit, and used as the parent context when spawning
// children contexts for RPC requests. // children contexts for RPC requests.
runCtx context.Context runCtx context.Context //nolint:containedctx
cancel context.CancelFunc cancel context.CancelFunc
// filename is the log file's name. // filename is the log file's name.
@ -118,7 +118,7 @@ func NewHarnessNode(t *testing.T, cfg *BaseNodeConfig) (*HarnessNode, error) {
var dbName string var dbName string
if cfg.DBBackend == BackendPostgres { if cfg.DBBackend == BackendPostgres {
var err error var err error
dbName, err = createTempPgDb() dbName, err = createTempPgDB()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -126,7 +126,7 @@ func NewHarnessNode(t *testing.T, cfg *BaseNodeConfig) (*HarnessNode, error) {
} }
cfg.OriginalExtraArgs = cfg.ExtraArgs cfg.OriginalExtraArgs = cfg.ExtraArgs
cfg.postgresDbName = dbName cfg.postgresDBName = dbName
return &HarnessNode{ return &HarnessNode{
T: t, T: t,
@ -614,8 +614,8 @@ func (hn *HarnessNode) attachPubKey() error {
// cleanup cleans up all the temporary files created by the node's process. // cleanup cleans up all the temporary files created by the node's process.
func (hn *HarnessNode) cleanup() error { func (hn *HarnessNode) cleanup() error {
if hn.Cfg.backupDbDir != "" { if hn.Cfg.backupDBDir != "" {
err := os.RemoveAll(hn.Cfg.backupDbDir) err := os.RemoveAll(hn.Cfg.backupDBDir)
if err != nil { if err != nil {
return fmt.Errorf("unable to remove backup dir: %v", return fmt.Errorf("unable to remove backup dir: %v",
err) err)
@ -791,16 +791,16 @@ func (hn *HarnessNode) printErrf(format string, a ...interface{}) {
// BackupDB creates a backup of the current database. // BackupDB creates a backup of the current database.
func (hn *HarnessNode) BackupDB() error { func (hn *HarnessNode) BackupDB() error {
if hn.Cfg.backupDbDir != "" { if hn.Cfg.backupDBDir != "" {
return fmt.Errorf("backup already created") return fmt.Errorf("backup already created")
} }
if hn.Cfg.postgresDbName != "" { if hn.Cfg.postgresDBName != "" {
// Backup database. // Backup database.
backupDBName := hn.Cfg.postgresDbName + "_backup" backupDBName := hn.Cfg.postgresDBName + "_backup"
err := executePgQuery( err := executePgQuery(
"CREATE DATABASE " + backupDBName + " WITH TEMPLATE " + "CREATE DATABASE " + backupDBName + " WITH TEMPLATE " +
hn.Cfg.postgresDbName, hn.Cfg.postgresDBName,
) )
if err != nil { if err != nil {
return err return err
@ -818,7 +818,7 @@ func (hn *HarnessNode) BackupDB() error {
err) err)
} }
hn.Cfg.backupDbDir = tempDir hn.Cfg.backupDBDir = tempDir
} }
return nil return nil
@ -826,39 +826,39 @@ func (hn *HarnessNode) BackupDB() error {
// RestoreDB restores a database backup. // RestoreDB restores a database backup.
func (hn *HarnessNode) RestoreDB() error { func (hn *HarnessNode) RestoreDB() error {
if hn.Cfg.postgresDbName != "" { if hn.Cfg.postgresDBName != "" {
// Restore database. // Restore database.
backupDBName := hn.Cfg.postgresDbName + "_backup" backupDBName := hn.Cfg.postgresDBName + "_backup"
err := executePgQuery( err := executePgQuery(
"DROP DATABASE " + hn.Cfg.postgresDbName, "DROP DATABASE " + hn.Cfg.postgresDBName,
) )
if err != nil { if err != nil {
return err return err
} }
err = executePgQuery( err = executePgQuery(
"ALTER DATABASE " + backupDBName + " RENAME TO " + "ALTER DATABASE " + backupDBName + " RENAME TO " +
hn.Cfg.postgresDbName, hn.Cfg.postgresDBName,
) )
if err != nil { if err != nil {
return err return err
} }
} else { } else {
// Restore files. // Restore files.
if hn.Cfg.backupDbDir == "" { if hn.Cfg.backupDBDir == "" {
return fmt.Errorf("no database backup created") return fmt.Errorf("no database backup created")
} }
err := copyAll(hn.Cfg.DBDir(), hn.Cfg.backupDbDir) err := copyAll(hn.Cfg.DBDir(), hn.Cfg.backupDBDir)
if err != nil { if err != nil {
return fmt.Errorf("unable to copy database files: %w", return fmt.Errorf("unable to copy database files: %w",
err) err)
} }
if err := os.RemoveAll(hn.Cfg.backupDbDir); err != nil { if err := os.RemoveAll(hn.Cfg.backupDBDir); err != nil {
return fmt.Errorf("unable to remove backup dir: %w", return fmt.Errorf("unable to remove backup dir: %w",
err) err)
} }
hn.Cfg.backupDbDir = "" hn.Cfg.backupDBDir = ""
} }
return nil return nil
@ -868,8 +868,8 @@ func postgresDatabaseDsn(dbName string) string {
return fmt.Sprintf(postgresDsn, dbName) return fmt.Sprintf(postgresDsn, dbName)
} }
// createTempPgDb creates a temp postgres database. // createTempPgDB creates a temp postgres database.
func createTempPgDb() (string, error) { func createTempPgDB() (string, error) {
// Create random database name. // Create random database name.
randBytes := make([]byte, 8) randBytes := make([]byte, 8)
_, err := rand.Read(randBytes) _, err := rand.Read(randBytes)

View File

@ -49,7 +49,7 @@ type HarnessRPC struct {
// runCtx is a context with cancel method. It's used to signal when the // runCtx is a context with cancel method. It's used to signal when the
// node needs to quit, and used as the parent context when spawning // node needs to quit, and used as the parent context when spawning
// children contexts for RPC requests. // children contexts for RPC requests.
runCtx context.Context runCtx context.Context //nolint:containedctx
cancel context.CancelFunc cancel context.CancelFunc
} }