示例#1
0
func ReceiveTask(w http.ResponseWriter, r *http.Request) {
	db := liboct.GetDefaultDB()
	realURL, params := liboct.ReceiveFile(w, r, OCTDCacheDir)

	logrus.Debugf("ReceiveTask %v", realURL)

	if id, ok := params["id"]; ok {
		if strings.HasSuffix(realURL, ".tar.gz") {
			liboct.UntarFile(realURL, strings.TrimSuffix(realURL, ".tar.gz"))
		}
		var task liboct.TestTask
		task.ID = id
		task.BundleURL = realURL
		if name, ok := params["name"]; ok {
			task.Name = name
		} else {
			task.Name = id
			logrus.Warnf("Cannot find the name of the task.")
		}
		db.Update(liboct.DBTask, id, task)

		liboct.RenderOK(w, "", nil)
	} else {
		liboct.RenderErrorf(w, fmt.Sprintf("Cannot find the task id: %d", id))
	}
}
示例#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)
}