-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_test.go
126 lines (111 loc) ยท 3.38 KB
/
upload_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/stretchr/testify/assert"
"github.com/umi0410/ezconfig"
"net/http"
"os"
"path"
"testing"
"time"
)
var (
uploader Uploader
uploadTaskChan chan *ImageUploadTask
uploaderQuit chan interface{}
uploadPathForTest string = "test_data" // ์ฃผ์. ์ด ํจ์ค๋ ์ํ ํ ์ฌ๋ผ์ง.
)
func init() {
ezconfig.LoadConfig("KHUMU", Config, []string{"./config", os.Getenv("KHUMU_CONFIG_PATH")})
}
func BeforeEachUploadTest_DiskUploader(tb testing.TB) {
uploaderQuit = make(chan interface{}, 100)
uploadTaskChan = make(chan *ImageUploadTask, 100)
uploader = &DiskUploader{
UploadTaskChan: uploadTaskChan,
Quit: uploaderQuit,
}
err := os.Mkdir(uploadPathForTest, 0755)
assert.NoError(tb, err)
}
func AfterEachUploadTest_DiskUploader(tb testing.TB) {
err := os.RemoveAll(uploadPathForTest)
assert.NoError(tb, err)
uploaderQuit = nil
uploadTaskChan = nil
uploader = nil
}
func BeforeEachUploadTest_S3Uploader(tb testing.TB) {
uploadTaskChan = make(chan *ImageUploadTask, 100)
uploaderQuit = make(chan interface{}, 100)
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String("ap-northeast-2"),
},
})
assert.NoError(tb, err)
uploader = NewS3Uploader(uploadTaskChan, uploaderQuit, sess)
}
// S3์ ํ
์คํธ ๋ฐ์ดํฐ๋ ์ง์ด๋ค.
func AfterEachUploadTest_S3Uploader(tb testing.TB) {
s3Service := s3.New(uploader.(*S3Uploader).sess)
objects, err := s3Service.ListObjects(
&s3.ListObjectsInput{
Bucket: aws.String(uploader.(*S3Uploader).bucketName),
Prefix: aws.String(uploadPathForTest),
},
)
assert.NoError(tb, err)
objectIdentifiers := make([]*s3.ObjectIdentifier, 0)
for _, object := range objects.Contents {
objectIdentifiers = append(objectIdentifiers, &s3.ObjectIdentifier{Key: object.Key})
}
_, err = s3Service.DeleteObjects(
&s3.DeleteObjectsInput{
Bucket: aws.String(uploader.(*S3Uploader).bucketName),
Delete: &s3.Delete{
Objects: objectIdentifiers,
},
},
)
assert.NoError(tb, err)
uploaderQuit = nil
uploadTaskChan = nil
uploader = nil
}
func TestS3Uploader_Upload(t *testing.T) {
BeforeEachUploadTest_S3Uploader(t)
defer AfterEachUploadTest_S3Uploader(t)
imageData := DownloadSampleImage(t)
task := &ImageUploadTask{
BaseImageTask: &BaseImageTask{
ImageData: imageData, OriginalFileName: "google_logo.png", HashedFileName: "abcd1234abcd", Extension: "png",
},
UploadPath: uploadPathForTest,
}
err := uploader.Upload(task)
assert.NoError(t, err)
resp, err := http.Get(fmt.Sprintf("%s%s/%s", Config.Storage.Aws.Endpoint, task.UploadPath, task.HashedFileName+"."+task.Extension))
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func TestDiskUploader_Start(t *testing.T) {
BeforeEachUploadTest_DiskUploader(t)
defer AfterEachUploadTest_DiskUploader(t)
imageData := DownloadSampleImage(t)
go uploader.Start()
task := &ImageUploadTask{
BaseImageTask: &BaseImageTask{
ImageData: imageData, OriginalFileName: "google_logo.png", HashedFileName: "abcd1234abcd", Extension: "png",
},
UploadPath: uploadPathForTest,
}
uploadTaskChan <- task
time.Sleep(time.Second * 3)
uploaderQuit <- struct{}{}
_, err := os.Stat(path.Join(uploadPathForTest, "abcd1234abcd.png"))
assert.NoError(t, err)
}