Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
bugfix: Use a local file instead of a remote file as the url to be do…
Browse files Browse the repository at this point in the history
…wnloaded

Signed-off-by: Starnop <[email protected]>
  • Loading branch information
starnop committed Jul 30, 2019
1 parent 5e82b2b commit 8d2e165
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 12 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ unit-test: build-dirs
./hack/unit-test.sh
.PHONY: unit-test

# TODO: output the log file when the test is failed
integration-test:
@go test ./test
.PHONY: integration-test
Expand Down
71 changes: 62 additions & 9 deletions test/cli_dfget_p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package main

import (
"fmt"
"io/ioutil"
"os"
. "path/filepath"
"time"

Expand Down Expand Up @@ -49,14 +51,65 @@ func (s *DFGetP2PTestSuite) TearDownSuite(c *check.C) {
s.starter.Clean()
}

func (s *DFGetP2PTestSuite) TestDownload(c *check.C) {
cmd, err := s.starter.DFGet(5*time.Second,
"-u", "https://lowzj.com",
"-o", Join(s.starter.Home, "a.test"),
"--node", fmt.Sprintf("127.0.0.1:%d", environment.SupernodeListenPort),
"--notbs")
cmd.Wait()
func (s *DFGetP2PTestSuite) TestDownloadFile(c *check.C) {
var cases = []struct {
filePath string
fileContent []byte
targetPath string
createFile bool
execSuccess bool
timeout time.Duration
}{
{
filePath: "normal.txt",
fileContent: []byte("hello Dragonfly"),
targetPath: Join(s.starter.Home, "normal.test"),
createFile: true,
execSuccess: true,
timeout: 5,
},
{
filePath: "empty.txt",
fileContent: []byte(""),
targetPath: Join(s.starter.Home, "empty.test"),
createFile: true,
execSuccess: true,
timeout: 5,
},
{
filePath: "notExist.txt",
fileContent: []byte(""),
targetPath: Join(s.starter.Home, "notExist.test"),
createFile: false,
execSuccess: false,
timeout: 5,
},
}

for _, ca := range cases {
if ca.createFile {
s.starter.WriteSupernodeFileServer(ca.filePath, ca.fileContent, os.ModePerm)
}
cmd, err := s.starter.DFGet(ca.timeout*time.Second,
"-u", fmt.Sprintf("http://127.0.0.1:%d/%s", environment.SupernodeDownloadPort, ca.filePath),
"-o", ca.targetPath,
"--node", fmt.Sprintf("127.0.0.1:%d", environment.SupernodeListenPort),
"--notbs")
cmd.Wait()

c.Assert(err, check.IsNil)
c.Assert(cmd.ProcessState.Success(), check.Equals, true)
c.Assert(err, check.IsNil)
execResult := cmd.ProcessState.Success()
if ca.execSuccess {
c.Assert(execResult, check.Equals, true)

if execResult {
// check the downloaded file content
data, err := ioutil.ReadFile(ca.targetPath)
c.Assert(err, check.IsNil)
c.Assert(data, check.DeepEquals, ca.fileContent)
}
} else {
c.Assert(execResult, check.Equals, false)
}
}
}
16 changes: 13 additions & 3 deletions test/command/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ type Starter struct {
// is killed.
fileSrv map[*exec.Cmd]*http.Server

// supernodeFileServerHome is the home dir of supernode file server.
supernodeFileServerHome string

lock sync.Mutex
}

Expand All @@ -130,6 +133,7 @@ func (s *Starter) getCmdGo(dir string, running time.Duration, args ...string) (c
"--home-dir=" + dir,
"--port=" + strconv.Itoa(environment.SupernodeListenPort),
"--advertise-ip=127.0.0.1",
"--download-port=" + strconv.Itoa(environment.SupernodeDownloadPort),
"--debug",
}, args...)

Expand All @@ -149,13 +153,19 @@ func (s *Starter) Supernode(running time.Duration, args ...string) (
s.Kill(cmd)
return nil, err
}
if _, err = s.fileServer(cmd, fp.Join(dir, "repo")); err != nil {
s.supernodeFileServerHome = fp.Join(dir, "repo")
if _, err = s.fileServer(cmd, s.supernodeFileServerHome, environment.SupernodeDownloadPort); err != nil {
s.Kill(cmd)
return nil, err
}
return cmd, err
}

// WriteSupernodeFileServer write a file to the supernode file server.
func (s *Starter) WriteSupernodeFileServer(filePath string, data []byte, perm os.FileMode) error {
return ioutil.WriteFile(fp.Join(s.supernodeFileServerHome, filePath), data, perm)
}

// DFDaemon starts dfdaemon.
func (s *Starter) DFDaemon(running time.Duration, port int) (*exec.Cmd, error) {
return s.execCmd(running, dfdaemonPath,
Expand Down Expand Up @@ -253,9 +263,9 @@ func (s *Starter) addCmd(cmd *exec.Cmd) {
s.listMap[cmd] = el
}

func (s *Starter) fileServer(cmd *exec.Cmd, root string) (*http.Server, error) {
func (s *Starter) fileServer(cmd *exec.Cmd, root string, port int) (*http.Server, error) {
server := &http.Server{
Addr: ":8001",
Addr: ":" + strconv.Itoa(port),
Handler: http.FileServer(http.Dir(root)),
}
err := make(chan error)
Expand Down
3 changes: 3 additions & 0 deletions test/environment/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ var (
// SupernodeListenPort is the port that supernode will listen.
SupernodeListenPort = 8008

// SupernodeDownloadPort is the port that supernode will listen.
SupernodeDownloadPort = 8009

// DragonflySupernodeBinary is default binary
DragonflySupernodeBinary = "/usr/local/bin/supernode"

Expand Down

0 comments on commit 8d2e165

Please sign in to comment.