|
| 1 | +package exec |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + |
| 10 | + cfg "github.com/cloudposse/atmos/pkg/config" |
| 11 | + "github.com/cloudposse/atmos/pkg/schema" |
| 12 | + u "github.com/cloudposse/atmos/pkg/utils" |
| 13 | +) |
| 14 | + |
| 15 | +func TestExecuteDescribeAffectedCmd(t *testing.T) { |
| 16 | + stacksPath := "../../tests/fixtures/scenarios/atmos-describe-affected" |
| 17 | + |
| 18 | + err := os.Setenv("ATMOS_CLI_CONFIG_PATH", stacksPath) |
| 19 | + assert.NoError(t, err, "Setting 'ATMOS_CLI_CONFIG_PATH' environment variable should execute without error") |
| 20 | + |
| 21 | + err = os.Setenv("ATMOS_BASE_PATH", stacksPath) |
| 22 | + assert.NoError(t, err, "Setting 'ATMOS_BASE_PATH' environment variable should execute without error") |
| 23 | + |
| 24 | + defer func() { |
| 25 | + err := os.Unsetenv("ATMOS_BASE_PATH") |
| 26 | + assert.NoError(t, err) |
| 27 | + err = os.Unsetenv("ATMOS_CLI_CONFIG_PATH") |
| 28 | + assert.NoError(t, err) |
| 29 | + }() |
| 30 | + |
| 31 | + cmd := &cobra.Command{ |
| 32 | + Use: "describe affected", |
| 33 | + Short: "List Atmos components and stacks affected by two Git commits", |
| 34 | + Long: "Identify and list Atmos components and stacks impacted by changes between two Git commits.", |
| 35 | + FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false}, |
| 36 | + Run: func(cmd *cobra.Command, args []string) { |
| 37 | + err := ExecuteDescribeAffectedCmd(cmd, args) |
| 38 | + if err != nil { |
| 39 | + u.PrintErrorMarkdownAndExit("", err, "") |
| 40 | + } |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + cmd.PersistentFlags().String("repo-path", "", "Filesystem path to the already cloned target repository with which to compare the current branch") |
| 45 | + cmd.PersistentFlags().String("ref", "", "Git reference with which to compare the current branch. Refer to [10.3 Git Internals Git References](https://git-scm.com/book/en/v2/Git-Internals-Git-References) for more details") |
| 46 | + cmd.PersistentFlags().String("sha", "", "Git commit SHA with which to compare the current branch") |
| 47 | + cmd.PersistentFlags().String("file", "", "Write the result to the file") |
| 48 | + cmd.PersistentFlags().String("format", "json", "The output format. (`json` is default)") |
| 49 | + cmd.PersistentFlags().Bool("verbose", false, "Print more detailed output when cloning and checking out the Git repository") |
| 50 | + cmd.PersistentFlags().String("ssh-key", "", "Path to PEM-encoded private key to clone private repos using SSH") |
| 51 | + cmd.PersistentFlags().String("ssh-key-password", "", "Encryption password for the PEM-encoded private key if the key contains a password-encrypted PEM block") |
| 52 | + cmd.PersistentFlags().Bool("include-spacelift-admin-stacks", false, "Include the Spacelift admin stack of any stack that is affected by config changes") |
| 53 | + cmd.PersistentFlags().Bool("include-dependents", false, "Include the dependent components and stacks") |
| 54 | + cmd.PersistentFlags().Bool("include-settings", false, "Include the `settings` section for each affected component") |
| 55 | + cmd.PersistentFlags().Bool("upload", false, "Upload the affected components and stacks to a specified HTTP endpoint") |
| 56 | + cmd.PersistentFlags().Bool("clone-target-ref", false, "Clone the target reference with which to compare the current branch\n"+ |
| 57 | + "If set to `false` (default), the target reference will be checked out instead\n"+ |
| 58 | + "This requires that the target reference is already cloned by Git, and the information about it exists in the `.git` directory") |
| 59 | + cmd.PersistentFlags().Bool("process-templates", true, "Enable/disable Go template processing in Atmos stack manifests when executing the command") |
| 60 | + cmd.PersistentFlags().Bool("process-functions", true, "Enable/disable YAML functions processing in Atmos stack manifests when executing the command") |
| 61 | + cmd.PersistentFlags().StringSlice("skip", nil, "Skip executing a YAML function when processing Atmos stack manifests") |
| 62 | + cmd.PersistentFlags().Bool("no-color", false, "Disable color output") |
| 63 | + cmd.PersistentFlags().StringP("query", "q", "", "Query the results of an `atmos describe` command using `yq` expressions") |
| 64 | + cmd.PersistentFlags().String("logs-level", "Info", "Logs level. Supported log levels are Trace, Debug, Info, Warning, Off. If the log level is set to Off, Atmos will not log any messages") |
| 65 | + cmd.PersistentFlags().String("logs-file", "/dev/stderr", "The file to write Atmos logs to. Logs can be written to any file or any standard file descriptor, including '/dev/stdout', '/dev/stderr' and '/dev/null'") |
| 66 | + cmd.PersistentFlags().String("base-path", "", "Base path for Atmos project") |
| 67 | + cmd.PersistentFlags().StringSlice("config", []string{}, "Paths to configuration files (comma-separated or repeated flag)") |
| 68 | + cmd.PersistentFlags().StringSlice("config-path", []string{}, "Paths to configuration directories (comma-separated or repeated flag)") |
| 69 | + cmd.PersistentFlags().StringP("stack", "s", "", |
| 70 | + "Filter by a specific stack\n"+ |
| 71 | + "The filter supports names of the top-level stack manifests (including subfolder paths), and `atmos` stack names (derived from the context vars)", |
| 72 | + ) |
| 73 | + |
| 74 | + // Execute the command |
| 75 | + cmd.SetArgs([]string{"--clone-target-ref=true", "--include-settings=true"}) |
| 76 | + err = cmd.Execute() |
| 77 | + assert.NoError(t, err, "'atmos describe affected' command should execute without error") |
| 78 | +} |
| 79 | + |
| 80 | +func TestExecuteDescribeAffectedWithTargetRepoPath(t *testing.T) { |
| 81 | + stacksPath := "../../tests/fixtures/scenarios/atmos-describe-affected" |
| 82 | + |
| 83 | + err := os.Setenv("ATMOS_CLI_CONFIG_PATH", stacksPath) |
| 84 | + assert.NoError(t, err, "Setting 'ATMOS_CLI_CONFIG_PATH' environment variable should execute without error") |
| 85 | + |
| 86 | + err = os.Setenv("ATMOS_BASE_PATH", stacksPath) |
| 87 | + assert.NoError(t, err, "Setting 'ATMOS_BASE_PATH' environment variable should execute without error") |
| 88 | + |
| 89 | + defer func() { |
| 90 | + err := os.Unsetenv("ATMOS_BASE_PATH") |
| 91 | + assert.NoError(t, err) |
| 92 | + err = os.Unsetenv("ATMOS_CLI_CONFIG_PATH") |
| 93 | + assert.NoError(t, err) |
| 94 | + }() |
| 95 | + |
| 96 | + atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true) |
| 97 | + assert.Nil(t, err) |
| 98 | + |
| 99 | + // We are using `atmos.yaml` from this dir. This `atmos.yaml` has set base_path: "./", |
| 100 | + // which will be wrong for the remote repo which is cloned into a temp dir. |
| 101 | + // Set the correct base path for the cloned remote repo |
| 102 | + atmosConfig.BasePath = "./tests/fixtures/scenarios/atmos-describe-affected" |
| 103 | + |
| 104 | + // Point to the same local repository |
| 105 | + // This will compare this local repository with itself as the remote target, which should result in an empty `affected` list |
| 106 | + repoPath := "../../" |
| 107 | + |
| 108 | + affected, _, _, _, err := ExecuteDescribeAffectedWithTargetRepoPath( |
| 109 | + &atmosConfig, |
| 110 | + repoPath, |
| 111 | + false, |
| 112 | + false, |
| 113 | + true, |
| 114 | + "", |
| 115 | + false, |
| 116 | + false, |
| 117 | + nil, |
| 118 | + ) |
| 119 | + assert.Nil(t, err) |
| 120 | + |
| 121 | + // The `affected` list should be empty, since the local repo is compared with itself. |
| 122 | + assert.Equal(t, 0, len(affected)) |
| 123 | +} |
0 commit comments