github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/e2e/e2eutil/deployments.go (about)

     1  package e2eutil
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/hashicorp/nomad/testutil"
     8  )
     9  
    10  func WaitForLastDeploymentStatus(jobID, ns, status string, wc *WaitConfig) error {
    11  	var nsArg = []string{}
    12  	if ns != "" {
    13  		nsArg = []string{"-namespace", ns}
    14  	}
    15  
    16  	var got string
    17  	var err error
    18  	interval, retries := wc.OrDefault()
    19  	testutil.WaitForResultRetries(retries, func() (bool, error) {
    20  		time.Sleep(interval)
    21  
    22  		cmd := []string{"nomad", "job", "status"}
    23  		cmd = append(cmd, nsArg...)
    24  		cmd = append(cmd, jobID)
    25  
    26  		out, err := Command(cmd[0], cmd[1:]...)
    27  		if err != nil {
    28  			return false, fmt.Errorf("could not get job status: %v\n%v", err, out)
    29  		}
    30  
    31  		section, err := GetSection(out, "Latest Deployment")
    32  		if err != nil {
    33  			return false, fmt.Errorf("could not find Latest Deployment section: %w", err)
    34  		}
    35  
    36  		fields, err := ParseFields(section)
    37  		if err != nil {
    38  			return false, fmt.Errorf("could not parse Latest Deployment section: %w", err)
    39  		}
    40  
    41  		got = fields["Status"]
    42  		return got == status, nil
    43  	}, func(e error) {
    44  		err = fmt.Errorf("deployment status check failed: got %#v", got)
    45  	})
    46  	return err
    47  }
    48  
    49  func LastDeploymentID(jobID, ns string) (string, error) {
    50  
    51  	var nsArg = []string{}
    52  	if ns != "" {
    53  		nsArg = []string{"-namespace", ns}
    54  	}
    55  
    56  	cmd := []string{"nomad", "deployment", "list"}
    57  	cmd = append(cmd, nsArg...)
    58  
    59  	out, err := Command(cmd[0], cmd[1:]...)
    60  	if err != nil {
    61  		return "", fmt.Errorf("could not get deployment list: %v\n%v", err, out)
    62  	}
    63  	rows, err := ParseColumns(out)
    64  	if err != nil {
    65  		return "", fmt.Errorf("could not parse deployment list output: %w", err)
    66  	}
    67  	for _, row := range rows {
    68  		if row["Job ID"] == jobID {
    69  			return row["ID"], nil
    70  		}
    71  	}
    72  	return "", fmt.Errorf("could not find a recent deployment for job")
    73  }