|
| 1 | +package apps |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/databricks/databricks-sdk-go/apierr" |
| 9 | + "github.com/databricks/databricks-sdk-go/retries" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestAppDeploymentError_Error_WithoutProfile(t *testing.T) { |
| 16 | + originalErr := errors.New("deployment failed: timeout") |
| 17 | + appErr := &AppDeploymentError{ |
| 18 | + Underlying: originalErr, |
| 19 | + appName: "my-app", |
| 20 | + profile: "", |
| 21 | + } |
| 22 | + |
| 23 | + result := appErr.Error() |
| 24 | + |
| 25 | + assert.Contains(t, result, "deployment failed: timeout") |
| 26 | + assert.Contains(t, result, "To view app logs, run:") |
| 27 | + assert.Contains(t, result, "databricks apps logs my-app --tail-lines 100") |
| 28 | + assert.NotContains(t, result, "--profile") |
| 29 | +} |
| 30 | + |
| 31 | +func TestAppDeploymentError_Error_WithProfile(t *testing.T) { |
| 32 | + originalErr := errors.New("deployment failed: timeout") |
| 33 | + appErr := &AppDeploymentError{ |
| 34 | + Underlying: originalErr, |
| 35 | + appName: "my-app", |
| 36 | + profile: "production", |
| 37 | + } |
| 38 | + |
| 39 | + result := appErr.Error() |
| 40 | + |
| 41 | + assert.Contains(t, result, "deployment failed: timeout") |
| 42 | + assert.Contains(t, result, "To view app logs, run:") |
| 43 | + assert.Contains(t, result, "databricks apps logs my-app --tail-lines 100 --profile production") |
| 44 | +} |
| 45 | + |
| 46 | +func TestAppDeploymentError_Unwrap(t *testing.T) { |
| 47 | + originalErr := errors.New("original error") |
| 48 | + appErr := &AppDeploymentError{ |
| 49 | + Underlying: originalErr, |
| 50 | + appName: "my-app", |
| 51 | + profile: "", |
| 52 | + } |
| 53 | + |
| 54 | + unwrapped := appErr.Unwrap() |
| 55 | + |
| 56 | + require.Equal(t, originalErr, unwrapped) |
| 57 | + assert.ErrorIs(t, appErr, originalErr, "errors.Is should work with wrapped error") |
| 58 | +} |
| 59 | + |
| 60 | +func TestWrapDeploymentError(t *testing.T) { |
| 61 | + tests := []struct { |
| 62 | + name string |
| 63 | + err error |
| 64 | + appName string |
| 65 | + expectWrapped bool |
| 66 | + description string |
| 67 | + }{ |
| 68 | + { |
| 69 | + name: "nil error", |
| 70 | + err: nil, |
| 71 | + appName: "test-app", |
| 72 | + expectWrapped: false, |
| 73 | + description: "nil error should return nil unchanged", |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "plain error without retries wrapper", |
| 77 | + err: errors.New("some error"), |
| 78 | + appName: "test-app", |
| 79 | + expectWrapped: false, |
| 80 | + description: "plain errors should not be wrapped", |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "retries.Err with Halt=false", |
| 84 | + err: retries.Continues("still in progress"), |
| 85 | + appName: "test-app", |
| 86 | + expectWrapped: false, |
| 87 | + description: "transient retries errors should not be wrapped", |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "retries.Err with 404 API error (not found)", |
| 91 | + err: retries.Halt(fmt.Errorf("API error: %w", &apierr.APIError{ |
| 92 | + StatusCode: 404, |
| 93 | + ErrorCode: "NOT_FOUND", |
| 94 | + Message: "App with name test-app does not exist or is deleted.", |
| 95 | + })), |
| 96 | + appName: "test-app", |
| 97 | + expectWrapped: false, |
| 98 | + description: "404 not found errors should not be wrapped with logs hint", |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "retries.Err with 400 API error (bad request)", |
| 102 | + err: retries.Halt(fmt.Errorf("API error: %w", &apierr.APIError{ |
| 103 | + StatusCode: 400, |
| 104 | + ErrorCode: "BAD_REQUEST", |
| 105 | + Message: "Invalid request parameters", |
| 106 | + })), |
| 107 | + appName: "test-app", |
| 108 | + expectWrapped: false, |
| 109 | + description: "400 bad request errors should not be wrapped with logs hint", |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "retries.Err with 403 API error (forbidden)", |
| 113 | + err: retries.Halt(fmt.Errorf("API error: %w", &apierr.APIError{ |
| 114 | + StatusCode: 403, |
| 115 | + ErrorCode: "FORBIDDEN", |
| 116 | + Message: "Access denied", |
| 117 | + })), |
| 118 | + appName: "test-app", |
| 119 | + expectWrapped: false, |
| 120 | + description: "403 forbidden errors should not be wrapped with logs hint", |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "retries.Err with 500 API error (server error)", |
| 124 | + err: retries.Halt(fmt.Errorf("API error: %w", &apierr.APIError{ |
| 125 | + StatusCode: 500, |
| 126 | + ErrorCode: "INTERNAL_ERROR", |
| 127 | + Message: "Internal server error", |
| 128 | + })), |
| 129 | + appName: "test-app", |
| 130 | + expectWrapped: true, |
| 131 | + description: "500 server errors during wait should be wrapped with logs hint", |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "retries.Err without API error (deployment failure)", |
| 135 | + err: retries.Halt(errors.New("failed to reach SUCCEEDED, got FAILED: Error building app")), |
| 136 | + appName: "test-app", |
| 137 | + expectWrapped: true, |
| 138 | + description: "deployment failures should be wrapped with logs hint", |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "retries.Err without API error (timeout)", |
| 142 | + err: retries.Halt(errors.New("timeout waiting for deployment")), |
| 143 | + appName: "test-app", |
| 144 | + expectWrapped: true, |
| 145 | + description: "timeout errors should be wrapped with logs hint", |
| 146 | + }, |
| 147 | + } |
| 148 | + |
| 149 | + for _, tt := range tests { |
| 150 | + t.Run(tt.name, func(t *testing.T) { |
| 151 | + cmd := &cobra.Command{} |
| 152 | + result := wrapDeploymentError(cmd, tt.appName, tt.err) |
| 153 | + |
| 154 | + if tt.expectWrapped { |
| 155 | + var appErr *AppDeploymentError |
| 156 | + require.ErrorAs(t, result, &appErr, tt.description) |
| 157 | + assert.Equal(t, tt.appName, appErr.appName) |
| 158 | + assert.ErrorIs(t, result, tt.err, "wrapped error should unwrap to original") |
| 159 | + assert.Contains(t, result.Error(), "To view app logs, run:", "should contain logs hint") |
| 160 | + } else { |
| 161 | + assert.Equal(t, tt.err, result, tt.description) |
| 162 | + if tt.err != nil { |
| 163 | + assert.NotContains(t, result.Error(), "To view app logs", "should not contain logs hint") |
| 164 | + } |
| 165 | + } |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
0 commit comments