diff options
author | Zeger-Jan van de Weg <git@zjvandeweg.nl> | 2019-09-02 10:38:43 +0300 |
---|---|---|
committer | Zeger-Jan van de Weg <git@zjvandeweg.nl> | 2019-09-02 10:38:43 +0300 |
commit | 41789fd1cb1387d188bf9765d50340bf3667d754 (patch) | |
tree | b2ff0f47901cb133ac3caf4eb90c5bf9bdfe9f61 | |
parent | 9453022b5e91029e958890d0ac17aa3de14f1207 (diff) | |
parent | 392a1282e13489451c5277316bcca1c80a953474 (diff) |
Merge branch 'jv-refactor-namespace-tests' into 'master'
NamespaceService: test filesystem outcomes
See merge request gitlab-org/gitaly!1465
-rw-r--r-- | internal/service/namespace/.gitignore | 1 | ||||
-rw-r--r-- | internal/service/namespace/namespace_test.go | 134 |
2 files changed, 87 insertions, 48 deletions
diff --git a/internal/service/namespace/.gitignore b/internal/service/namespace/.gitignore new file mode 100644 index 000000000..d383c56ff --- /dev/null +++ b/internal/service/namespace/.gitignore @@ -0,0 +1 @@ +testdata diff --git a/internal/service/namespace/namespace_test.go b/internal/service/namespace/namespace_test.go index f21dc0174..5f110c5c0 100644 --- a/internal/service/namespace/namespace_test.go +++ b/internal/service/namespace/namespace_test.go @@ -2,8 +2,9 @@ package namespace import ( "context" - "io/ioutil" + "log" "os" + "path/filepath" "testing" "github.com/stretchr/testify/require" @@ -19,15 +20,22 @@ func TestMain(m *testing.M) { } func testMain(m *testing.M) int { - storageOtherDir, _ := ioutil.TempDir("", "gitaly-repository-exists-test") - defer os.Remove(storageOtherDir) + config.Config.Storages = nil - oldStorages := config.Config.Storages - config.Config.Storages = []config.Storage{ - {Name: "default", Path: testhelper.GitlabTestStoragePath()}, - {Name: "other", Path: storageOtherDir}, + for _, st := range []string{"default", "other"} { + dir, err := filepath.Abs(filepath.Join("testdata", st)) + if err != nil { + log.Fatal(err) + } + + if err := os.RemoveAll(dir); err != nil { + log.Fatal(err) + } + + config.Config.Storages = append(config.Config.Storages, + config.Storage{Name: st, Path: dir}, + ) } - defer func() { config.Config.Storages = oldStorages }() return m.Run() } @@ -43,9 +51,13 @@ func TestNamespaceExists(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - _, err := client.AddNamespace(ctx, &gitalypb.AddNamespaceRequest{StorageName: "default", Name: "existing"}) - require.NoError(t, err, "Namespace creation failed") - defer client.RemoveNamespace(ctx, &gitalypb.RemoveNamespaceRequest{StorageName: "default", Name: "existing"}) + const ( + existingStorage = "default" + existingNamespace = "existing" + ) + + storageDir := prepareStorageDir(t, existingStorage) + require.NoError(t, os.MkdirAll(filepath.Join(storageDir, existingNamespace), 0755)) queries := []struct { desc string @@ -56,7 +68,7 @@ func TestNamespaceExists(t *testing.T) { { desc: "empty name", request: &gitalypb.NamespaceExistsRequest{ - StorageName: "default", + StorageName: existingStorage, Name: "", }, errorCode: codes.InvalidArgument, @@ -64,7 +76,7 @@ func TestNamespaceExists(t *testing.T) { { desc: "Namespace doesn't exists", request: &gitalypb.NamespaceExistsRequest{ - StorageName: "default", + StorageName: existingStorage, Name: "not-existing", }, errorCode: codes.OK, @@ -74,7 +86,7 @@ func TestNamespaceExists(t *testing.T) { desc: "Wrong storage path", request: &gitalypb.NamespaceExistsRequest{ StorageName: "other", - Name: "existing", + Name: existingNamespace, }, errorCode: codes.OK, exists: false, @@ -82,8 +94,8 @@ func TestNamespaceExists(t *testing.T) { { desc: "Namespace exists", request: &gitalypb.NamespaceExistsRequest{ - StorageName: "default", - Name: "existing", + StorageName: existingStorage, + Name: existingNamespace, }, errorCode: codes.OK, exists: true, @@ -92,8 +104,6 @@ func TestNamespaceExists(t *testing.T) { for _, tc := range queries { t.Run(tc.desc, func(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() response, err := client.NamespaceExists(ctx, tc.request) require.Equal(t, tc.errorCode, helper.GrpcCode(err)) @@ -105,6 +115,14 @@ func TestNamespaceExists(t *testing.T) { } } +func prepareStorageDir(t *testing.T, storageName string) string { + storageDir, err := helper.GetStorageByName(storageName) + require.NoError(t, err) + require.NoError(t, os.RemoveAll(storageDir)) + require.NoError(t, os.MkdirAll(storageDir, 0755)) + return storageDir +} + func TestAddNamespace(t *testing.T) { server, serverSocketPath := runNamespaceServer(t) defer server.Stop() @@ -112,6 +130,9 @@ func TestAddNamespace(t *testing.T) { client, conn := newNamespaceClient(t, serverSocketPath) defer conn.Close() + const existingStorage = "default" + storageDir := prepareStorageDir(t, existingStorage) + queries := []struct { desc string request *gitalypb.AddNamespaceRequest @@ -120,7 +141,7 @@ func TestAddNamespace(t *testing.T) { { desc: "No name", request: &gitalypb.AddNamespaceRequest{ - StorageName: "default", + StorageName: existingStorage, Name: "", }, errorCode: codes.InvalidArgument, @@ -128,7 +149,7 @@ func TestAddNamespace(t *testing.T) { { desc: "Namespace is successfully created", request: &gitalypb.AddNamespaceRequest{ - StorageName: "default", + StorageName: existingStorage, Name: "create-me", }, errorCode: codes.OK, @@ -136,7 +157,7 @@ func TestAddNamespace(t *testing.T) { { desc: "Idempotent on creation", request: &gitalypb.AddNamespaceRequest{ - StorageName: "default", + StorageName: existingStorage, Name: "create-me", }, errorCode: codes.OK, @@ -162,10 +183,9 @@ func TestAddNamespace(t *testing.T) { // Clean up if tc.errorCode == codes.OK { - client.RemoveNamespace(ctx, &gitalypb.RemoveNamespaceRequest{ - StorageName: tc.request.StorageName, - Name: tc.request.Name, - }) + require.Equal(t, existingStorage, tc.request.StorageName, "sanity check") + + requireIsDir(t, filepath.Join(storageDir, tc.request.Name)) } }) } @@ -181,6 +201,13 @@ func TestRemoveNamespace(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() + const ( + existingStorage = "default" + existingNamespace = "created" + ) + + storageDir := prepareStorageDir(t, existingStorage) + queries := []struct { desc string request *gitalypb.RemoveNamespaceRequest @@ -189,15 +216,15 @@ func TestRemoveNamespace(t *testing.T) { { desc: "Namespace is successfully removed", request: &gitalypb.RemoveNamespaceRequest{ - StorageName: "default", - Name: "created", + StorageName: existingStorage, + Name: existingNamespace, }, errorCode: codes.OK, }, { desc: "Idempotent on deletion", request: &gitalypb.RemoveNamespaceRequest{ - StorageName: "default", + StorageName: existingStorage, Name: "not-there", }, errorCode: codes.OK, @@ -214,11 +241,15 @@ func TestRemoveNamespace(t *testing.T) { for _, tc := range queries { t.Run(tc.desc, func(t *testing.T) { - _, err := client.AddNamespace(ctx, &gitalypb.AddNamespaceRequest{StorageName: "default", Name: "created"}) - require.NoError(t, err, "setup failed") + require.NoError(t, os.MkdirAll(filepath.Join(storageDir, existingNamespace), 0755), "test setup") - _, err = client.RemoveNamespace(ctx, tc.request) + _, err := client.RemoveNamespace(ctx, tc.request) require.Equal(t, tc.errorCode, helper.GrpcCode(err)) + + if tc.errorCode == codes.OK { + require.Equal(t, existingStorage, tc.request.StorageName, "sanity check") + testhelper.AssertFileNotExists(t, filepath.Join(storageDir, tc.request.Name)) + } }) } } @@ -233,6 +264,14 @@ func TestRenameNamespace(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() + const ( + existingStorage = "default" + existingNamespace = "existing" + ) + + storageDir := prepareStorageDir(t, existingStorage) + require.NoError(t, os.MkdirAll(filepath.Join(storageDir, existingNamespace), 0755)) + queries := []struct { desc string request *gitalypb.RenameNamespaceRequest @@ -241,9 +280,9 @@ func TestRenameNamespace(t *testing.T) { { desc: "Renaming an existing namespace", request: &gitalypb.RenameNamespaceRequest{ - From: "existing", + From: existingNamespace, To: "new-path", - StorageName: "default", + StorageName: existingStorage, }, errorCode: codes.OK, }, @@ -252,7 +291,7 @@ func TestRenameNamespace(t *testing.T) { request: &gitalypb.RenameNamespaceRequest{ From: "", To: "new-path", - StorageName: "default", + StorageName: existingStorage, }, errorCode: codes.InvalidArgument, }, @@ -261,27 +300,21 @@ func TestRenameNamespace(t *testing.T) { request: &gitalypb.RenameNamespaceRequest{ From: "non-existing", To: "new-path", - StorageName: "default", + StorageName: existingStorage, }, errorCode: codes.InvalidArgument, }, { desc: "existing destination namespace", request: &gitalypb.RenameNamespaceRequest{ - From: "existing", - To: "existing", - StorageName: "default", + From: existingNamespace, + To: existingNamespace, + StorageName: existingStorage, }, errorCode: codes.InvalidArgument, }, } - _, err := client.AddNamespace(ctx, &gitalypb.AddNamespaceRequest{ - StorageName: "default", - Name: "existing", - }) - require.NoError(t, err) - for _, tc := range queries { t.Run(tc.desc, func(t *testing.T) { _, err := client.RenameNamespace(ctx, tc.request) @@ -289,11 +322,16 @@ func TestRenameNamespace(t *testing.T) { require.Equal(t, tc.errorCode, helper.GrpcCode(err)) if tc.errorCode == codes.OK { - client.RemoveNamespace(ctx, &gitalypb.RemoveNamespaceRequest{ - StorageName: "default", - Name: "new-path", - }) + toDir := filepath.Join(storageDir, tc.request.To) + requireIsDir(t, toDir) + require.NoError(t, os.RemoveAll(toDir)) } }) } } + +func requireIsDir(t *testing.T, dir string) { + fi, err := os.Stat(dir) + require.NoError(t, err) + require.True(t, fi.IsDir(), "%v is directory", dir) +} |