Task 6 Integration Tests For CLI Config With IP Addresses

by ADMIN 58 views

Hey guys! Today, we're diving into Task 6, which is all about adding integration tests for the CLI config command, specifically focusing on how it handles IP addresses. This is super important because we need to make sure our command-line interface (CLI) can correctly manage different types of values, not just strings. Let's break it down!

Description

The main goal here is to create integration tests that ensure our CLI config command correctly handles IP addresses and other value types. Think of it as making sure our tool can speak the language of the internet and other data formats without getting confused. It’s like teaching it to not only understand words but also addresses and numbers, making it a well-rounded communicator.

Why Integration Tests?

Integration tests are crucial because they verify that different parts of our system work together as expected. In this case, we want to ensure that the CLI can interact with the configuration files, set values, and retrieve them correctly. It's like checking if all the players in a team can pass the ball to each other effectively.

Step-by-Step Instructions

Okay, let’s get into the nitty-gritty. Here’s how we’re going to tackle this task, step by step:

  1. Create a new test file: We’ll start by creating a new file named tests/integration/test_cli_config.py. This is where all our test code will live. Think of it as setting up our testing playground.
  2. Copy the complete test code: Next, we'll copy the complete test code provided below into the file. This is the blueprint for our tests, outlining what we want to check and how to do it. It's like getting the instructions for a new game.
  3. Save the file: Once the code is in place, we’ll save the file. This is like saving our progress in a game, so we don't have to start from scratch if something goes wrong.
  4. Run the tests: Finally, we'll run the tests to verify they pass. This is the moment of truth, where we see if our code works as expected. It's like pressing the play button and watching our creation come to life.

Complete Test File Content

Here’s the complete test file content that we’ll be using. This code includes several test cases to cover different scenarios, such as setting IP addresses, handling multiple values, and validating input.

"""Integration tests for CLI config command."""

import subprocess
import tempfile
from pathlib import Path

import pytest
import yaml


class TestCLIConfigIntegration:
    """Test CLI config command with real values."""

    @pytest.fixture
    def temp_config_dir(self):
        """Create a temporary config directory."""
        with tempfile.TemporaryDirectory() as tmpdir:
            config_dir = Path(tmpdir) / ".shard-md"
            config_dir.mkdir()
            yield config_dir

    def test_cli_set_ip_address(self, temp_config_dir):
        """Test setting IP address via CLI."""
        config_file = temp_config_dir / "config.yaml"

        # Create initial config
        initial_config = {
            "chromadb": {"host": "localhost", "port": 8000}
        }
        with open(config_file, "w") as f:
            yaml.dump(initial_config, f)

        # Set IP address via CLI
        result = subprocess.run(
            ["shard-md", "config", "set", "chromadb.host", "192.168.1.100"],
            cwd=temp_config_dir.parent,
            capture_output=True,
            text=True
        )

        assert result.returncode == 0

        # Read config and verify
        with open(config_file) as f:
            config = yaml.safe_load(f)

        assert config["chromadb"]["host"] == "192.168.1.100"

    def test_cli_set_multiple_values(self, temp_config_dir):
        """Test setting multiple configuration values."""
        config_file = temp_config_dir / "config.yaml"

        # Create initial config
        with open(config_file, "w") as f:
            yaml.dump({"chromadb": {}}, f)

        # Set multiple values
        commands = [
            ["shard-md", "config", "set", "chromadb.host", "10.0.0.1"],
            ["shard-md", "config", "set", "chromadb.port", "9000"],
            ["shard-md", "config", "set", "chromadb.ssl", "true"],
        ]

        for cmd in commands:
            result = subprocess.run(
                cmd,
                cwd=temp_config_dir.parent,
                capture_output=True,
                text=True
            )
            assert result.returncode == 0

        # Verify all values
        with open(config_file) as f:
            config = yaml.safe_load(f)

        assert config["chromadb"]["host"] == "10.0.0.1"
        assert config["chromadb"]["port"] == 9000  # Should be int in YAML
        assert config["chromadb"]["ssl"] is True  # Should be bool in YAML

    def test_cli_show_with_ip_address(self, temp_config_dir):
        """Test showing config with IP addresses."""
        config_file = temp_config_dir / "config.yaml"

        # Create config with IP
        config_data = {
            "chromadb": {
                "host": "172.16.0.1",
                "port": 8080
            }
        }
        with open(config_file, "w") as f:
            yaml.dump(config_data, f)

        # Show config
        result = subprocess.run(
            ["shard-md", "config", "show", "--section", "chromadb"],
            cwd=temp_config_dir.parent,
            capture_output=True,
            text=True
        )

        assert result.returncode == 0
        assert "172.16.0.1" in result.stdout
        assert "8080" in result.stdout

    def test_cli_validation_errors(self, temp_config_dir):
        """Test that invalid values are rejected with helpful errors."""
        config_file = temp_config_dir / "config.yaml"

        # Create initial config
        with open(config_file, "w") as f:
            yaml.dump({"chromadb": {}}, f)

        # Try to set invalid port
        result = subprocess.run(
            ["shard-md", "config", "set", "chromadb.port", "70000"],
            cwd=temp_config_dir.parent,
            capture_output=True,
            text=True
        )

        # Should fail with validation error
        assert result.returncode != 0
        assert "Invalid" in result.stderr or "65535" in result.stderr

    @pytest.mark.parametrize("ip_address", [
        "192.168.1.1",
        "10.0.0.1",
        "172.31.255.255",
        "8.8.8.8",
        "1.1.1.1",
    ])
    def test_cli_various_ip_formats(self, temp_config_dir, ip_address):
        """Test various IP address formats via CLI."""
        config_file = temp_config_dir / "config.yaml"

        # Create initial config
        with open(config_file, "w") as f:
            yaml.dump({"chromadb": {}}, f)

        # Set IP address
        result = subprocess.run(
            ["shard-md", "config", "set", "chromadb.host", ip_address],
            cwd=temp_config_dir.parent,
            capture_output=True,
            text=True
        )

        assert result.returncode == 0

        # Verify
        with open(config_file) as f:
            config = yaml.safe_load(f)

        assert config["chromadb"]["host"] == ip_address

