Skip to content

Commit

Permalink
Enhance ibm_db driver to read Db2 credential from Env var for testing (
Browse files Browse the repository at this point in the history
…ibmdb#894)

Signed-off-by: bchoudhary <[email protected]>
  • Loading branch information
bchoudhary6415 authored Oct 26, 2023
1 parent 5b48b75 commit 516f4a7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 12 deletions.
50 changes: 43 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,16 +436,52 @@ directory. A valid config.py will need to be created to configure your Db2
settings. A config.py.sample exists that can be copied and modified for your
environment.

The config.py should look like this:
* Set Environment Variables DB2_USER, DB2_PASSWD accordingly.
```
For example, by sourcing the following ENV variables:
For Linux
export DB2_USER=<Username>
export DB2_PASSWD=<Password>

For windows
set DB2_USER=<Username>
set DB2_PASSWD=<Password>

```
* OR
```
If not using environment variables, update user and password information in
config.json file.

```
The config.py should look like this:
```python
test_dir = 'ibm_db_tests' # Location of testsuite file (relative to current directory)

database = 'test' # Database to connect to
user = 'db2inst1' # User ID to connect with
password = 'password' # Password for given User ID
hostname = 'localhost' # Hostname
port = 50000 # Port Number
file_path = 'config.json'

with open(file_path, 'r') as file:
data = json.load(file)

database = data['database'] # Database to connect to
hostname = data['hostname'] # Hostname
port = data['port'] # Port Number

env_not_set = False
if 'DB2_USER' in os.environ:
user = os.getenv('DB2_USER') # User ID to connect with
else:
user = data['user']
env_not_set = True
if 'DB2_PASSWD' in os.environ:
password = os.getenv('DB2_PASSWD') # Password for given User ID
else:
password = data['password']
env_not_set = True

if env_not_set == True:
warnings.warn("Warning: Environment variable DB2_USER or DB2_PASSWD is not set.")
print("Please set it before running test file and avoid")
print("hardcoded password in config.json file." )
```

Point the database to mydatabase as created by the following command.
Expand Down
7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"database" : "sample",
"user" : "db2inst1",
"password" : "password",
"hostname" : "localhost",
"port" : 50000
}
34 changes: 29 additions & 5 deletions config.py.sample
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
test_dir = 'ibm_db_tests' # Location of testsuite file (relative to current directory) (Don't change this.)

database = 'sample' # Database to connect to. Please use an empty database for best results.
# database = 'sample' # Database to connect to. Please use an empty database for best results.

import sys, os, platform
import warnings
import json
if sys.platform != 'zos':
user = 'db2inst1' # User ID to connect with
password = 'password' # Password for given User ID
hostname = 'localhost' # Hostname
port = 50000 # Port Number
file_path = 'config.json'

with open(file_path, 'r') as file:
data = json.load(file)

database = data['database'] # Database to connect to
hostname = data['hostname'] # Hostname
port = data['port'] # Port Number

env_not_set = False
if 'DB2_USER' in os.environ:
user = os.getenv('DB2_USER') # User ID to connect with
else:
user = data['user']
env_not_set = True
if 'DB2_PASSWD' in os.environ:
password = os.getenv('DB2_PASSWD') # Password for given User ID
else:
password = data['password']
env_not_set = True

if env_not_set == True:
warnings.warn("Warning: Environment variable DB2_USER or DB2_PASSWD is not set.")
print("Please set it before running test file and avoid")
print("hardcoded password in config.json file." )
exit
else:
database = "DSN=%s%s" % (platform.node(), os.getenv("SUBSYSTEM"))
user = os.getenv('USER') # User ID to connect with
Expand Down

0 comments on commit 516f4a7

Please sign in to comment.