-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): new storage config input from file when creating a repo (#…
…2756) * feat(cli): new storage config input from file when creating a repo * feat(cli): allow token from stdin * fix(cli): improve code coverage --------- Co-authored-by: Shikhar Mall <small@kopia.io>
- Loading branch information
Showing
5 changed files
with
113 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cli_test | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/kopia/kopia/repo" | ||
"github.com/kopia/kopia/repo/blob" | ||
"github.com/kopia/kopia/repo/blob/filesystem" | ||
"github.com/kopia/kopia/tests/testenv" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRepositoryCreateWithConfigFile(t *testing.T) { | ||
env := testenv.NewCLITest(t, nil, testenv.NewInProcRunner(t)) | ||
|
||
_, stderr := env.RunAndExpectFailure(t, "repo", "create", "from-config", "--file", path.Join(env.ConfigDir, "does_not_exist.config")) | ||
require.Contains(t, stderr, "can't connect to storage: one of --token-file, --token-stdin or --token must be provided") | ||
|
||
_, stderr = env.RunAndExpectFailure(t, "repo", "connect", "from-config") | ||
require.Contains(t, stderr, "can't connect to storage: one of --file, --token-file, --token-stdin or --token must be provided") | ||
|
||
_, stderr = env.RunAndExpectFailure(t, "repo", "create", "from-config", "--token", "bad-token") | ||
require.Contains(t, stderr, "can't connect to storage: invalid token: unable to decode token") | ||
|
||
storageCfgFName := path.Join(env.ConfigDir, "storage-config.json") | ||
ci := blob.ConnectionInfo{ | ||
Type: "filesystem", | ||
Config: filesystem.Options{Path: env.RepoDir}, | ||
} | ||
token, err := repo.EncodeToken("12345678", ci) | ||
require.Nil(t, err) | ||
|
||
// expect failure before writing to file | ||
_, stderr = env.RunAndExpectFailure(t, "repo", "create", "from-config", "--token-file", storageCfgFName) | ||
require.Contains(t, strings.Join(stderr, "\n"), "can't connect to storage: unable to open token file") | ||
|
||
require.Nil(t, os.WriteFile(storageCfgFName, []byte(token), 0o600)) | ||
|
||
defer os.Remove(storageCfgFName) //nolint:errcheck,gosec | ||
|
||
env.RunAndExpectSuccess(t, "repo", "create", "from-config", "--token-file", storageCfgFName) | ||
} | ||
|
||
func TestRepositoryCreateWithConfigFromStdin(t *testing.T) { | ||
runner := testenv.NewInProcRunner(t) | ||
env := testenv.NewCLITest(t, nil, runner) | ||
|
||
ci := blob.ConnectionInfo{ | ||
Type: "filesystem", | ||
Config: filesystem.Options{Path: env.RepoDir}, | ||
} | ||
token, err := repo.EncodeToken("12345678", ci) | ||
require.Nil(t, err) | ||
|
||
// set stdin | ||
runner.SetNextStdin(strings.NewReader(token)) | ||
|
||
env.RunAndExpectSuccess(t, "repo", "create", "from-config", "--token-stdin") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters