// Common rdiff-backup tests: Initialize an rdiff-backup instance with the
// config passed and test fail if the generated command doesn't match the
// expected command line regexp. The value of dryRun is passed to the
// initializon of rdiff-backup. If must_error is true, the call to
// NewRdiffBackupTransport *must* fail, or the test will fail.
func rdiffBackupTest(t *testing.T, cfg *config.Config, expect string, dryRun bool, mustError bool) {
	fakeExecute := NewFakeExecute()

	log := logger.New("")

	// Create a new rdiff-backup object with our fakeExecute and a sinking outLogWriter.
	rdiffBackup, err := NewRdiffBackupTransport(cfg, fakeExecute, log, dryRun)
	if err != nil {
		if mustError {
			return
		}
		t.Fatalf("NewRdiffBackupTransport failed: %v", err)
	}
	if mustError {
		t.Fatalf("NewRdiffBackupTransport should return have failed, but got nil error")
	}
	if err := rdiffBackup.Run(); err != nil {
		t.Fatalf("Run failed: %v", err)
	}
	matched, err := regexp.MatchString(expect, fakeExecute.Cmd())
	if err != nil {
		t.Fatalf("error during regexp match: %v", err)
	}
	if !matched {
		t.Fatalf("name should match %s; is %s", expect, fakeExecute.Cmd())
	}
}
Example #2
0
// Return a Backup object with fake calls
func fakeBackup() *Backup {
	log = logger.New("")
	fakeConfig := &config.Config{}

	fakeBackup := &Backup{
		log:    log,
		config: fakeConfig,
		dryRun: false}
	return fakeBackup
}
Example #3
0
// main
func main() {
	log = logger.New("")

	// Parse command line flags and read config file.
	if err := parseFlags(); err != nil {
		log.Fatalf("Error: %v\n", err)
	}

	// Set verbose level
	verbose := int(opt.verbose)
	if verbose > 0 {
		log.SetVerboseLevel(verbose)
	}

	// Open and parse config file
	cfg, err := os.Open(opt.config)
	if err != nil {
		log.Fatalf("Unable to open config file: %v\n", err)
	}
	config, err := config.ParseConfig(cfg)
	if err != nil {
		log.Fatalf("Configuration error in %q: %v\n", opt.config, err)
	}

	// Create output log. Use the name specified in the config, if any,
	// or create a "standard" name using the backup name and date.
	logFilename := config.Logfile
	if logFilename == "" {
		logFilename = logPath(config.Name, config.LogDir)
	}
	outLog, err := logOpen(logFilename)
	if err != nil {
		log.Fatalf("Unable to open/create logfile: %v\n", err)
	}
	defer outLog.Close()

	// Configure log to log everything to stderr and outLog
	log.SetMirrorOutput(outLog)

	if opt.dryrun {
		log.Verboseln(1, "Warning: Dry-Run mode. Won't execute any commands.")
	}

	// Create new Backup and execute.
	b := NewBackup(config, log, opt.dryrun)

	if err = b.Run(); err != nil {
		log.Fatalln(err)
	}
	log.Verboseln(1, "*** Backup Result: Success")
}