Skip to content

Commit

Permalink
Merge pull request #648 from fairDataSociety/podSnapshot.0
Browse files Browse the repository at this point in the history
pod snapshot restapi added
  • Loading branch information
asabya authored Oct 22, 2024
2 parents 8ddae3b + bfd9680 commit bce6a42
Show file tree
Hide file tree
Showing 8 changed files with 754 additions and 28 deletions.
1 change: 1 addition & 0 deletions cmd/dfs/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ func startHttpService(logger logging.Logger) *http.Server {
router.HandleFunc("/public-file", handler.PublicPodGetFileHandler)
router.HandleFunc("/public-dir", handler.PublicPodGetDirHandler)
router.HandleFunc("/public-kv", handler.PublicPodKVEntryGetHandler)
router.HandleFunc("/public-pod-snapshot", handler.PodReceiveSnapshotHandler).Methods("GET")

redirectHandler := func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, r.URL.Path+"/", http.StatusMovedPermanently)
Expand Down
307 changes: 306 additions & 1 deletion cmd/dfs/cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"testing"
"time"

"github.com/fairdatasociety/fairOS-dfs/pkg/pod"

"github.com/fairdatasociety/fairOS-dfs/pkg/acl/acl"
"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -60,7 +62,7 @@ func TestApis(t *testing.T) {
mockClient := bee.NewBeeClient(beeUrl, mock.BatchOkStr, true, 0, logger)
ens := mock2.NewMockNamespaceManager()

users := user.NewUsers(mockClient, ens, 500, 0, logger)
users := user.NewUsers(mockClient, ens, -1, 0, logger)
dfsApi := dfs.NewMockDfsAPI(mockClient, users, logger)
handler = api.NewMockHandler(dfsApi, logger, []string{"http://localhost:3000"})
defer handler.Close()
Expand Down Expand Up @@ -1084,6 +1086,309 @@ func TestApis(t *testing.T) {
}
})

