|
| 1 | +package launch |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/spf13/pflag" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + fly "github.com/superfly/fly-go" |
| 11 | + "github.com/superfly/flyctl/internal/appconfig" |
| 12 | + "github.com/superfly/flyctl/internal/flag/flagctx" |
| 13 | + "github.com/superfly/flyctl/internal/flyutil" |
| 14 | + "github.com/superfly/flyctl/internal/mock" |
| 15 | + "github.com/superfly/flyctl/iostreams" |
| 16 | +) |
| 17 | + |
| 18 | +func newDetermineOrgCtx(t *testing.T, orgFlag string) context.Context { |
| 19 | + t.Helper() |
| 20 | + |
| 21 | + ctx := context.Background() |
| 22 | + |
| 23 | + flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 24 | + flagSet.String("org", "", "") |
| 25 | + flagSet.Bool("attach", false, "") |
| 26 | + |
| 27 | + if orgFlag != "" { |
| 28 | + require.NoError(t, flagSet.Set("org", orgFlag)) |
| 29 | + } |
| 30 | + |
| 31 | + return flagctx.NewContext(ctx, flagSet) |
| 32 | +} |
| 33 | + |
| 34 | +func TestDetermineOrg(t *testing.T) { |
| 35 | + personalOrg := fly.Organization{ |
| 36 | + ID: "org-id-personal", |
| 37 | + Slug: "personal", |
| 38 | + RawSlug: "logan-griswold-339", |
| 39 | + Name: "Logan Griswold", |
| 40 | + Type: "PERSONAL", |
| 41 | + } |
| 42 | + teamOrg := fly.Organization{ |
| 43 | + ID: "org-id-team", |
| 44 | + Slug: "my-team", |
| 45 | + RawSlug: "my-team", |
| 46 | + Name: "My Team", |
| 47 | + Type: "SHARED", |
| 48 | + } |
| 49 | + orgs := []fly.Organization{personalOrg, teamOrg} |
| 50 | + |
| 51 | + makeClient := func() *mock.Client { |
| 52 | + return &mock.Client{ |
| 53 | + GetOrganizationsFunc: func(ctx context.Context, filters ...fly.OrganizationFilter) ([]fly.Organization, error) { |
| 54 | + return orgs, nil |
| 55 | + }, |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + t.Run("no org flag defaults to personal", func(t *testing.T) { |
| 60 | + ctx := newDetermineOrgCtx(t, "") |
| 61 | + ctx = flyutil.NewContextWithClient(ctx, makeClient()) |
| 62 | + |
| 63 | + org, _, err := determineOrg(ctx, nil) |
| 64 | + require.NoError(t, err) |
| 65 | + assert.Equal(t, "personal", org.Slug) |
| 66 | + assert.Equal(t, "logan-griswold-339", org.RawSlug) |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("canonical personal slug", func(t *testing.T) { |
| 70 | + ctx := newDetermineOrgCtx(t, "personal") |
| 71 | + ctx = flyutil.NewContextWithClient(ctx, makeClient()) |
| 72 | + |
| 73 | + org, _, err := determineOrg(ctx, nil) |
| 74 | + require.NoError(t, err) |
| 75 | + assert.Equal(t, "personal", org.Slug) |
| 76 | + assert.Equal(t, "logan-griswold-339", org.RawSlug) |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("real raw slug of personal org", func(t *testing.T) { |
| 80 | + ctx := newDetermineOrgCtx(t, "logan-griswold-339") |
| 81 | + ctx = flyutil.NewContextWithClient(ctx, makeClient()) |
| 82 | + |
| 83 | + org, _, err := determineOrg(ctx, nil) |
| 84 | + require.NoError(t, err) |
| 85 | + assert.Equal(t, "personal", org.Slug) |
| 86 | + assert.Equal(t, "logan-griswold-339", org.RawSlug) |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("team org by slug", func(t *testing.T) { |
| 90 | + ctx := newDetermineOrgCtx(t, "my-team") |
| 91 | + ctx = flyutil.NewContextWithClient(ctx, makeClient()) |
| 92 | + |
| 93 | + org, _, err := determineOrg(ctx, nil) |
| 94 | + require.NoError(t, err) |
| 95 | + assert.Equal(t, "my-team", org.Slug) |
| 96 | + }) |
| 97 | + |
| 98 | + t.Run("org by display name", func(t *testing.T) { |
| 99 | + ctx := newDetermineOrgCtx(t, "My Team") |
| 100 | + ctx = flyutil.NewContextWithClient(ctx, makeClient()) |
| 101 | + |
| 102 | + org, _, err := determineOrg(ctx, nil) |
| 103 | + require.NoError(t, err) |
| 104 | + assert.Equal(t, "my-team", org.Slug) |
| 105 | + }) |
| 106 | + |
| 107 | + t.Run("unknown org returns error and falls back to personal", func(t *testing.T) { |
| 108 | + ctx := newDetermineOrgCtx(t, "does-not-exist") |
| 109 | + ctx = flyutil.NewContextWithClient(ctx, makeClient()) |
| 110 | + |
| 111 | + org, _, err := determineOrg(ctx, nil) |
| 112 | + assert.Error(t, err) |
| 113 | + require.NotNil(t, org) |
| 114 | + assert.Equal(t, "personal", org.Slug) |
| 115 | + }) |
| 116 | +} |
| 117 | + |
| 118 | +// newDetermineBaseAppConfigCtx builds a context wired with the flags that |
| 119 | +// determineBaseAppConfig reads. Pass configPath="" to leave --config unset. |
| 120 | +func newDetermineBaseAppConfigCtx(t *testing.T, copyConfigFlag, explicitConfigPath bool) context.Context { |
| 121 | + t.Helper() |
| 122 | + |
| 123 | + ctx := context.Background() |
| 124 | + ctx = iostreams.NewContext(ctx, iostreams.System()) |
| 125 | + |
| 126 | + flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 127 | + flagSet.String("config", "", "") |
| 128 | + flagSet.Bool("copy-config", false, "") |
| 129 | + flagSet.Bool("attach", false, "") |
| 130 | + flagSet.Bool("yes", false, "") |
| 131 | + |
| 132 | + if copyConfigFlag { |
| 133 | + require.NoError(t, flagSet.Set("copy-config", "true")) |
| 134 | + } |
| 135 | + if explicitConfigPath { |
| 136 | + require.NoError(t, flagSet.Set("config", "fly.custom.toml")) |
| 137 | + } |
| 138 | + |
| 139 | + return flagctx.NewContext(ctx, flagSet) |
| 140 | +} |
| 141 | + |
| 142 | +func TestDetermineBaseAppConfig(t *testing.T) { |
| 143 | + // existingCfg simulates what LoadAppConfigIfPresent puts in context when |
| 144 | + // the customer has a custom fly.toml with a non-default dockerfile. |
| 145 | + existingCfg := appconfig.NewConfig() |
| 146 | + existingCfg.Build = &appconfig.Build{ |
| 147 | + Dockerfile: "docker.ui-server.dockerfile", |
| 148 | + } |
| 149 | + |
| 150 | + t.Run("no flags and no existing config returns blank config", func(t *testing.T) { |
| 151 | + ctx := newDetermineBaseAppConfigCtx(t, false, false) |
| 152 | + // No config in context — simulates no fly.toml present. |
| 153 | + |
| 154 | + cfg, copied, err := determineBaseAppConfig(ctx) |
| 155 | + require.NoError(t, err) |
| 156 | + assert.False(t, copied) |
| 157 | + assert.Nil(t, cfg.Build) |
| 158 | + }) |
| 159 | + |
| 160 | + t.Run("--copy-config adopts existing config without prompting", func(t *testing.T) { |
| 161 | + ctx := newDetermineBaseAppConfigCtx(t, true, false) |
| 162 | + ctx = appconfig.WithConfig(ctx, existingCfg) |
| 163 | + |
| 164 | + cfg, copied, err := determineBaseAppConfig(ctx) |
| 165 | + require.NoError(t, err) |
| 166 | + assert.True(t, copied) |
| 167 | + assert.Equal(t, "docker.ui-server.dockerfile", cfg.Build.Dockerfile) |
| 168 | + }) |
| 169 | + |
| 170 | + t.Run("explicit --config adopts existing config without prompting", func(t *testing.T) { |
| 171 | + // This is the deployer scenario: --config fly.custom.toml is passed but |
| 172 | + // --copy-config is not. The explicit path signals intent, so we must |
| 173 | + // not fall through to source scanning with an empty config. |
| 174 | + ctx := newDetermineBaseAppConfigCtx(t, false, true) |
| 175 | + ctx = appconfig.WithConfig(ctx, existingCfg) |
| 176 | + |
| 177 | + cfg, copied, err := determineBaseAppConfig(ctx) |
| 178 | + require.NoError(t, err) |
| 179 | + assert.True(t, copied) |
| 180 | + assert.Equal(t, "docker.ui-server.dockerfile", cfg.Build.Dockerfile) |
| 181 | + }) |
| 182 | + |
| 183 | + t.Run("no flags in non-interactive mode returns error", func(t *testing.T) { |
| 184 | + ctx := newDetermineBaseAppConfigCtx(t, false, false) |
| 185 | + ctx = appconfig.WithConfig(ctx, existingCfg) |
| 186 | + // Non-interactive iostreams → prompt.Confirm returns ErrNonInteractive. |
| 187 | + ios, _, _, _ := iostreams.Test() |
| 188 | + ctx = iostreams.NewContext(ctx, ios) |
| 189 | + |
| 190 | + _, _, err := determineBaseAppConfig(ctx) |
| 191 | + assert.Error(t, err) |
| 192 | + }) |
| 193 | +} |
0 commit comments