2017-09-02 12:24:17 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2017-09-02 12:52:12 +02:00
|
|
|
"github.com/kr/pretty"
|
2017-09-02 12:24:17 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zrepl/zrepl/zfs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var testCmd = &cobra.Command{
|
|
|
|
Use: "test",
|
|
|
|
Short: "test configuration",
|
|
|
|
}
|
|
|
|
|
|
|
|
var testConfigSyntaxCmd = &cobra.Command{
|
|
|
|
Use: "config",
|
2017-09-02 12:52:12 +02:00
|
|
|
Short: "parse config file and dump parsed datastructure",
|
2017-09-02 12:24:17 +02:00
|
|
|
Run: doTestConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
var testDatasetMapFilter = &cobra.Command{
|
|
|
|
Use: "pattern jobtype.name test/zfs/dataset/path",
|
|
|
|
Short: "test dataset mapping / filter specified in config",
|
|
|
|
Example: ` zrepl test pattern prune.clean_backups tank/backups/legacyscript/foo`,
|
|
|
|
Run: doTestDatasetMapFilter,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(testCmd)
|
|
|
|
testCmd.AddCommand(testConfigSyntaxCmd)
|
|
|
|
testCmd.AddCommand(testDatasetMapFilter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestConfig(cmd *cobra.Command, args []string) {
|
|
|
|
log.Printf("config ok")
|
2017-09-02 12:52:12 +02:00
|
|
|
|
|
|
|
log.Printf("%# v", pretty.Formatter(conf))
|
|
|
|
|
2017-09-02 12:24:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestDatasetMapFilter(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) != 2 {
|
|
|
|
log.Printf("specify job name as first postitional argument, test input as second")
|
|
|
|
log.Printf(cmd.UsageString())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
n, i := args[0], args[1]
|
2017-09-10 16:13:05 +02:00
|
|
|
|
|
|
|
jobi, ok := conf.Jobs[n]
|
|
|
|
if !ok {
|
|
|
|
log.Printf("no job %s defined in config")
|
2017-09-02 12:24:17 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
var mf *DatasetMapFilter
|
2017-09-02 12:24:17 +02:00
|
|
|
switch j := jobi.(type) {
|
2017-09-10 16:13:05 +02:00
|
|
|
case *PullJob:
|
|
|
|
mf = j.Mapping
|
|
|
|
case *SourceJob:
|
|
|
|
mf = j.Datasets
|
|
|
|
case *LocalJob:
|
2017-09-02 12:24:17 +02:00
|
|
|
mf = j.Mapping
|
|
|
|
default:
|
|
|
|
panic("incomplete implementation")
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := zfs.NewDatasetPath(i)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("cannot parse test input as ZFS dataset path: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-09-16 19:42:42 +02:00
|
|
|
if mf.filterMode{
|
2017-09-02 12:24:17 +02:00
|
|
|
pass, err := mf.Filter(ip)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error evaluating filter: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
log.Printf("filter result: %v", pass)
|
|
|
|
} else {
|
|
|
|
res, err := mf.Map(ip)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error evaluating mapping: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-09-02 12:40:22 +02:00
|
|
|
toStr := "NO MAPPING"
|
|
|
|
if res != nil {
|
|
|
|
toStr = res.ToString()
|
|
|
|
}
|
|
|
|
log.Printf("%s => %s", ip.ToString(), toStr)
|
2017-09-02 12:24:17 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|