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

Code before:
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// 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))

Review comment (bug):
Those three variables work identically, making them really function as single one. And the if statement line below just works as a turn off switch. Based on that I think we can just remove all of them and move the condition outside function. Like

```
shouldReport := failed || force || persistResults
if shouldReport {
  t.Report(path)
}
```



Code after:
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// 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