Skip to content

Commit

Permalink
core: Add a couple tests for the new logic
Browse files Browse the repository at this point in the history
  • Loading branch information
victorges committed Jun 24, 2024
1 parent 545098e commit 4f56725
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions core/uploader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -61,6 +62,78 @@ func TestItWritesSlowInputIncrementally(t *testing.T) {
require.Equal(t, 0, len(expectedLines), "Expected to have received each manifest line sequentially")
}

func TestUploadFileWithBackup(t *testing.T) {
dir, err := os.MkdirTemp(os.TempDir(), "TestUploadFileWithBackup-*")
require.NoError(t, err)
defer os.RemoveAll(dir)

testFile := filepath.Join(dir, "input.txt")
require.NoError(t, os.WriteFile(testFile, []byte("test"), 0644))

fakeStorage := "s3+https://fake.service.livepeer.com/bucket/"
backupStorage := filepath.Join(dir, "backup") + "/"
fakeOutput := fakeStorage + "hls/123/file.txt"
expectedOutFile := backupStorage + "hls/123/file.txt"

storageFallbackURLs := map[string]string{
fakeStorage: "file://" + backupStorage,
}
out, written, err := uploadFileWithBackup(mustParseURL(fakeOutput), testFile, nil, 0, false, storageFallbackURLs)

Check failure on line 81 in core/uploader_test.go

View workflow job for this annotation

GitHub Actions / Run tests defined for the project

cannot use testFile (variable of type string) as []byte value in argument to uploadFileWithBackup
require.NoError(t, err)
require.Equal(t, expectedOutFile, out.URL)
require.Equal(t, int64(4), written)

b, err := os.ReadFile(expectedOutFile)
require.NoError(t, err)
require.Equal(t, []byte("test"), b)
}

func TestBuildBackupURI(t *testing.T) {
storageFallbackURLs := map[string]string{
"http://localhost:8888/": "http://localhost:8888/os-recordings-backup/",
"s3+https://user:[email protected]/bucket/": "s3+https://resu:[email protected]/backup-bucket/",
}
testCases := []struct {
name string
input string
expectedErr string
expected string
}{
{
name: "http",
input: "http://localhost:8888/folder/",
expected: "http://localhost:8888/os-recordings-backup/folder/",
},
{
name: "s3",
input: "s3+https://user:[email protected]/bucket/",
expected: "s3+https://resu:[email protected]/backup-bucket/",
},
{
name: "s3 with nested file",
input: "s3+https://user:[email protected]/bucket/folder/file.ts",
expected: "s3+https://resu:[email protected]/backup-bucket/folder/file.ts",
},
{
name: "no backup URL",
input: "s3+https://another.storage.fish/bucket/folder/file.ts",
expectedErr: "no backup URL",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require := require.New(t)
uri, err := buildBackupURI(mustParseURL(tc.input), storageFallbackURLs)
if tc.expectedErr != "" {
require.ErrorContains(err, tc.expectedErr)
return
}
require.NoError(err)
require.Equal(tc.expected, uri.String())
})
}
}

// SlowReader outputs the lines of text with a delay before each one
type SlowReader struct {
lines []string
Expand All @@ -78,3 +151,11 @@ func (sr *SlowReader) Read(b []byte) (n int, err error) {

return 0, io.EOF
}

func mustParseURL(s string) *url.URL {
u, err := url.Parse(s)
if err != nil {
panic(err)
}
return u
}

0 comments on commit 4f56725

Please sign in to comment.