t.Run("signup-login-pod-dir-file-snapshot", func(t *testing.T) {
c := http.Client{Timeout: time.Duration(1) * time.Minute}
userRequest := &common.UserSignupRequest{
UserName: randStringRunes(16),
Password: randStringRunes(12),
}

userTwoRequest := &common.UserSignupRequest{
UserName: randStringRunes(16),
Password: randStringRunes(12),
}

userBytes, err := json.Marshal(userRequest)
if err != nil {
t.Fatal(err)
}

userTwoBytes, err := json.Marshal(userTwoRequest)
if err != nil {
t.Fatal(err)
}

cookies := [][]string{}

for _, user := range [][]byte{userBytes, userTwoBytes} {
signupRequestDataHttpReq, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s%s", basev2, string(common.UserSignup)), bytes.NewBuffer(user))
if err != nil {
t.Fatal(err)
}
signupRequestDataHttpReq.Header.Add("Content-Type", "application/json")
signupRequestDataHttpReq.Header.Add("Content-Length", strconv.Itoa(len(user)))
signupRequestResp, err := c.Do(signupRequestDataHttpReq)
if err != nil {
t.Fatal(err)
}

err = signupRequestResp.Body.Close()
if err != nil {
t.Fatal(err)
}
if signupRequestResp.StatusCode != http.StatusCreated {
t.Fatal("Signup failed", signupRequestResp.StatusCode)
}

userLoginHttpReq, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s%s", basev2, string(common.UserLogin)), bytes.NewBuffer(user))
if err != nil {
t.Fatal(err)

}
userLoginHttpReq.Header.Add("Content-Type", "application/json")
userLoginHttpReq.Header.Add("Content-Length", strconv.Itoa(len(user)))
userLoginResp, err := c.Do(userLoginHttpReq)
if err != nil {
t.Fatal(err)
}
err = userLoginResp.Body.Close()
if err != nil {
t.Fatal(err)
}
if userLoginResp.StatusCode != http.StatusOK {
t.Fatal("user should be able to login")
}
cookie := userLoginResp.Header["Set-Cookie"]
cookies = append(cookies, cookie)
}

// pod new
podRequest := &common.PodRequest{
PodName: randStringRunes(16),
}
podBytes, err := json.Marshal(podRequest)
if err != nil {
t.Fatal(err)
}
podNewHttpReq, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s%s", basev1, string(common.PodNew)), bytes.NewBuffer(podBytes))
if err != nil {
t.Fatal(err)
}
podNewHttpReq.Header.Set("Cookie", cookies[0][0])
podNewHttpReq.Header.Add("Content-Type", "application/json")
podNewHttpReq.Header.Add("Content-Length", strconv.Itoa(len(podBytes)))
podNewResp, err := c.Do(podNewHttpReq)
if err != nil {
t.Fatal(err)
}

err = podNewResp.Body.Close()
if err != nil {
t.Fatal(err)
}
if podNewResp.StatusCode != 201 {
t.Fatal("pod creation failed")
}

entries := []struct {
path string
isDir bool
size int64
content []byte
}{
{
path: "/dir1",
isDir: true,
},
{
path: "/dir2",
isDir: true,
},
{
path: "/dir3",
isDir: true,
},
{
path: "/file1",
size: 1024 * 1024,
},
{
path: "/dir1/file11",
size: 1024 * 512,
},
{
path: "/dir1/file12",
size: 1024 * 1024,
},
{
path: "/dir3/file31",
size: 1024 * 1024,
},
{
path: "/dir3/file32",
size: 1024 * 1024,
},
{
path: "/dir3/file33",
size: 1024,
},
{
path: "/dir2/dir4",
isDir: true,
},
{
path: "/dir2/dir4/dir5",
isDir: true,
},
{
path: "/dir2/dir4/file241",
size: 5 * 1024 * 1024,
},
{
path: "/dir2/dir4/dir5/file2451",
size: 10 * 1024 * 1024,
},
}

for _, v := range entries {
if v.isDir {
mkdirRqst := common.FileSystemRequest{
PodName: podRequest.PodName,
DirectoryPath: v.path,
}
mkDirBytes, err := json.Marshal(mkdirRqst)
if err != nil {
t.Fatal(err)
}
mkDirHttpReq, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s%s", basev1, string(common.DirMkdir)), bytes.NewBuffer(mkDirBytes))
if err != nil {
t.Fatal(err)

}
mkDirHttpReq.Header.Set("Cookie", cookies[0][0])
mkDirHttpReq.Header.Add("Content-Type", "application/json")
mkDirHttpReq.Header.Add("Content-Length", strconv.Itoa(len(mkDirBytes)))
mkDirResp, err := c.Do(mkDirHttpReq)
if err != nil {
t.Fatal(err)
}
err = mkDirResp.Body.Close()
if err != nil {
t.Fatal(err)
}
if mkDirResp.StatusCode != 201 {
t.Fatal("mkdir failed")
}
} else {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
contentLength := fmt.Sprintf("%d", v.size)

err = writer.WriteField("podName", podRequest.PodName)
if err != nil {
t.Fatal(err)
}
err = writer.WriteField("contentLength", contentLength)
if err != nil {
t.Fatal(err)
}
err = writer.WriteField("dirPath", filepath.Dir(v.path))
if err != nil {
t.Fatal(err)
}
err = writer.WriteField("blockSize", "1Mb")
if err != nil {
t.Fatal(err)
}
part, err := writer.CreateFormFile("files", filepath.Base(v.path))
if err != nil {
t.Fatal(err)
}
reader := &io.LimitedReader{R: rand.Reader, N: v.size}
_, err = io.Copy(part, reader)
if err != nil {
t.Fatal(err)
}

err = writer.Close()
if err != nil {
t.Fatal(err)
}

uploadReq, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s%s", basev1, string(common.FileUpload)), body)
if err != nil {
t.Fatal(err)

}
uploadReq.Header.Set("Cookie", cookies[0][0])
contentType := fmt.Sprintf("multipart/form-data;boundary=%v", writer.Boundary())
uploadReq.Header.Add("Content-Type", contentType)
uploadResp, err := c.Do(uploadReq)
if err != nil {
t.Fatal(err)
}
err = uploadResp.Body.Close()
if err != nil {
t.Fatal(err)
}
if uploadResp.StatusCode != 200 {
t.Fatal("upload failed")
}
}
}
<-time.After(time.Second * 2)
podShareRequest := &common.PodShareRequest{
PodName: podRequest.PodName,
SharedPodName: "sharedPod",
}
podShareBytes, err := json.Marshal(podShareRequest)
if err != nil {
t.Fatal(err)
}
podShareHttpReq, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s%s", basev1, "/pod/share"), bytes.NewBuffer(podShareBytes))
if err != nil {
t.Fatal(err)
}
podShareHttpReq.Header.Set("Cookie", cookies[0][0])
podShareHttpReq.Header.Add("Content-Type", "application/json")
podShareHttpReq.Header.Add("Content-Length", strconv.Itoa(len(podShareBytes)))
podShareResp, err := c.Do(podShareHttpReq)
if err != nil {
t.Fatal(err)
}

podShareData := &api.PodSharingReference{}
podShareRespBytes, err := io.ReadAll(podShareResp.Body)
if err != nil {
t.Fatal(err)
}

err = json.Unmarshal(podShareRespBytes, podShareData)
if err != nil {
t.Fatal(err)
}
err = podShareResp.Body.Close()
if err != nil {
t.Fatal(err)
}
if podShareResp.StatusCode != 200 {
t.Fatal("pod share failed")
}
podSnapHttpReq, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s%s%s", "http://localhost:9090", "/public-pod-snapshot?sharingRef=", podShareData.Reference), http.NoBody)
if err != nil {
t.Fatal(err)
}
podSnapHttpReq.Header.Add("Content-Type", "application/json")
podSnapResp, err := c.Do(podSnapHttpReq)
if err != nil {
t.Fatal(err)
}

data, err := io.ReadAll(podSnapResp.Body)
if err != nil {
t.Fatal(err)
}
snap := &pod.DirSnapShot{}
err = json.Unmarshal(data, snap)
if err != nil {
t.Fatal(err)
}
err = podSnapResp.Body.Close()
if err != nil {
t.Fatal(err)
}
})

t.Run("group-test", func(t *testing.T) {
c := http.Client{Timeout: time.Duration(1) * time.Minute}
userRequest := &common.UserSignupRequest{
Expand Down
Loading

0 comments on commit bce6a42

Please sign in to comment.