|
| 1 | +package orchestration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/codefly-dev/cli/pkg/environments" |
| 9 | + basev0 "github.com/codefly-dev/core/generated/go/codefly/base/v0" |
| 10 | + "github.com/codefly-dev/core/resources" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestProjectedFederationResolvesWithoutAnAuthoredFile(t *testing.T) { |
| 15 | + for _, authored := range []bool{false, true} { |
| 16 | + t.Run(map[bool]string{false: "generated only", true: "preserves authored secret"}[authored], func(t *testing.T) { |
| 17 | + loader := staticWorkspaceLoader{} |
| 18 | + if authored { |
| 19 | + secret := workspaceConfiguration("federation", "REGISTRATION_SECRET", "fixture-secret") |
| 20 | + secret.Infos[0].ConfigurationValues[0].Secret = true |
| 21 | + loader.confs = []*basev0.Configuration{secret} |
| 22 | + } |
| 23 | + world := loadedWorkspaceWorld(t, loader) |
| 24 | + calls := 0 |
| 25 | + world.RenderOwnedGroups = func(context.Context, *resources.Service) ([]*basev0.Configuration, error) { |
| 26 | + calls++ |
| 27 | + return []*basev0.Configuration{workspaceConfiguration("federation", environments.FederationHostCoordinateKey, "owner/local/host")}, nil |
| 28 | + } |
| 29 | + service := &resources.Service{WorkspaceConfigurationDependencies: []string{"federation"}} |
| 30 | + confs, err := world.workspaceConfigurationsFor(t.Context(), service, nil, resources.NewNativeNetworkAccess()) |
| 31 | + require.NoError(t, err) |
| 32 | + require.Equal(t, 1, calls) |
| 33 | + values := map[string]*basev0.ConfigurationValue{} |
| 34 | + for _, conf := range confs { |
| 35 | + for _, info := range conf.Infos { |
| 36 | + for _, value := range info.ConfigurationValues { |
| 37 | + values[value.Key] = value |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + require.Equal(t, "owner/local/host", values[environments.FederationHostCoordinateKey].Value) |
| 42 | + if authored { |
| 43 | + require.Equal(t, "fixture-secret", values["REGISTRATION_SECRET"].Value) |
| 44 | + require.True(t, values["REGISTRATION_SECRET"].Secret) |
| 45 | + } |
| 46 | + }) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestProjectedFederationDoesNotRelaxRequiredConfigurations(t *testing.T) { |
| 51 | + for _, scenario := range []string{"no hook", "no projection", "other group", "unknown loader", "ambiguous", "projection failure"} { |
| 52 | + t.Run(scenario, func(t *testing.T) { |
| 53 | + world := loadedWorkspaceWorld(t, staticWorkspaceLoader{}) |
| 54 | + world.RenderOwnedGroups = func(context.Context, *resources.Service) ([]*basev0.Configuration, error) { |
| 55 | + if scenario == "projection failure" { |
| 56 | + return nil, errors.New("host projection failed") |
| 57 | + } |
| 58 | + if scenario == "no projection" { |
| 59 | + return nil, nil |
| 60 | + } |
| 61 | + return []*basev0.Configuration{workspaceConfiguration("federation", environments.FederationHostCoordinateKey, "owner/local/host")}, nil |
| 62 | + } |
| 63 | + group := "federation" |
| 64 | + if scenario == "no hook" { |
| 65 | + world.RenderOwnedGroups = nil |
| 66 | + } |
| 67 | + if scenario == "other group" { |
| 68 | + group = "required-secret" |
| 69 | + } |
| 70 | + if scenario == "unknown loader" { |
| 71 | + world.providedWorkspaceConfigurationInfos = nil |
| 72 | + } |
| 73 | + if scenario == "ambiguous" { |
| 74 | + world.ambiguousWorkspaceConfigurations = func() map[string]error { return map[string]error{"federation": errors.New("conflicting groups")} } |
| 75 | + } |
| 76 | + _, err := world.workspaceConfigurationsFor(t.Context(), &resources.Service{WorkspaceConfigurationDependencies: []string{group}}, nil, resources.NewNativeNetworkAccess()) |
| 77 | + require.Error(t, err) |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestNativeFederationProjectionThroughConfigurationResolver(t *testing.T) { |
| 83 | + host := &environments.EnvironmentHost{Delivery: "authority/local/grpc", Coordinate: "owner/local/host", Domain: "owner"} |
| 84 | + for _, scenario := range []string{"host", "other service", "excluded", "authored invalid reference"} { |
| 85 | + t.Run(scenario, func(t *testing.T) { |
| 86 | + loader := staticWorkspaceLoader{} |
| 87 | + if scenario == "authored invalid reference" { |
| 88 | + loader.confs = []*basev0.Configuration{workspaceConfiguration("federation", "registration", "${endpoint:missing/service/grpc}")} |
| 89 | + } |
| 90 | + world := loadedWorkspaceWorld(t, loader) |
| 91 | + world.RenderOwnedGroups = func(_ context.Context, service *resources.Service) ([]*basev0.Configuration, error) { |
| 92 | + group, err := environments.NativeHostFederation(host, service) |
| 93 | + if group == nil || err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + return []*basev0.Configuration{group}, nil |
| 97 | + } |
| 98 | + service := &resources.Service{Name: "local", WorkspaceConfigurationDependencies: []string{"federation"}} |
| 99 | + service.WithModule("authority") |
| 100 | + if scenario == "other service" { |
| 101 | + service.Name = "edge" |
| 102 | + } |
| 103 | + if scenario == "excluded" { |
| 104 | + world.excludedWorkspaceConfigurations = map[string]bool{"federation": true} |
| 105 | + } |
| 106 | + confs, err := world.workspaceConfigurationsFor(t.Context(), service, nil, resources.NewNativeNetworkAccess()) |
| 107 | + if scenario == "other service" || scenario == "authored invalid reference" { |
| 108 | + require.Error(t, err) |
| 109 | + return |
| 110 | + } |
| 111 | + require.NoError(t, err) |
| 112 | + if scenario == "excluded" { |
| 113 | + require.Empty(t, confs) |
| 114 | + } else { |
| 115 | + require.Len(t, confs, 1) |
| 116 | + require.Len(t, confs[0].Infos[0].ConfigurationValues, 3) |
| 117 | + } |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
0 commit comments