Skip to content

Commit db4fced

Browse files
committed
fix: improve logger debug output handling
- Update HelmLogger to properly handle HELM_DEBUG=0 and HELM_DEBUG=false - Add pytest-style unit tests for HELM_DEBUG=0 and HELM_DEBUG=false cases - Add test-plugin.sh script with --debug flag for testing
1 parent 95acb29 commit db4fced

File tree

3 files changed

+126
-3
lines changed

3 files changed

+126
-3
lines changed

helm_values_manager/utils/logger.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class HelmLogger:
1515
Logger class that follows Helm plugin conventions.
1616
1717
This logger:
18-
1. Writes debug messages only when HELM_DEBUG is set
18+
1. Writes debug messages only when HELM_DEBUG is set and not "0" or "false"
1919
2. Writes all messages to stderr (Helm convention)
2020
3. Uses string formatting for better performance
2121
4. Provides consistent error and debug message formatting
@@ -24,13 +24,14 @@ class HelmLogger:
2424
@staticmethod
2525
def debug(msg: str, *args: Any) -> None:
2626
"""
27-
Print debug message if HELM_DEBUG is set.
27+
Print debug message if HELM_DEBUG is set and not "0" or "false".
2828
2929
Args:
3030
msg: Message with optional string format placeholders
3131
args: Values to substitute in the message
3232
"""
33-
if os.environ.get("HELM_DEBUG"):
33+
debug_val = os.environ.get("HELM_DEBUG", "").lower()
34+
if debug_val and debug_val not in ("0", "false"):
3435
if args:
3536
msg = msg % args
3637
print("[debug] %s" % msg, file=sys.stderr)

tests/unit/utils/test_logger.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ def test_debug_with_helm_debug_disabled(logger):
3434
assert stderr.getvalue() == ""
3535

3636

37+
def test_debug_with_helm_debug_zero(logger):
38+
"""Test debug output when HELM_DEBUG=0."""
39+
stderr = StringIO()
40+
with (
41+
mock.patch.dict(os.environ, {"HELM_DEBUG": "0"}),
42+
mock.patch("helm_values_manager.utils.logger.sys.stderr", stderr),
43+
):
44+
logger.debug("Test message")
45+
assert stderr.getvalue() == ""
46+
47+
48+
def test_debug_with_helm_debug_false(logger):
49+
"""Test debug output when HELM_DEBUG=false."""
50+
stderr = StringIO()
51+
with (
52+
mock.patch.dict(os.environ, {"HELM_DEBUG": "false"}),
53+
mock.patch("helm_values_manager.utils.logger.sys.stderr", stderr),
54+
):
55+
logger.debug("Test message")
56+
assert stderr.getvalue() == ""
57+
58+
3759
def test_debug_with_formatting(logger):
3860
"""Test debug output with string formatting."""
3961
stderr = StringIO()

tools/test-plugin.sh

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/bin/bash
2+
set -e # Exit on any error
3+
4+
# Colors for output
5+
GREEN='\033[0;32m'
6+
RED='\033[0;31m'
7+
NC='\033[0m' # No Color
8+
9+
# Default values
10+
INSTALL_SOURCE="local"
11+
GITHUB_URL="https://github.com/Zipstack/helm-values-manager" # Correct capitalization
12+
DEBUG_FLAG=""
13+
14+
# Parse command line arguments
15+
while [[ $# -gt 0 ]]; do
16+
case $1 in
17+
--source)
18+
if [[ "$2" != "local" && "$2" != "github" ]]; then
19+
echo "Error: --source must be either 'local' or 'github'"
20+
exit 1
21+
fi
22+
INSTALL_SOURCE="$2"
23+
shift 2
24+
;;
25+
--github-url)
26+
# Remove .git suffix if present
27+
GITHUB_URL="${2%.git}"
28+
shift 2
29+
;;
30+
--debug)
31+
DEBUG_FLAG="--debug"
32+
shift
33+
;;
34+
*)
35+
echo "Unknown option: $1"
36+
echo "Usage: $0 [--source local|github] [--github-url URL] [--debug]"
37+
echo "Example:"
38+
echo " $0 # Install from local directory"
39+
echo " $0 --source github # Install from default GitHub repo"
40+
echo " $0 --source github --github-url URL # Install from custom GitHub repo"
41+
echo " $0 --debug # Run with debug output"
42+
exit 1
43+
;;
44+
esac
45+
done
46+
47+
# Get the absolute path to the plugin directory if installing locally
48+
if [ "$INSTALL_SOURCE" = "local" ]; then
49+
PLUGIN_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
50+
fi
51+
52+
echo "Installing helm-values-manager plugin..."
53+
# Remove existing plugin if it exists
54+
helm plugin remove values-manager 2>/dev/null || true
55+
56+
# Install plugin based on source
57+
if [ "$INSTALL_SOURCE" = "local" ]; then
58+
echo "Installing from local directory: $PLUGIN_DIR"
59+
helm plugin install "$PLUGIN_DIR"
60+
else
61+
echo "Installing from GitHub: $GITHUB_URL"
62+
helm plugin install "$GITHUB_URL"
63+
fi
64+
65+
echo -e "\n${GREEN}Running test sequence...${NC}"
66+
67+
# Clean up any existing test files
68+
rm -f helm-values.json .helm-values.lock
69+
70+
# Initialize with a valid release name
71+
echo -e "\n${GREEN}1. Initializing helm values configuration...${NC}"
72+
helm values-manager init -r "test-release" $DEBUG_FLAG
73+
74+
# Add deployments
75+
echo -e "\n${GREEN}2. Adding deployments...${NC}"
76+
helm values-manager add-deployment test-deployment $DEBUG_FLAG
77+
helm values-manager add-deployment dev $DEBUG_FLAG
78+
helm values-manager add-deployment prod $DEBUG_FLAG
79+
80+
# Add value configurations
81+
echo -e "\n${GREEN}3. Adding value configurations...${NC}"
82+
helm values-manager add-value-config -p "app.config.name" -d "Application name" -r $DEBUG_FLAG
83+
helm values-manager add-value-config -p "app.config.replicas" -d "Number of replicas" $DEBUG_FLAG
84+
85+
# Set values for different environments
86+
echo -e "\n${GREEN}4. Setting values...${NC}"
87+
helm values-manager set-value -p "app.config.name" -v "my-test-app" -d dev $DEBUG_FLAG
88+
helm values-manager set-value -p "app.config.replicas" -v "1" -d dev $DEBUG_FLAG
89+
helm values-manager set-value -p "app.config.name" -v "my-prod-app" -d prod $DEBUG_FLAG
90+
helm values-manager set-value -p "app.config.replicas" -v "3" -d prod $DEBUG_FLAG
91+
92+
# Verify configurations
93+
echo -e "\n${GREEN}5. Verifying configurations...${NC}"
94+
echo -e "\nhelm-values.json contents:"
95+
cat helm-values.json
96+
97+
echo -e "\n.helm-values.lock contents:"
98+
cat .helm-values.lock
99+
100+
echo -e "\n${GREEN}Test sequence completed successfully!${NC}"

0 commit comments

Comments
 (0)