Files
tasks/temporal/server_test.go
298643d670 Update Configuration Loading (#8477)
## What Changed

This PR introduces a new `--config-file` flag (and the
`TEMPORAL_SERVER_CONFIG_FILE_PATH ` environment variable) to remove the
dependency on `dockerize` in the Temporal server Docker image.

When a configuration file is specified using either the CLI flag or the
environment variable, the server will load configuration **only** from
that file.
Users who want templating behavior similar to `dockerize` can enable it
by adding the comment `# enable-template` at the top of the
configuration file.

---

### Key Changes

1. **New `--config-file` flag:**

* Adds a global `--config-file` flag that accepts a path to a single
configuration file (absolute or relative to the project root).
* Can also be set via the `TEMPORAL_SERVER_CONFIG_FILE_PATH `
environment variable.

2. **Deprecated legacy flags:**

* The `--config`, `--env`, and `--zone` flags are now marked as
**deprecated** in CLI help text.
   * These flags still work for backward compatibility.

3. **Embedded config template:**

* The `config_template.yaml` file is now embedded in the binary to
support loading configuration from environment variables.
* Templating is supported if the file includes the `# enable-template`
comment at the top.

4. **Templating support:**

* Configuration files can use templating by including `#
enable-template` at the beginning of the YAML file.

---

### Configuration Loading Priority (Highest to Lowest)

1. **`--config-file` specified** → Load that specific file
2. **`--config`, `--env`, or `--zone` specified** → Load from
configuration directory (**deprecated**)
3. **No configuration specified** → Load from embedded template using
environment variables (default)

---

### Expected Behavior

The following examples illustrate how the new configuration loading
logic behaves:

* **Default behavior:**
Running `temporal start` without flags loads configuration from
environment variables only using the embedded template.

* **Using `--config-file`:**
`temporal --config-file=/path/to/config.yaml start` loads configuration
from the specified file path.

* **Using `TEMPORAL_SERVER_CONFIG_FILE_PATH`:**
Setting `TEMPORAL_SERVER_CONFIG_FILE_PATH=/path/to/config.yaml temporal
start` has the same effect as using the flag.

* **Validation and error handling:**
The CLI returns clear error messages when conflicting flags or
environment variables are used, or when a specified file does not exist.
---

## Breaking Change

The default behavior of `temporal start` has changed.
It now loads configuration **from environment variables** instead of
using a default template path.

---------

Co-authored-by: Alex Stanfield <[email protected]>
Co-authored-by: michaely520 <[email protected]>
Co-authored-by: Yichao Yang <[email protected]>
Co-authored-by: David Reiss <[email protected]>
2025-11-18 16:23:25 +00:00

254 lines
6.8 KiB
Go

package temporal_test
import (
"context"
"fmt"
"path"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.temporal.io/server/common/config"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
_ "go.temporal.io/server/common/persistence/sql/sqlplugin/sqlite" // needed to register the sqlite plugin
"go.temporal.io/server/common/testing/testtelemetry"
"go.temporal.io/server/service/frontend"
"go.temporal.io/server/temporal"
"go.temporal.io/server/tests/testutils"
"google.golang.org/grpc"
)
// TestNewServer verifies that NewServer doesn't cause any fx errors, and that there are no unexpected error logs after
// running for a few seconds.
func TestNewServer(t *testing.T) {
startAndStopServer(t)
}
// TestNewServerWithOTEL verifies that NewServer doesn't cause any fx errors when OTEL is enabled.
func TestNewServerWithOTEL(t *testing.T) {
t.Setenv("OTEL_TRACES_EXPORTER", "otlp")
t.Setenv("OTEL_BSP_SCHEDULE_DELAY", "100")
t.Setenv("OTEL_EXPORTER_OTLP_TRACES_INSECURE", "true")
t.Run("with OTEL Collector running", func(t *testing.T) {
collector, err := testtelemetry.StartMemoryCollector(t)
require.NoError(t, err)
t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", collector.Addr())
startAndStopServer(t)
require.NotEmpty(t, collector.Spans(), "expected at least one OTEL span")
})
t.Run("without OTEL Collector running", func(t *testing.T) {
startAndStopServer(t)
})
}
func startAndStopServer(t *testing.T) {
cfg := loadConfig(t)
// The prometheus reporter does not shut down in-between test runs.
// This will assign a random port to the prometheus reporter,
// so that it doesn't conflict with other tests.
cfg.Global.Metrics.Prometheus.ListenAddress = ":0"
logDetector := newErrorLogDetector(t, log.NewTestLogger())
logDetector.Start()
server, err := temporal.NewServer(
temporal.ForServices(temporal.DefaultServices),
temporal.WithConfig(cfg),
temporal.WithLogger(logDetector),
temporal.WithChainedFrontendGrpcInterceptors(getFrontendInterceptors()),
)
require.NoError(t, err)
t.Cleanup(func() {
logDetector.Stop()
assert.NoError(t, server.Stop())
})
require.NoError(t, server.Start())
time.Sleep(10 * time.Second) //nolint:forbidigo
}
func loadConfig(t *testing.T) *config.Config {
cfg := loadSQLiteConfig(t)
setTestPorts(cfg)
return cfg
}
// loadSQLiteConfig loads the config for the sqlite persistence store. We use sqlite because it doesn't require any
// external dependencies, so it's easy to run this test in isolation.
func loadSQLiteConfig(t *testing.T) *config.Config {
configDir := path.Join(testutils.GetRepoRootDirectory(), "config")
cfg, err := config.Load(
config.WithEnv("development-sqlite"),
config.WithConfigDir(configDir),
)
require.NoError(t, err)
cfg.DynamicConfigClient.Filepath = path.Join(configDir, "dynamicconfig", "development-sql.yaml")
return cfg
}
// setTestPorts sets the ports of all services to something different from the default ports, so that we can run the
// tests in parallel.
func setTestPorts(cfg *config.Config) {
port := 10000
for k, v := range cfg.Services {
rpc := v.RPC
rpc.GRPCPort = port
port++
rpc.MembershipPort = port
port++
v.RPC = rpc
cfg.Services[k] = v
}
}
func getFrontendInterceptors() func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
switch info.Server.(type) {
case *frontend.Handler, *frontend.OperatorHandler, *frontend.AdminHandler, *frontend.WorkflowHandler:
return handler(ctx, req)
default:
panic("Frontend gRPC interceptor provided to non-frontend handler")
}
}
}
type errorLogDetector struct {
t testing.TB
on atomic.Bool
logger log.Logger
}
func (d *errorLogDetector) logUnexpected(operation string, msg string, tags []tag.Tag) {
if !d.on.Load() {
return
}
msg = fmt.Sprintf("unexpected %v log: %v", operation, msg)
d.t.Error(msg)
d.logger.Error(msg, tags...)
}
func (d *errorLogDetector) Debug(string, ...tag.Tag) {}
func (d *errorLogDetector) Info(string, ...tag.Tag) {}
func (d *errorLogDetector) DPanic(msg string, tags ...tag.Tag) {
d.logUnexpected("DPanic", msg, tags)
}
func (d *errorLogDetector) Panic(msg string, tags ...tag.Tag) {
d.logUnexpected("Panic", msg, tags)
}
func (d *errorLogDetector) Fatal(msg string, tags ...tag.Tag) {
d.logUnexpected("Fatal", msg, tags)
}
func (d *errorLogDetector) Start() {
d.on.Store(true)
}
func (d *errorLogDetector) Stop() {
d.on.Store(false)
}
func (d *errorLogDetector) Warn(msg string, tags ...tag.Tag) {
for _, s := range []string{
"error creating sdk client",
"Failed to poll for task",
"OTEL error", // logged when OTEL collector is not running
} {
if strings.Contains(msg, s) {
return
}
}
d.logUnexpected("Warn", msg, tags)
}
func (d *errorLogDetector) Error(msg string, tags ...tag.Tag) {
for _, s := range []string{
"Unable to process new range",
"Unable to call",
"service failures",
} {
if strings.Contains(msg, s) {
return
}
}
d.logUnexpected("Error", msg, tags)
}
// newErrorLogDetector returns a logger that fails the test if it logs any errors or warnings, except for the ones that
// are expected. Ideally, there are no "expected" errors or warnings, but we still want this test to avoid introducing
// any new ones while we are working on removing the existing ones.
func newErrorLogDetector(t testing.TB, baseLogger log.Logger) *errorLogDetector {
return &errorLogDetector{
t: t,
logger: baseLogger,
}
}
type fakeTest struct {
testing.TB
errorLogs []string
}
func (f *fakeTest) Error(args ...any) {
require.Len(f.TB, args, 1)
msg, ok := args[0].(string)
require.True(f.TB, ok)
f.errorLogs = append(f.errorLogs, msg)
}
func TestErrorLogDetector(t *testing.T) {
t.Parallel()
f := &fakeTest{TB: t}
d := newErrorLogDetector(f, log.NewNoopLogger())
d.Start()
d.Debug("debug")
d.Info("info")
d.Warn("error creating sdk client")
d.Warn("unexpected warning")
d.Warn("Failed to poll for task")
d.Error("Unable to process new range")
d.Error("Unable to call matching.PollActivityTaskQueue")
d.Error("service failures")
d.Error("unexpected error")
d.DPanic("dpanic")
d.Panic("panic")
d.Fatal("fatal")
expectedMsgs := []string{
"unexpected Warn log: unexpected warning",
"unexpected Error log: unexpected error",
"unexpected DPanic log: dpanic",
"unexpected Panic log: panic",
"unexpected Fatal log: fatal",
}
assert.Equal(t, expectedMsgs, f.errorLogs)
d.Stop()
f.errorLogs = nil
d.Warn("unexpected warning")
d.Error("unexpected error")
d.DPanic("dpanic")
d.Panic("panic")
d.Fatal("fatal")
assert.Empty(t, f.errorLogs, "should not fail the test if the detector is stopped")
}