...

Source file src/github.com/Microsoft/hcsshim/internal/hcs/errors_test.go

Documentation: github.com/Microsoft/hcsshim/internal/hcs

     1  //go:build windows
     2  
     3  package hcs
     4  
     5  import (
     6  	"errors"
     7  	"fmt"
     8  	"net"
     9  	"testing"
    10  )
    11  
    12  type MyError struct {
    13  	S string
    14  }
    15  
    16  func (e *MyError) Error() string {
    17  	return fmt.Sprintf("error happened: %s", e.S)
    18  }
    19  
    20  func TestHcsErrorUnwrap(t *testing.T) {
    21  	err := &MyError{"test test"}
    22  	herr := HcsError{
    23  		Op:  t.Name(),
    24  		Err: err,
    25  	}
    26  
    27  	for _, nerr := range []net.Error{
    28  		&herr,
    29  		&SystemError{
    30  			ID:       t.Name(),
    31  			HcsError: herr,
    32  		},
    33  		&ProcessError{
    34  			SystemID: t.Name(),
    35  			HcsError: herr,
    36  		},
    37  	} {
    38  		t.Run(fmt.Sprintf("%T", nerr), func(t *testing.T) {
    39  			if !errors.Is(nerr, err) {
    40  				t.Errorf("error '%v' did not unwrap to %v", nerr, err)
    41  			}
    42  
    43  			var e *MyError
    44  			if !(errors.As(nerr, &e) && e.S == err.S) {
    45  				t.Errorf("error '%v' did not unwrap '%v' properly", errors.Unwrap(nerr), e)
    46  			}
    47  
    48  			if nerr.Timeout() {
    49  				t.Errorf("expected .Timeout() on '%v' to be false", nerr)
    50  			}
    51  
    52  			//nolint:staticcheck // Temporary() is deprecated
    53  			if nerr.Temporary() {
    54  				t.Errorf("expected .Temporary() on '%v' to be false", nerr)
    55  			}
    56  		})
    57  	}
    58  }
    59  
    60  func TestHcsErrorUnwrapTimeout(t *testing.T) {
    61  	err := fmt.Errorf("error: %w", ErrTimeout)
    62  	herr := HcsError{
    63  		Op:  "test",
    64  		Err: err,
    65  	}
    66  
    67  	for _, nerr := range []net.Error{
    68  		&herr,
    69  		&SystemError{
    70  			ID:       t.Name(),
    71  			HcsError: herr,
    72  		},
    73  		&ProcessError{
    74  			SystemID: t.Name(),
    75  			HcsError: herr,
    76  		},
    77  	} {
    78  		t.Run(fmt.Sprintf("%T", nerr), func(t *testing.T) {
    79  			if !errors.Is(nerr, ErrTimeout) {
    80  				t.Errorf("error '%v' did not unwrap to %v", nerr, ErrTimeout)
    81  			}
    82  
    83  			if !errors.Is(nerr, err) {
    84  				t.Errorf("error '%v' did not unwrap to %v", nerr, err)
    85  			}
    86  
    87  			if !IsTimeout(nerr) {
    88  				t.Errorf("expected error '%v' to be timeout", nerr)
    89  			}
    90  
    91  			if nerr.Timeout() {
    92  				t.Errorf("expected .Timeout() on '%v' to be false", nerr)
    93  			}
    94  
    95  			//nolint:staticcheck // Temporary() is deprecated
    96  			if nerr.Temporary() {
    97  				t.Errorf("expected .Temporary() on '%v' to be false", nerr)
    98  			}
    99  		})
   100  	}
   101  }
   102  
   103  var errNet = netError{}
   104  
   105  type netError struct{}
   106  
   107  func (e netError) Error() string   { return "temporary timeout" }
   108  func (e netError) Timeout() bool   { return true }
   109  func (e netError) Temporary() bool { return true }
   110  
   111  func TestHcsErrorUnwrapNet(t *testing.T) {
   112  	err := fmt.Errorf("error: %w", errNet)
   113  	herr := HcsError{
   114  		Op:  "test",
   115  		Err: err,
   116  	}
   117  
   118  	for _, nerr := range []net.Error{
   119  		&herr,
   120  		&SystemError{
   121  			ID:       t.Name(),
   122  			HcsError: herr,
   123  		},
   124  		&ProcessError{
   125  			SystemID: t.Name(),
   126  			HcsError: herr,
   127  		},
   128  	} {
   129  		t.Run(fmt.Sprintf("%T", nerr), func(t *testing.T) {
   130  			if !errors.Is(nerr, errNet) {
   131  				t.Errorf("error '%v' did not unwrap to %v", nerr, errNet)
   132  			}
   133  
   134  			if !errors.Is(nerr, err) {
   135  				t.Errorf("error '%v' did not unwrap to %v", nerr, err)
   136  			}
   137  
   138  			if !IsTimeout(nerr) {
   139  				t.Errorf("expected error '%v' to be timeout", nerr)
   140  			}
   141  
   142  			if !nerr.Timeout() {
   143  				t.Errorf("expected .Timeout() on '%v' to be true", nerr)
   144  			}
   145  
   146  			//nolint:staticcheck // Temporary() is deprecated
   147  			if !nerr.Temporary() {
   148  				t.Errorf("expected .Temporary() on '%v' to be true", nerr)
   149  			}
   150  		})
   151  	}
   152  }
   153  

View as plain text