github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/allocrunner/taskrunner/getter/error_test.go (about)

     1  package getter
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/nomad/structs"
     8  	"github.com/shoenig/test/must"
     9  )
    10  
    11  func TestError_Error(t *testing.T) {
    12  	cases := []struct {
    13  		name string
    14  		err  *Error
    15  		exp  string
    16  	}{
    17  		{"object nil", nil, "<nil>"},
    18  		{"error nil", new(Error), "<nil>"},
    19  		{"has error", &Error{Err: errors.New("oops")}, "oops"},
    20  	}
    21  
    22  	for _, tc := range cases {
    23  		t.Run(tc.name, func(t *testing.T) {
    24  			e := Error{Err: tc.err}
    25  			result := e.Error()
    26  			must.Eq(t, tc.exp, result)
    27  		})
    28  	}
    29  }
    30  
    31  func TestError_IsRecoverable(t *testing.T) {
    32  	var _ structs.Recoverable = (*Error)(nil)
    33  	must.True(t, (&Error{Recoverable: true}).IsRecoverable())
    34  	must.False(t, (&Error{Recoverable: false}).IsRecoverable())
    35  }
    36  
    37  func TestError_Equal(t *testing.T) {
    38  	cases := []struct {
    39  		name string
    40  		a    *Error
    41  		b    *Error
    42  		exp  bool
    43  	}{
    44  		{name: "both nil", a: nil, b: nil, exp: true},
    45  		{name: "one nil", a: new(Error), b: nil, exp: false},
    46  		{
    47  			name: "different url",
    48  			a:    &Error{URL: "example.com/a"},
    49  			b:    &Error{URL: "example.com/b"},
    50  			exp:  false,
    51  		},
    52  		{
    53  			name: "different err",
    54  			a:    &Error{URL: "example.com/z", Err: errors.New("b")},
    55  			b:    &Error{URL: "example.com/z", Err: errors.New("a")},
    56  			exp:  false,
    57  		},
    58  		{
    59  			name: "nil vs not nil err",
    60  			a:    &Error{URL: "example.com/z", Err: errors.New("b")},
    61  			b:    &Error{URL: "example.com/z", Err: nil},
    62  			exp:  false,
    63  		},
    64  		{
    65  			name: "different recoverable",
    66  			a:    &Error{URL: "example.com", Err: errors.New("a"), Recoverable: false},
    67  			b:    &Error{URL: "example.com", Err: errors.New("b"), Recoverable: true},
    68  			exp:  false,
    69  		},
    70  		{
    71  			name: "same no error",
    72  			a:    &Error{URL: "example.com", Err: nil, Recoverable: true},
    73  			b:    &Error{URL: "example.com", Err: nil, Recoverable: true},
    74  			exp:  true,
    75  		},
    76  		{
    77  			name: "same with error",
    78  			a:    &Error{URL: "example.com", Err: errors.New("a"), Recoverable: true},
    79  			b:    &Error{URL: "example.com", Err: errors.New("a"), Recoverable: true},
    80  			exp:  true,
    81  		},
    82  	}
    83  
    84  	for _, tc := range cases {
    85  		t.Run(tc.name, func(t *testing.T) {
    86  			result := tc.a.Equal(tc.b)
    87  			must.Eq(t, tc.exp, result)
    88  		})
    89  	}
    90  }