github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/internal/testing/apitests/operator_test.go (about)

     1  package apitests
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/consul/sdk/testutil/retry"
     7  	"github.com/hashicorp/nomad/api"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestAPI_OperatorSchedulerGetSetConfiguration(t *testing.T) {
    12  	t.Parallel()
    13  	require := require.New(t)
    14  	c, s := makeClient(t, nil, nil)
    15  	defer s.Stop()
    16  
    17  	operator := c.Operator()
    18  	var config *api.SchedulerConfigurationResponse
    19  	retry.Run(t, func(r *retry.R) {
    20  		var err error
    21  		config, _, err = operator.SchedulerGetConfiguration(nil)
    22  		r.Check(err)
    23  	})
    24  	require.True(config.SchedulerConfig.PreemptionConfig.SystemSchedulerEnabled)
    25  	require.False(config.SchedulerConfig.PreemptionConfig.BatchSchedulerEnabled)
    26  	require.False(config.SchedulerConfig.PreemptionConfig.ServiceSchedulerEnabled)
    27  
    28  	// Change a config setting
    29  	newConf := &api.SchedulerConfiguration{
    30  		PreemptionConfig: api.PreemptionConfig{
    31  			SystemSchedulerEnabled:  false,
    32  			BatchSchedulerEnabled:   true,
    33  			ServiceSchedulerEnabled: true,
    34  		},
    35  	}
    36  	resp, wm, err := operator.SchedulerSetConfiguration(newConf, nil)
    37  	require.Nil(err)
    38  	require.NotZero(wm.LastIndex)
    39  	// non CAS requests should update on success
    40  	require.True(resp.Updated)
    41  
    42  	config, _, err = operator.SchedulerGetConfiguration(nil)
    43  	require.Nil(err)
    44  	require.False(config.SchedulerConfig.PreemptionConfig.SystemSchedulerEnabled)
    45  	require.True(config.SchedulerConfig.PreemptionConfig.BatchSchedulerEnabled)
    46  	require.True(config.SchedulerConfig.PreemptionConfig.ServiceSchedulerEnabled)
    47  }
    48  
    49  func TestAPI_OperatorSchedulerCASConfiguration(t *testing.T) {
    50  	t.Parallel()
    51  	require := require.New(t)
    52  	c, s := makeClient(t, nil, nil)
    53  	defer s.Stop()
    54  
    55  	operator := c.Operator()
    56  	var config *api.SchedulerConfigurationResponse
    57  	retry.Run(t, func(r *retry.R) {
    58  		var err error
    59  		config, _, err = operator.SchedulerGetConfiguration(nil)
    60  		r.Check(err)
    61  	})
    62  	require.True(config.SchedulerConfig.PreemptionConfig.SystemSchedulerEnabled)
    63  	require.False(config.SchedulerConfig.PreemptionConfig.BatchSchedulerEnabled)
    64  	require.False(config.SchedulerConfig.PreemptionConfig.ServiceSchedulerEnabled)
    65  
    66  	// Pass an invalid ModifyIndex
    67  	{
    68  		newConf := &api.SchedulerConfiguration{
    69  			PreemptionConfig: api.PreemptionConfig{SystemSchedulerEnabled: false, BatchSchedulerEnabled: true},
    70  			ModifyIndex:      config.SchedulerConfig.ModifyIndex - 1,
    71  		}
    72  		resp, wm, err := operator.SchedulerCASConfiguration(newConf, nil)
    73  		require.Nil(err)
    74  		require.NotZero(wm.LastIndex)
    75  		require.False(resp.Updated)
    76  	}
    77  
    78  	// Pass a valid ModifyIndex
    79  	{
    80  		newConf := &api.SchedulerConfiguration{
    81  			PreemptionConfig: api.PreemptionConfig{SystemSchedulerEnabled: false, BatchSchedulerEnabled: true},
    82  			ModifyIndex:      config.SchedulerConfig.ModifyIndex,
    83  		}
    84  		resp, wm, err := operator.SchedulerCASConfiguration(newConf, nil)
    85  		require.Nil(err)
    86  		require.NotZero(wm.LastIndex)
    87  		require.True(resp.Updated)
    88  	}
    89  
    90  	config, _, err := operator.SchedulerGetConfiguration(nil)
    91  	require.Nil(err)
    92  	require.False(config.SchedulerConfig.PreemptionConfig.SystemSchedulerEnabled)
    93  	require.True(config.SchedulerConfig.PreemptionConfig.BatchSchedulerEnabled)
    94  	require.False(config.SchedulerConfig.PreemptionConfig.ServiceSchedulerEnabled)
    95  }