-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new `--progress` option that defaults to `text` and show upload progress when uploading an AMI.
- Loading branch information
Showing
9 changed files
with
198 additions
and
14 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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package uploader_test | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
Check failure on line 6 in bib/internal/uploader/aws_test.go GitHub Actions / ⌨ Lint
|
||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/aws/aws-sdk-go/service/s3/s3manager" | ||
"github.com/cheggaaa/pb" | ||
|
||
"github.com/osbuild/bootc-image-builder/bib/internal/uploader" | ||
) | ||
|
||
type FakeAwsUploader struct { | ||
uploadCalled int | ||
registerCalled int | ||
} | ||
|
||
func (f *FakeAwsUploader) UploadFromReader(r io.Reader, bucketName, keyName string) (*s3manager.UploadOutput, error) { | ||
f.uploadCalled++ | ||
|
||
if _, err := ioutil.ReadAll(r); err != nil { | ||
panic(err) | ||
} | ||
|
||
return &s3manager.UploadOutput{Location: "some-location"}, nil | ||
} | ||
|
||
func (f *FakeAwsUploader) Register(name, bucket, key string, shareWith []string, rpmArch string, bootMode *string) (*string, *string, error) { | ||
f.registerCalled++ | ||
|
||
s1 := "ret1" | ||
s2 := "ret2" | ||
return &s1, &s2, nil | ||
} | ||
|
||
func TestUploadAndRegisterNoProgressBar(t *testing.T) { | ||
fakeStdout := bytes.NewBuffer(nil) | ||
restore := uploader.MockOsStdout(fakeStdout) | ||
defer restore() | ||
|
||
fakeDiskFile := filepath.Join(t.TempDir(), "fake-disk.img") | ||
err := os.WriteFile(fakeDiskFile, nil, 0644) | ||
require.Nil(t, err) | ||
fakeUploader := &FakeAwsUploader{} | ||
|
||
err = uploader.UploadAndRegister(fakeUploader, fakeDiskFile, "bucketName", "imageName", nil) | ||
require.Nil(t, err) | ||
|
||
assert.Equal(t, fakeUploader.uploadCalled, 1) | ||
assert.Equal(t, fakeUploader.registerCalled, 1) | ||
|
||
assert.Contains(t, fakeStdout.String(), "Uploading ") | ||
assert.Contains(t, fakeStdout.String(), "Registering AMI ") | ||
} | ||
|
||
func TestUploadAndRegisterProgressBar(t *testing.T) { | ||
fakeStdout := bytes.NewBuffer(nil) | ||
restore := uploader.MockOsStdout(fakeStdout) | ||
defer restore() | ||
|
||
fakeDiskFile := filepath.Join(t.TempDir(), "fake-disk.img") | ||
err := os.WriteFile(fakeDiskFile, nil, 0644) | ||
require.Nil(t, err) | ||
err = os.Truncate(fakeDiskFile, 10*1024*1024) | ||
require.Nil(t, err) | ||
|
||
fakeUploader := &FakeAwsUploader{} | ||
|
||
pbar := pb.New(0) | ||
|
||
err = uploader.UploadAndRegister(fakeUploader, fakeDiskFile, "bucketName", "imageName", pbar) | ||
require.Nil(t, err) | ||
|
||
assert.Equal(t, fakeUploader.uploadCalled, 1) | ||
assert.Equal(t, fakeUploader.registerCalled, 1) | ||
|
||
assert.Contains(t, fakeStdout.String(), "Uploading ") | ||
assert.Contains(t, fakeStdout.String(), "10.00 MiB / 10.00 MiB [============================================] 100.00%") | ||
assert.Contains(t, fakeStdout.String(), "Registering AMI ") | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package uploader | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
func MockOsStdout(new io.Writer) (restore func()) { | ||
saved := osStdout | ||
osStdout = new | ||
return func() { | ||
osStdout = saved | ||
} | ||
} |
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