github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/api/types/container/hostconfig_test.go (about)

     1  package container
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/Prakhar-Agarwal-byte/moby/errdefs"
     7  	"gotest.tools/v3/assert"
     8  	is "gotest.tools/v3/assert/cmp"
     9  )
    10  
    11  func TestValidateRestartPolicy(t *testing.T) {
    12  	tests := []struct {
    13  		name        string
    14  		input       RestartPolicy
    15  		expectedErr string
    16  	}{
    17  		{
    18  			name:  "empty",
    19  			input: RestartPolicy{},
    20  		},
    21  		{
    22  			name:        "empty with invalid MaxRestartCount (for backward compatibility)",
    23  			input:       RestartPolicy{MaximumRetryCount: 123},
    24  			expectedErr: "", // Allowed for backward compatibility
    25  		},
    26  		{
    27  			name:        "empty with negative MaxRestartCount)",
    28  			input:       RestartPolicy{MaximumRetryCount: -123},
    29  			expectedErr: "", // Allowed for backward compatibility
    30  		},
    31  		{
    32  			name:  "always",
    33  			input: RestartPolicy{Name: RestartPolicyAlways},
    34  		},
    35  		{
    36  			name:        "always with MaxRestartCount",
    37  			input:       RestartPolicy{Name: RestartPolicyAlways, MaximumRetryCount: 123},
    38  			expectedErr: "invalid restart policy: maximum retry count can only be used with 'on-failure'",
    39  		},
    40  		{
    41  			name:        "always with negative MaxRestartCount",
    42  			input:       RestartPolicy{Name: RestartPolicyAlways, MaximumRetryCount: -123},
    43  			expectedErr: "invalid restart policy: maximum retry count can only be used with 'on-failure' and cannot be negative",
    44  		},
    45  		{
    46  			name:  "unless-stopped",
    47  			input: RestartPolicy{Name: RestartPolicyUnlessStopped},
    48  		},
    49  		{
    50  			name:        "unless-stopped with MaxRestartCount",
    51  			input:       RestartPolicy{Name: RestartPolicyUnlessStopped, MaximumRetryCount: 123},
    52  			expectedErr: "invalid restart policy: maximum retry count can only be used with 'on-failure'",
    53  		},
    54  		{
    55  			name:        "unless-stopped with negative MaxRestartCount",
    56  			input:       RestartPolicy{Name: RestartPolicyUnlessStopped, MaximumRetryCount: -123},
    57  			expectedErr: "invalid restart policy: maximum retry count can only be used with 'on-failure' and cannot be negative",
    58  		},
    59  		{
    60  			name:  "disabled",
    61  			input: RestartPolicy{Name: RestartPolicyDisabled},
    62  		},
    63  		{
    64  			name:        "disabled with MaxRestartCount",
    65  			input:       RestartPolicy{Name: RestartPolicyDisabled, MaximumRetryCount: 123},
    66  			expectedErr: "invalid restart policy: maximum retry count can only be used with 'on-failure'",
    67  		},
    68  		{
    69  			name:        "disabled with negative MaxRestartCount",
    70  			input:       RestartPolicy{Name: RestartPolicyDisabled, MaximumRetryCount: -123},
    71  			expectedErr: "invalid restart policy: maximum retry count can only be used with 'on-failure' and cannot be negative",
    72  		},
    73  		{
    74  			name:  "on-failure",
    75  			input: RestartPolicy{Name: RestartPolicyOnFailure},
    76  		},
    77  		{
    78  			name:  "on-failure with MaxRestartCount",
    79  			input: RestartPolicy{Name: RestartPolicyOnFailure, MaximumRetryCount: 123},
    80  		},
    81  		{
    82  			name:        "on-failure with negative MaxRestartCount",
    83  			input:       RestartPolicy{Name: RestartPolicyOnFailure, MaximumRetryCount: -123},
    84  			expectedErr: "invalid restart policy: maximum retry count cannot be negative",
    85  		},
    86  		{
    87  			name:        "unknown policy",
    88  			input:       RestartPolicy{Name: "unknown"},
    89  			expectedErr: "invalid restart policy: unknown policy 'unknown'; use one of 'no', 'always', 'on-failure', or 'unless-stopped'",
    90  		},
    91  	}
    92  
    93  	for _, tc := range tests {
    94  		tc := tc
    95  		t.Run(tc.name, func(t *testing.T) {
    96  			err := ValidateRestartPolicy(tc.input)
    97  			if tc.expectedErr == "" {
    98  				assert.Check(t, err)
    99  			} else {
   100  				assert.Check(t, is.ErrorType(err, errdefs.IsInvalidParameter))
   101  				assert.Check(t, is.Error(err, tc.expectedErr))
   102  			}
   103  		})
   104  	}
   105  }