{"id":34704093,"url":"https://github.com/nawafswe/mockchaos","last_synced_at":"2025-12-24T23:00:44.649Z","repository":{"id":327889599,"uuid":"1107924060","full_name":"NawafSwe/mockchaos","owner":"NawafSwe","description":"Mock HTTP/gRPC servers with chaos simulation. Configure latency, status codes, and failures via JSON for tests and Kubernetes deployments.","archived":false,"fork":false,"pushed_at":"2025-12-07T09:41:30.000Z","size":117,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-12-10T05:55:31.611Z","etag":null,"topics":["chaos-engineering","engineering-platforms","golang","grpc","http","mock-server"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NawafSwe.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-12-01T19:40:02.000Z","updated_at":"2025-12-09T07:17:44.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/NawafSwe/mockchaos","commit_stats":null,"previous_names":["nawafswe/mockchaos"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/NawafSwe/mockchaos","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NawafSwe%2Fmockchaos","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NawafSwe%2Fmockchaos/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NawafSwe%2Fmockchaos/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NawafSwe%2Fmockchaos/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NawafSwe","download_url":"https://codeload.github.com/NawafSwe/mockchaos/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NawafSwe%2Fmockchaos/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28012084,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-24T02:00:07.193Z","response_time":83,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chaos-engineering","engineering-platforms","golang","grpc","http","mock-server"],"created_at":"2025-12-24T23:00:37.185Z","updated_at":"2025-12-24T23:00:44.634Z","avatar_url":"https://github.com/NawafSwe.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mockchaos\n\n[![Build Status](https://github.com/nawafswe/mockchaos/actions/workflows/test.yaml/badge.svg)](https://github.com/nawafswe/mockchaos/actions)\n[![Coverage](https://codecov.io/gh/nawafswe/mockchaos/branch/main/graph/badge.svg)](https://codecov.io/gh/nawafswe/mockchaos)\n[![Go Report Card](https://goreportcard.com/badge/github.com/nawafswe/mockchaos)](https://goreportcard.com/report/github.com/nawafswe/mockchaos)\n[![Go Version](https://img.shields.io/badge/go-1.25.3-blue.svg)](https://golang.org)\n[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE)\n[![Release](https://img.shields.io/github/v/release/nawafswe/mockchaos)](https://github.com/nawafswe/mockchaos/releases)\n[![GitHub Stars](https://img.shields.io/github/stars/nawafswe/mockchaos?style=social)](https://github.com/nawafswe/mockchaos/stargazers)\n[![GitHub Forks](https://img.shields.io/github/forks/nawafswe/mockchaos?style=social)](https://github.com/nawafswe/mockchaos/network/members)\n\n**Mockchaos** is a Go library for creating mock HTTP and gRPC servers with configurable latencies, status codes, and responses. It's designed for chaos engineering, integration testing, and simulating real-world API behavior in development environments.\n\n## Features\n\n- 🚀 **HTTP \u0026 gRPC Mocking**: Create mock servers for both HTTP REST APIs and gRPC services\n- ⏱️ **Latency Simulation**: Configure random latencies to simulate network delays\n- 🎲 **Randomized Responses**: Randomly select from multiple status codes and latencies\n- 📝 **JSON Configuration**: Define mocks using simple JSON files\n- 🧪 **Test Utilities**: Built-in test helpers for integration testing\n- 🐳 **Standalone Servers**: Run as standalone servers for Kubernetes deployments\n- 🔄 **Dynamic gRPC**: Automatically register services from `.proto` files\n\n## Installation\n\n```bash\ngo get github.com/nawafswe/mockchaos\n```\n\n## When to Use Mockchaos\n\nMockchaos is ideal for:\n\n- **Integration Testing**: Test your services against mock dependencies with realistic latencies\n- **Chaos Engineering**: Simulate network delays, errors, and failures\n- **Local Development**: Replace external services with mocks during development\n- **CI/CD Pipelines**: Run integration tests without depending on external services\n- **Kubernetes Deployments**: Deploy mock servers as sidecars or standalone services\n\n## Quick Start\n\n### HTTP Mocking\n\n#### In Integration Tests\n\n```go\npackage mytest\n\nimport (\n    \"net/http\"\n    \"testing\"\n    \"time\"\n    \n    \"github.com/nawafswe/mockchaos/core/http\"\n    \"github.com/nawafswe/mockchaos/httptest\"\n)\n\nfunc TestMyService(t *testing.T) {\n    handlers := []http.Handler{\n        {\n            Path:      \"/api/users\",\n            Method:    http.MethodGet,\n            Response:  []byte(`{\"id\": 1, \"name\": \"Alice\"}`),\n            Statuses:  []int{200, 500},  // Randomly returns 200 or 500\n            Latencies: []time.Duration{100 * time.Millisecond, 200 * time.Millisecond},\n            Headers:   map[string]string{\"Content-Type\": \"application/json\"},\n        },\n    }\n    \n    svc := httptest.NewServer(t, handlers...)\n    defer svc.Close()\n    \n    // Use svc.URL() to make requests to the mock server\n    resp, err := http.Get(svc.URL() + \"/api/users\")\n    // ...\n}\n```\n\n#### From JSON Configuration\n\n```go\npackage main\n\nimport (\n    \"log\"\n    \"os\"\n    \"github.com/nawafswe/mockchaos/core/http\"\n)\n\nfunc main() {\n    content, err := os.ReadFile(\"mocks/handlers.json\")\n    if err != nil {\n        log.Fatal(err)\n    }\n    \n    handlers, err := http.ParseHandlers(content)\n    if err != nil {\n        log.Fatal(err)\n    }\n    \n    handler := http.NewHTTPHandler(handlers...)\n    // Use with your HTTP server\n}\n```\n\n#### JSON Format for HTTP\n\n```json\n[\n  {\n    \"path\": \"/api/v1/users\",\n    \"method\": \"GET\",\n    \"response\": {\n      \"id\": 1,\n      \"name\": \"Alice\"\n    },\n    \"statuses\": [200, 500],\n    \"latencies\": [\"100ms\", \"200ms\", \"500ms\"],\n    \"headers\": {\n      \"Content-Type\": \"application/json\"\n    }\n  }\n]\n```\n\n### gRPC Mocking\n\n#### In Integration Tests\n\nSee `examples/grpctest/grpc_test.go` for a complete example:\n\n```go\npackage grpctest\n\nimport (\n    \"context\"\n    \"testing\"\n    \"time\"\n    \n    \"github.com/nawafswe/mockchaos/grpctest\"\n    \"google.golang.org/grpc\"\n    \"your-service/proto\"\n)\n\nfunc TestMyGRPCService(t *testing.T) {\n    svc := grpctest.NewServer(t, func(server *grpc.Server) {\n        proto.RegisterMyServiceServer(server, \u0026myMockServer{})\n    })\n    defer svc.Close()\n    \n    client := proto.NewMyServiceClient(svc.Client)\n    resp, err := client.GetData(context.Background(), \u0026proto.GetDataRequest{})\n    // ...\n}\n```\n\n#### From JSON Configuration\n\n```json\n[\n  {\n    \"service\": \"orders.OrderService\",\n    \"method\": \"GetOrder\",\n    \"response_proto_type\": \"orders.GetOrderResponse\",\n    \"response\": {\n      \"order\": {\n        \"orderId\": \"123\",\n        \"status\": \"PENDING\",\n        \"grandTotal\": 29.99\n      }\n    },\n    \"status_codes\": [0, 13],\n    \"latencies\": [\"100ms\", \"300ms\"],\n    \"error_message\": \"mock internal error\"\n  }\n]\n```\n\n**gRPC Status Codes**: Use numeric codes (0=OK, 13=INTERNAL, 14=UNAVAILABLE, etc.)\n\n## Latency Simulation\n\nMockchaos supports random latency simulation to mimic real-world network conditions:\n\n### How It Works\n\n1. **Multiple Latencies**: Provide an array of durations\n2. **Random Selection**: Each request randomly selects one latency from the array\n3. **Applied Before Response**: The latency is applied before sending the response\n\n### Examples\n\n```go\n// Single latency (always the same)\nLatencies: []time.Duration{100 * time.Millisecond}\n\n// Multiple latencies (randomly selected)\nLatencies: []time.Duration{\n    50 * time.Millisecond,\n    100 * time.Millisecond,\n    200 * time.Millisecond,\n    500 * time.Millisecond,\n}\n\n// In JSON format\n\"latencies\": [\"50ms\", \"100ms\", \"200ms\", \"500ms\"]\n```\n\n### Use Cases\n\n- **Chaos Engineering**: Test how your service handles variable latencies\n- **Timeout Testing**: Verify timeout handling with high latencies\n- **Performance Testing**: Simulate slow dependencies\n- **Realistic Testing**: Mimic real-world network conditions\n\n## Standalone Server\n\nMockchaos includes a CLI tool for running standalone mock servers:\n\n### Installation\n\n```bash\ngo install github.com/nawafswe/mockchaos/cmd/mockchaos@latest\n```\n\n### HTTP Server\n\n```bash\nmockchaos \\\n  -mock_server=http-svc \\\n  -http_port=8080 \\\n  -mocks_path=./http-mocks\n```\n\n### gRPC Server\n\n```bash\nmockchaos \\\n  -mock_server=grpc-svc \\\n  -grpc_port=50051 \\\n  -grpc_proto_dir=./protos \\\n  -mocks_path=./grpc-mocks/orders\n```\n\n### Flags\n\n- `-mock_server`: Service type (`http-svc` or `grpc-svc`)\n- `-http_port`: HTTP server port (default: 8080)\n- `-grpc_port`: gRPC server port (default: 50051)\n- `-grpc_proto_dir`: Directory containing `.proto` files (required for gRPC)\n- `-mocks_path`: Directory containing JSON mock files (required)\n\nThe `mocks_path` directory is recursively scanned for all `.json` files, which are automatically loaded and combined.\n\n## Kubernetes Deployment\n\nSee `examples/k8s/app.yaml` for a complete Kubernetes deployment example.\n\n- **Multiple Services**: Host multiple gRPC or HTTP services with different mock paths to isolate services and resources (e.g., deploy separate gRPC services for restaurants and orders)\n\n### Example Deployment\n\n```yaml\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n  name: http-mockserver\nspec:\n  template:\n    spec:\n      containers:\n        - name: http\n          image: your-image\n          command: [\"/bin/sh\", \"-c\"]\n          args:\n            - |\n              ./bin/mockchaos \\\n                -mock_server=http-svc \\\n                -http_port=8080 \\\n                -mocks_path=./http-mocks\n```\n\n## Package Structure\n\n- **`core/http`**: Core HTTP mocking functionality\n- **`core/grpc`**: Core gRPC mocking functionality\n- **`httptest`**: Test utilities for HTTP mocking\n- **`grpctest`**: Test utilities for gRPC mocking\n- **`http`**: Standalone HTTP server implementation\n- **`grpc`**: Standalone gRPC server implementation\n- **`cmd/mockchaos`**: CLI tool for running standalone servers\n\n## Examples\n\nSee the `examples/` directory for complete working examples:\n\n- **`examples/httptest/`**: HTTP integration test example with latency simulation and JSON configuration\n- **`examples/grpctest/`**: gRPC integration test example with latency simulation\n- **`examples/k8s/`**: Kubernetes deployment configuration\n\n### Running the Examples\n\n**HTTP Example:**\n```bash\ncd examples/httptest\ngo test -v\n```\n\n**gRPC Example:**\n```bash\ncd examples/grpctest\ngo generate  # Generate protobuf files\ngo test -v\n```\n\n## Behavior\n\n### HTTP Mocking\n\n- **Matching**: Exact match on HTTP method and path\n- **Status Codes**: Randomly selected from the `statuses` array\n- **Latencies**: Randomly selected from the `latencies` array (if provided)\n- **No Match**: Returns `404 Not Found` with `{\"error\": \"Not found\"}`\n\n### gRPC Mocking\n\n- **Service Registration**: Automatically registers services from `.proto` files\n- **Status Codes**: Randomly selected from the `status_codes` array\n- **Latencies**: Randomly selected from the `latencies` array (if provided)\n- **No Match**: Returns `Unimplemented` status error\n\n## Contributing\nContributions are welcome! Please see our [Contributing Guide](CONTRIBUTING.md) for details on how to contribute.\n\n## License\n\nSee [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnawafswe%2Fmockchaos","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnawafswe%2Fmockchaos","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnawafswe%2Fmockchaos/lists"}