github.com/hernad/nomad@v1.6.112/e2e/e2eutil/deployments.go (about)

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