Beispiel #1
0
func (suite *GraphTestSuite) BenchmarkWrite(t *C) {
	var err error
	var info graph.NodeInfo

	info.ParentId = graph.RootNodeId
	info.Mode = os.FileMode(0755)
	info.Type = "application/json"
	for i := 0; err == nil && i < t.N; i++ {
		info.Name = fmt.Sprint(i)
		_, err = suite.ng.NewNodeWithNodeInfo(info)
	}

	if err != nil {
		panic(err)
	}
}
Beispiel #2
0
func (suite *ApiTestSuite) TestUpdateNode_movesFileSuccessfully(t *C) {
	folder, err := suite.ng.CreateDirectory(suite.ng.RootNode.Id, "folder 1")
	t.Check(err, IsNil)

	nodeInfo := graph.NodeInfo{
		Name: "file.txt",
		Mode: 0755,
	}
	id, err := suite.createNodeWithSize(suite.ng.RootNode.Id, nodeInfo, 1024)
	t.Check(err, IsNil)

	nodeInfo.ParentId = folder.Id

	req := suite.request(api.UpdateNode.Build(id), encode(nodeInfo))
	resp, err := suite.client.Do(req)
	t.Check(err, IsNil)
	t.Check(msg(resp), Equals, "")

	t.Check(folder.Children(), HasLen, 1)
	t.Check(folder.Children()[0].Name(), Equals, "file.txt")
}
Beispiel #3
0
func (suite *ApiTestSuite) TestUpdateNode_ignoresNewSize(t *C) {
	ni := graph.NodeInfo{
		Name: "abc.txt",
		Mode: 0755,
	}

	id, err := suite.createNode(suite.ng.RootNode.Id, ni)
	t.Check(err, IsNil)

	ni.Size = 1024

	endpoint := api.UpdateNode.Build(id)
	req := suite.request(endpoint, encode(ni))

	resp, err := suite.client.Do(req)
	t.Check(err, IsNil)
	t.Check(resp.StatusCode, Equals, http.StatusOK)

	changedNode := suite.ng.NodeWithId(id)
	t.Check(changedNode.Size(), Equals, int64(0))
}
Beispiel #4
0
func (suite *ApiTestSuite) TestUpdateNode_updatesNode(t *C) {
	ni := graph.NodeInfo{
		Name: "thing.txt",
		Mode: 0755,
	}

	id, err := suite.createNode(suite.ng.RootNode.Id, ni)
	t.Check(err, IsNil)

	ni.Name = "abcd.ghi"
	ni.Mode = 0700

	endpoint := api.UpdateNode.Build(id)
	req := suite.request(endpoint, encode(ni))

	resp, err := suite.client.Do(req)
	t.Check(err, IsNil)
	t.Check(resp.StatusCode, Equals, http.StatusOK)

	changedNode := suite.ng.RootNode.Children()[0]
	t.Check(changedNode.Name(), Equals, ni.Name)
	t.Check(changedNode.Mode(), Equals, ni.Mode)
	t.Check(changedNode.Size(), Equals, ni.Size)
}
Beispiel #5
0
// POST v1/node/{parentId}
// body -> {nodeInfo}
// returns -> {nodeInfo}
func (restApi OlympusApi) CreateNode(writer http.ResponseWriter, req *http.Request) {
	parent := restApi.graph.NodeWithId(paramFromRequest("parentId", req))
	if !parent.Exists() {
		writeNodeNotFoundError(parent.Id, writer)
		return
	}

	var nodeInfo graph.NodeInfo
	defer req.Body.Close()
	decoder := decoderFromHeader(req.Body, req.Header)

	if err := decoder.Decode(&nodeInfo); err != nil {
		writer.WriteHeader(http.StatusBadRequest)
		writer.Write([]byte(err.Error()))
		return
	} else if node := restApi.graph.NodeWithName(parent.Id, nodeInfo.Name); node != nil && node.Exists() {
		http.Error(writer, fmt.Sprintf("Node exists, call /v1/touch/%s/ to update this object", node.Id),
			http.StatusBadRequest)
	} else {
		nodeInfo.ParentId = parent.Id
		nodeInfo.MTime = time.Now()

		if nodeInfo.Type == "" {
			nodeInfo.Type = util.MimeType(nodeInfo.Name)
		}

		if newNode, err := restApi.graph.NewNodeWithNodeInfo(nodeInfo); err != nil {
			http.Error(writer, err.Error(), http.StatusBadRequest)
		} else {
			encoder := encoderFromHeader(writer, req.Header)
			writer.WriteHeader(http.StatusCreated)
			nodeInfo = newNode.NodeInfo()
			encoder.Encode(nodeInfo)
		}
	}
}