diff options
author | Jacob Vosmaer <jacob@gitlab.com> | 2018-05-17 17:16:13 +0300 |
---|---|---|
committer | Jacob Vosmaer <jacob@gitlab.com> | 2018-05-17 17:16:13 +0300 |
commit | 46a14eea1e03adbf82c58434d0292ffafceeb47e (patch) | |
tree | 2b4982c5a0c887c7ca47e9835b6bbf5199e74c9a | |
parent | 733cce24a4aa18df51a16871f427ae9aeb9f8796 (diff) |
Add existince checks to DeleteSite
-rw-r--r-- | internal/service/deploy/deploy.go | 8 | ||||
-rw-r--r-- | internal/service/deploy/deploy_test.go | 20 |
2 files changed, 23 insertions, 5 deletions
diff --git a/internal/service/deploy/deploy.go b/internal/service/deploy/deploy.go index a6db96ae..a34fabd2 100644 --- a/internal/service/deploy/deploy.go +++ b/internal/service/deploy/deploy.go @@ -38,5 +38,13 @@ func (s *server) DeleteSite(ctx context.Context, req *pb.DeleteSiteRequest) (*em } siteDir := path.Join(s.rootDir, req.Path) + st, err := os.Stat(siteDir) + if err != nil { + return nil, status.Errorf(codes.FailedPrecondition, "request.Path: %v", err) + } + if !st.IsDir() { + return nil, status.Errorf(codes.FailedPrecondition, "not a directory: %q", req.Path) + } + return &empty.Empty{}, os.RemoveAll(siteDir) } diff --git a/internal/service/deploy/deploy_test.go b/internal/service/deploy/deploy_test.go index 09609e94..774b060a 100644 --- a/internal/service/deploy/deploy_test.go +++ b/internal/service/deploy/deploy_test.go @@ -22,11 +22,7 @@ const ( ) func TestDeleteSite(t *testing.T) { - sitePath := "foo/bar" - testSiteDir := path.Join(testRootDir, sitePath) - require.NoError(t, os.RemoveAll(testSiteDir)) - require.NoError(t, os.MkdirAll(testSiteDir, 0755)) - + sitePath, testSiteDir := setupTestSite(t) require.NoError(t, ioutil.WriteFile(path.Join(testSiteDir, "hello"), []byte("world"), 0644)) s := runDeployServer(t) @@ -48,7 +44,19 @@ func TestDeleteSite(t *testing.T) { require.NoError(t, err, "parent directory should still exist") } +func setupTestSite(t *testing.T) (sitePath string, testSiteDir string) { + sitePath = "foo/bar" + testSiteDir = path.Join(testRootDir, sitePath) + require.NoError(t, os.RemoveAll(testSiteDir)) + require.NoError(t, os.MkdirAll(testSiteDir, 0755)) + + return sitePath, testSiteDir +} + func TestDeleteSiteFail(t *testing.T) { + sitePath, testSiteDir := setupTestSite(t) + require.NoError(t, ioutil.WriteFile(path.Join(testSiteDir, "hello"), []byte("world"), 0644)) + s := runDeployServer(t) defer s.Stop() @@ -68,6 +76,8 @@ func TestDeleteSiteFail(t *testing.T) { {desc: "traversal middle", path: "bar/../foo", code: codes.InvalidArgument}, {desc: "traversal end", path: "foo/bar/..", code: codes.InvalidArgument}, {desc: "path starting with period", path: ".foo/bar", code: codes.InvalidArgument}, + {desc: "directory does not exist", path: "does/not/exist", code: codes.FailedPrecondition}, + {desc: "path is a file not a directory", path: path.Join(sitePath, "hello"), code: codes.FailedPrecondition}, } for _, tc := range testCases { |