github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/e2e/periodic/periodic.go (about)

     1  package periodic
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/nomad/e2e/e2eutil"
     7  	"github.com/hashicorp/nomad/e2e/framework"
     8  	"github.com/hashicorp/nomad/helper/uuid"
     9  	"github.com/hashicorp/nomad/testutil"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  type PeriodicTest struct {
    14  	framework.TC
    15  	jobIDs []string
    16  }
    17  
    18  func init() {
    19  	framework.AddSuites(&framework.TestSuite{
    20  		Component:   "Periodic",
    21  		CanRunLocal: true,
    22  		Cases: []framework.TestCase{
    23  			new(PeriodicTest),
    24  		},
    25  	})
    26  }
    27  
    28  func (tc *PeriodicTest) BeforeAll(f *framework.F) {
    29  	e2eutil.WaitForLeader(f.T(), tc.Nomad())
    30  }
    31  
    32  func (tc *PeriodicTest) AfterEach(f *framework.F) {
    33  	nomadClient := tc.Nomad()
    34  	j := nomadClient.Jobs()
    35  
    36  	for _, id := range tc.jobIDs {
    37  		j.Deregister(id, true, nil)
    38  	}
    39  	_, err := e2eutil.Command("nomad", "system", "gc")
    40  	f.NoError(err)
    41  }
    42  
    43  func (tc *PeriodicTest) TestPeriodicDispatch_Basic(f *framework.F) {
    44  	t := f.T()
    45  
    46  	uuid := uuid.Generate()
    47  	jobID := fmt.Sprintf("periodicjob-%s", uuid[0:8])
    48  	tc.jobIDs = append(tc.jobIDs, jobID)
    49  
    50  	// register job
    51  	require.NoError(t, e2eutil.Register(jobID, "periodic/input/simple.nomad"))
    52  
    53  	// force dispatch
    54  	require.NoError(t, e2eutil.PeriodicForce(jobID))
    55  
    56  	testutil.WaitForResult(func() (bool, error) {
    57  		children, err := e2eutil.PreviouslyLaunched(jobID)
    58  		if err != nil {
    59  			return false, err
    60  		}
    61  
    62  		for _, c := range children {
    63  			if c["Status"] == "dead" {
    64  				return true, nil
    65  			}
    66  		}
    67  		return false, fmt.Errorf("expected periodic job to be dead")
    68  	}, func(err error) {
    69  		require.NoError(t, err)
    70  	})
    71  
    72  	// Assert there are no pending children
    73  	summary, err := e2eutil.ChildrenJobSummary(jobID)
    74  	require.NoError(t, err)
    75  	require.Len(t, summary, 1)
    76  	require.Equal(t, summary[0]["Pending"], "0")
    77  	require.Equal(t, summary[0]["Dead"], "1")
    78  }