github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration/service/jobs_test.go (about)

     1  package service
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/Prakhar-Agarwal-byte/moby/api/types"
     7  	swarmtypes "github.com/Prakhar-Agarwal-byte/moby/api/types/swarm"
     8  	"github.com/Prakhar-Agarwal-byte/moby/integration/internal/swarm"
     9  	"gotest.tools/v3/assert"
    10  	"gotest.tools/v3/poll"
    11  	"gotest.tools/v3/skip"
    12  )
    13  
    14  // The file jobs_test.go contains tests that verify that services which are in
    15  // the mode ReplicatedJob or GlobalJob.
    16  
    17  // TestCreateJob tests that a Service can be created and run with
    18  // mode ReplicatedJob
    19  func TestCreateJob(t *testing.T) {
    20  	skip.If(t, testEnv.IsRemoteDaemon)
    21  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
    22  
    23  	ctx := setupTest(t)
    24  
    25  	d := swarm.NewSwarm(ctx, t, testEnv)
    26  	defer d.Stop(t)
    27  
    28  	client := d.NewClientT(t)
    29  	defer client.Close()
    30  
    31  	for _, mode := range []swarmtypes.ServiceMode{
    32  		{ReplicatedJob: &swarmtypes.ReplicatedJob{}},
    33  		{GlobalJob: &swarmtypes.GlobalJob{}},
    34  	} {
    35  		id := swarm.CreateService(ctx, t, d, swarm.ServiceWithMode(mode))
    36  
    37  		poll.WaitOn(t, swarm.RunningTasksCount(ctx, client, id, 1), swarm.ServicePoll)
    38  	}
    39  }
    40  
    41  // TestReplicatedJob tests that running a replicated job starts the requisite
    42  // number of tasks,
    43  func TestReplicatedJob(t *testing.T) {
    44  	skip.If(t, testEnv.IsRemoteDaemon)
    45  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
    46  
    47  	ctx := setupTest(t)
    48  
    49  	// we need variables, because the replicas field takes a pointer
    50  	maxConcurrent := uint64(2)
    51  	// there is overhead, especially in the test environment, associated with
    52  	// starting tasks. if total is set too high, then the time needed to
    53  	// complete the test, even if everything is proceeding ideally, may exceed
    54  	// the time the test has to execute
    55  	//
    56  	// in CI,the test has been seen to time out with as few as 7 completions
    57  	// after 15 seconds. this means 7 completions ought not be too many.
    58  	total := uint64(7)
    59  
    60  	d := swarm.NewSwarm(ctx, t, testEnv)
    61  	defer d.Stop(t)
    62  
    63  	client := d.NewClientT(t)
    64  	defer client.Close()
    65  
    66  	id := swarm.CreateService(ctx, t, d,
    67  		swarm.ServiceWithMode(swarmtypes.ServiceMode{
    68  			ReplicatedJob: &swarmtypes.ReplicatedJob{
    69  				MaxConcurrent:    &maxConcurrent,
    70  				TotalCompletions: &total,
    71  			},
    72  		}),
    73  		// just run a command to execute and exit peacefully.
    74  		swarm.ServiceWithCommand([]string{"true"}),
    75  	)
    76  
    77  	service, _, err := client.ServiceInspectWithRaw(
    78  		ctx, id, types.ServiceInspectOptions{},
    79  	)
    80  	assert.NilError(t, err)
    81  
    82  	poll.WaitOn(t, swarm.JobComplete(ctx, client, service), swarm.ServicePoll)
    83  }
    84  
    85  // TestUpdateJob tests that a job can be updated, and that it runs with the
    86  // correct parameters.
    87  func TestUpdateReplicatedJob(t *testing.T) {
    88  	skip.If(t, testEnv.IsRemoteDaemon)
    89  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
    90  
    91  	ctx := setupTest(t)
    92  
    93  	d := swarm.NewSwarm(ctx, t, testEnv)
    94  	defer d.Stop(t)
    95  
    96  	client := d.NewClientT(t)
    97  	defer client.Close()
    98  
    99  	// Create the job service
   100  	id := swarm.CreateService(ctx, t, d,
   101  		swarm.ServiceWithMode(swarmtypes.ServiceMode{
   102  			ReplicatedJob: &swarmtypes.ReplicatedJob{
   103  				// use the default, empty values.
   104  			},
   105  		}),
   106  		// run "true" so the task exits with 0
   107  		swarm.ServiceWithCommand([]string{"true"}),
   108  	)
   109  
   110  	service, _, err := client.ServiceInspectWithRaw(
   111  		ctx, id, types.ServiceInspectOptions{},
   112  	)
   113  	assert.NilError(t, err)
   114  
   115  	// wait for the job to completed
   116  	poll.WaitOn(t, swarm.JobComplete(ctx, client, service), swarm.ServicePoll)
   117  
   118  	// update the job.
   119  	spec := service.Spec
   120  	spec.TaskTemplate.ForceUpdate++
   121  
   122  	_, err = client.ServiceUpdate(
   123  		ctx, id, service.Version, spec, types.ServiceUpdateOptions{},
   124  	)
   125  	assert.NilError(t, err)
   126  
   127  	service2, _, err := client.ServiceInspectWithRaw(
   128  		ctx, id, types.ServiceInspectOptions{},
   129  	)
   130  	assert.NilError(t, err)
   131  
   132  	// assert that the job iteration has increased
   133  	assert.Assert(t,
   134  		service.JobStatus.JobIteration.Index < service2.JobStatus.JobIteration.Index,
   135  	)
   136  
   137  	// now wait for the service to complete a second time.
   138  	poll.WaitOn(t, swarm.JobComplete(ctx, client, service2), swarm.ServicePoll)
   139  }