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 d509819
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 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,75 @@ 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)

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), []byte("test"), nil, 0, false, storageFallbackURLs)
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 +148,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 d509819

Please sign in to comment.