This test suite includes several key tests:

  • test_cli_set_ip_address: This test specifically checks if the CLI can correctly set an IP address in the configuration.
  • test_cli_set_multiple_values: Here, we ensure that the CLI can handle multiple configuration values simultaneously, including different data types like strings, integers, and booleans. It’s like checking if our tool can juggle multiple balls without dropping any.
  • test_cli_show_with_ip_address: This test verifies that the CLI can display the configuration correctly when it includes IP addresses. It’s like making sure our tool can show its work clearly.
  • test_cli_validation_errors: This one is crucial for error handling. It checks if the CLI can reject invalid values and provide helpful error messages. It’s like teaching our tool to say “Oops, that’s not right!” when needed.
  • test_cli_various_ip_formats: This test uses a parameterized approach to check if the CLI can handle various IP address formats. It's like giving our tool a variety of addresses to make sure it understands them all.

Files to Create

  • tests/integration/test_cli_config.py

This is the only file we need to create. It’s our testing hub, where all the action happens.

Acceptance Criteria

To make sure we’ve nailed this task, we have a few acceptance criteria to meet:

  • [x] Integration tests pass: All our tests should run successfully, indicating that our CLI config command is working as expected. It’s like getting a perfect score on a test.
  • [x] CLI correctly sets IP addresses: The CLI should be able to set IP addresses in the configuration without any issues. This is the core functionality we’re testing.
  • [x] CLI correctly handles various data types: Our CLI should be able to handle different data types, such as strings, integers, and booleans. It’s like making sure our tool is multilingual.
  • [x] Validation errors are properly reported: If a user tries to set an invalid value, the CLI should provide a clear and helpful error message. This ensures a good user experience.

Testing Instructions

To run the tests, we’ll use the following command:

# Run integration tests
pytest tests/integration/test_cli_config.py -v

This command tells pytest to run the tests in our test_cli_config.py file. The -v flag stands for “verbose,” which means we’ll get detailed output about each test.

Expected Output:

We should see all tests passing. This is the green light we’re looking for, indicating that our CLI config command is functioning correctly.

Related To

  • Parent issue: #94
  • Depends on: Tasks 1-3 (#95, #96, #97)
  • Priority: LOW

This task is part of a larger effort to improve our CLI configuration handling. It depends on the completion of Tasks 1-3, which likely involve setting up the basic configuration infrastructure. The priority is marked as LOW, meaning it’s important but not urgent. We can tackle this after handling more pressing issues.

Conclusion

So there you have it, guys! Adding integration tests for the CLI config command with IP addresses is a crucial step in ensuring the reliability and robustness of our tool. By following these steps and meeting the acceptance criteria, we can be confident that our CLI can handle IP addresses and other data types correctly. Let's get testing!