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

View File

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