@@ -19,7 +19,9 @@ package application
1919import (
2020 "context"
2121 "fmt"
22+ "net/http"
2223 "reflect"
24+ "strings"
2325 "testing"
2426
2527 cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
@@ -29,6 +31,7 @@ import (
2931 "k8s.io/apimachinery/pkg/runtime"
3032 "k8s.io/apimachinery/pkg/runtime/schema"
3133 "k8s.io/apimachinery/pkg/types"
34+ "k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
3235 "k8s.io/apiserver/pkg/endpoints/request"
3336 "k8s.io/apiserver/pkg/registry/rest"
3437 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -206,6 +209,58 @@ func TestDeletePropagatesWriteOptions(t *testing.T) {
206209 }
207210}
208211
212+ // assertWireStatus checks the answer the aggregated endpoint would put on the
213+ // wire: ErrorToAPIStatus type-switches on the APIStatus interface and never
214+ // unwraps, so a merely fmt.Errorf-wrapped backend rejection collapses to a
215+ // generic 500 with an empty reason even while errors.Is/As still succeed.
216+ func assertWireStatus (t * testing.T , err error , wantCode int32 , wantReason metav1.StatusReason , wantContext string ) {
217+ t .Helper ()
218+ status := responsewriters .ErrorToAPIStatus (err )
219+ if status .Code != wantCode || status .Reason != wantReason {
220+ t .Fatalf ("wire status = %d %q, want %d %q (err: %v)" , status .Code , status .Reason , wantCode , wantReason , err )
221+ }
222+ if ! strings .Contains (status .Message , wantContext ) {
223+ t .Fatalf ("wire message %q lost the %q context" , status .Message , wantContext )
224+ }
225+ }
226+
227+ func TestCreatePreservesBackendStatus (t * testing.T ) {
228+ alreadyExists := apierrors .NewAlreadyExists (
229+ schema.GroupResource {Group : helmv2 .GroupVersion .Group , Resource : "helmreleases" },
230+ "postgresql-example" ,
231+ )
232+ r := newOptionsTestREST (t , interceptor.Funcs {
233+ Create : func (_ context.Context , _ client.WithWatch , _ client.Object , _ ... client.CreateOption ) error {
234+ return alreadyExists
235+ },
236+ })
237+ ctx := request .WithNamespace (context .Background (), optionsTestNamespace )
238+ _ , err := r .Create (ctx , optionsTestApplication (), nil , & metav1.CreateOptions {})
239+ if ! apierrors .IsAlreadyExists (err ) {
240+ t .Fatalf ("Create returned %v, want a preserved AlreadyExists" , err )
241+ }
242+ assertWireStatus (t , err , http .StatusConflict , metav1 .StatusReasonAlreadyExists , "failed to create HelmRelease" )
243+ }
244+
245+ func TestUpdatePreservesBackendStatus (t * testing.T ) {
246+ forbidden := apierrors .NewForbidden (
247+ schema.GroupResource {Group : helmv2 .GroupVersion .Group , Resource : "helmreleases" },
248+ "postgresql-example" ,
249+ fmt .Errorf ("rejected by admission policy" ),
250+ )
251+ r := newOptionsTestREST (t , interceptor.Funcs {
252+ Update : func (_ context.Context , _ client.WithWatch , _ client.Object , _ ... client.UpdateOption ) error {
253+ return forbidden
254+ },
255+ }, optionsTestHelmRelease ())
256+ ctx := request .WithNamespace (context .Background (), optionsTestNamespace )
257+ _ , _ , err := r .Update (ctx , "example" , rest .DefaultUpdatedObjectInfo (optionsTestApplication ()), nil , nil , false , & metav1.UpdateOptions {})
258+ if ! apierrors .IsForbidden (err ) {
259+ t .Fatalf ("Update returned %v, want a preserved Forbidden" , err )
260+ }
261+ assertWireStatus (t , err , http .StatusForbidden , metav1 .StatusReasonForbidden , "failed to update HelmRelease" )
262+ }
263+
209264func TestDeletePreservesConflictError (t * testing.T ) {
210265 conflict := apierrors .NewConflict (
211266 schema.GroupResource {Group : helmv2 .GroupVersion .Group , Resource : "helmreleases" },
@@ -218,7 +273,9 @@ func TestDeletePreservesConflictError(t *testing.T) {
218273 },
219274 }, optionsTestHelmRelease ())
220275 ctx := request .WithNamespace (context .Background (), optionsTestNamespace )
221- if _ , _ , err := r .Delete (ctx , "example" , nil , & metav1.DeleteOptions {}); ! apierrors .IsConflict (err ) {
276+ _ , _ , err := r .Delete (ctx , "example" , nil , & metav1.DeleteOptions {})
277+ if ! apierrors .IsConflict (err ) {
222278 t .Fatalf ("Delete returned %v, want a preserved Conflict" , err )
223279 }
280+ assertWireStatus (t , err , http .StatusConflict , metav1 .StatusReasonConflict , "failed to delete HelmRelease" )
224281}
0 commit comments