Skip to content

Commit 9a8a33e

Browse files
test: mock test for services(s3 and sm)
1 parent 761ee1f commit 9a8a33e

File tree

3 files changed

+434
-0
lines changed

3 files changed

+434
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
setmetatable(_G, nil)
2+
3+
-- to get a definitive result
4+
-- luacheck:ignore
5+
ngx.time = function()
6+
return 1667543171
7+
end
8+
9+
-- hock request sending
10+
package.loaded["resty.aws.request.execute"] = function(...)
11+
return ...
12+
end
13+
14+
local AWS = require("resty.aws")
15+
local AWS_global_config = require("resty.aws.config").global
16+
17+
18+
local config = AWS_global_config
19+
config.tls = true
20+
local aws = AWS(config)
21+
22+
23+
aws.config.credentials = aws:Credentials {
24+
accessKeyId = "test_id",
25+
secretAccessKey = "test_key",
26+
}
27+
28+
aws.config.region = "test_region"
29+
30+
describe("Secret Manager service", function()
31+
local sm
32+
before_each(function()
33+
sm = assert(aws:SecretsManager {})
34+
end)
35+
36+
after_each(function()
37+
end)
38+
39+
local testcases = {
40+
-- API = { param, expected_result_aws, },
41+
getSecretValue = {
42+
{
43+
SecretId = "test_id",
44+
VersionStage = "AWSCURRENT",
45+
},
46+
{
47+
['body'] = '{"VersionStage":"AWSCURRENT","SecretId":"test_id"}',
48+
['headers'] = {
49+
['Authorization'] = 'AWS4-HMAC-SHA256 Credential=test_id/20221104/test_region/secretsmanager/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date;x-amz-target, Signature=81618df993cf58510f22d95efb815d03a9bd3cfb7af0ef766e6980f7a99799ff',
50+
['Content-Length'] = 50,
51+
['Content-Type'] = 'application/x-amz-json-1.1',
52+
['Host'] = 'secretsmanager.test_region.amazonaws.com',
53+
['X-Amz-Date'] = '20221104T062611Z',
54+
['X-Amz-Target'] = 'secretsmanager.GetSecretValue'
55+
},
56+
['host'] = 'secretsmanager.test_region.amazonaws.com',
57+
['method'] = 'POST',
58+
['path'] = '/',
59+
['port'] = 443,
60+
['query'] = {},
61+
['tls'] = true,
62+
},
63+
},
64+
}
65+
66+
for api, test in pairs(testcases) do
67+
it("SecretsManager:" .. api, function()
68+
local param = test[1]
69+
local expected_result_aws = test[2]
70+
71+
local result_aws = assert(sm[api](sm, param))
72+
73+
assert.same(expected_result_aws, result_aws)
74+
end)
75+
end
76+
end)

