package main import ( "bytes" "context" "errors" "testing" "proxy-pool/internal/controller/bootstrap" ) func TestExecuteUsesFlagBeforeEnvironment(t *testing.T) { t.Parallel() var received bootstrap.Options code := execute(context.Background(), []string{"-config", "flag.yaml"}, func(name string) string { if name == configEnvironment { return "environment.yaml" } if name == fingerprintKeyEnvironment { return "0123456789abcdef0123456789abcdef" } return "" }, func(_ context.Context, options bootstrap.Options) error { received = options return nil }, &bytes.Buffer{}) if code != 0 || received.ConfigPath != "flag.yaml" || received.Resolver == nil || len(received.FingerprintKey) != 32 { t.Fatalf("execute() = %d, options = %+v", code, received) } } func TestExecuteFallsBackToEnvironment(t *testing.T) { t.Parallel() var received bootstrap.Options code := execute(context.Background(), nil, func(name string) string { if name == configEnvironment { return "environment.yaml" } return "0123456789abcdef0123456789abcdef" }, func( _ context.Context, options bootstrap.Options, ) error { received = options return nil }, &bytes.Buffer{}) if code != 0 || received.ConfigPath != "environment.yaml" { t.Fatalf("execute() = %d, config = %q", code, received.ConfigPath) } } func TestExecuteReturnsUsageCodeWithoutConfiguration(t *testing.T) { t.Parallel() called := false var stderr bytes.Buffer code := execute(context.Background(), nil, func(string) string { return "" }, func( context.Context, bootstrap.Options, ) error { called = true return nil }, &stderr) if code != 2 || called || stderr.Len() == 0 { t.Fatalf("execute() = %d, called = %t, stderr = %q", code, called, stderr.String()) } } func TestExecuteMapsStartupFailureAndSignalCancellation(t *testing.T) { t.Parallel() var stderr bytes.Buffer want := errors.New("startup failed") code := execute(context.Background(), []string{"-config", "config.yaml"}, func(string) string { return "" }, func( context.Context, bootstrap.Options, ) error { return want }, &stderr) if code != 1 || !bytes.Contains(stderr.Bytes(), []byte(want.Error())) { t.Fatalf("execute(startup failure) = %d, stderr = %q", code, stderr.String()) } ctx, cancel := context.WithCancel(context.Background()) cancel() stderr.Reset() code = execute(ctx, []string{"-config", "config.yaml"}, func(string) string { return "" }, func( context.Context, bootstrap.Options, ) error { return context.Canceled }, &stderr) if code != 0 || stderr.Len() != 0 { t.Fatalf("execute(canceled) = %d, stderr = %q", code, stderr.String()) } }