github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/storage/stores/indexshipper/downloads/testutil.go (about)

     1  package downloads
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strconv"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  type mockIndex struct {
    15  	*os.File
    16  }
    17  
    18  func openMockIndexFile(t *testing.T, path string) *mockIndex {
    19  	fl, err := os.Open(path)
    20  	require.NoError(t, err)
    21  	return &mockIndex{fl}
    22  }
    23  
    24  func (m *mockIndex) Name() string {
    25  	return filepath.Base(m.File.Name())
    26  }
    27  
    28  func (m *mockIndex) Path() string {
    29  	return m.File.Name()
    30  }
    31  
    32  func (m *mockIndex) Reader() (io.ReadSeeker, error) {
    33  	return m.File, nil
    34  }
    35  
    36  func setupIndexesAtPath(t *testing.T, userID, path string, start, end int) []string {
    37  	require.NoError(t, os.MkdirAll(path, 0755))
    38  	var testIndexes []string
    39  	for ; start < end; start++ {
    40  		fileName := buildIndexFilename(userID, start)
    41  		indexPath := filepath.Join(path, fileName)
    42  
    43  		require.NoError(t, ioutil.WriteFile(indexPath, []byte(fileName), 0755))
    44  		testIndexes = append(testIndexes, indexPath)
    45  	}
    46  
    47  	return testIndexes
    48  }
    49  
    50  func buildIndexFilename(userID string, indexNum int) string {
    51  	if userID == "" {
    52  		return strconv.Itoa(indexNum)
    53  	}
    54  
    55  	return userID + "-" + strconv.Itoa(indexNum)
    56  }