|
| 1 | +//go:build integration |
| 2 | +// +build integration |
| 3 | + |
| 4 | +package preflight |
| 5 | + |
| 6 | +import ( |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "github.com/superfly/flyctl/test/preflight/testlib" |
| 9 | + "os" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +// TODO: list of things to test |
| 14 | +// - App scope is properly determined |
| 15 | +// - Org scope is properly determined |
| 16 | +// - App identified by flag is prioritized over the fly.toml file, because it's more visible |
| 17 | + |
| 18 | +func TestTokensListDeterminesScopeApp(t *testing.T) { |
| 19 | + // Get library from preflight test lib using env variables form .direnv/preflight |
| 20 | + f := testlib.NewTestEnvFromEnv(t) |
| 21 | + // No need to run this on alternate sizes as it only tests the generated config. |
| 22 | + if f.VMSize != "" { |
| 23 | + t.Skip() |
| 24 | + } |
| 25 | + |
| 26 | + // Create fly.toml in current directory for the purpose of testing --config purposes |
| 27 | + appName := f.CreateRandomAppName() |
| 28 | + f.Fly( |
| 29 | + "launch --now --org %s --name %s --region %s --image nginx --internal-port 80 --ha=false", |
| 30 | + f.OrgSlug(), appName, f.PrimaryRegion(), |
| 31 | + ) |
| 32 | + |
| 33 | + // List of commands to verify scope app is selected for |
| 34 | + commandList := [9](string){ |
| 35 | + // default, no flags |
| 36 | + "tokens list", |
| 37 | + // --app flag only |
| 38 | + "tokens list -a " + appName, |
| 39 | + // --config flag only |
| 40 | + "tokens list -c fly.toml", |
| 41 | + // --app flag, with --org flag |
| 42 | + "tokens list -a " + appName + " -o " + f.OrgSlug(), |
| 43 | + // --config flag, with --org flag |
| 44 | + "tokens list -c fly.toml -o " + f.OrgSlug(), |
| 45 | + // --scope is app |
| 46 | + "tokens list -s app -a " + appName, |
| 47 | + // --scope is app, with --org flag |
| 48 | + "tokens list -s app -a " + appName + " -o " + f.OrgSlug(), |
| 49 | + // --scope is org, but --config flag added |
| 50 | + "tokens list -s app -c fly.toml -o " + f.OrgSlug(), |
| 51 | + // --scope is org, but --app flag added |
| 52 | + "tokens list -s org " + f.OrgSlug() + " -a " + appName, |
| 53 | + } |
| 54 | + |
| 55 | + for _, cmd_ := range commandList { |
| 56 | + console_out := f.Fly(cmd_).StdOutString() |
| 57 | + require.Contains(f, console_out, `Tokens for app`) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestTokensListDeterminesScopeOrg(t *testing.T) { |
| 62 | + // Get library from preflight test lib using env variables form .direnv/preflight |
| 63 | + f := testlib.NewTestEnvFromEnv(t) |
| 64 | + // No need to run this on alternate sizes as it only tests the generated config. |
| 65 | + if f.VMSize != "" { |
| 66 | + t.Skip() |
| 67 | + } |
| 68 | + |
| 69 | + // List of commands to verify scope org is selected for |
| 70 | + commandList := [2](string){ |
| 71 | + // --org flag alone |
| 72 | + "tokens list --org " + f.OrgSlug(), |
| 73 | + // --scope org |
| 74 | + "tokens list --scope org " + f.OrgSlug(), |
| 75 | + } |
| 76 | + |
| 77 | + for _, cmd_ := range commandList { |
| 78 | + console_out := f.Fly(cmd_).StdOutString() |
| 79 | + require.Contains(f, console_out, `Tokens for organization`) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestTokensListPrioritizesAppFromFlagOverToml(t *testing.T) { |
| 84 | + // Get library from preflight test lib using env variables form .direnv/preflight |
| 85 | + f := testlib.NewTestEnvFromEnv(t) |
| 86 | + // No need to run this on alternate sizes as it only tests the generated config. |
| 87 | + if f.VMSize != "" { |
| 88 | + t.Skip() |
| 89 | + } |
| 90 | + |
| 91 | + // Create fly.toml in current directory |
| 92 | + // Get this appName for verifying that the flag is prioritized over toml appname content |
| 93 | + appNameA := f.CreateRandomAppName() |
| 94 | + f.Fly( |
| 95 | + "launch --now --org %s --name %s --region %s --image nginx --internal-port 80 --ha=false", |
| 96 | + f.OrgSlug(), appNameA, f.PrimaryRegion(), |
| 97 | + ) |
| 98 | + |
| 99 | + // Delete this first app's toml file, so we can create a new one |
| 100 | + os.Remove(f.WorkDir() + "/fly.toml") |
| 101 | + appNameB := f.CreateRandomAppName() |
| 102 | + f.Fly( |
| 103 | + "launch --now --org %s --name %s --region %s --image nginx --internal-port 80 --ha=false", |
| 104 | + f.OrgSlug(), appNameB, f.PrimaryRegion(), |
| 105 | + ) |
| 106 | + // By default use the app name found in the current dir should be used ( appNameB ) |
| 107 | + console_out := f.Fly("tokens list").StdOutString() |
| 108 | + require.Contains(f, console_out, `Tokens for app "`+appNameB+`"`) |
| 109 | + |
| 110 | + // But, the app name passed in --app should be prioritized over the toml appname ( appNameA ) |
| 111 | + console_out = f.Fly("tokens list --app " + appNameA).StdOutString() |
| 112 | + require.Contains(f, console_out, `Tokens for app "`+appNameA+`"`) |
| 113 | +} |
0 commit comments