Skip to content

Commit e8ea111

Browse files
authored
Merge pull request #43 from st-tech/refactor_provider
Refactor Providers: CloudStorageProvider and NotificationServiceProvider
2 parents 5b472c2 + bcaab13 commit e8ea111

18 files changed

+717
-301
lines changed

controllers/cloudstorage.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

controllers/cloudstorage_test.go

Lines changed: 0 additions & 108 deletions
This file was deleted.

controllers/cloudstorages/aws.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cloudstorages
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type AWSCloudStorageProvider struct {
8+
providerName string
9+
}
10+
11+
func (p *AWSCloudStorageProvider) GetName() string {
12+
return p.providerName
13+
}
14+
15+
func (p *AWSCloudStorageProvider) GetCloudStoragePath(bucket string, gatlingName string, subDir string) string {
16+
// Format s3:<bucket>/<gatling-name>/<sub-dir>
17+
return fmt.Sprintf("s3:%s/%s/%s", bucket, gatlingName, subDir)
18+
}
19+
20+
func (p *AWSCloudStorageProvider) GetCloudStorageReportURL(bucket string, gatlingName string, subDir string) string {
21+
// Format https://<bucket>.s3.amazonaws.com/<gatling-name>/<sub-dir>/index.html
22+
return fmt.Sprintf("https://%s.s3.amazonaws.com/%s/%s/index.html", bucket, gatlingName, subDir)
23+
}
24+
25+
func (p *AWSCloudStorageProvider) GetGatlingTransferResultCommand(resultsDirectoryPath string, region string, storagePath string) string {
26+
template := `
27+
RESULTS_DIR_PATH=%s
28+
rclone config create s3 s3 env_auth=true region %s
29+
for source in $(find ${RESULTS_DIR_PATH} -type f -name *.log)
30+
do
31+
rclone copyto ${source} --s3-no-check-bucket --s3-env-auth %s/${HOSTNAME}.log
32+
done
33+
`
34+
return fmt.Sprintf(template, resultsDirectoryPath, region, storagePath)
35+
}
36+
37+
func (p *AWSCloudStorageProvider) GetGatlingAggregateResultCommand(resultsDirectoryPath string, region string, storagePath string) string {
38+
template := `
39+
GATLING_AGGREGATE_DIR=%s
40+
rclone config create s3 s3 env_auth=true region %s
41+
rclone copy --s3-no-check-bucket --s3-env-auth %s ${GATLING_AGGREGATE_DIR}
42+
`
43+
return fmt.Sprintf(template, resultsDirectoryPath, region, storagePath)
44+
}
45+
46+
func (p *AWSCloudStorageProvider) GetGatlingTransferReportCommand(resultsDirectoryPath string, region string, storagePath string) string {
47+
template := `
48+
GATLING_AGGREGATE_DIR=%s
49+
rclone config create s3 s3 env_auth=true region %s
50+
rclone copy ${GATLING_AGGREGATE_DIR} --exclude "*.log" --s3-no-check-bucket --s3-env-auth %s
51+
`
52+
return fmt.Sprintf(template, resultsDirectoryPath, region, storagePath)
53+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package cloudstorages
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
)
7+
8+
var _ = Describe("GetName", func() {
9+
var (
10+
provider string
11+
expectedValue string
12+
)
13+
BeforeEach(func() {
14+
provider = "aws"
15+
expectedValue = "aws"
16+
})
17+
Context("Provider is aws", func() {
18+
It("should get provider name = aws", func() {
19+
csp := &AWSCloudStorageProvider{providerName: provider}
20+
Expect(csp.GetName()).To(Equal(expectedValue))
21+
})
22+
})
23+
})
24+
25+
var _ = Describe("GetCloudStoragePath", func() {
26+
var (
27+
provider string
28+
bucket string
29+
gatlingName string
30+
subDir string
31+
expectedValue string
32+
)
33+
BeforeEach(func() {
34+
provider = "aws"
35+
bucket = "testBucket"
36+
gatlingName = "testGatling"
37+
subDir = "subDir"
38+
expectedValue = "s3:testBucket/testGatling/subDir"
39+
})
40+
Context("Provider is aws", func() {
41+
It("path is aws s3 bucket", func() {
42+
csp := &AWSCloudStorageProvider{providerName: provider}
43+
Expect(csp.GetCloudStoragePath(bucket, gatlingName, subDir)).To(Equal(expectedValue))
44+
})
45+
})
46+
})
47+
48+
var _ = Describe("GetCloudStorageReportURL", func() {
49+
var (
50+
provider string
51+
bucket string
52+
gatlingName string
53+
subDir string
54+
expectedValue string
55+
)
56+
BeforeEach(func() {
57+
provider = "aws"
58+
bucket = "testBucket"
59+
gatlingName = "testGatling"
60+
subDir = "subDir"
61+
expectedValue = "https://testBucket.s3.amazonaws.com/testGatling/subDir/index.html"
62+
})
63+
Context("Provider is aws", func() {
64+
It("path is aws s3 bucket", func() {
65+
csp := &AWSCloudStorageProvider{providerName: provider}
66+
Expect(csp.GetCloudStorageReportURL(bucket, gatlingName, subDir)).To(Equal(expectedValue))
67+
})
68+
})
69+
})
70+
71+
var _ = Describe("GetGatlingTransferResultCommand", func() {
72+
var (
73+
provider string
74+
resultsDirectoryPath string
75+
region string
76+
storagePath string
77+
expectedValue string
78+
)
79+
BeforeEach(func() {
80+
provider = "aws"
81+
resultsDirectoryPath = "testResultsDirectoryPath"
82+
region = "ap-northeast-1"
83+
storagePath = "testStoragePath"
84+
expectedValue = `
85+
RESULTS_DIR_PATH=testResultsDirectoryPath
86+
rclone config create s3 s3 env_auth=true region ap-northeast-1
87+
for source in $(find ${RESULTS_DIR_PATH} -type f -name *.log)
88+
do
89+
rclone copyto ${source} --s3-no-check-bucket --s3-env-auth testStoragePath/${HOSTNAME}.log
90+
done
91+
`
92+
})
93+
Context("Provider is aws", func() {
94+
It("returns commands with s3 rclone config", func() {
95+
csp := &AWSCloudStorageProvider{providerName: provider}
96+
Expect(csp.GetGatlingTransferResultCommand(resultsDirectoryPath, region, storagePath)).To(Equal(expectedValue))
97+
})
98+
})
99+
})
100+
101+
var _ = Describe("GetGatlingAggregateResultCommand", func() {
102+
var (
103+
provider string
104+
resultsDirectoryPath string
105+
region string
106+
storagePath string
107+
expectedValue string
108+
)
109+
BeforeEach(func() {
110+
provider = "aws"
111+
resultsDirectoryPath = "testResultsDirectoryPath"
112+
region = "ap-northeast-1"
113+
storagePath = "testStoragePath"
114+
expectedValue = `
115+
GATLING_AGGREGATE_DIR=testResultsDirectoryPath
116+
rclone config create s3 s3 env_auth=true region ap-northeast-1
117+
rclone copy --s3-no-check-bucket --s3-env-auth testStoragePath ${GATLING_AGGREGATE_DIR}
118+
`
119+
})
120+
Context("Provider is aws", func() {
121+
It("returns commands with s3 rclone config", func() {
122+
csp := &AWSCloudStorageProvider{providerName: provider}
123+
Expect(csp.GetGatlingAggregateResultCommand(resultsDirectoryPath, region, storagePath)).To(Equal(expectedValue))
124+
})
125+
})
126+
})
127+
128+
var _ = Describe("GetGatlingTransferReportCommand", func() {
129+
var (
130+
provider string
131+
resultsDirectoryPath string
132+
region string
133+
storagePath string
134+
expectedValue string
135+
)
136+
BeforeEach(func() {
137+
provider = "aws"
138+
resultsDirectoryPath = "testResultsDirectoryPath"
139+
region = "ap-northeast-1"
140+
storagePath = "testStoragePath"
141+
expectedValue = `
142+
GATLING_AGGREGATE_DIR=testResultsDirectoryPath
143+
rclone config create s3 s3 env_auth=true region ap-northeast-1
144+
rclone copy ${GATLING_AGGREGATE_DIR} --exclude "*.log" --s3-no-check-bucket --s3-env-auth testStoragePath
145+
`
146+
})
147+
Context("Provider is aws", func() {
148+
It("returns commands with s3 rclone config", func() {
149+
csp := &AWSCloudStorageProvider{providerName: provider}
150+
Expect(csp.GetGatlingTransferReportCommand(resultsDirectoryPath, region, storagePath)).To(Equal(expectedValue))
151+
})
152+
})
153+
})
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cloudstorages
2+
3+
import (
4+
"sync"
5+
)
6+
7+
type CloudStorageProvider interface {
8+
GetName() string
9+
GetCloudStoragePath(bucket string, gatlingName string, subDir string) string
10+
GetCloudStorageReportURL(bucket string, gatlingName string, subDir string) string
11+
GetGatlingTransferResultCommand(resultsDirectoryPath string, region string, storagePath string) string
12+
GetGatlingAggregateResultCommand(resultsDirectoryPath string, region string, storagePath string) string
13+
GetGatlingTransferReportCommand(resultsDirectoryPath string, region string, storagePath string) string
14+
}
15+
16+
// use sync.Map to achieve thread safe read and write to map
17+
var cloudStorageProvidersSyncMap = &sync.Map{}
18+
19+
func GetProvider(provider string) *CloudStorageProvider {
20+
v, ok := cloudStorageProvidersSyncMap.Load(provider)
21+
if !ok {
22+
var csp CloudStorageProvider
23+
switch provider {
24+
case "aws":
25+
csp = &AWSCloudStorageProvider{providerName: provider}
26+
case "gcp":
27+
csp = &GCPCloudStorageProvider{providerName: provider}
28+
default:
29+
return nil
30+
}
31+
v, _ = cloudStorageProvidersSyncMap.LoadOrStore(provider, &csp)
32+
}
33+
return v.(*CloudStorageProvider)
34+
}

0 commit comments

Comments
 (0)