mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
api(delete) now you get an error trying to delete files a directory (http.StatusForbidden)
This commit is contained in:
parent
215ff2839d
commit
7cebc3999a
@ -131,7 +131,7 @@ func (e Error) Write(w http.ResponseWriter) {
|
||||
switch e.ErrorCode {
|
||||
case EcodeKeyNotFound:
|
||||
status = http.StatusNotFound
|
||||
case EcodeNotFile, EcodeDirNotEmpty:
|
||||
case EcodeNotFile, EcodeDirNotEmpty, EcodeNotDir:
|
||||
status = http.StatusForbidden
|
||||
case EcodeTestFailed, EcodeNodeExist:
|
||||
status = http.StatusPreconditionFailed
|
||||
|
@ -30,6 +30,30 @@ func TestV2DeleteKey(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// Ensures that a key is deleted when 'dir' is not specified or false
|
||||
//
|
||||
// $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
|
||||
// $ curl -X DELETE localhost:4001/v2/keys/foo/bar?dir=true -> fail
|
||||
// $ curl -X DELETE localhost:4001/v2/keys/foo/bar?dir=false
|
||||
//
|
||||
func TestV2DeleteKeyAsDirectory(t *testing.T) {
|
||||
tests.RunServer(func(s *server.Server) {
|
||||
v := url.Values{}
|
||||
v.Set("value", "XXX")
|
||||
resp, err := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
|
||||
tests.ReadBody(resp)
|
||||
resp, err = tests.DeleteForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar?dir=true"), url.Values{})
|
||||
assert.Equal(t, resp.StatusCode, http.StatusForbidden)
|
||||
bodyJson := tests.ReadBodyJSON(resp)
|
||||
assert.Equal(t, bodyJson["errorCode"], 104, "")
|
||||
resp, err = tests.DeleteForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar?dir=false"), url.Values{})
|
||||
assert.Equal(t, resp.StatusCode, http.StatusOK)
|
||||
body := tests.ReadBody(resp)
|
||||
assert.Nil(t, err, "")
|
||||
assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo/bar","modifiedIndex":3,"createdIndex":2},"prevNode":{"key":"/foo/bar","value":"XXX","modifiedIndex":2,"createdIndex":2}}`, "")
|
||||
})
|
||||
}
|
||||
|
||||
// Ensures that an empty directory is deleted when dir is set.
|
||||
//
|
||||
// $ curl -X PUT localhost:4001/v2/keys/foo?dir=true
|
||||
@ -52,7 +76,7 @@ func TestV2DeleteEmptyDirectory(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// Ensures that a not-empty directory is deleted when dir is set.
|
||||
// Ensures that a not-empty directory is deleted when dir and recursive is set.
|
||||
//
|
||||
// $ curl -X PUT localhost:4001/v2/keys/foo/bar?dir=true
|
||||
// $ curl -X DELETE localhost:4001/v2/keys/foo?dir=true ->fail
|
||||
@ -91,6 +115,33 @@ func TestV2DeleteDirectoryRecursiveImpliesDir(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// Ensures that prevIndex / prevValue are invalid for for directory deletion
|
||||
//
|
||||
// $ curl -X PUT localhost:4001/v2/keys/foo?dir=true
|
||||
// $ curl -X DELETE localhost:4001/v2/keys/foo?prevValue=X -> fail
|
||||
// $ curl -X DELETE localhost:4001/v2/keys/foo?prevIndex=0 -> fail
|
||||
// $ curl -X DELETE localhost:4001/v2/keys/foo?dir=true
|
||||
//
|
||||
func TestV2DeleteDirectoryRejectPrevArguments(t *testing.T) {
|
||||
tests.RunServer(func(s *server.Server) {
|
||||
resp, err := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?dir=true"), url.Values{})
|
||||
tests.ReadBody(resp)
|
||||
resp, err = tests.DeleteForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?prevValue=X"), url.Values{})
|
||||
assert.Equal(t, resp.StatusCode, http.StatusForbidden)
|
||||
bodyJson := tests.ReadBodyJSON(resp)
|
||||
assert.Equal(t, bodyJson["errorCode"], 102, "")
|
||||
resp, err = tests.DeleteForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?prevIndex=0"), url.Values{})
|
||||
assert.Equal(t, resp.StatusCode, http.StatusForbidden)
|
||||
bodyJson = tests.ReadBodyJSON(resp)
|
||||
assert.Equal(t, bodyJson["errorCode"], 102, "")
|
||||
resp, err = tests.DeleteForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?dir=true"), url.Values{})
|
||||
assert.Equal(t, resp.StatusCode, http.StatusOK)
|
||||
body := tests.ReadBody(resp)
|
||||
assert.Nil(t, err, "")
|
||||
assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo","dir":true,"modifiedIndex":3,"createdIndex":2},"prevNode":{"key":"/foo","dir":true,"modifiedIndex":2,"createdIndex":2}}`, "")
|
||||
})
|
||||
}
|
||||
|
||||
// Ensures that a key is deleted if the previous index matches
|
||||
//
|
||||
// $ curl -X PUT localhost:4001/v2/keys/foo -d value=XXX
|
||||
|
@ -280,6 +280,10 @@ func (s *store) Delete(nodePath string, dir, recursive bool) (*Event, error) {
|
||||
|
||||
if n.IsDir() {
|
||||
eNode.Dir = true
|
||||
} else {
|
||||
if dir {
|
||||
return nil, etcdErr.NewError(etcdErr.EcodeNotDir, n.Path, s.Index())
|
||||
}
|
||||
}
|
||||
|
||||
callback := func(path string) { // notify function
|
||||
|
Loading…
x
Reference in New Issue
Block a user