FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
testcontainers-python/modules/generic/example_basic.py at main · guenhter/testcontainers-python · GitHub
guenhter
/
testcontainers-python
Public
forked from
testcontainers/testcontainers-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
testcontainers-python
/
modules
/
generic
/
example_basic.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
115 lines (95 loc) · 4.13 KB
Breadcrumbs
testcontainers-python
/
modules
/
generic
/
example_basic.py
Copy path
File metadata and controls
115 lines (95 loc) · 4.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
requests
from
testcontainers
.
generic
import
GenericContainer
def
basic_example
():
# Example 1: Nginx container
with
GenericContainer
(
"nginx:latest"
)
as
nginx
:
# Get connection parameters
host
=
nginx
.
get_container_host_ip
()
port
=
nginx
.
get_exposed_port
(
80
)
# Test Nginx
response
=
requests
.
get
(
f"http://
{
host
}
:
{
port
}
"
)
print
(
"
\n
Nginx response:"
)
print
(
f"Status code:
{
response
.
status_code
}
"
)
print
(
f"Content type:
{
response
.
headers
.
get
(
'content-type'
)
}
"
)
# Example 2: Redis container with custom configuration
with
GenericContainer
(
"redis:latest"
)
as
redis
:
# Get connection parameters
host
=
redis
.
get_container_host_ip
()
port
=
redis
.
get_exposed_port
(
6379
)
# Test Redis
import
redis
r
=
redis
.
Redis
(
host
=
host
,
port
=
port
)
r
.
set
(
"test_key"
,
"Hello, Redis!"
)
value
=
r
.
get
(
"test_key"
)
print
(
"
\n
Redis test:"
)
print
(
f"Retrieved value:
{
value
.
decode
()
}
"
)
# Example 3: PostgreSQL container with environment variables
with
GenericContainer
(
"postgres:latest"
,
environment
=
{
"POSTGRES_USER"
:
"testuser"
,
"POSTGRES_PASSWORD"
:
"testpass"
,
"POSTGRES_DB"
:
"testdb"
},
)
as
postgres
:
# Get connection parameters
host
=
postgres
.
get_container_host_ip
()
port
=
postgres
.
get_exposed_port
(
5432
)
# Test PostgreSQL
import
psycopg2
conn
=
psycopg2
.
connect
(
host
=
host
,
port
=
port
,
user
=
"testuser"
,
password
=
"testpass"
,
database
=
"testdb"
)
cur
=
conn
.
cursor
()
cur
.
execute
(
"SELECT version();"
)
version
=
cur
.
fetchone
()
print
(
"
\n
PostgreSQL test:"
)
print
(
f"Version:
{
version
[
0
]
}
"
)
cur
.
close
()
conn
.
close
()
# Example 4: Custom container with volume mounting
with
GenericContainer
(
"python:3.9-slim"
,
volumes
=
{
"/tmp/test"
: {
"bind"
:
"/app"
,
"mode"
:
"rw"
}})
as
python
:
# Get container ID
container_id
=
python
.
get_container_id
()
print
(
f"
\n
Python container ID:
{
container_id
}
"
)
# Execute command in container
exit_code
,
output
=
python
.
exec_run
(
"python -c 'print(
\"
Hello from container!
\"
)'"
)
print
(
f"Command output:
{
output
.
decode
()
}
"
)
# Example 5: Container with health check
with
GenericContainer
(
"mongo:latest"
,
healthcheck
=
{
"test"
: [
"CMD"
,
"mongosh"
,
"--eval"
,
"db.adminCommand('ping')"
],
"interval"
:
1000000000
,
# 1 second
"timeout"
:
3000000000
,
# 3 seconds
"retries"
:
3
,
},
)
as
mongo
:
# Get connection parameters
host
=
mongo
.
get_container_host_ip
()
port
=
mongo
.
get_exposed_port
(
27017
)
# Test MongoDB
from
pymongo
import
MongoClient
client
=
MongoClient
(
f"mongodb://
{
host
}
:
{
port
}
"
)
db
=
client
.
test_db
collection
=
db
.
test_collection
collection
.
insert_one
({
"test"
:
"Hello, MongoDB!"
})
result
=
collection
.
find_one
()
print
(
"
\n
MongoDB test:"
)
print
(
f"Retrieved document:
{
result
}
"
)
# Example 6: Container with network
with
GenericContainer
(
"nginx:latest"
,
network
=
"test_network"
)
as
nginx_network
:
# Get network info
network_info
=
nginx_network
.
get_network_info
()
print
(
"
\n
Network test:"
)
print
(
f"Network name:
{
network_info
[
'Name'
]
}
"
)
print
(
f"Network ID:
{
network_info
[
'Id'
]
}
"
)
# Example 7: Container with resource limits
with
GenericContainer
(
"nginx:latest"
,
mem_limit
=
"512m"
,
cpu_period
=
100000
,
cpu_quota
=
50000
)
as
nginx_limits
:
# Get container stats
stats
=
nginx_limits
.
get_stats
()
print
(
"
\n
Resource limits test:"
)
print
(
f"Memory limit:
{
stats
[
'memory_stats'
][
'limit'
]
}
"
)
print
(
f"CPU usage:
{
stats
[
'cpu_stats'
][
'cpu_usage'
][
'total_usage'
]
}
"
)
# Example 8: Container with custom command
with
GenericContainer
(
"python:3.9-slim"
,
command
=
[
"python"
,
"-c"
,
"print('Custom command test')"
])
as
python_cmd
:
# Get logs
logs
=
python_cmd
.
get_logs
()
print
(
"
\n
Custom command test:"
)
print
(
f"Container logs:
{
logs
.
decode
()
}
"
)
if
__name__
==
"__main__"
:
basic_example
()
Back
|
FazBrowse Home
|
New Git URL