1
+ import { GetObjectTaggingCommand , PutObjectCommand , S3Client } from '@aws-sdk/client-s3' ;
2
+ import { mockClient } from 'aws-sdk-client-mock' ;
3
+ import 'aws-sdk-client-mock-jest' ;
1
4
import axios from 'axios' ;
2
5
import { PassThrough } from 'stream' ;
3
6
@@ -21,20 +24,13 @@ mockStream.push(mockResponse);
21
24
mockStream . end ( ) ;
22
25
23
26
jest . mock ( 'axios' ) ;
24
- const mockedAxios = axios as jest . Mocked < typeof axios > ;
25
- mockedAxios . request . mockResolvedValue ( {
27
+ const mockAxios = axios as jest . Mocked < typeof axios > ;
28
+ mockAxios . get . mockResolvedValue ( {
26
29
data : mockStream ,
27
30
} ) ;
28
31
29
- const mockS3 = {
30
- getObjectTagging : jest . fn ( ) ,
31
- upload : jest . fn ( ) . mockImplementation ( ( ) => {
32
- return { promise : jest . fn ( ( ) => Promise . resolve ( ) ) } ;
33
- } ) ,
34
- } ;
35
- jest . mock ( 'aws-sdk' , ( ) => ( {
36
- S3 : jest . fn ( ) . mockImplementation ( ( ) => mockS3 ) ,
37
- } ) ) ;
32
+ process . env . AWS_REGION = 'us-east-1' ;
33
+ const mockS3client = mockClient ( S3Client ) ;
38
34
39
35
const bucketName = 'my-bucket' ;
40
36
const objectExtension : Record < string , string > = {
@@ -54,14 +50,14 @@ const latestRelease = '2.296.2';
54
50
55
51
beforeEach ( ( ) => {
56
52
jest . clearAllMocks ( ) ;
53
+ mockS3client . reset ( ) ;
57
54
} ) ;
58
55
59
56
jest . setTimeout ( 60 * 1000 ) ;
60
57
61
58
describe ( 'Synchronize action distribution (no S3 tags).' , ( ) => {
62
59
beforeEach ( ( ) => {
63
60
process . env . S3_BUCKET_NAME = bucketName ;
64
-
65
61
mockOctokit . repos . getLatestRelease . mockImplementation ( ( ) => ( {
66
62
data : mockDataLatestRelease ,
67
63
} ) ) ;
@@ -70,25 +66,19 @@ describe('Synchronize action distribution (no S3 tags).', () => {
70
66
test . each ( runnerOs ) ( '%p Distribution is S3 has no tags.' , async ( os ) => {
71
67
process . env . S3_OBJECT_KEY = bucketObjectKey ( os ) ;
72
68
process . env . GITHUB_RUNNER_OS = os ;
73
- mockS3 . getObjectTagging . mockImplementation ( ( ) => {
74
- return {
75
- promise ( ) {
76
- return Promise . resolve ( {
77
- TagSet : undefined ,
78
- } ) ;
79
- } ,
80
- } ;
69
+ mockS3client . on ( GetObjectTaggingCommand ) . resolves ( {
70
+ TagSet : undefined ,
81
71
} ) ;
82
72
83
73
await sync ( ) ;
84
- expect ( mockS3 . upload ) . toBeCalledTimes ( 1 ) ;
74
+ expect ( mockS3client ) . toHaveReceivedCommandTimes ( PutObjectCommand , 1 ) ;
85
75
} ) ;
86
76
} ) ;
87
77
88
- describe ( 'Synchronize action distribution (up-to-date) .' , ( ) => {
78
+ describe ( 'Synchronize action distribution.' , ( ) => {
89
79
beforeEach ( ( ) => {
90
80
process . env . S3_BUCKET_NAME = bucketName ;
91
-
81
+ mockS3client . reset ( ) ;
92
82
mockOctokit . repos . getLatestRelease . mockImplementation ( ( ) => ( {
93
83
data : mockDataLatestRelease ,
94
84
} ) ) ;
@@ -97,67 +87,81 @@ describe('Synchronize action distribution (up-to-date).', () => {
97
87
test . each ( runnerOs ) ( '%p Distribution is up-to-date with latest release.' , async ( os ) => {
98
88
process . env . S3_OBJECT_KEY = bucketObjectKey ( os ) ;
99
89
process . env . GITHUB_RUNNER_OS = os ;
100
- mockS3 . getObjectTagging . mockImplementation ( ( ) => {
101
- return {
102
- promise ( ) {
103
- return Promise . resolve ( {
104
- TagSet : [ { Key : 'name' , Value : `actions-runner-${ os } -x64-${ latestRelease } ${ objectExtension [ os ] } ` } ] ,
105
- } ) ;
106
- } ,
107
- } ;
90
+ mockS3client . on ( GetObjectTaggingCommand ) . resolves ( {
91
+ TagSet : [ { Key : 'name' , Value : `actions-runner-${ os } -x64-${ latestRelease } ${ objectExtension [ os ] } ` } ] ,
108
92
} ) ;
109
93
110
94
await sync ( ) ;
111
95
expect ( mockOctokit . repos . getLatestRelease ) . toBeCalledTimes ( 1 ) ;
112
- expect ( mockS3 . getObjectTagging ) . toBeCalledWith ( {
96
+ expect ( mockS3client ) . toHaveReceivedNthCommandWith ( 1 , GetObjectTaggingCommand , {
113
97
Bucket : bucketName ,
114
98
Key : bucketObjectKey ( os ) ,
115
99
} ) ;
116
- expect ( mockS3 . upload ) . toBeCalledTimes ( 0 ) ;
100
+
101
+ expect ( mockS3client ) . toHaveReceivedCommandTimes ( PutObjectCommand , 0 ) ;
117
102
} ) ;
118
103
119
104
test . each ( runnerOs ) ( '%p Distribution should update to release.' , async ( os ) => {
120
105
process . env . S3_OBJECT_KEY = bucketObjectKey ( os ) ;
121
106
process . env . GITHUB_RUNNER_OS = os ;
122
- mockS3 . getObjectTagging . mockImplementation ( ( ) => {
123
- return {
124
- promise ( ) {
125
- return Promise . resolve ( {
126
- TagSet : [ { Key : 'name' , Value : `actions-runner-${ os } -x64-0${ objectExtension [ os ] } ` } ] ,
127
- } ) ;
128
- } ,
129
- } ;
107
+
108
+ mockS3client . on ( GetObjectTaggingCommand ) . resolves ( {
109
+ TagSet : [ { Key : 'name' , Value : `actions-runner-${ os } -x64-0${ objectExtension [ os ] } ` } ] ,
130
110
} ) ;
131
111
132
112
await sync ( ) ;
133
113
expect ( mockOctokit . repos . getLatestRelease ) . toBeCalledTimes ( 1 ) ;
134
- expect ( mockS3 . getObjectTagging ) . toBeCalledWith ( {
114
+ expect ( mockS3client ) . toHaveReceivedNthCommandWith ( 1 , GetObjectTaggingCommand , {
135
115
Bucket : bucketName ,
136
116
Key : bucketObjectKey ( os ) ,
137
117
} ) ;
138
- expect ( mockS3 . upload ) . toBeCalledTimes ( 1 ) ;
139
- const s3JsonBody = mockS3 . upload . mock . calls [ 0 ] [ 0 ] ;
140
- expect ( s3JsonBody [ 'Tagging' ] ) . toEqual ( `name=actions-runner-${ os } -x64-${ latestRelease } ${ objectExtension [ os ] } ` ) ;
118
+
119
+ expect ( mockS3client ) . toHaveReceivedNthSpecificCommandWith ( 1 , PutObjectCommand , {
120
+ Bucket : bucketName ,
121
+ Key : bucketObjectKey ( os ) ,
122
+ Tagging : `name=actions-runner-${ os } -x64-${ latestRelease } ${ objectExtension [ os ] } ` ,
123
+ } ) ;
124
+ } ) ;
125
+
126
+ test . each ( runnerOs ) ( '%p Distribution should update to release (tags look-up errored)' , async ( os ) => {
127
+ process . env . S3_OBJECT_KEY = bucketObjectKey ( os ) ;
128
+ process . env . GITHUB_RUNNER_OS = os ;
129
+
130
+ mockS3client . on ( GetObjectTaggingCommand ) . rejects ( new Error ( 'No tags' ) ) ;
131
+
132
+ await sync ( ) ;
133
+ expect ( mockOctokit . repos . getLatestRelease ) . toBeCalledTimes ( 1 ) ;
134
+ expect ( mockS3client ) . toHaveReceivedNthCommandWith ( 1 , GetObjectTaggingCommand , {
135
+ Bucket : bucketName ,
136
+ Key : bucketObjectKey ( os ) ,
137
+ } ) ;
138
+
139
+ expect ( mockS3client ) . toHaveReceivedNthSpecificCommandWith ( 1 , PutObjectCommand , {
140
+ Bucket : bucketName ,
141
+ Key : bucketObjectKey ( os ) ,
142
+ Tagging : `name=actions-runner-${ os } -x64-${ latestRelease } ${ objectExtension [ os ] } ` ,
143
+ } ) ;
141
144
} ) ;
142
145
143
146
test . each ( runnerOs ) ( '%p Tags, but no version, distribution should update.' , async ( os ) => {
144
147
process . env . S3_OBJECT_KEY = bucketObjectKey ( os ) ;
145
148
process . env . GITHUB_RUNNER_OS = os ;
146
- mockS3 . getObjectTagging . mockImplementation ( ( ) => {
147
- return {
148
- promise ( ) {
149
- return Promise . resolve ( { TagSet : [ { Key : 'someKey' , Value : 'someValue' } ] } ) ;
150
- } ,
151
- } ;
149
+ mockS3client . on ( GetObjectTaggingCommand ) . resolves ( {
150
+ TagSet : [ { Key : 'someKey' , Value : `someValue` } ] ,
152
151
} ) ;
153
152
154
153
await sync ( ) ;
155
154
expect ( mockOctokit . repos . getLatestRelease ) . toBeCalledTimes ( 1 ) ;
156
- expect ( mockS3 . getObjectTagging ) . toBeCalledWith ( {
155
+ expect ( mockS3client ) . toHaveReceivedNthCommandWith ( 1 , GetObjectTaggingCommand , {
156
+ Bucket : bucketName ,
157
+ Key : bucketObjectKey ( os ) ,
158
+ } ) ;
159
+
160
+ expect ( mockS3client ) . toHaveReceivedNthSpecificCommandWith ( 1 , PutObjectCommand , {
157
161
Bucket : bucketName ,
158
162
Key : bucketObjectKey ( os ) ,
163
+ Tagging : `name=actions-runner-${ os } -x64-${ latestRelease } ${ objectExtension [ os ] } ` ,
159
164
} ) ;
160
- expect ( mockS3 . upload ) . toBeCalledTimes ( 1 ) ;
161
165
} ) ;
162
166
} ) ;
163
167
0 commit comments