func TestRun(t *testing.T) { mb, err := mailbox.Create("test.testrun") if err != nil { t.Fatal(err) } key := mailbox.AccessKey{MailboxId: mb.Id} err = key.Create() if err != nil { t.Fatal(err) } msg, err := mb.PutMessage(`$("test");`) if err != nil { t.Fatal(err) } viper.Set("host", ":5112") viper.Set("mailbox", mb.Id) viper.Set("access_key", key.Secret) viper.Set("access_key_name", key.Name) viper.Set("show_requests", true) go serverCmd.Run(serverCmd, []string{}) runCmd.ParseFlags([]string{"-1", "-d"}) runCmd.Run(runCmd, []string{}) dep, _ := mailbox.FindDeployment(msg.Deployment) resp, _ := dep.GetResponses() if len(resp) != 1 { t.Fatal("No response") } }
func TestAutoCreateDeploy(t *testing.T) { mb, _ := mailbox.Create("put.autocreate.deploy") msg, err := pmClient.Put([]string{mb.Id}, "", "TEST MESSAGE", "blah", "") if err != nil { t.Fatal(err) } if msg.Deployment == "" { t.Fatal("Deployment is empty") } dep, err := mailbox.FindDeployment(msg.Deployment) if err != nil { t.Fatal(err) } if dep.Name != "blah" { t.Fatal("Deployment name not set") } }
// deployRespond is used by scripts to "respond" with information from the // remote system. These responses can then be reported to the admin client that // deployed the original script. func deployRespond(w http.ResponseWriter, r *http.Request) { var request api.ResponseRequest err := readRequest(r, &request) if err != nil { sendError(w, "Could not parse request") return } accessKey, err := mailbox.FindKeyByName(request.AccessKeyName) if accessKey == nil { sendError(w, "Access key is not valid") return } if !request.Validate(accessKey.Secret) { sendError(w, "Could not validate signature") return } msg, err := mailbox.FindMessage(request.Message) if err != nil { sendError(w, err.Error()) return } if msg == nil { sendError(w, "Could not find message "+request.Message) return } mb, err := mailbox.Find(msg.Mailbox) if err != nil { sendError(w, err.Error()) return } if mb == nil { sendError(w, "Mailbox not found") return } dep, err := mailbox.FindDeployment(msg.Deployment) if err != nil { sendError(w, err.Error()) return } if dep == nil { sendError(w, "Could not find deployment "+msg.Deployment) return } if !accessKey.CanGet(mb) { sendError(w, "Not allowed to respond to deploys") return } err = dep.AddResponse(msg.Mailbox, request.Response, request.Error) if err != nil { sendError(w, err.Error()) return } response := api.SimpleResponse{Success: true} response.Sign(accessKey.Name, accessKey.Secret) log.Infof("Reponse added to %s from %s", dep.Id, msg.Mailbox) writeResponse(&w, response) }
// 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) }