|
| 1 | +package driver |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func Test_httpStatus(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + err error |
| 14 | + wantCode int |
| 15 | + wantOK bool |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "nil error", |
| 19 | + err: nil, |
| 20 | + wantOK: false, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "unrelated error", |
| 24 | + err: errors.New("connection refused"), |
| 25 | + wantOK: false, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "winrm basic auth 401", |
| 29 | + err: fmt.Errorf("http response error: 401 - invalid content type"), |
| 30 | + wantCode: http.StatusUnauthorized, |
| 31 | + wantOK: true, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "winrm cert auth 401", |
| 35 | + err: fmt.Errorf("http error 401: Unauthorized"), |
| 36 | + wantCode: http.StatusUnauthorized, |
| 37 | + wantOK: true, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "403 forbidden", |
| 41 | + err: fmt.Errorf("http response error: 403 - access denied"), |
| 42 | + wantCode: http.StatusForbidden, |
| 43 | + wantOK: true, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "500 server error", |
| 47 | + err: fmt.Errorf("http response error: 500 - internal"), |
| 48 | + wantCode: http.StatusInternalServerError, |
| 49 | + wantOK: true, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "wrapped winrm error", |
| 53 | + err: fmt.Errorf("WinRM command failed: %w", fmt.Errorf("http response error: 401 - invalid content type")), |
| 54 | + wantCode: http.StatusUnauthorized, |
| 55 | + wantOK: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "double-wrapped", |
| 59 | + err: fmt.Errorf("outer: %w", fmt.Errorf("WinRM command failed: %w", fmt.Errorf("http response error: 401 - x"))), |
| 60 | + wantCode: http.StatusUnauthorized, |
| 61 | + wantOK: true, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tc := range tests { |
| 66 | + t.Run(tc.name, func(t *testing.T) { |
| 67 | + code, ok := httpStatus(tc.err) |
| 68 | + if ok != tc.wantOK { |
| 69 | + t.Errorf("httpStatus() ok = %v, want %v", ok, tc.wantOK) |
| 70 | + } |
| 71 | + if code != tc.wantCode { |
| 72 | + t.Errorf("httpStatus() code = %d, want %d", code, tc.wantCode) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestWrapCommandError(t *testing.T) { |
| 79 | + tests := []struct { |
| 80 | + name string |
| 81 | + err error |
| 82 | + wantNil bool |
| 83 | + wantIsAuth bool |
| 84 | + }{ |
| 85 | + { |
| 86 | + name: "nil", |
| 87 | + err: nil, |
| 88 | + wantNil: true, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "unrelated error passes through", |
| 92 | + err: errors.New("timeout"), |
| 93 | + wantIsAuth: false, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "401 becomes ErrUnauthorized", |
| 97 | + err: fmt.Errorf("http response error: 401 - invalid content type"), |
| 98 | + wantIsAuth: true, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "403 becomes ErrUnauthorized", |
| 102 | + err: fmt.Errorf("http response error: 403 - forbidden"), |
| 103 | + wantIsAuth: true, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "500 does not become ErrUnauthorized", |
| 107 | + err: fmt.Errorf("http response error: 500 - internal"), |
| 108 | + wantIsAuth: false, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "wrapped 401", |
| 112 | + err: fmt.Errorf("WinRM command failed: %w", fmt.Errorf("http response error: 401 - x")), |
| 113 | + wantIsAuth: true, |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + for _, tc := range tests { |
| 118 | + t.Run(tc.name, func(t *testing.T) { |
| 119 | + result := WrapCommandError(tc.err) |
| 120 | + if tc.wantNil { |
| 121 | + if result != nil { |
| 122 | + t.Fatalf("WrapCommandError(nil) = %v, want nil", result) |
| 123 | + } |
| 124 | + return |
| 125 | + } |
| 126 | + if result == nil { |
| 127 | + t.Fatal("WrapCommandError() returned nil for non-nil input") |
| 128 | + } |
| 129 | + isAuth := errors.Is(result, ErrUnauthorized) |
| 130 | + if isAuth != tc.wantIsAuth { |
| 131 | + t.Errorf("errors.Is(result, ErrUnauthorized) = %v, want %v (err=%v)", isAuth, tc.wantIsAuth, result) |
| 132 | + } |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments