Test CLI init/connect and without s3 prefix on bucket (#1033)

## Description

Regression test for bucket prefix trimming

## Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [x] 🤖 Test
- [ ] 💻 CI/Deployment
- [ ] 🐹 Trivial/Minor

## Issue(s)

* #913 
* #975 

## Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
- [x]  Unit test
- [ ] 💚 E2E
This commit is contained in:
ashmrtn 2022-10-04 09:45:09 -07:00 committed by GitHub
parent 3df3d68a3e
commit dd34ecd5f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 104 additions and 56 deletions

View File

@ -4,12 +4,25 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const (
initCommand = "init"
connectCommand = "connect"
)
var repoCommands = []func(parent *cobra.Command) *cobra.Command{ var repoCommands = []func(parent *cobra.Command) *cobra.Command{
addS3Commands, addS3Commands,
} }
// AddCommands attaches all `corso repo * *` commands to the parent. // AddCommands attaches all `corso repo * *` commands to the parent.
func AddCommands(parent *cobra.Command) { func AddCommands(parent *cobra.Command) {
var (
// Get new instances so that setting the context during tests works
// properly.
repoCmd = repoCmd()
initCmd = initCmd()
connectCmd = connectCmd()
)
parent.AddCommand(repoCmd) parent.AddCommand(repoCmd)
repoCmd.AddCommand(initCmd) repoCmd.AddCommand(initCmd)
repoCmd.AddCommand(connectCmd) repoCmd.AddCommand(connectCmd)
@ -22,12 +35,14 @@ func AddCommands(parent *cobra.Command) {
// The repo category of commands. // The repo category of commands.
// `corso repo [<subcommand>] [<flag>...]` // `corso repo [<subcommand>] [<flag>...]`
var repoCmd = &cobra.Command{ func repoCmd() *cobra.Command {
Use: "repo", return &cobra.Command{
Short: "Manage your repositories", Use: "repo",
Long: `Initialize, configure, and connect to your account backup repositories.`, Short: "Manage your repositories",
RunE: handleRepoCmd, Long: `Initialize, configure, and connect to your account backup repositories.`,
Args: cobra.NoArgs, RunE: handleRepoCmd,
Args: cobra.NoArgs,
}
} }
// Handler for flat calls to `corso repo`. // Handler for flat calls to `corso repo`.
@ -38,16 +53,15 @@ func handleRepoCmd(cmd *cobra.Command, args []string) error {
// The repo init subcommand. // The repo init subcommand.
// `corso repo init <repository> [<flag>...]` // `corso repo init <repository> [<flag>...]`
var ( func initCmd() *cobra.Command {
initCommand = "init" return &cobra.Command{
initCmd = &cobra.Command{
Use: initCommand, Use: initCommand,
Short: "Initialize a repository.", Short: "Initialize a repository.",
Long: `Create a new repository to store your backups.`, Long: `Create a new repository to store your backups.`,
RunE: handleInitCmd, RunE: handleInitCmd,
Args: cobra.NoArgs, Args: cobra.NoArgs,
} }
) }
// Handler for calls to `corso repo init`. // Handler for calls to `corso repo init`.
func handleInitCmd(cmd *cobra.Command, args []string) error { func handleInitCmd(cmd *cobra.Command, args []string) error {
@ -56,16 +70,15 @@ func handleInitCmd(cmd *cobra.Command, args []string) error {
// The repo connect subcommand. // The repo connect subcommand.
// `corso repo connect <repository> [<flag>...]` // `corso repo connect <repository> [<flag>...]`
var ( func connectCmd() *cobra.Command {
connectCommand = "connect" return &cobra.Command{
connectCmd = &cobra.Command{
Use: connectCommand, Use: connectCommand,
Short: "Connect to a repository.", Short: "Connect to a repository.",
Long: `Connect to an existing repository.`, Long: `Connect to an existing repository.`,
RunE: handleConnectCmd, RunE: handleConnectCmd,
Args: cobra.NoArgs, Args: cobra.NoArgs,
} }
) }
// Handler for calls to `corso repo connect`. // Handler for calls to `corso repo connect`.
func handleConnectCmd(cmd *cobra.Command, args []string) error { func handleConnectCmd(cmd *cobra.Command, args []string) error {

View File

@ -3,6 +3,7 @@ package repo_test
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
@ -38,27 +39,44 @@ func (suite *S3IntegrationSuite) SetupSuite() {
} }
func (suite *S3IntegrationSuite) TestInitS3Cmd() { func (suite *S3IntegrationSuite) TestInitS3Cmd() {
ctx := tester.NewContext() table := []struct {
t := suite.T() name string
bucketPrefix string
}{
{
name: "NoPrefix",
bucketPrefix: "",
},
{
name: "S3Prefix",
bucketPrefix: "s3://",
},
}
st := tester.NewPrefixedS3Storage(t) for _, test := range table {
cfg, err := st.S3Config() suite.T().Run(test.name, func(t *testing.T) {
require.NoError(t, err) ctx := tester.NewContext()
vpr, configFP, err := tester.MakeTempTestConfigClone(t, nil) st := tester.NewPrefixedS3Storage(t)
require.NoError(t, err) cfg, err := st.S3Config()
require.NoError(t, err)
ctx = config.SetViper(ctx, vpr) vpr, configFP, err := tester.MakeTempTestConfigClone(t, nil)
require.NoError(t, err)
cmd := tester.StubRootCmd( ctx = config.SetViper(ctx, vpr)
"repo", "init", "s3",
"--config-file", configFP,
"--bucket", cfg.Bucket,
"--prefix", cfg.Prefix)
cli.BuildCommandTree(cmd)
// run the command cmd := tester.StubRootCmd(
require.NoError(t, cmd.ExecuteContext(ctx)) "repo", "init", "s3",
"--config-file", configFP,
"--bucket", test.bucketPrefix+cfg.Bucket,
"--prefix", cfg.Prefix)
cli.BuildCommandTree(cmd)
// run the command
require.NoError(t, cmd.ExecuteContext(ctx))
})
}
} }
func (suite *S3IntegrationSuite) TestInitS3Cmd_missingBucket() { func (suite *S3IntegrationSuite) TestInitS3Cmd_missingBucket() {
@ -85,37 +103,54 @@ func (suite *S3IntegrationSuite) TestInitS3Cmd_missingBucket() {
} }
func (suite *S3IntegrationSuite) TestConnectS3Cmd() { func (suite *S3IntegrationSuite) TestConnectS3Cmd() {
ctx := tester.NewContext() table := []struct {
t := suite.T() name string
bucketPrefix string
st := tester.NewPrefixedS3Storage(t) }{
cfg, err := st.S3Config() {
require.NoError(t, err) name: "NoPrefix",
bucketPrefix: "",
force := map[string]string{ },
tester.TestCfgAccountProvider: "M365", {
tester.TestCfgStorageProvider: "S3", name: "S3Prefix",
tester.TestCfgPrefix: cfg.Prefix, bucketPrefix: "s3://",
},
} }
vpr, configFP, err := tester.MakeTempTestConfigClone(t, force)
require.NoError(t, err)
ctx = config.SetViper(ctx, vpr) for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
ctx := tester.NewContext()
st := tester.NewPrefixedS3Storage(t)
cfg, err := st.S3Config()
require.NoError(t, err)
// init the repo first force := map[string]string{
_, err = repository.Initialize(ctx, account.Account{}, st, control.Options{}) tester.TestCfgAccountProvider: "M365",
require.NoError(t, err) tester.TestCfgStorageProvider: "S3",
tester.TestCfgPrefix: cfg.Prefix,
}
vpr, configFP, err := tester.MakeTempTestConfigClone(t, force)
require.NoError(t, err)
// then test it ctx = config.SetViper(ctx, vpr)
cmd := tester.StubRootCmd(
"repo", "connect", "s3",
"--config-file", configFP,
"--bucket", cfg.Bucket,
"--prefix", cfg.Prefix)
cli.BuildCommandTree(cmd)
// run the command // init the repo first
require.NoError(t, cmd.ExecuteContext(ctx)) _, err = repository.Initialize(ctx, account.Account{}, st, control.Options{})
require.NoError(t, err)
// then test it
cmd := tester.StubRootCmd(
"repo", "connect", "s3",
"--config-file", configFP,
"--bucket", test.bucketPrefix+cfg.Bucket,
"--prefix", cfg.Prefix,
)
cli.BuildCommandTree(cmd)
// run the command
assert.NoError(t, cmd.ExecuteContext(ctx))
})
}
} }
func (suite *S3IntegrationSuite) TestConnectS3Cmd_BadBucket() { func (suite *S3IntegrationSuite) TestConnectS3Cmd_BadBucket() {