spec/04-services/02-s3.lua

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
setmetatable(_G, nil)
2+
3+
-- to get a definitive result
4+
-- luacheck:ignore
5+
ngx.time = function()
6+
return 1667543171
7+
end
8+
9+
-- hock request sending
10+
package.loaded["resty.aws.request.execute"] = function(...)
11+
return ...
12+
end
13+
14+
local AWS = require("resty.aws")
15+
local AWS_global_config = require("resty.aws.config").global
16+
17+
18+
local config = AWS_global_config
19+
config.tls = true
20+
local aws = AWS(config)
21+
22+
23+
aws.config.credentials = aws:Credentials {
24+
accessKeyId = "test_id",
25+
secretAccessKey = "test_key",
26+
}
27+
28+
aws.config.region = "test_region"
29+
30+
describe("S3 service", function()
31+
local s3, s3_3rd
32+
before_each(function()
33+
s3 = assert(aws:S3 {})
34+
s3_3rd = assert(aws:S3 {
35+
scheme = "http",
36+
endpoint = "testendpoint.com",
37+
port = 80,
38+
tls = false,
39+
})
40+
end)
41+
42+
after_each(function()
43+
end)
44+
45+
local testcases = {
46+
-- API = { param, expected_result_aws, expected_result_3rd_patry, },
47+
putObject = {
48+
{
49+
Bucket = "testbucket",
50+
Key = "testkey",
51+
Body = "testbody",
52+
Metadata = {
53+
test = "test",
54+
}
55+
},
56+
{
57+
body = 'testbody',
58+
headers = {
59+
['Authorization'] = 'AWS4-HMAC-SHA256 Credential=test_id/20221104/test_region/s3/aws4_request, SignedHeaders=content-length;host;x-amz-content-sha256;x-amz-date;x-amz-meta-test, Signature=57e7e544e5bce7cdf6321768d7577212a874a3504031fba4bb97ab2e5245532f',
60+
['Content-Length'] = 8,
61+
['Host'] = 'testbucket.s3.test_region.amazonaws.com',
62+
['X-Amz-Content-Sha256'] = '2417e54e58ac3752d4d82355e13053e0b3d9601d09d4fd5027be26da405b8ccd',
63+
['X-Amz-Date'] = '20221104T062611Z',
64+
['X-Amz-Meta-Test'] = 'test',
65+
},
66+
host = 'testbucket.s3.test_region.amazonaws.com',
67+
method = 'PUT',
68+
path = '/testkey',
69+
port = 443,
70+
query = {},
71+
tls = true,
72+
},
73+
{
74+
body = 'testbody',
75+
headers = {
76+
['Authorization'] = 'AWS4-HMAC-SHA256 Credential=test_id/20221104/test_region/s3/aws4_request, SignedHeaders=content-length;host;x-amz-content-sha256;x-amz-date;x-amz-meta-test, Signature=c821cc6d135ee1abe2efd235d7a8f699fbaa90e979584cc274f1ea1610679f86',
77+
['Content-Length'] = 8,
78+
['Host'] = 'testbucket.testendpoint.com',
79+
['X-Amz-Content-Sha256'] = '2417e54e58ac3752d4d82355e13053e0b3d9601d09d4fd5027be26da405b8ccd',
80+
['X-Amz-Date'] = '20221104T062611Z',
81+
['X-Amz-Meta-Test'] = 'test',
82+
},
83+
host = 'testbucket.testendpoint.com',
84+
method = 'PUT',
85+
path = '/testkey',
86+
port = 80,
87+
query = {},
88+
tls = false,
89+
},
90+
},
91+
getObject = {
92+
{
93+
Bucket = "testbucket",
94+
Key = "testkey",
95+
},
96+
{
97+
['headers'] = {
98+
['Authorization'] = 'AWS4-HMAC-SHA256 Credential=test_id/20221104/test_region/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=a1cab7c5a3e2ec70af4acaed2dd5382842080af7dbe8f4416540cc99357b322b',
99+
['Host'] = 'testbucket.s3.test_region.amazonaws.com',
100+
['X-Amz-Content-Sha256'] = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
101+
['X-Amz-Date'] = '20221104T062611Z'
102+
},
103+
['host'] = 'testbucket.s3.test_region.amazonaws.com',
104+
['method'] = 'GET',
105+
['path'] = '/testkey',
106+
['port'] = 443,
107+
['query'] = {},
108+
['tls'] = true
109+
},
110+
{
111+
['headers'] = {
112+
['Authorization'] = 'AWS4-HMAC-SHA256 Credential=test_id/20221104/test_region/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=f0ba4ea255b0678c5e9339e44a976e3f6547bddfaf5dfe5a86403dc97d891010',
113+
['Host'] = 'testbucket.testendpoint.com',
114+
['X-Amz-Content-Sha256'] = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
115+
['X-Amz-Date'] = '20221104T062611Z'
116+
},
117+
['host'] = 'testbucket.testendpoint.com',
118+
['method'] = 'GET',
119+
['path'] = '/testkey',
120+
['port'] = 80,
121+
['query'] = {},
122+
['tls'] = false,
123+
}
124+
},
125+
getBucketAcl = {
126+
{
127+
Bucket = "testbucket",
128+
},
129+
{
130+
['headers'] = {
131+
['Authorization'] = 'AWS4-HMAC-SHA256 Credential=test_id/20221104/test_region/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=69eb892e8f3cae5b3f777c31e4318d946ce0ebba97f8539a5064e1709d8477c6',
132+
['Host'] = 'testbucket.s3.test_region.amazonaws.com',
133+
['X-Amz-Content-Sha256'] = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
134+
['X-Amz-Date'] = '20221104T062611Z'
135+
},
136+
['host'] = 'testbucket.s3.test_region.amazonaws.com',
137+
['method'] = 'GET',
138+
['path'] = '',
139+
['port'] = 443,
140+
['query'] = {
141+
['acl'] = ''
142+
},
143+
['tls'] = true,
144+
},
145+
{
146+
['headers'] = {
147+
['Authorization'] = 'AWS4-HMAC-SHA256 Credential=test_id/20221104/test_region/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=0aac6b456c28cd393ca06a779074c4797155338569fad6f3e95ea348406b16a9',
148+
['Host'] = 'testbucket.testendpoint.com',
149+
['X-Amz-Content-Sha256'] = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
150+
['X-Amz-Date'] = '20221104T062611Z'
151+
},
152+
['host'] = 'testbucket.testendpoint.com',
153+
['method'] = 'GET',
154+
['path'] = '',
155+
['port'] = 80,
156+
['query'] = {
157+
['acl'] = ''
158+
},
159+
['tls'] = false,
160+
},
161+
},
162+
}
163+
164+
165+
for api, test in pairs(testcases) do
166+
it("s3:" .. api, function()
167+
local param = test[1]
168+
local expected_result_aws = test[2]
169+
local expected_result_3rd_patry = test[3]
170+
171+
local result_aws = assert(s3[api](s3, param))
172+
local result_3rd_patry = assert(s3_3rd[api](s3_3rd, param))
173+
174+
assert.same(expected_result_aws, result_aws)
175+
assert.same(expected_result_3rd_patry, result_3rd_patry)
176+
end)
177+
end
178+
end)

0 commit comments

Comments
 (0)