-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: Add a couple tests for the new logic
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"io" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
@@ -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) | ||
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 | ||
|
@@ -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 | ||
} |