// Copyright (C) 2017-2021  Nexedi SA and Contributors.
//                          Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.

package fs1tools

// XXX + py3

//go:generate sh -c "python2 -m ZODB.scripts.fstail -n 1000000 ../testdata/1.fs >testdata/1.fstail.ok"
//go:generate sh -c "python2 -c 'from ZODB.FileStorage import fsdump; fsdump.main()' ../testdata/1.fs >testdata/1.fsdump.ok"
//go:generate sh -c "python2 -c 'from ZODB.FileStorage.fsdump import Dumper; import sys; d = Dumper(sys.argv[1]); d.dump()' ../testdata/1.fs >testdata/1.fsdumpv.ok"

// fstail.py crashes on empty.fs . The output should be empty file so generate it with just echo.
////go:generate sh -c "python2 -m ZODB.scripts.fstail -n 1000000 ../testdata/empty.fs >testdata/empty.fstail.ok"
//go:generate sh -c "echo -n >testdata/empty.fstail.ok"
//go:generate sh -c "python2 -c 'from ZODB.FileStorage import fsdump; fsdump.main()' ../testdata/empty.fs >testdata/empty.fsdump.ok"
//go:generate sh -c "python2 -c 'from ZODB.FileStorage.fsdump import Dumper; import sys; d = Dumper(sys.argv[1]); d.dump()' ../testdata/empty.fs >testdata/empty.fsdumpv.ok"

//go:generate sh -c "python2 -m ZODB.scripts.fstail -n 1000000 ../testdata/whiteout.fs >testdata/whiteout.fstail.ok"
//go:generate sh -c "python2 -c 'from ZODB.FileStorage import fsdump; fsdump.main()' ../testdata/whiteout.fs >testdata/whiteout.fsdump.ok"
//go:generate sh -c "python2 -c 'from ZODB.FileStorage.fsdump import Dumper; import sys; d = Dumper(sys.argv[1]); d.dump()' ../testdata/whiteout.fs >testdata/whiteout.fsdumpv.ok"

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"testing"

	"lab.nexedi.com/kirr/neo/go/zodb/storage/fs1"

	"github.com/kylelemons/godebug/diff"
)

func loadFile(t *testing.T, path string) string {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	return string(data)
}

func testDump(t *testing.T, dir fs1.IterDir, newd func() Dumper) {
	testv := []string{"1", "empty", "whiteout"}
	for _, tt := range testv {
		t.Run("db=" + tt, func(t *testing.T) {
			d   := newd()
			buf := bytes.Buffer{}

			err := Dump(&buf, fmt.Sprintf("../testdata/%s.fs", tt), dir, d)
			if err != nil {
				t.Fatalf("%s: %v", d.DumperName(), err)
			}

			dumpOk := loadFile(t, fmt.Sprintf("testdata/%s.%s.ok", tt, d.DumperName()))

			if dumpOk != buf.String() {
				t.Errorf("%s: dump different:\n%v", d.DumperName(), diff.Diff(dumpOk, buf.String()))
			}
		})
	}
}

func newFsDump()  Dumper { return &DumperFsDump{} }
func newFsDumpv() Dumper { return &DumperFsDumpVerbose{} }
func newFsTail()  Dumper { return &DumperFsTail{Ntxn: 1000000} }

func TestFsDump(t *testing.T)	{ testDump(t, fs1.IterForward,  newFsDump) }
func TestFsDumpv(t *testing.T)	{ testDump(t, fs1.IterForward,  newFsDumpv) }
func TestFsTail(t *testing.T)	{ testDump(t, fs1.IterBackward, newFsTail) }

func BenchmarkTail(b *testing.B) {
	// FIXME small testdata/1.fs is not representative for benchmarking
	for i := 0; i < b.N; i++ {
		err := Dump(ioutil.Discard, "../testdata/1.fs", fs1.IterBackward, &DumperFsTail{Ntxn: 1000000})
		if err != nil {
			b.Fatal(err)
		}
	}
}
