Esempio n. 1
0
func TestDeployListByName(t *testing.T) {
	deployment1, err := generateDeployment()
	if err != nil {
		t.Fatal(err)
	}
	deployment1.Deployment.Name = "test"
	deployment1.Deployment.Save()
	generateDeployment()

	if err != nil {
		t.Fatal(err)
	}
	key := mailbox.AccessKey{FullAccess: true}
	key.Create()
	req := api.DeploymentStatsRequest{
		Count:       10,
		NamePattern: "t*t",
	}
	req.Sign(key.Name, key.Secret)

	var resp api.DeploymentStatsResponse
	code := doRequest(t, req, &resp, "deploy/list")
	if code != 200 {
		t.Fatalf("Server repsponded with %d", code)
	}
	if len(resp.Deployments) != 1 {
		t.Fatalf("Deployment length %d != 1", len(resp.Deployments))
	}
}
Esempio n. 2
0
func TestDeployInfoListByToken(t *testing.T) {
	_, err := generateDeployment()
	key := mailbox.AccessKey{FullAccess: true}
	key.Create()
	if err != nil {
		t.Fatal(err)
	}
	dep2, err := generateDeployment()
	if err != nil {
		t.Fatal(err)
	}
	req := api.DeploymentStatsRequest{
		Count:        2,
		TokenPattern: dep2.AccessKey.Name,
	}
	req.Sign(key.Name, key.Secret)
	var resp api.DeploymentStatsResponse
	code := doRequest(t, req, &resp, "deploy/list")
	if code != 200 {
		t.Fatalf("Server repsponded with %d", code)
	}
	if len(resp.Deployments) != 1 {
		t.Fatalf("Deployment count %d!=1", len(resp.Deployments))
	}
}
Esempio n. 3
0
func (client *Client) DeploymentDetail(id string) (*api.DeploymentStatsResponse, error) {
	request := api.DeploymentStatsRequest{Deployment: id}
	request.Sign(client.AccessKeyName, client.AccessKey)
	var response api.DeploymentStatsResponse
	err := client.request("deploy/list", request, &response)
	if !response.Validate(client.AccessKey) {
		return nil, errors.New("Could not validate signature")
	}
	return &response, err
}
Esempio n. 4
0
func TestDeployInfoList(t *testing.T) {
	mb, _ := mailbox.Create("stats.deployinfo")
	mb.PutMessage("test")
	mb.PutMessage("test2")
	key := mailbox.AccessKey{FullAccess: true}
	key.Create()
	req := api.DeploymentStatsRequest{Count: 2}
	req.Sign(key.Name, key.Secret)
	var resp api.DeploymentStatsResponse
	code := doRequest(t, req, &resp, "deploy/list")
	if code != 200 {
		t.Fatalf("Server repsponded with %d", code)
	}
	if len(resp.Deployments) != 2 {
		t.Fatalf("Deployment count %d!=2", len(resp.Deployments))
	}
}
Esempio n. 5
0
func (client *Client) ListDeploys(namePattern string, limitToken bool,
	count int) (*api.DeploymentStatsResponse, error) {
	request := api.DeploymentStatsRequest{
		Count:        int64(count),
		NamePattern:  namePattern,
		TokenPattern: ".*",
	}
	request.Sign(client.AccessKeyName, client.AccessKey)
	if limitToken {
		request.TokenPattern = client.AccessKeyName
	}
	var response api.DeploymentStatsResponse
	err := client.request("deploy/list", request, &response)
	if err != nil {
		return nil, err
	}
	if !response.Validate(client.AccessKey) {
		return nil, errors.New("Could not validate signature")
	}
	return &response, nil
}
Esempio n. 6
0
func (client *Client) PollDeployment(depId string,
	f func(*api.DeploymentStats) bool) (*api.DeploymentStats, error) {
	loop := true
	var response *api.DeploymentStatsResponse
	for loop != false {
		request := api.DeploymentStatsRequest{Deployment: depId}
		request.Sign(client.AccessKeyName, client.AccessKey)
		err := client.request("deploy/list", request, &response)
		if err != nil {
			return nil, err
		}
		if !response.Validate(client.AccessKey) {
			return nil, errors.New("Could not validate signature")
		}
		if len(response.Deployments) == 0 {
			return nil, errors.New("Could not find deployment")
		}
		loop = f(&response.Deployments[0])
		time.Sleep(1 * time.Second)
	}
	return &response.Deployments[0], nil
}
Esempio n. 7
0
// deployInfo is used to both report a list of recent deployments and get
// details and responses on a specific deployment.
func deployInfo(w http.ResponseWriter, r *http.Request) {
	var request api.DeploymentStatsRequest
	err := readRequest(r, &request)
	if err != nil {
		sendError(w, "Could not parse request")
		return
	}

	if request.NamePattern == "" {
		request.NamePattern = ".*"
	}

	if request.TokenPattern == "" {
		request.TokenPattern = ".*"
	}

	accessKey, err := mailbox.FindKeyByName(request.AccessKeyName)
	if err != nil || accessKey == nil {
		sendError(w, "Access key is invalid")
		return
	}
	if !accessKey.CanAdmin() {
		sendError(w, "Not allowed to list deployments")
		return
	}
	if !request.Validate(accessKey.Secret) {
		sendError(w, "Signature invalid")
		return
	}
	response := api.DeploymentStatsResponse{}
	if request.Deployment == "" {
		log.Infof("Listing all deploys for %s", accessKey.Name)
		deployments, err := mailbox.ListDeployments(request.NamePattern,
			int(request.Count), request.TokenPattern)
		if err != nil {
			sendError(w, err.Error())
			return
		}
		for _, d := range deployments {
			dStats, err := d.Stats()
			if err != nil {
				sendError(w, err.Error())
				return
			}
			statsResp := api.DeploymentStats{
				Name:          d.Name,
				Id:            d.Id,
				PendingCount:  dStats.PendingCount,
				MessageCount:  dStats.MessageCount,
				ResponseCount: dStats.ResponseCount,
				CreatedAt:     d.DeployedAt,
				DeployedBy:    d.DeployedBy,
			}
			response.Deployments = append(response.Deployments, statsResp)
		}
	} else {
		dep, err := mailbox.FindDeployment(request.Deployment)
		if err != nil {
			sendError(w, err.Error())
			return
		}

		if dep == nil {
			sendError(w, "Deployment not found")
			return
		}

		dStats, err := dep.Stats()
		if err != nil {
			sendError(w, err.Error())
			return
		}
		deploymentResponses, err := dep.GetResponses()
		if err != nil {
			sendError(w, err.Error())
			return
		}
		statsResp := api.DeploymentStats{
			Name:          dep.Name,
			Id:            dep.Id,
			PendingCount:  dStats.PendingCount,
			MessageCount:  dStats.MessageCount,
			ResponseCount: dStats.ResponseCount,
			CreatedAt:     dep.DeployedAt,
			Responses:     []api.DeploymentResponse{},
		}
		for _, r := range deploymentResponses {
			apiR := api.DeploymentResponse{
				Mailbox:     r.Mailbox,
				Response:    r.Response,
				RespondedAt: r.RespondedAt,
				IsError:     r.IsError,
			}
			statsResp.Responses = append(statsResp.Responses, apiR)
		}
		response.Deployments = append(response.Deployments, statsResp)
	}
	response.Sign(accessKey.Name, accessKey.Secret)
	writeResponse(&w, response)
}