2019-08-20 17:04:13 +02:00
package main
import (
"context"
"flag"
"fmt"
2019-09-29 18:44:59 +02:00
"path/filepath"
2019-08-20 17:04:13 +02:00
"time"
"github.com/fatih/color"
2019-09-29 18:44:59 +02:00
"github.com/pkg/errors"
2019-08-20 17:04:13 +02:00
"github.com/zrepl/zrepl/config"
"github.com/zrepl/zrepl/daemon/logging"
"github.com/zrepl/zrepl/logger"
"github.com/zrepl/zrepl/platformtest"
"github.com/zrepl/zrepl/platformtest/tests"
)
func main ( ) {
2019-09-29 18:44:59 +02:00
var args struct {
2019-10-14 17:45:44 +02:00
createArgs platformtest . ZpoolCreateArgs
stopAndKeepPoolOnFail bool
2019-08-20 17:04:13 +02:00
}
2019-09-29 18:44:59 +02:00
flag . StringVar ( & args . createArgs . PoolName , "poolname" , "" , "" )
flag . StringVar ( & args . createArgs . ImagePath , "imagepath" , "" , "" )
flag . Int64Var ( & args . createArgs . ImageSize , "imagesize" , 100 * ( 1 << 20 ) , "" )
flag . StringVar ( & args . createArgs . Mountpoint , "mountpoint" , "none" , "" )
2019-10-14 17:45:44 +02:00
flag . BoolVar ( & args . stopAndKeepPoolOnFail , "failure.stop-and-keep-pool" , false , "if a test case fails, stop test execution and keep pool as it was when the test failed" )
2019-09-29 18:44:59 +02:00
flag . Parse ( )
2019-08-20 17:04:13 +02:00
outlets := logger . NewOutlets ( )
outlet , level , err := logging . ParseOutlet ( config . LoggingOutletEnum { Ret : & config . StdoutLoggingOutlet {
LoggingOutletCommon : config . LoggingOutletCommon {
Level : "debug" ,
Format : "human" ,
} ,
} } )
if err != nil {
panic ( err )
}
outlets . Add ( outlet , level )
logger := logger . NewLogger ( outlets , 1 * time . Second )
2019-09-29 18:44:59 +02:00
if err := args . createArgs . Validate ( ) ; err != nil {
logger . Error ( err . Error ( ) )
panic ( err )
2019-08-20 17:04:13 +02:00
}
2019-09-29 18:44:59 +02:00
ctx := platformtest . WithLogger ( context . Background ( ) , logger )
ex := platformtest . NewEx ( logger )
2019-08-20 17:04:13 +02:00
bold := color . New ( color . Bold )
2019-10-14 17:32:58 +02:00
boldRed := color . New ( color . Bold , color . FgHiRed )
boldGreen := color . New ( color . Bold , color . FgHiGreen )
2019-08-20 17:04:13 +02:00
for _ , c := range tests . Cases {
2019-09-29 18:44:59 +02:00
// ATTENTION future parallelism must pass c by value into closure!
2019-10-14 17:45:44 +02:00
err := func ( ) ( err error ) {
var oErr = & err
2019-09-29 18:44:59 +02:00
bold . Printf ( "BEGIN TEST CASE %s\n" , c )
pool , err := platformtest . CreateOrReplaceZpool ( ctx , ex , args . createArgs )
if err != nil {
return errors . Wrap ( err , "create test pool" )
}
defer func ( ) {
2019-10-14 17:45:44 +02:00
if err := recover ( ) ; err != nil {
* oErr = errors . Errorf ( "panic while running test: %v" , err ) // shadow
if args . stopAndKeepPoolOnFail {
return
}
// fallthrough
}
2019-09-29 18:44:59 +02:00
if err := pool . Destroy ( ctx , ex ) ; err != nil {
fmt . Printf ( "error destroying test pool: %s" , err )
}
} ( )
ctx := & platformtest . Context {
Context : ctx ,
RootDataset : filepath . Join ( pool . Name ( ) , "rootds" ) ,
}
c ( ctx )
return nil
} ( )
if err != nil {
2019-10-14 17:32:58 +02:00
boldRed . Printf ( "TEST CASE FAILED WITH ERROR:\n" )
2019-10-14 17:45:44 +02:00
fmt . Printf ( "%+v\n" , err ) // print with stack trace
if args . stopAndKeepPoolOnFail {
bold . Printf ( "STOPPING TEST RUN AT FAILING TEST PER USER REQUEST\n" )
return
}
2019-10-14 17:32:58 +02:00
} else {
boldGreen . Printf ( "DONE TEST CASE %s\n" , c )
2019-09-29 18:44:59 +02:00
}
2019-10-14 17:32:58 +02:00
fmt . Println ( )
2019-08-20 17:04:13 +02:00
}
}