Repository: etcd-io/etcd
File: server/storage/wal/wal_test.go (Go)

Code before:
	// file is preallocated to segment size; only read data written by wal
	off, err := w.tail().Seek(0, io.SeekCurrent)
	if err != nil {
		t.Fatal(err)
	}
	gd := make([]byte, off)
	f, err := os.Open(filepath.Join(p, filepath.Base(w.tail().Name())))
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()
	if _, err = io.ReadFull(f, gd); err != nil {
		t.Fatalf("err = %v, want nil", err)
	}

	if os.Getenv("ETCD_UPDATE_FIXTURE_DATA") == "true" {
		os.WriteFile("testdata/TestNew.wal", gd, os.FileMode(0644))
	}
	fixtureData, err := os.ReadFile("testdata/TestNew.wal")
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(gd, fixtureData) {
		t.Log("data did not match fixture, rerun with ETCD_UPDATE_FIXTURE_DATA=true to regenerate fixture")
		t.Errorf("data = %v, want %v", gd, fixtureData)
	}

	var wb bytes.Buffer
	e := newEncoder(&wb, 0, 0)
	err = e.encode(&walpb.Record{Type: CrcType, Crc: 0})
	require.NoErrorf(t, err, "err = %v, want nil", err)
	err = e.encode(&walpb.Record{Type: MetadataType, Data: []byte("somedata")})
	require.NoErrorf(t, err, "err = %v, want nil", err)
	r := &walpb.Record{
		Type: SnapshotType,
		Data: pbutil.MustMarshal(&walpb.Snapshot{}),
	}
	err = e.encode(r)
	require.NoErrorf(t, err, "err = %v, want nil", err)
	e.flush()
	if !bytes.Equal(gd, wb.Bytes()) {
		t.Errorf("data = %v, want %v", gd, wb.Bytes())
	}
}

func TestCreateNewWALFile(t *testing.T) {
	tests := []struct {
		name     string
		fileType any
		forceNew bool
	}{

Review comment (suggestion):
Do you intentionally keep this code, or just forget to remove it?

do not quite understand the purpose of this code.

Code after:
	// file is preallocated to segment size; only read data written by wal
	off, err := w.tail().Seek(0, io.SeekCurrent)
	if err != nil {
		t.Fatal(err)
	}
	gd := make([]byte, off)
	f, err := os.Open(filepath.Join(p, filepath.Base(w.tail().Name())))
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()
	if _, err = io.ReadFull(f, gd); err != nil {
		t.Fatalf("err = %v, want nil", err)
	}

	if os.Getenv("ETCD_UPDATE_FIXTURE_DATA") == "true" {
		os.WriteFile("testdata/TestNew.wal", gd, os.FileMode(0644))
	}
	fixtureData, err := os.ReadFile("testdata/TestNew.wal")
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(gd, fixtureData) {
		t.Log("data did not match fixture, rerun with ETCD_UPDATE_FIXTURE_DATA=true to regenerate fixture")
		t.Errorf("data = %v, want %v", gd, fixtureData)
	}

	var wb bytes.Buffer
	e := newEncoder(&wb, 0, 0)
	err = e.encode(&walpb.Record{Type: new(CrcType), Crc: new(uint32(0))})
	require.NoErrorf(t, err, "err = %v, want nil", err)
	err = e.encode(&walpb.Record{Type: new(MetadataType), Data: []byte("somedata")})
	require.NoErrorf(t, err, "err = %v, want nil", err)
	r := &walpb.Record{
		Type: new(SnapshotType),
		Data: pbutil.MustMarshal(&walpb.Snapshot{Index: new(uint64(0)), Term: new(uint64(0))}),
	}
	err = e.encode(r)
	require.NoErrorf(t, err, "err = %v, want nil", err)
	e.flush()
	if !bytes.Equal(gd, wb.Bytes()) {
		t.Errorf("data = %v, want %v", gd, wb.Bytes())
	}
}

func TestCreateNewWALFile(t *testing.T) {
	tests := []struct {
		name     string
		fileType any
		forceNew bool
	}{