示例#1
0
func RegisterToTestServer() {
	post_url := pubConfig.TSurl + "/resource"

	//Seems there will be lots of coding while getting the system info
	//Using config now.

	file, err := os.Open("./host.conf")
	defer file.Close()
	if err != nil {
		logrus.Info(err)
		return
	}
	buf := bytes.NewBufferString("")
	buf.ReadFrom(file)
	liboct.SendCommand(post_url, buf.Bytes())
}
示例#2
0
func RunTest(casePath string, sAddr string) {
	var caseBundle string
	var caseTar string

	if _, err := os.Stat(TestCache); err != nil {
		os.MkdirAll(TestCache, 0777)
	}
	if p, err := os.Stat(casePath); err != nil {
		logrus.Fatal(err)
	} else if p.IsDir() {
		caseBundle = casePath
	} else {
		caseBundle, _ = ioutil.TempDir(TestCache, "oct-")
		defer os.RemoveAll(caseBundle)
		if strings.HasSuffix(casePath, ".json") {
			copy(path.Join(caseBundle, "case.json"), casePath)
		} else if strings.HasSuffix(casePath, ".tar.gz") {
			caseTar = casePath
			liboct.UntarFile(casePath, caseBundle)
		} else {
			logrus.Fatalf("%s is not a valid test case", casePath)
		}
	}

	//Check if it is a valid test case
	if _, err := liboct.CaseFromBundle(caseBundle); err != nil {
		logrus.Fatal(err)
	}
	if len(caseTar) == 0 {
		caseTar = liboct.TarDir(caseBundle)
		defer os.Remove(caseTar)
	}

	logrus.Debugf("Bundle %s, tar %s, sending to %s", caseBundle, caseTar, sAddr)

	params := make(map[string]string)
	//params["id"] = liboct.MD5(fmt.Sprintf("%d", time.Now().Unix()))

	postURL := fmt.Sprintf("%s/task", sAddr)
	ret := liboct.SendFile(postURL, caseTar, params)
	if ret.Status != liboct.RetStatusOK {
		logrus.Warnf("Failed to apply run task %v", ret)
		return
	} else {
		logrus.Debugf("Success in apply run task %v", ret)
	}

	taskID := ret.Message
	postURL = fmt.Sprintf("%s/task/%s", sAddr, taskID)
	ret = liboct.SendCommand(postURL, []byte("deploy"))
	if ret.Status != liboct.RetStatusOK {
		logrus.Warnf("Failed to deploy task %v", ret)
		return
	} else {
		logrus.Debugf("Success in deploy task %v", ret)
	}

	ret = liboct.SendCommand(postURL, []byte("run"))
	if ret.Status != liboct.RetStatusOK {
		logrus.Warnf("Failed to run task %v", ret)
		return
	} else {
		logrus.Debugf("Success in run task %v", ret)
	}

	ret = liboct.SendCommand(postURL, []byte("collect"))
	if ret.Status != liboct.RetStatusOK {
		logrus.Warnf("Failed to run task %v", ret)
		return
	} else {
		logrus.Debugf("Success in run task %v", ret)
	}

	getURL := fmt.Sprintf("%s/task/%s/report", sAddr, taskID)
	resp, err := http.Get(getURL)
	if err != nil {
		logrus.Warnf("Failed to get report")
		return
	}
	defer resp.Body.Close()
	respBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		logrus.Warnf("The report is empty")
		return
	}

	reportTar := fmt.Sprintf("%s/%s-report.tar.gz", TestCache, taskID)
	ioutil.WriteFile(reportTar, respBody, 0644)
	liboct.UntarFile(reportTar, fmt.Sprintf("%s/%s", TestCache, taskID))
	logrus.Infof("Success in run the test, the report generated here:\n%v", fmt.Sprintf("%s/%s", TestCache, taskID))
	os.Remove(reportTar)
}