|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/GoCodeAlone/workflow/config" |
| 8 | +) |
| 9 | + |
| 10 | +// TestDevIntegration_GenerateDevCompose verifies that generateDevCompose |
| 11 | +// produces a valid docker-compose YAML from a fixture config. |
| 12 | +func TestDevIntegration_GenerateDevCompose(t *testing.T) { |
| 13 | + t.Run("single service with postgres", func(t *testing.T) { |
| 14 | + cfg := &config.WorkflowConfig{ |
| 15 | + Modules: []config.ModuleConfig{ |
| 16 | + {Name: "db", Type: "database.postgres"}, |
| 17 | + {Name: "server", Type: "http.server", Config: map[string]any{"port": 8080}}, |
| 18 | + }, |
| 19 | + } |
| 20 | + |
| 21 | + out, err := generateDevCompose(cfg) |
| 22 | + if err != nil { |
| 23 | + t.Fatalf("generateDevCompose returned error: %v", err) |
| 24 | + } |
| 25 | + |
| 26 | + // Must contain the postgres service. |
| 27 | + if !strings.Contains(out, "postgres:16") { |
| 28 | + t.Errorf("expected postgres:16 image in output, got:\n%s", out) |
| 29 | + } |
| 30 | + |
| 31 | + // Must contain the default app service. |
| 32 | + if !strings.Contains(out, "app:") || (!strings.Contains(out, "app:dev") && !strings.Contains(out, "build:")) { |
| 33 | + // either image: app:dev or build: context is acceptable |
| 34 | + if !strings.Contains(out, "app") { |
| 35 | + t.Errorf("expected app service in output, got:\n%s", out) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // Must contain volume for postgres persistence. |
| 40 | + if !strings.Contains(out, "pgdata") { |
| 41 | + t.Errorf("expected pgdata volume in output, got:\n%s", out) |
| 42 | + } |
| 43 | + |
| 44 | + // Must declare services: section. |
| 45 | + if !strings.Contains(out, "services:") { |
| 46 | + t.Errorf("expected 'services:' key in output, got:\n%s", out) |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("multi-service with redis and nats", func(t *testing.T) { |
| 51 | + cfg := &config.WorkflowConfig{ |
| 52 | + Modules: []config.ModuleConfig{ |
| 53 | + {Name: "cache", Type: "cache.redis"}, |
| 54 | + {Name: "mq", Type: "messaging.nats"}, |
| 55 | + }, |
| 56 | + Services: map[string]*config.ServiceConfig{ |
| 57 | + "api": { |
| 58 | + Binary: "./cmd/api", |
| 59 | + Expose: []config.ExposeConfig{{Port: 8080, Protocol: "http"}}, |
| 60 | + }, |
| 61 | + "worker": { |
| 62 | + Binary: "./cmd/worker", |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + out, err := generateDevCompose(cfg) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("generateDevCompose returned error: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + // Infrastructure images must be present. |
| 73 | + if !strings.Contains(out, "redis:7-alpine") { |
| 74 | + t.Errorf("expected redis:7-alpine in output, got:\n%s", out) |
| 75 | + } |
| 76 | + if !strings.Contains(out, "nats:latest") { |
| 77 | + t.Errorf("expected nats:latest in output, got:\n%s", out) |
| 78 | + } |
| 79 | + |
| 80 | + // Both services must appear. |
| 81 | + if !strings.Contains(out, "api:") { |
| 82 | + t.Errorf("expected 'api' service in output, got:\n%s", out) |
| 83 | + } |
| 84 | + if !strings.Contains(out, "worker:") { |
| 85 | + t.Errorf("expected 'worker' service in output, got:\n%s", out) |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("port mappings match config", func(t *testing.T) { |
| 90 | + cfg := &config.WorkflowConfig{ |
| 91 | + Modules: []config.ModuleConfig{ |
| 92 | + {Name: "db", Type: "database.postgres"}, |
| 93 | + }, |
| 94 | + Services: map[string]*config.ServiceConfig{ |
| 95 | + "api": { |
| 96 | + Expose: []config.ExposeConfig{ |
| 97 | + {Port: 9090, Protocol: "http"}, |
| 98 | + {Port: 9091, Protocol: "grpc"}, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + out, err := generateDevCompose(cfg) |
| 105 | + if err != nil { |
| 106 | + t.Fatalf("generateDevCompose returned error: %v", err) |
| 107 | + } |
| 108 | + |
| 109 | + // Both exposed ports must appear as port mappings. |
| 110 | + if !strings.Contains(out, "9090:9090") { |
| 111 | + t.Errorf("expected port mapping 9090:9090 in output, got:\n%s", out) |
| 112 | + } |
| 113 | + if !strings.Contains(out, "9091:9091") { |
| 114 | + t.Errorf("expected port mapping 9091:9091 in output, got:\n%s", out) |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + t.Run("empty config generates minimal app service", func(t *testing.T) { |
| 119 | + cfg := &config.WorkflowConfig{ |
| 120 | + Modules: []config.ModuleConfig{}, |
| 121 | + } |
| 122 | + |
| 123 | + out, err := generateDevCompose(cfg) |
| 124 | + if err != nil { |
| 125 | + t.Fatalf("generateDevCompose returned error: %v", err) |
| 126 | + } |
| 127 | + |
| 128 | + // At minimum there should be an 'app' service. |
| 129 | + if !strings.Contains(out, "app") { |
| 130 | + t.Errorf("expected 'app' service in minimal output, got:\n%s", out) |
| 131 | + } |
| 132 | + |
| 133 | + // There should be no postgres, redis, or nats since no modules declared. |
| 134 | + if strings.Contains(out, "postgres:") { |
| 135 | + t.Errorf("unexpected postgres image in minimal output, got:\n%s", out) |
| 136 | + } |
| 137 | + }) |
| 138 | + |
| 139 | + t.Run("http server port detected for app service", func(t *testing.T) { |
| 140 | + cfg := &config.WorkflowConfig{ |
| 141 | + Modules: []config.ModuleConfig{ |
| 142 | + {Name: "server", Type: "http.server", Config: map[string]any{"port": 3000}}, |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + out, err := generateDevCompose(cfg) |
| 147 | + if err != nil { |
| 148 | + t.Fatalf("generateDevCompose returned error: %v", err) |
| 149 | + } |
| 150 | + |
| 151 | + // Port 3000 should be mapped for the app service. |
| 152 | + if !strings.Contains(out, "3000:3000") { |
| 153 | + t.Errorf("expected port mapping 3000:3000 in output, got:\n%s", out) |
| 154 | + } |
| 155 | + }) |
| 156 | +} |
0 commit comments