Repository: etcd-io/etcd
File: tests/robustness/report/report.go (Go)

Code before:
// limitations under the License.

package report

import (
	"fmt"
	"go.uber.org/zap"
	"os"
	"path/filepath"

	"go.etcd.io/etcd/tests/v3/framework/e2e"
)

type TestReport struct {
	Logger    *zap.Logger
	Cluster   *e2e.EtcdProcessCluster
	Client    []ClientReport
	Visualize func(lg *zap.Logger, path string) error
}

func (r *TestReport) Report(failed, force, persistResults bool, path string) error {
	if !failed && !force && !persistResults {
		return nil
	}
	r.Logger.Info("Saving robustness test report", zap.String("path", path))
	for _, member := range r.Cluster.Procs {
		memberDataDir := filepath.Join(path, fmt.Sprintf("server-%s", member.Config().Name))
		if err := persistMemberDataDir(r.Logger, member, memberDataDir); err != nil {
			return err
		}
	}
	if r.Client != nil {
		if err := persistClientReports(r.Logger, path, r.Client); err != nil {
			return err
		}
	}
	if r.Visualize != nil {
		if err := r.Visualize(r.Logger, filepath.Join(path, "history.html")); err != nil {
			return err
		}
	}
	return nil
}

func persistMemberDataDir(lg *zap.Logger, member e2e.EtcdProcess, path string) error {
	lg.Info("Saving member data dir", zap.String("member", member.Config().Name), zap.String("path", path))
	return os.Rename(memberDataDir(member), path)
}

func memberDataDir(member e2e.EtcdProcess) string {
	lazyFS := member.LazyFS()

Review comment (suggestion):
Think this will panic as r.Cluster is empty in antithesis tests. To make data directories work with antithesis we need to change `TestReport` to use list of directories instead of cluster object.

For example:
```
type TestReport struct {
	Logger    *zap.Logger
	// map from server name to data dir path
	ServersDataPath map[string]string
	Client    []ClientReport
	Visualize func(lg *zap.Logger, path string) error
}

// Used by robustness
func serverDataPaths(c *e2e.EtcdProcessCluster) map[string]string {
	serverDataPaths := map[string]string{}
	for _, member := range c.Procs {
		serverDataPaths[member.Config().Name] = memberDataDir(member)
	}
}

// Used by antithesis
var serverDataPaths = map[string]string{
	"etcd0": "/var/etcddata0",
	"etcd1": "/var/etcddata1",
	"etcd2": "/var/etcddata2",
}

func (r *TestReport) Report(path string) error {
	for server, dataPath := range r.ServersDataPath {
		serverReportPath := filepath.Join(path, fmt.Sprintf("server-%s", server))
		err := os.Rename(dataPath, serverReportPath)
		...
	}
}
```


Code after:
// limitations under the License.

package report

import (
	"fmt"
	"os"
	"path/filepath"

	"go.uber.org/zap"

	"go.etcd.io/etcd/tests/v3/framework/e2e"
)

type TestReport struct {
	Logger          *zap.Logger
	Cluster         *e2e.EtcdProcessCluster
	ServersDataPath map[string]string
	Client          []ClientReport
	Visualize       func(lg *zap.Logger, path string) error
}

func (r *TestReport) Report(path string) error {
	r.Logger.Info("Saving robustness test report", zap.String("path", path))
	err := os.RemoveAll(path)
	if err != nil {
		r.Logger.Error("Failed to remove report dir", zap.Error(err))
	}
	for server, dataPath := range r.ServersDataPath {
		serverReportPath := filepath.Join(path, fmt.Sprintf("server-%s", server))
		r.Logger.Info("Saving member data dir", zap.String("member", server), zap.String("data-dir", dataPath), zap.String("path", serverReportPath))
		if err := os.CopyFS(serverReportPath, os.DirFS(dataPath)); err != nil {
			return err
		}
	}
	if r.Client != nil {
		if err := persistClientReports(r.Logger, path, r.Client); err != nil {
			return err
		}
	}
	if r.Visualize != nil {
		if err := r.Visualize(r.Logger, filepath.Join(path, "history.html")); err != nil {
			return err
		}
	}
	return nil
}

func ServerDataPaths(c *e2e.EtcdProcessCluster) map[string]string {
	dataPaths := make(map[string]string)
	for _, member := range c.Procs {