diff options
author | James Liu <jliu@gitlab.com> | 2023-10-16 08:18:26 +0300 |
---|---|---|
committer | James Liu <jliu@gitlab.com> | 2023-10-20 06:05:25 +0300 |
commit | a0daebb8248a28c463501b311b98404d3e87c763 (patch) | |
tree | e2bd1a274dbef76b0df82524a2a829965c2d6606 | |
parent | da0e1ee67e706eb33e22aadc0a87f8b728342303 (diff) |
test: Use table tests in `gitaly-backup create`
The majority of setup, assertion, and teardown logic for the two tests
for the `gitaly-backup create` command are shared. Refactor the two
separate test functions into a single function utilising table tests,
since we'll be adding additional tests soon.
-rw-r--r-- | cmd/gitaly-backup/create_test.go | 185 |
1 files changed, 84 insertions, 101 deletions
diff --git a/cmd/gitaly-backup/create_test.go b/cmd/gitaly-backup/create_test.go index b38bf24ca..8b319943b 100644 --- a/cmd/gitaly-backup/create_test.go +++ b/cmd/gitaly-backup/create_test.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "context" "encoding/json" "flag" "io" @@ -21,108 +22,90 @@ import ( ) func TestCreateSubcommand(t *testing.T) { - cfg := testcfg.Build(t) - - cfg.SocketPath = testserver.RunGitalyServer(t, cfg, setup.RegisterAll) - - ctx := testhelper.Context(t) - path := testhelper.TempDir(t) - - var repos []*gitalypb.Repository - for i := 0; i < 5; i++ { - repo, repoPath := gittest.CreateRepository(t, ctx, cfg) - gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch(git.DefaultBranch)) - repos = append(repos, repo) + tests := []struct { + Name string + Flags func(backupRoot string) []string + ServerOpts func(ctx context.Context, backupRoot string) []testserver.GitalyServerOpt + ExpectedErrMessage string + }{ + { + Name: "when a local backup is created", + Flags: func(backupRoot string) []string { + return []string{"-path", backupRoot, "-id", "the-new-backup"} + }, + ServerOpts: func(ctx context.Context, backupRoot string) []testserver.GitalyServerOpt { + return nil + }, + ExpectedErrMessage: "create: pipeline: 1 failures encountered:\n - invalid: manager: could not dial source: invalid connection string: \"invalid\"\n", + }, + { + Name: "when a server-side backup is created", + Flags: func(path string) []string { + return []string{"-server-side", "-id", "the-new-backup"} + }, + ServerOpts: func(ctx context.Context, backupRoot string) []testserver.GitalyServerOpt { + backupSink, err := backup.ResolveSink(ctx, backupRoot) + require.NoError(t, err) + + backupLocator, err := backup.ResolveLocator("pointer", backupSink) + require.NoError(t, err) + + return []testserver.GitalyServerOpt{ + testserver.WithBackupSink(backupSink), + testserver.WithBackupLocator(backupLocator), + } + }, + ExpectedErrMessage: "create: pipeline: 1 failures encountered:\n - invalid: server-side create: could not dial source: invalid connection string: \"invalid\"\n", + }, } - var stdin bytes.Buffer - - encoder := json.NewEncoder(&stdin) - for _, repo := range repos { - require.NoError(t, encoder.Encode(map[string]string{ - "address": cfg.SocketPath, - "token": cfg.Auth.Token, - "storage_name": repo.StorageName, - "relative_path": repo.RelativePath, - "gl_project_path": repo.GlProjectPath, - })) - } - - require.NoError(t, encoder.Encode(map[string]string{ - "address": "invalid", - "token": "invalid", - "relative_path": "invalid", - })) - - cmd := createSubcommand{backupPath: path} - - fs := flag.NewFlagSet("create", flag.ContinueOnError) - cmd.Flags(fs) - - require.NoError(t, fs.Parse([]string{"-path", path, "-id", "the-new-backup"})) - require.EqualError(t, - cmd.Run(ctx, testhelper.SharedLogger(t), &stdin, io.Discard), - "create: pipeline: 1 failures encountered:\n - invalid: manager: could not dial source: invalid connection string: \"invalid\"\n") - - for _, repo := range repos { - bundlePath := filepath.Join(path, strings.TrimSuffix(repo.RelativePath, ".git"), "the-new-backup", "001.bundle") - require.FileExists(t, bundlePath) - } -} - -func TestCreateSubcommand_serverSide(t *testing.T) { - ctx := testhelper.Context(t) - - backupRoot := testhelper.TempDir(t) - backupSink, err := backup.ResolveSink(ctx, backupRoot) - require.NoError(t, err) - - backupLocator, err := backup.ResolveLocator("pointer", backupSink) - require.NoError(t, err) - - cfg := testcfg.Build(t) - cfg.SocketPath = testserver.RunGitalyServer(t, cfg, setup.RegisterAll, - testserver.WithBackupSink(backupSink), - testserver.WithBackupLocator(backupLocator), - ) - - var repos []*gitalypb.Repository - for i := 0; i < 5; i++ { - repo, repoPath := gittest.CreateRepository(t, ctx, cfg) - gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch(git.DefaultBranch)) - repos = append(repos, repo) - } - - var stdin bytes.Buffer - - encoder := json.NewEncoder(&stdin) - for _, repo := range repos { - require.NoError(t, encoder.Encode(map[string]string{ - "address": cfg.SocketPath, - "token": cfg.Auth.Token, - "storage_name": repo.StorageName, - "relative_path": repo.RelativePath, - "gl_project_path": repo.GlProjectPath, - })) - } - - require.NoError(t, encoder.Encode(map[string]string{ - "address": "invalid", - "token": "invalid", - "relative_path": "invalid", - })) - - cmd := createSubcommand{} - fs := flag.NewFlagSet("create", flag.ContinueOnError) - cmd.Flags(fs) - - require.NoError(t, fs.Parse([]string{"-server-side", "-id", "the-new-backup"})) - require.EqualError(t, - cmd.Run(ctx, testhelper.SharedLogger(t), &stdin, io.Discard), - "create: pipeline: 1 failures encountered:\n - invalid: server-side create: could not dial source: invalid connection string: \"invalid\"\n") - - for _, repo := range repos { - bundlePath := filepath.Join(backupRoot, strings.TrimSuffix(repo.RelativePath, ".git"), "the-new-backup", "001.bundle") - require.FileExists(t, bundlePath) + for _, tc := range tests { + t.Run(tc.Name, func(t *testing.T) { + ctx := testhelper.Context(t) + path := testhelper.TempDir(t) + + cfg := testcfg.Build(t) + cfg.SocketPath = testserver.RunGitalyServer(t, cfg, setup.RegisterAll, tc.ServerOpts(ctx, path)...) + + var repos []*gitalypb.Repository + for i := 0; i < 5; i++ { + repo, repoPath := gittest.CreateRepository(t, ctx, cfg) + gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch(git.DefaultBranch)) + repos = append(repos, repo) + } + + var stdin bytes.Buffer + encoder := json.NewEncoder(&stdin) + + for _, repo := range repos { + require.NoError(t, encoder.Encode(map[string]string{ + "address": cfg.SocketPath, + "token": cfg.Auth.Token, + "storage_name": repo.StorageName, + "relative_path": repo.RelativePath, + "gl_project_path": repo.GlProjectPath, + })) + } + + require.NoError(t, encoder.Encode(map[string]string{ + "address": "invalid", + "token": "invalid", + "relative_path": "invalid", + })) + + cmd := createSubcommand{} + fs := flag.NewFlagSet("create", flag.ContinueOnError) + cmd.Flags(fs) + require.NoError(t, fs.Parse(tc.Flags(path))) + + require.EqualError(t, + cmd.Run(ctx, testhelper.SharedLogger(t), &stdin, io.Discard), + tc.ExpectedErrMessage) + + for _, repo := range repos { + bundlePath := filepath.Join(path, strings.TrimSuffix(repo.RelativePath, ".git"), "the-new-backup", "001.bundle") + require.FileExists(t, bundlePath) + } + }) } } |