Skip to content

Commit 299c9a6

Browse files
authored
Testing - All languages - Remediate Dockerfiles (#5416)
1 parent f2c6867 commit 299c9a6

File tree

18 files changed

+281
-149
lines changed

18 files changed

+281
-149
lines changed

cpp/Dockerfile

+6-6
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ RUN \
4444
# Copy the C++ example code.
4545
RUN mkdir -p /src/cpp
4646

47-
COPY cpp /src/cpp/
47+
COPY . /src/cpp/
4848

49-
# The sample files are needed for some of the automated tests.
50-
RUN mkdir -p /src/resources/sample_files
51-
COPY resources/sample_files /src/resources/sample_files
49+
# Commented until https://github.com/awsdocs/aws-doc-sdk-examples/issues/5454 resolved.
50+
## The sample files are needed for some of the automated tests.
51+
#RUN mkdir -p /src/resources/sample_files
52+
#COPY resources/sample_files /src/resources/sample_files
5253

5354
WORKDIR /src/cpp/
5455

@@ -57,5 +58,4 @@ RUN useradd -ms /bin/bash tests && \
5758

5859
USER tests
5960

60-
CMD ["bash"]
61-
61+
CMD ["python3", "/src/cpp/run_automated_tests.py "]

cpp/README.rst

+15
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ undergoing active development. Refer to
5858
[this GitHub issue](https://github.com/awsdocs/aws-doc-sdk-examples/issues/4133)
5959
for more information.
6060

61+
Build steps
62+
-----------
63+
To build the docker image, run the following command from the shell. This command must be run in
64+
the "aws-doc-sdk-examples" directory, the parent directory of "cpp", in order to access the resources folder.
65+
66+
.. code-block:: bash
67+
68+
docker build -f cpp/Dockerfile -t <container_tag> .
69+
70+
The following command will run the docker image, copying your AWS credentials.
71+
72+
.. code-block:: bash
73+
74+
docker run -it --volume ~/.aws/credentials:/home/tests/.aws/credentials <container_tag>
75+
6176
Automated tests
6277
===================
6378

cpp/run_automated_tests.py

+64-51
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,22 @@
2424
#
2525

2626

27-
28-
import os
29-
import subprocess
30-
import sys
27+
import datetime
3128
import getopt
3229
import glob
30+
import os
3331
import re
34-
import datetime
32+
import subprocess
3533
import sys
3634

3735
build_sub_dir = "build_tests"
3836

39-
def build_cmake_tests(cmake_files, executable_pattern) :
37+
38+
def build_cmake_tests(cmake_files, executable_pattern):
4039
global build_sub_dir
4140
run_files = []
4241

43-
if len (cmake_files) == 0:
42+
if len(cmake_files) == 0:
4443
return [1, []]
4544

4645
has_error = False
@@ -53,36 +52,39 @@ def build_cmake_tests(cmake_files, executable_pattern) :
5352

5453
parallel_build = os.getenv("PARALLEL_BUILD")
5554

56-
for cmake_file in cmake_files :
55+
for cmake_file in cmake_files:
5756
source_dir = os.path.dirname(cmake_file)
5857
module_build_dir = os.path.join(build_dir, source_dir)
5958
os.makedirs(name=module_build_dir, exist_ok=True)
6059
os.chdir(module_build_dir)
6160

62-
cmake_command = ['cmake']
61+
cmake_command = ["cmake"]
6362
if cmake_args is not None:
6463
cmake_command.append(cmake_args)
6564
cmake_command.append(os.path.join(base_dir, source_dir))
6665

6766
if sys.platform == "win32":
68-
cmake_command.append('-DBIN_SUB_DIR=/Debug')
67+
cmake_command.append("-DBIN_SUB_DIR=/Debug")
6968

7069
result_code = subprocess.call(cmake_command, shell=False)
71-
if result_code != 0 :
70+
if result_code != 0:
7271
print(f"Error with cmake for {source_dir}")
7372
has_error = True
7473
continue
7574

7675
if parallel_build is not None:
7776
print("building parallel")
78-
result_code = subprocess.call(['cmake', '--build', '.', '--parallel', f'{parallel_build}'], shell=False)
77+
result_code = subprocess.call(
78+
["cmake", "--build", ".", "--parallel", f"{parallel_build}"],
79+
shell=False,
80+
)
7981
else:
80-
result_code = subprocess.call(['cmake', '--build', '.'], shell=False)
82+
result_code = subprocess.call(["cmake", "--build", "."], shell=False)
8183

82-
if result_code != 0 :
84+
if result_code != 0:
8385
has_error = True
8486
continue
85-
for executable in executable_pattern :
87+
for executable in executable_pattern:
8688
run_files.extend(glob.glob(f"{module_build_dir}{executable}"))
8789

8890
if has_error:
@@ -92,25 +94,25 @@ def build_cmake_tests(cmake_files, executable_pattern) :
9294

9395

9496
def build_tests(service="*"):
95-
cmake_files = glob.glob( f"example_code/{service}/tests/CMakeLists.txt")
96-
cmake_files.extend(glob.glob( f"example_code/{service}/gtests/CMakeLists.txt"))
97+
cmake_files = glob.glob(f"example_code/{service}/tests/CMakeLists.txt")
98+
cmake_files.extend(glob.glob(f"example_code/{service}/gtests/CMakeLists.txt"))
9799

98100
executable_pattern = ["/*_gtest", "/Debug/*_gtest.exe"]
99101

100102
return build_cmake_tests(cmake_files, executable_pattern)
101103

102104

103-
def run_tests(run_files = [], type1=False, type2=False, type3=False):
105+
def run_tests(run_files=[], type1=False, type2=False, type3=False):
104106
global build_sub_dir
105107
has_error = False
106108
filters = []
107-
if type1 :
109+
if type1:
108110
filters.append("*_1_")
109111

110-
if type2 :
112+
if type2:
111113
filters.append("*_2_")
112114

113-
if type3 :
115+
if type3:
114116
filters.append("*_3_")
115117

116118
filter_arg = ""
@@ -123,9 +125,11 @@ def run_tests(run_files = [], type1=False, type2=False, type3=False):
123125
run_dir = os.path.join(build_sub_dir, "integration_tests_run")
124126
os.makedirs(name=run_dir, exist_ok=True)
125127
os.chdir(run_dir)
126-
for run_file in run_files :
128+
for run_file in run_files:
127129
print(f"Calling '{run_file} {filter_arg}'.")
128-
proc = subprocess.Popen([run_file, filter_arg], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
130+
proc = subprocess.Popen(
131+
[run_file, filter_arg], stdout=subprocess.PIPE, stderr=subprocess.STDOUT
132+
)
129133
for line in proc.stdout:
130134
line = line.decode("utf-8")
131135
sys.stdout.write(line)
@@ -141,29 +145,32 @@ def run_tests(run_files = [], type1=False, type2=False, type3=False):
141145

142146
proc.wait()
143147

144-
if proc.returncode != 0 :
148+
if proc.returncode != 0:
145149
has_error = True
146150

147-
print('-' * 88)
151+
print("-" * 88)
148152
print(f"{passed_tests} tests passed.")
149153
print(f"{failed_tests} tests failed.")
150154

151155
os.chdir(old_dir)
152156
if has_error:
153157
return [1, passed_tests, failed_tests]
154-
else :
158+
else:
155159
return [0, passed_tests, failed_tests]
156160

161+
157162
def test_hello_service(service="*"):
158-
print('-'*88)
163+
print("-" * 88)
159164
print(f"Running hello tests for {service}.")
160165

161166
print(os.getcwd())
162167
cmake_files = glob.glob(f"example_code/{service}/hello_{service}/CMakeLists.txt")
163168

164-
(err_code, run_files) = build_cmake_tests(cmake_files, ['/hello_*', '/Debug/hello_*.exe'])
169+
(err_code, run_files) = build_cmake_tests(
170+
cmake_files, ["/hello_*", "/Debug/hello_*.exe"]
171+
)
165172

166-
if err_code != 0 :
173+
if err_code != 0:
167174
print("Build hello tests failed.")
168175
return [err_code, 0, 0]
169176

@@ -175,45 +182,49 @@ def test_hello_service(service="*"):
175182
passed_count = 0
176183
failed_count = 0
177184
has_error = False
178-
for run_file in run_files :
185+
for run_file in run_files:
179186
path_split = os.path.splitext(run_file)
180-
if (path_split[1] == '.exe') or (path_split[1] == ''):
187+
if (path_split[1] == ".exe") or (path_split[1] == ""):
181188
print(f"Calling '{run_file}'.")
182189
completedProcess = subprocess.run([run_file], stdout=subprocess.DEVNULL)
183-
if completedProcess.returncode != 0 :
190+
if completedProcess.returncode != 0:
184191
print(f"Error with {run_file}")
185192
has_error = True
186193
failed_count = failed_count + 1
187194
else:
188195
passed_count = passed_count + 1
189196

190-
print('-'*88)
197+
print("-" * 88)
191198
print(f"{passed_count} tests passed.")
192199
print(f"{failed_count} tests failed.")
193200
print(f"Total cmake files - {len(cmake_files)}")
194201

195202
os.chdir(old_dir)
196203

197204
if has_error:
198-
return [1, passed_count, failed_count]
199-
else :
200-
return [0, passed_count, failed_count]
205+
return [1, passed_count, failed_count]
206+
else:
207+
return [0, passed_count, failed_count]
208+
201209

202210
def main(argv):
203211
type1 = False
204212
type2 = False
205213
type3 = False
206214
service = "*"
215+
run_dir = os.path.dirname(os.path.realpath(__file__))
216+
print(f"Running script from directory {run_dir}")
217+
os.chdir(run_dir)
207218

208219
opts, args = getopt.getopt(argv, "h123s:")
209220
for opt, arg in opts:
210-
if opt == '-h':
211-
print('run_automated_tests.py -1 -2 -3 -s <service>')
212-
print('Where:')
213-
print(' 1. Requires credentials and pre-configured resources.')
214-
print(' 2. Requires credentials.')
215-
print(' 3. Does not require credentials.')
216-
print(' s. Test this service (regular expression).')
221+
if opt == "-h":
222+
print("run_automated_tests.py -1 -2 -3 -s <service>")
223+
print("Where:")
224+
print(" 1. Requires credentials and pre-configured resources.")
225+
print(" 2. Requires credentials.")
226+
print(" 3. Does not require credentials.")
227+
print(" s. Test this service (regular expression).")
217228
sys.exit()
218229
elif opt in ("-1"):
219230
type1 = True
@@ -230,16 +241,20 @@ def main(argv):
230241

231242
[err_code, run_files] = build_tests(service=service)
232243

233-
if err_code == 0 :
234-
[err_code, passed_count, failed_count]= run_tests(run_files = run_files, type1=type1, type2=type2, type3=type3)
244+
if err_code == 0:
245+
[err_code, passed_count, failed_count] = run_tests(
246+
run_files=run_files, type1=type1, type2=type2, type3=type3
247+
)
235248

236249
os.chdir(base_dir)
237250
if err_code == 0:
238-
[err_code, hello_passed_count, hello_failed_count] = test_hello_service(service=service)
251+
[err_code, hello_passed_count, hello_failed_count] = test_hello_service(
252+
service=service
253+
)
239254
passed_count = passed_count + hello_passed_count
240255
failed_count = failed_count + hello_failed_count
241256

242-
print('-'*88)
257+
print("-" * 88)
243258
print(f"{passed_count} tests passed.")
244259
print(f"{failed_count} tests failed.")
245260

@@ -249,8 +264,6 @@ def main(argv):
249264

250265

251266
if __name__ == "__main__":
252-
result = main(sys.argv[1:])
253-
254-
exit(result)
255-
267+
result = main(sys.argv[1:])
256268

269+
exit(result)

dotnetv3/Dockerfile

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
# syntax=docker/dockerfile:1
2-
# Status: Beta
3-
# GA updates: https://github.com/awsdocs/aws-doc-sdk-examples/issues/4126
42
FROM mcr.microsoft.com/dotnet/sdk:6.0
53

64
# Enable the .NET 5.0 SDK if running 5.0 examples:
@@ -14,5 +12,4 @@ COPY . /dotnetv3
1412

1513
RUN cd /dotnetv3 && dotnet build
1614

17-
CMD ["dotnet", "test", "dotnetv3", "--filter", "Category=Unit"]
18-
15+
CMD ["dotnet", "test", "dotnetv3", "--filter", "Category=Unit"]

gov2/Dockerfile

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
# syntax=docker/dockerfile:1
22
FROM golang:1.19.2
3+
4+
# Update image
5+
RUN apt-get update && \
6+
apt-get upgrade -y && \
7+
apt-get clean && \
8+
rm -rf /var/lib/apt/lists/*
9+
10+
# Copy source code
311
COPY . /gov2
4-
# If your corporate network blocks proxy.golang.org, uncomment this line.
12+
13+
# Perform build steps
514
RUN go env -w GOPROXY=direct
6-
## Run tests in all gov2 folders.
7-
## Pass 'integration' to run_all_tests.sh to run integration tests.
8-
## Output to a file by appending > test-run-unit-$(date +"%Y-%m-%d").out
9-
#RUN cd /gov2 && \
10-
# ./run_all_tests.sh
11-
CMD ["cd", "/gov2", "&&", "./run_all_tests.sh", "integration"]
15+
16+
# Set non-root user
17+
RUN useradd -m automation && \
18+
chown -R automation:automation /gov2/
19+
USER automation:automation
20+
21+
# Set default command
22+
CMD ["./run_all_tests.sh", "integration"]

javascriptv3/Dockerfile

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
# syntax=docker/dockerfile:1
2-
# Status: Beta
3-
# GA updates: https://github.com/awsdocs/aws-doc-sdk-examples/issues/4127
42
FROM node:18
3+
4+
# Update image
5+
RUN apt-get update && \
6+
apt-get upgrade -y && \
7+
apt-get clean && \
8+
rm -rf /var/lib/apt/lists/*
9+
10+
# Copy source code
511
COPY . /javascriptv3
6-
RUN npm install --prefix /javascriptv3
7-
CMD ["bash"]
12+
13+
# Perform build steps
14+
RUN npm install -C /javascriptv3
15+
16+
# Set non-root user
17+
RUN useradd -m automation && \
18+
chown -R automation:automation /javascriptv3/
19+
USER automation:automation
20+
21+
# Set default command
22+
CMD ["./scripts/run_tests.sh", "integration"]

javascriptv3/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "root",
33
"private": true,
44
"scripts": {
5-
"test": "./scripts/test.sh",
5+
"test": "scripts/run_tests.sh",
66
"lint": "lint-staged",
77
"ci-lint": "eslint"
88
},

0 commit comments

Comments
 (0)