diff --git a/.gitignore b/.gitignore index 0f3e46e..11c9a06 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ _tmp _bin strelets.bin boyar.bin -e2e.test \ No newline at end of file +e2e.test +agent/download/ \ No newline at end of file diff --git a/agent/agent.go b/agent/agent.go index daa87aa..3291f98 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -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") @@ -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") diff --git a/agent/agent_test.go b/agent/agent_test.go new file mode 100644 index 0000000..93cbfd1 --- /dev/null +++ b/agent/agent_test.go @@ -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") + } + +}