github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/e2e/deployment/deployment.go (about)

     1  package deployment
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/nomad/e2e/framework"
     7  	"github.com/hashicorp/nomad/nomad/structs"
     8  	"github.com/hashicorp/nomad/testutil"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/hashicorp/nomad/e2e/e2eutil"
    12  	"github.com/hashicorp/nomad/helper/uuid"
    13  )
    14  
    15  type DeploymentTest struct {
    16  	framework.TC
    17  	jobIds []string
    18  }
    19  
    20  func init() {
    21  	framework.AddSuites(&framework.TestSuite{
    22  		Component:   "Deployment",
    23  		CanRunLocal: true,
    24  		Cases: []framework.TestCase{
    25  			new(DeploymentTest),
    26  		},
    27  	})
    28  }
    29  
    30  func (tc *DeploymentTest) BeforeAll(f *framework.F) {
    31  	// Ensure cluster has leader before running tests
    32  	e2eutil.WaitForLeader(f.T(), tc.Nomad())
    33  	e2eutil.WaitForNodesReady(f.T(), tc.Nomad(), 4)
    34  }
    35  
    36  func (tc *DeploymentTest) TestDeploymentAutoPromote(f *framework.F) {
    37  	t := f.T()
    38  	nomadClient := tc.Nomad()
    39  	run := structs.DeploymentStatusRunning
    40  	uuid := uuid.Generate()
    41  	// unique each run, cluster could have previous jobs
    42  	jobId := "deployment" + uuid[0:8]
    43  	tc.jobIds = append(tc.jobIds, jobId)
    44  	e2eutil.RegisterAndWaitForAllocs(t, nomadClient, "deployment/input/deployment_auto0.nomad", jobId, "")
    45  	ds := e2eutil.DeploymentsForJob(t, nomadClient, jobId)
    46  	require.Equal(t, 1, len(ds))
    47  	deploy := ds[0]
    48  
    49  	// Upgrade
    50  	e2eutil.RegisterAllocs(t, nomadClient, "deployment/input/deployment_auto1.nomad", jobId, "")
    51  
    52  	// Find the deployment we don't already have
    53  	testutil.WaitForResult(func() (bool, error) {
    54  		ds = e2eutil.DeploymentsForJob(t, nomadClient, jobId)
    55  		for _, d := range ds {
    56  			if d.ID != deploy.ID {
    57  				deploy = d
    58  				return true, nil
    59  			}
    60  		}
    61  		return false, fmt.Errorf("missing update deployment for job %s", jobId)
    62  	}, func(e error) {
    63  		require.NoError(t, e)
    64  	})
    65  
    66  	// Deployment is auto pending the upgrade of "two" which has a longer time to health
    67  	e2eutil.WaitForDeployment(t, nomadClient, deploy.ID, run, structs.DeploymentStatusDescriptionRunningAutoPromotion)
    68  
    69  	// Deployment is eventually running
    70  	e2eutil.WaitForDeployment(t, nomadClient, deploy.ID, run, structs.DeploymentStatusDescriptionRunning)
    71  
    72  	deploy, _, _ = nomadClient.Deployments().Info(deploy.ID, nil)
    73  	require.Equal(t, run, deploy.Status)
    74  	require.Equal(t, structs.DeploymentStatusDescriptionRunning, deploy.StatusDescription)
    75  }
    76  
    77  func (tc *DeploymentTest) AfterEach(f *framework.F) {
    78  	nomadClient := tc.Nomad()
    79  	jobs := nomadClient.Jobs()
    80  	// Stop all jobs in test
    81  	for _, id := range tc.jobIds {
    82  		jobs.Deregister(id, true, nil)
    83  	}
    84  	tc.jobIds = []string{}
    85  	// Garbage collect
    86  	nomadClient.System().GarbageCollect()
    87  }