2020-09-16 20:10:06 +00:00
|
|
|
package e2eutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
)
|
|
|
|
|
2020-09-28 20:37:34 +00:00
|
|
|
func WaitForLastDeploymentStatus(jobID, ns, status string, wc *WaitConfig) error {
|
|
|
|
var nsArg = []string{}
|
|
|
|
if ns != "" {
|
|
|
|
nsArg = []string{"-namespace", ns}
|
|
|
|
}
|
|
|
|
|
2020-09-16 20:10:06 +00:00
|
|
|
var got string
|
|
|
|
var err error
|
|
|
|
interval, retries := wc.OrDefault()
|
|
|
|
testutil.WaitForResultRetries(retries, func() (bool, error) {
|
|
|
|
time.Sleep(interval)
|
|
|
|
|
2020-09-28 20:37:34 +00:00
|
|
|
cmd := []string{"nomad", "job", "status"}
|
|
|
|
cmd = append(cmd, nsArg...)
|
|
|
|
cmd = append(cmd, jobID)
|
|
|
|
|
|
|
|
out, err := Command(cmd[0], cmd[1:]...)
|
2020-09-16 20:10:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("could not get job status: %v\n%v", err, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
section, err := GetSection(out, "Latest Deployment")
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("could not find Latest Deployment section: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fields, err := ParseFields(section)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("could not parse Latest Deployment section: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got = fields["Status"]
|
|
|
|
return got == status, nil
|
|
|
|
}, func(e error) {
|
|
|
|
err = fmt.Errorf("deployment status check failed: got %#v", got)
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2020-09-29 15:02:16 +00:00
|
|
|
|
|
|
|
func LastDeploymentID(jobID, ns string) (string, error) {
|
|
|
|
|
|
|
|
var nsArg = []string{}
|
|
|
|
if ns != "" {
|
|
|
|
nsArg = []string{"-namespace", ns}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := []string{"nomad", "deployment", "list"}
|
|
|
|
cmd = append(cmd, nsArg...)
|
|
|
|
|
|
|
|
out, err := Command(cmd[0], cmd[1:]...)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not get deployment list: %v\n%v", err, out)
|
|
|
|
}
|
|
|
|
rows, err := ParseColumns(out)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not parse deployment list output: %w", err)
|
|
|
|
}
|
|
|
|
for _, row := range rows {
|
|
|
|
if row["Job ID"] == jobID {
|
|
|
|
return row["ID"], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("could not find a recent deployment for job")
|
|
|
|
}
|