Repository: etcd-io/etcd
File: tests/antithesis/test-template/robustness/main.go (Go)

Code before:
		if localpath != "" {
			etcdDataPath = localpath
		}
		reportPath = localReportPath
	}

	persistedRequestdirs := []string{
		fmt.Sprintf(etcdDataPath, 0),
		fmt.Sprintf(etcdDataPath, 1),
		fmt.Sprintf(etcdDataPath, 2),
	}

	if err := os.RemoveAll(reportPath); err != nil {
		panic(err)
	}
	if err := os.MkdirAll(reportPath, 0o700); err != nil {
		panic(err)
	}

	ctx := context.Background()
	baseTime := time.Now()
	duration := time.Duration(robustnessrand.RandRange(5, 15) * int64(time.Second))
	testRobustness(ctx, hosts, persistedRequestdirs, reportPath, baseTime, duration)
}

func testRobustness(ctx context.Context, hosts, persistedRequestdirs []string, reportPath string, baseTime time.Time, duration time.Duration) {
	lg, err := zap.NewProduction()
	if err != nil {
		panic(err)
	}
	serverDataPaths := make(map[string]string)
	for i := range len(persistedRequestdirs) {
		serverDataPaths[fmt.Sprintf("etcd%d", i)] = persistedRequestdirs[i]
	}
	lg.Info("Start traffic generation", zap.Duration("duration", duration))
	r := report.TestReport{Logger: lg}
	r.Client = runTraffic(ctx, lg, hosts, baseTime, duration)
	lg.Info("Completed traffic generation")

	etcdDataDirs, err := report.PersistedRequestsDirs(lg, persistedRequestdirs)
	if err != nil {
		lg.Error("Failed to create PersistedRequestdirs to access etcd WAL files")
		panic(err)
	}
	validateConfig := validate.Config{ExpectRevisionUnique: traffic.EtcdAntithesis.ExpectUniqueRevision()}
	result, err := validate.ValidateAndReturnVisualize(lg, validateConfig, r.Client, etcdDataDirs, 5*time.Minute)
	if err != nil {
		lg.Info("Validation error", zap.Error(err))
		assert.Unreachable("Validation error", map[string]any{"error": err})
		return
	}

Review comment (refactor):
Please move it to separate function. We should try to simplify the `testRobustness` as possible.

Code after:
		etcdDataPath = defaultLocalEtcdDataPath
		localpath := os.Getenv(localEtcdDataPathEnv)
		if localpath != "" {
			etcdDataPath = localpath
		}
		reportPath = localReportPath
	}

	persistedRequestdirs := []string{
		fmt.Sprintf(etcdDataPath, 0),
		fmt.Sprintf(etcdDataPath, 1),
		fmt.Sprintf(etcdDataPath, 2),
	}

	ctx := context.Background()
	baseTime := time.Now()
	duration := time.Duration(robustnessrand.RandRange(5, 15) * int64(time.Second))

	lg, err := zap.NewProduction()
	if err != nil {
		panic(err)
	}
	r := report.TestReport{Logger: lg, ServersDataPath: map[string]string{}}
	for i := range len(persistedRequestdirs) {
		r.ServersDataPath[fmt.Sprintf("etcd%d", i)] = persistedRequestdirs[i]
	}
	defer func() {
		if err := r.Report(reportPath); err != nil {
			lg.Error("Failed to save validation report", zap.Error(err))
		}
	}()
	testRobustness(ctx, lg, &r, hosts, baseTime, duration)
}

func testRobustness(ctx context.Context, lg *zap.Logger, r *report.TestReport, hosts []string, baseTime time.Time, duration time.Duration) {
	lg.Info("Start traffic generation", zap.Duration("duration", duration))
	r.Client = runTraffic(ctx, lg, hosts, baseTime, duration)
	lg.Info("Completed traffic generation")

	etcdDataDirs, err := report.PersistedRequestsDirs(lg, slices.Collect(maps.Values(r.ServersDataPath)))
	if err != nil {
		lg.Error("Failed to create PersistedRequestdirs to access etcd WAL files")
		panic(err)
	}
	validateConfig := validate.Config{ExpectRevisionUnique: traffic.EtcdAntithesis.ExpectUniqueRevision()}
	result, err := validate.ValidateAndReturnVisualize(lg, validateConfig, r.Client, etcdDataDirs, 5*time.Minute)
	if err != nil {
		lg.Info("Validation error", zap.Error(err))
		assert.Unreachable("Validation error", map[string]any{"error": err})
		return
	}