github.com/cockroachdb/pebble@v0.0.0-20231214172447-ab4952c5f87b/sstable/writer_fixture_test.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied. See the License for the specific language governing
    13  // permissions and limitations under the License.
    14  
    15  package sstable
    16  
    17  import (
    18  	"bytes"
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/cockroachdb/errors"
    24  	"github.com/cockroachdb/pebble/vfs"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func runTestFixtureOutput(fixture TestFixtureInfo) error {
    29  	// Check that a freshly made table is byte-for-byte equal to a pre-made
    30  	// table.
    31  	want, err := os.ReadFile(filepath.Join("testdata", fixture.Filename))
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	fs := vfs.NewMem()
    37  	if err := fixture.Build(fs, "test.sst"); err != nil {
    38  		return err
    39  	}
    40  	f, err := fs.Open("test.sst")
    41  	if err != nil {
    42  		return err
    43  	}
    44  	defer f.Close()
    45  	stat, err := f.Stat()
    46  	if err != nil {
    47  		return err
    48  	}
    49  	got := make([]byte, stat.Size())
    50  	_, err = f.ReadAt(got, 0)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	if !bytes.Equal(got, want) {
    56  		i := 0
    57  		for ; i < len(got) && i < len(want) && got[i] == want[i]; i++ {
    58  		}
    59  		os.WriteFile("fail.txt", got, 0644)
    60  		return errors.Errorf("built table testdata/%s does not match pre-made table. From byte %d onwards,\ngot:\n% x\nwant:\n% x",
    61  			fixture.Filename, i, got[i:], want[i:])
    62  	}
    63  	return nil
    64  }
    65  
    66  func TestFixtureOutput(t *testing.T) {
    67  	for _, fixture := range TestFixtures {
    68  		// Note: we disabled the zstd fixture test when CGO_ENABLED=0, because the
    69  		// implementation between DataDog/zstd and klauspost/compress are
    70  		// different, which leads to different compression output
    71  		// <https://github.com/klauspost/compress/issues/109#issuecomment-498763233>.
    72  		// Since the fixture test requires bit-to-bit reproducibility, we cannot
    73  		// run the zstd test when the implementation is not based on facebook/zstd.
    74  		if !useStandardZstdLib && fixture.Compression == ZstdCompression {
    75  			continue
    76  		}
    77  		t.Run(fixture.Filename, func(t *testing.T) {
    78  			require.NoError(t, runTestFixtureOutput(fixture))
    79  		})
    80  	}
    81  }