Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cb-util
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christoph Alt
cb-util
Commits
ff7ba139
Commit
ff7ba139
authored
1 year ago
by
Razvan Vass
Browse files
Options
Downloads
Patches
Plain Diff
Treat errors for load config from env and tests for it
parent
77e12dc5
No related branches found
No related tags found
1 merge request
!8
Treat errors for load config from env and tests for it
Pipeline
#64943
passed
1 year ago
Stage: test
Stage: deploy
Stage: trigger
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
dashboards/upload.py
+7
-0
7 additions, 0 deletions
dashboards/upload.py
tests/test_dashboard_upload.py
+22
-0
22 additions, 0 deletions
tests/test_dashboard_upload.py
with
29 additions
and
0 deletions
dashboards/upload.py
+
7
−
0
View file @
ff7ba139
...
@@ -58,6 +58,12 @@ def load_config_from_env(env_path: str = ".env") -> Tuple[str, str]:
...
@@ -58,6 +58,12 @@ def load_config_from_env(env_path: str = ".env") -> Tuple[str, str]:
dotenv
.
load_dotenv
(
env_path
)
dotenv
.
load_dotenv
(
env_path
)
grafana_api_key
=
os
.
getenv
(
"
GRAFANA_API_KEY
"
)
grafana_api_key
=
os
.
getenv
(
"
GRAFANA_API_KEY
"
)
grafana_server
=
os
.
getenv
(
"
GRAFANA_SERVER
"
)
grafana_server
=
os
.
getenv
(
"
GRAFANA_SERVER
"
)
if
not
grafana_api_key
:
raise
ValueError
(
"
GRAFANA_API_KEY is None or not defined in the .env file
"
)
if
not
grafana_server
:
raise
ValueError
(
"
GRAFANA_SERVER is None or not defined in the .env file
"
)
return
grafana_server
,
grafana_api_key
return
grafana_server
,
grafana_api_key
...
@@ -65,3 +71,4 @@ def upload_dashboard(dashboard: Dashboard, folder: int) -> None:
...
@@ -65,3 +71,4 @@ def upload_dashboard(dashboard: Dashboard, folder: int) -> None:
grafana_server
,
grafana_api_key
=
load_config_from_env
()
grafana_server
,
grafana_api_key
=
load_config_from_env
()
dashboard_json
=
get_dashboard_json
(
dashboard
,
overwrite
=
True
,
folder
=
folder
)
dashboard_json
=
get_dashboard_json
(
dashboard
,
overwrite
=
True
,
folder
=
folder
)
upload_to_grafana
(
dashboard_json
,
grafana_server
,
grafana_api_key
,
verify
=
False
)
upload_to_grafana
(
dashboard_json
,
grafana_server
,
grafana_api_key
,
verify
=
False
)
This diff is collapsed.
Click to expand it.
tests/test_dashboard_upload.py
0 → 100644
+
22
−
0
View file @
ff7ba139
# Test case using pytest
import
pytest
from
dashboards.upload
import
load_config_from_env
from
unittest.mock
import
patch
import
os
def
test_load_config_from_env
():
# Case 1: Test if function raises exception for missing GRAFANA_API_KEY
with
pytest
.
raises
(
ValueError
)
as
e
:
load_config_from_env
(
env_path
=
"
.env
"
)
assert
str
(
e
.
value
)
==
"
GRAFANA_API_KEY is None or not defined in the .env file
"
# Case 2: Test if function raises exception for missing GRAFANA_SERVER
with
patch
.
dict
(
os
.
environ
,
{
"
GRAFANA_API_KEY
"
:
"
api_key
"
}):
with
pytest
.
raises
(
ValueError
)
as
e
:
load_config_from_env
(
env_path
=
"
.env
"
)
assert
str
(
e
.
value
)
==
"
GRAFANA_SERVER is None or not defined in the .env file
"
# Case 3: Test if function returns expected values when both variables are defined
with
patch
.
dict
(
os
.
environ
,
{
"
GRAFANA_API_KEY
"
:
"
api_key
"
,
"
GRAFANA_SERVER
"
:
"
server_url
"
}):
result
=
load_config_from_env
(
env_path
=
"
.env
"
)
assert
result
==
(
"
server_url
"
,
"
api_key
"
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment