|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "testing" |
| 7 | + |
| 8 | + basev0 "github.com/codefly-dev/core/generated/go/codefly/base/v0" |
| 9 | + runtimev0 "github.com/codefly-dev/core/generated/go/codefly/services/runtime/v0" |
| 10 | + "github.com/codefly-dev/core/resources" |
| 11 | + runners "github.com/codefly-dev/core/runners/base" |
| 12 | + "github.com/codefly-dev/core/standards" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +// fakeProc records what Start hands to the service process, standing in for |
| 17 | +// the uvicorn process so these tests need no python, docker or network. |
| 18 | +type fakeProc struct { |
| 19 | + envs []*resources.EnvironmentVariable |
| 20 | + started bool |
| 21 | + stopped bool |
| 22 | +} |
| 23 | + |
| 24 | +func (p *fakeProc) Start(context.Context) error { p.started = true; return nil } |
| 25 | +func (p *fakeProc) Run(context.Context) error { return nil } |
| 26 | +func (p *fakeProc) Stop(context.Context) error { p.stopped = true; return nil } |
| 27 | +func (p *fakeProc) Wait(context.Context) error { return nil } |
| 28 | +func (p *fakeProc) IsRunning(context.Context) (bool, error) { return p.started && !p.stopped, nil } |
| 29 | +func (p *fakeProc) WaitOn(string) {} |
| 30 | +func (p *fakeProc) WithDir(string) {} |
| 31 | +func (p *fakeProc) WithOutput(io.Writer) {} |
| 32 | +func (p *fakeProc) StdinPipe() (io.WriteCloser, error) { return nil, nil } |
| 33 | +func (p *fakeProc) StdoutPipe() (io.ReadCloser, error) { return nil, nil } |
| 34 | + |
| 35 | +func (p *fakeProc) WithEnvironmentVariables(_ context.Context, envs ...*resources.EnvironmentVariable) { |
| 36 | + p.envs = append(p.envs, envs...) |
| 37 | +} |
| 38 | + |
| 39 | +func (p *fakeProc) WithEnvironmentVariablesAppend(context.Context, *resources.EnvironmentVariable, string) { |
| 40 | +} |
| 41 | + |
| 42 | +type fakeRunnerEnvironment struct { |
| 43 | + procs []*fakeProc |
| 44 | +} |
| 45 | + |
| 46 | +func (e *fakeRunnerEnvironment) Init(context.Context) error { return nil } |
| 47 | +func (e *fakeRunnerEnvironment) Stop(context.Context) error { return nil } |
| 48 | +func (e *fakeRunnerEnvironment) Shutdown(context.Context) error { return nil } |
| 49 | +func (e *fakeRunnerEnvironment) WithBinary(string) error { return nil } |
| 50 | + |
| 51 | +func (e *fakeRunnerEnvironment) WithEnvironmentVariables(context.Context, ...*resources.EnvironmentVariable) { |
| 52 | +} |
| 53 | + |
| 54 | +func (e *fakeRunnerEnvironment) NewProcess(string, ...string) (runners.Proc, error) { |
| 55 | + proc := &fakeProc{} |
| 56 | + e.procs = append(e.procs, proc) |
| 57 | + return proc, nil |
| 58 | +} |
| 59 | + |
| 60 | +// startTestRuntime builds a Runtime already past Init: a runner environment is |
| 61 | +// in place, so Start goes straight to assembling the process. |
| 62 | +func startTestRuntime(t *testing.T) (*Runtime, *fakeRunnerEnvironment) { |
| 63 | + t.Helper() |
| 64 | + runtime := NewRuntime(NewService()) |
| 65 | + runtime.Location = t.TempDir() |
| 66 | + env := &fakeRunnerEnvironment{} |
| 67 | + runtime.runnerEnvironment = env |
| 68 | + t.Cleanup(runtime.Base.StopWatcher) |
| 69 | + return runtime, env |
| 70 | +} |
| 71 | + |
| 72 | +// processEnv returns every value the process received for key, in the order |
| 73 | +// they were applied. |
| 74 | +func processEnv(proc *fakeProc, key string) []string { |
| 75 | + var values []string |
| 76 | + for _, env := range proc.envs { |
| 77 | + if env.Key == key { |
| 78 | + values = append(values, env.ValueAsString()) |
| 79 | + } |
| 80 | + } |
| 81 | + return values |
| 82 | +} |
| 83 | + |
| 84 | +func dependencyMapping() *basev0.NetworkMapping { |
| 85 | + return &basev0.NetworkMapping{ |
| 86 | + Endpoint: &basev0.Endpoint{Module: "mod", Service: "store", Name: "rest", Api: standards.REST}, |
| 87 | + Instances: []*basev0.NetworkInstance{{ |
| 88 | + Access: resources.NewNativeNetworkAccess(), |
| 89 | + Hostname: "localhost", |
| 90 | + Port: 9876, |
| 91 | + Address: "http://localhost:9876", |
| 92 | + }}, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// TestStartHonorsStartRequest pins the three StartRequest inputs that shape the |
| 97 | +// process environment: `codefly run --set` overrides, the fixture selector, and |
| 98 | +// the resolved addresses of the endpoints this service depends on. |
| 99 | +func TestStartHonorsStartRequest(t *testing.T) { |
| 100 | + ctx := context.Background() |
| 101 | + runtime, env := startTestRuntime(t) |
| 102 | + |
| 103 | + dependency := dependencyMapping() |
| 104 | + resp, err := runtime.Start(ctx, &runtimev0.StartRequest{ |
| 105 | + Overrides: map[string]string{"LOG_LEVEL": "debug"}, |
| 106 | + Fixture: "seed", |
| 107 | + DependenciesNetworkMappings: []*basev0.NetworkMapping{dependency}, |
| 108 | + }) |
| 109 | + require.NoError(t, err) |
| 110 | + require.Equal(t, runtimev0.StartStatus_STARTED, resp.GetStatus().GetState(), resp.GetStatus().GetMessage()) |
| 111 | + require.Len(t, env.procs, 1) |
| 112 | + proc := env.procs[0] |
| 113 | + require.True(t, proc.started) |
| 114 | + |
| 115 | + require.Equal(t, []string{"debug"}, processEnv(proc, "LOG_LEVEL")) |
| 116 | + require.Equal(t, []string{"seed"}, processEnv(proc, resources.FixturePrefix)) |
| 117 | + |
| 118 | + dependencyKey := resources.EndpointAsEnvironmentVariableKey(resources.EndpointInformationFromProto(dependency.Endpoint)) |
| 119 | + require.Equal(t, []string{"http://localhost:9876"}, processEnv(proc, dependencyKey)) |
| 120 | +} |
| 121 | + |
| 122 | +// TestStartOverrideBeatsConfiguration pins the precedence between the two |
| 123 | +// sources that can name the same variable. The environment manager emits |
| 124 | +// overrides ahead of configuration-derived values, so a `--set` aimed at a |
| 125 | +// configuration key only takes effect because Start reorders them. |
| 126 | +func TestStartOverrideBeatsConfiguration(t *testing.T) { |
| 127 | + ctx := context.Background() |
| 128 | + runtime, env := startTestRuntime(t) |
| 129 | + |
| 130 | + require.NoError(t, runtime.EnvironmentVariables.AddConfigurations(ctx, &basev0.Configuration{ |
| 131 | + Origin: "mod/store", |
| 132 | + Infos: []*basev0.ConfigurationInformation{{ |
| 133 | + Name: "postgres", |
| 134 | + ConfigurationValues: []*basev0.ConfigurationValue{{Key: "url", Value: "from-configuration"}}, |
| 135 | + }}, |
| 136 | + })) |
| 137 | + |
| 138 | + key := resources.ServiceConfigurationKeyFromUnique("mod/store", "postgres", "url") |
| 139 | + resp, err := runtime.Start(ctx, &runtimev0.StartRequest{Overrides: map[string]string{key: "from-override"}}) |
| 140 | + require.NoError(t, err) |
| 141 | + require.Equal(t, runtimev0.StartStatus_STARTED, resp.GetStatus().GetState(), resp.GetStatus().GetMessage()) |
| 142 | + require.Len(t, env.procs, 1) |
| 143 | + |
| 144 | + require.Equal(t, []string{"from-override"}, processEnv(env.procs[0], key)) |
| 145 | +} |
| 146 | + |
| 147 | +// TestStartHotReloadKeepsProcessOnIdenticalRequest proves the hot-reload |
| 148 | +// fast path still avoids pointless restarts when nothing about the request |
| 149 | +// changed — uvicorn --reload already covers source edits. |
| 150 | +func TestStartHotReloadKeepsProcessOnIdenticalRequest(t *testing.T) { |
| 151 | + ctx := context.Background() |
| 152 | + runtime, env := startTestRuntime(t) |
| 153 | + runtime.FastAPI.Settings.HotReload = true |
| 154 | + |
| 155 | + req := &runtimev0.StartRequest{Overrides: map[string]string{"LOG_LEVEL": "debug"}} |
| 156 | + _, err := runtime.Start(ctx, req) |
| 157 | + require.NoError(t, err) |
| 158 | + require.Len(t, env.procs, 1) |
| 159 | + |
| 160 | + _, err = runtime.Start(ctx, &runtimev0.StartRequest{Overrides: map[string]string{"LOG_LEVEL": "debug"}}) |
| 161 | + require.NoError(t, err) |
| 162 | + require.Len(t, env.procs, 1, "an unchanged request must reuse the running process") |
| 163 | + require.False(t, env.procs[0].stopped) |
| 164 | +} |
| 165 | + |
| 166 | +// TestStartHotReloadReplacesProcessOnChangedRequest is the counterpart: a |
| 167 | +// running process keeps the environment it was launched with, so new inputs |
| 168 | +// only reach the service through a fresh process. |
| 169 | +func TestStartHotReloadReplacesProcessOnChangedRequest(t *testing.T) { |
| 170 | + ctx := context.Background() |
| 171 | + runtime, env := startTestRuntime(t) |
| 172 | + runtime.FastAPI.Settings.HotReload = true |
| 173 | + |
| 174 | + _, err := runtime.Start(ctx, &runtimev0.StartRequest{Overrides: map[string]string{"LOG_LEVEL": "debug"}}) |
| 175 | + require.NoError(t, err) |
| 176 | + require.Len(t, env.procs, 1) |
| 177 | + |
| 178 | + resp, err := runtime.Start(ctx, &runtimev0.StartRequest{Overrides: map[string]string{"LOG_LEVEL": "trace"}}) |
| 179 | + require.NoError(t, err) |
| 180 | + require.Equal(t, runtimev0.StartStatus_STARTED, resp.GetStatus().GetState(), resp.GetStatus().GetMessage()) |
| 181 | + require.Len(t, env.procs, 2) |
| 182 | + require.True(t, env.procs[0].stopped, "the process carrying the old environment must be torn down") |
| 183 | + require.Equal(t, []string{"trace"}, processEnv(env.procs[1], "LOG_LEVEL")) |
| 184 | +} |
0 commit comments