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

     1  package parameterized
     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 ParameterizedTest struct {
    14  	framework.TC
    15  	jobIDs []string
    16  }
    17  
    18  func init() {
    19  	framework.AddSuites(&framework.TestSuite{
    20  		Component:   "Parameterized",
    21  		CanRunLocal: true,
    22  		Cases: []framework.TestCase{
    23  			new(ParameterizedTest),
    24  		},
    25  	})
    26  }
    27  
    28  func (tc *ParameterizedTest) BeforeAll(f *framework.F) {
    29  	e2eutil.WaitForLeader(f.T(), tc.Nomad())
    30  }
    31  
    32  func (tc *ParameterizedTest) 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 *ParameterizedTest) TestParameterizedDispatch_Basic(f *framework.F) {
    44  	t := f.T()
    45  
    46  	uuid := uuid.Generate()
    47  	jobID := fmt.Sprintf("dispatch-%s", uuid[0:8])
    48  	tc.jobIDs = append(tc.jobIDs, jobID)
    49  
    50  	// register job
    51  	require.NoError(t, e2eutil.Register(jobID, "parameterized/input/simple.nomad"))
    52  
    53  	// force dispatch
    54  	dispatched := 4
    55  
    56  	for i := 0; i < dispatched; i++ {
    57  		require.NoError(t, e2eutil.Dispatch(jobID, map[string]string{"i": fmt.Sprintf("%v", i)}, ""))
    58  	}
    59  
    60  	testutil.WaitForResult(func() (bool, error) {
    61  		children, err := e2eutil.DispatchedJobs(jobID)
    62  		if err != nil {
    63  			return false, err
    64  		}
    65  
    66  		dead := 0
    67  		for _, c := range children {
    68  			if c["Status"] != "dead" {
    69  				return false, fmt.Errorf("expected periodic job to be dead")
    70  			}
    71  			dead++
    72  		}
    73  
    74  		if dead != dispatched {
    75  			return false, fmt.Errorf("expected %d but found %d children", dispatched, dead)
    76  		}
    77  
    78  		return true, nil
    79  	}, func(err error) {
    80  		require.NoError(t, err)
    81  	})
    82  
    83  	// Assert there are no pending children
    84  	summary, err := e2eutil.ChildrenJobSummary(jobID)
    85  	require.NoError(t, err)
    86  	require.Len(t, summary, 1)
    87  	require.Equal(t, summary[0]["Pending"], "0")
    88  	require.Equal(t, summary[0]["Running"], "0")
    89  	require.Equal(t, summary[0]["Dead"], fmt.Sprintf("%v", dispatched))
    90  }