Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions internal/settings/registry_resolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ func (r *OffChainRegistry) ID() string { return r.id }
func (r *OffChainRegistry) Type() RegistryType { return RegistryTypeOffChain }
func (r *OffChainRegistry) DonFamily() string { return r.donFamily }

// EffectiveDonFamily prefers envSet.DonFamily (CRE_CLI_DON_FAMILY at load); otherwise tenantCtx.DefaultDonFamily.
func EffectiveDonFamily(envSet *environments.EnvironmentSet, tenantCtx *tenantctx.EnvironmentContext) string {
if envSet != nil {
if v := strings.TrimSpace(envSet.DonFamily); v != "" {
return v
}
}
if tenantCtx != nil {
return strings.TrimSpace(tenantCtx.DefaultDonFamily)
}
return ""
}

// ResolveRegistry maps an optional deployment-registry value to a concrete
// ResolvedRegistry. When deploymentRegistry is empty the static EnvironmentSet
// values are used (backwards-compatible default). When set, it is looked up in
Expand All @@ -78,7 +91,7 @@ func ResolveRegistry(
envSet *environments.EnvironmentSet,
) (ResolvedRegistry, error) {
if deploymentRegistry == "" {
return defaultFromEnvironmentSet(envSet), nil
return defaultFromEnvironmentSet(envSet, tenantCtx), nil
}

if tenantCtx == nil {
Expand All @@ -92,7 +105,7 @@ func ResolveRegistry(
}

if ParseRegistryType(reg.Type) == RegistryTypeOffChain {
return NewOffChainRegistry(reg.ID, tenantCtx.DefaultDonFamily), nil
return NewOffChainRegistry(reg.ID, EffectiveDonFamily(envSet, tenantCtx)), nil
}

if reg.Address == nil || *reg.Address == "" {
Expand All @@ -111,7 +124,7 @@ func ResolveRegistry(
reg.ID,
*reg.Address,
chainName,
tenantCtx.DefaultDonFamily,
EffectiveDonFamily(envSet, tenantCtx),
envSet.WorkflowRegistryChainExplorerURL,
), nil
}
Expand All @@ -125,12 +138,12 @@ func ParseRegistryType(raw string) RegistryType {
return RegistryTypeOnChain
}

func defaultFromEnvironmentSet(envSet *environments.EnvironmentSet) *OnChainRegistry {
func defaultFromEnvironmentSet(envSet *environments.EnvironmentSet, tenantCtx *tenantctx.EnvironmentContext) *OnChainRegistry {
return NewOnChainRegistry(
fmt.Sprintf("onchain:%s", envSet.WorkflowRegistryChainName),
envSet.WorkflowRegistryAddress,
envSet.WorkflowRegistryChainName,
envSet.DonFamily,
EffectiveDonFamily(envSet, tenantCtx),
envSet.WorkflowRegistryChainExplorerURL,
)
}
Expand Down
75 changes: 71 additions & 4 deletions internal/settings/registry_resolution_test.go
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using assert.NoError(t, err) and assert.Equal(t, expected, actual) is easier to read in tests

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func stagingEnvSet() *environments.EnvironmentSet {
WorkflowRegistryAddress: "0xaE55eB3EDAc48a1163EE2cbb1205bE1e90Ea1135",
WorkflowRegistryChainName: "ethereum-testnet-sepolia",
WorkflowRegistryChainExplorerURL: "https://sepolia.etherscan.io",
DonFamily: "zone-a",
}
}

Expand All @@ -40,7 +39,7 @@ func sampleTenantCtx() *tenantctx.EnvironmentContext {
}
}

func TestResolveRegistry_EmptyFallsBackToEnvSet(t *testing.T) {
func TestResolveRegistry_Empty_AddressAndChainFromEnvSet_NoDonWithoutTenantOrEnv(t *testing.T) {
envSet := stagingEnvSet()
resolved, err := ResolveRegistry("", nil, envSet)
if err != nil {
Expand All @@ -57,14 +56,30 @@ func TestResolveRegistry_EmptyFallsBackToEnvSet(t *testing.T) {
if onchain.ChainName() != envSet.WorkflowRegistryChainName {
t.Errorf("expected chain %s, got %s", envSet.WorkflowRegistryChainName, onchain.ChainName())
}
if onchain.DonFamily() != envSet.DonFamily {
t.Errorf("expected don %s, got %s", envSet.DonFamily, onchain.DonFamily())
if onchain.DonFamily() != "" {
t.Errorf("expected empty DON family without tenant or CRE_CLI_DON_FAMILY, got %q", onchain.DonFamily())
}
if onchain.ExplorerURL() != envSet.WorkflowRegistryChainExplorerURL {
t.Errorf("expected explorer %s, got %s", envSet.WorkflowRegistryChainExplorerURL, onchain.ExplorerURL())
}
}

func TestResolveRegistry_DefaultRegistry_UsesTenantDonFamily(t *testing.T) {
envSet := stagingEnvSet()
tenantCtx := &tenantctx.EnvironmentContext{DefaultDonFamily: "tenant-zone"}
resolved, err := ResolveRegistry("", tenantCtx, envSet)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
onchain, ok := resolved.(*OnChainRegistry)
if !ok {
t.Fatalf("expected *OnChainRegistry, got %T", resolved)
}
if onchain.DonFamily() != "tenant-zone" {
t.Errorf("expected DON family from tenant context, got %q", onchain.DonFamily())
}
}

func TestResolveRegistry_OnChainFromContext(t *testing.T) {
resolved, err := ResolveRegistry("onchain:ethereum-testnet-sepolia", sampleTenantCtx(), stagingEnvSet())
if err != nil {
Expand All @@ -86,6 +101,58 @@ func TestResolveRegistry_OnChainFromContext(t *testing.T) {
}
}

func TestResolveRegistry_NamedRegistry_EnvOverridesTenantDonFamily(t *testing.T) {
envSet := stagingEnvSet()
envSet.DonFamily = "from-env-var"
tenantCtx := sampleTenantCtx()
tenantCtx.DefaultDonFamily = "from-tenant"

t.Run("on-chain named", func(t *testing.T) {
resolved, err := ResolveRegistry("onchain:ethereum-testnet-sepolia", tenantCtx, envSet)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
onchain, ok := resolved.(*OnChainRegistry)
if !ok {
t.Fatalf("expected *OnChainRegistry, got %T", resolved)
}
if onchain.DonFamily() != "from-env-var" {
t.Errorf("DON family: got %q, want from-env-var", onchain.DonFamily())
}
})

t.Run("private", func(t *testing.T) {
resolved, err := ResolveRegistry("private", tenantCtx, envSet)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
offchain, ok := resolved.(*OffChainRegistry)
if !ok {
t.Fatalf("expected *OffChainRegistry, got %T", resolved)
}
if offchain.DonFamily() != "from-env-var" {
t.Errorf("DON family: got %q, want from-env-var", offchain.DonFamily())
}
})
}

func TestResolveRegistry_DefaultRegistry_EnvOverridesTenantDonFamily(t *testing.T) {
envSet := stagingEnvSet()
envSet.DonFamily = "from-env-var"
tenantCtx := &tenantctx.EnvironmentContext{DefaultDonFamily: "tenant-zone"}
resolved, err := ResolveRegistry("", tenantCtx, envSet)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
onchain, ok := resolved.(*OnChainRegistry)
if !ok {
t.Fatalf("expected *OnChainRegistry, got %T", resolved)
}
if onchain.DonFamily() != "from-env-var" {
t.Errorf("expected CRE_CLI_DON_FAMILY to override tenant, got %q", onchain.DonFamily())
}
}

func TestResolveRegistry_OffChainFromContext(t *testing.T) {
resolved, err := ResolveRegistry("private", sampleTenantCtx(), stagingEnvSet())
if err != nil {
Expand Down
Loading