Skip to content

Commit

Permalink
handle logs
Browse files Browse the repository at this point in the history
  • Loading branch information
uv-orbs committed Apr 10, 2022
1 parent 0c44f0e commit 50a4fb2
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ _tmp
_bin
strelets.bin
boyar.bin
e2e.test
e2e.test
agent/download/
21 changes: 18 additions & 3 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,22 @@ func isNewContent(hashPath string, body []byte) bool {
/////////////////////////////////////////////////////////////
// write as it downloads and not load the whole file into memory.
func DownloadFile(targetPath, url, hashPath string) (string, error) {
client := http.Client{
Timeout: 5 * time.Second,
}

// Get the data
resp, err := http.Get(url)
resp, err := client.Get(url)
//resp, err := http.Get(url) //might take too long - no timeout

if err != nil {
return "", err
}
if resp.StatusCode != 200 {
stat := fmt.Sprintf("status: %d", resp.StatusCode)
return "", errors.New(stat)
}

logger.Info("response status: " + resp.Status)
if resp.ContentLength == 0 {
return "", errors.New("conten size is ZERO")
Expand All @@ -121,8 +132,12 @@ func DownloadFile(targetPath, url, hashPath string) (string, error) {
defer resp.Body.Close()

// read body
body := bytes.NewBuffer(make([]byte, 0, resp.ContentLength))
_, err = io.Copy(body, resp.Body)
body := new(bytes.Buffer)
body.ReadFrom(resp.Body)
// return buf.Len()

// body := bytes.NewBuffer(make([]byte, 0, resp.ContentLength))
// _, err = io.Copy(body, resp.Body)

if !isNewContent(hashPath, body.Bytes()) {
return "", errors.New("file content is not new")
Expand Down
84 changes: 84 additions & 0 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package agent

import (
"os"
"testing"

"github.com/orbs-network/scribe/log"
)

func Test_BoyarAgentConfigSingleton(t *testing.T) {
// init agent config
url := "http://localhost:8080/node/0xTEST/main.sh"

// init agent config
config := Config{
IntervalMinute: 1,
Url: url,
}
Init(&config)

agent1 := GetInstance()

// get same instance
agent2 := GetInstance()
if agent1.config.Url != agent2.config.Url {
t.Error("config url in two instances is not equal")
}
if agent1.config.IntervalMinute != agent2.config.IntervalMinute {
t.Error("config IntervalMinute in two instances is not equal")
}
}

func Test_BoyarAgentDownloadErr(t *testing.T) {
url := "http://www.notfound.com/main.sh"

dlPath := getDownloadPath()
targetPath := getTargetPath(dlPath)
res, err := DownloadFile(targetPath, url, dlPath)

if res != "" {
t.Errorf("res for url[%s] should be nil", res)
}
if err == nil {
t.Errorf("err for url[%s] should not be nil", res)
}

if err.Error() != "status: 404" {
t.Errorf("expected [status: 404] got[%s]", err.Error())
}
}

func Test_BoyarAgentDownloadOK(t *testing.T) {
logger = log.GetLogger()

url := "https://deployment.orbs.network/boyar_agent/node/0x9f0988Cd37f14dfe95d44cf21f9987526d6147Ba/main.sh"

dlPath := getDownloadPath()
targetPath := getTargetPath(dlPath)

// delete hash file so content will be new
hashFile := dlPath + "/last_hash.txt"
err := os.Remove(hashFile)
if err != nil {
t.Errorf("remove [%s] failed", hashFile)
}

// download
res, err := DownloadFile(targetPath, url, dlPath)

if res == "" {
t.Errorf("res for url[%s] is empty", url)
}
if err != nil {
t.Errorf("err for url[%s] should not be nil %s", url, err.Error())
}

// download again - expect content not new
res, err = DownloadFile(targetPath, url, dlPath)

if err.Error() != "file content is not new" {
t.Errorf("file content should have been the same")
}

}

0 comments on commit 50a4fb2

Please sign in to comment.