@@ -6,6 +6,8 @@ package target_test
6
6
import (
7
7
"encoding/base64"
8
8
"fmt"
9
+ "net/http"
10
+ "net/http/httptest"
9
11
"reflect"
10
12
"testing"
11
13
"time"
@@ -560,3 +562,71 @@ func (l loaderNewThrowsError) Load(location string) ([]byte, error) {
560
562
func (l loaderNewThrowsError ) Cleanup () error {
561
563
return l .baseLoader .Cleanup () //nolint:wrapcheck // baseLoader's error is sufficient
562
564
}
565
+
566
+ func TestErrorMessageForMalformedYAMLAndInvalidBase (t * testing.T ) {
567
+ // These testcases verify behavior for the scenario described in
568
+ // https://github.com/kubernetes-sigs/kustomize/issues/5692 .
569
+
570
+ // Use a test server to fake the remote file response
571
+ handler := http .NewServeMux ()
572
+ handler .HandleFunc ("/" , func (out http.ResponseWriter , req * http.Request ) {
573
+ // Per issue #5692, the server should return a 200 status code with a response body that fails to parse as YAML
574
+ out .WriteHeader (http .StatusOK )
575
+ _ , _ = out .Write ([]byte (`<!DOCTYPE html>
576
+ <html class="html-devise-layout ui-light-gray" lang="en">
577
+ <head prefix="og: http://ogp.me/ns#">` ))
578
+ })
579
+ svr := httptest .NewServer (handler )
580
+ defer svr .Close ()
581
+
582
+ th := kusttest_test .MakeHarness (t )
583
+ th .WriteF ("/should-fail/kustomization.yml" , "resources:\n - " + svr .URL )
584
+ th .WriteF ("/should-fail/remote-repo/kustomization.yml" , "this: is not a kustomization file!" )
585
+
586
+ ldrWrapper := func (baseLoader ifc.Loader ) ifc.Loader {
587
+ return & loaderWithRenamedRoots {
588
+ baseLoader : baseLoader ,
589
+ fakeRootMap : map [string ]string {
590
+ // Use the "remote-repo" subdir instead of the remote git repo
591
+ svr .URL : "remote-repo" ,
592
+ },
593
+ }
594
+ }
595
+
596
+ _ , err := makeAndLoadKustTargetWithLoaderOverride (t , th .GetFSys (), "/should-fail" , ldrWrapper ).AccumulateTarget ()
597
+ require .Error (t , err )
598
+ errString := err .Error ()
599
+ assert .Contains (t , errString , "accumulating resources from '" + svr .URL + "'" )
600
+ assert .Contains (t , errString , "MalformedYAMLError: yaml: line 3: mapping values are not allowed in this context" )
601
+ assert .Contains (t , errString , `invalid Kustomization: json: unknown field "this"` )
602
+ }
603
+
604
+ // loaderWithRenamedRoots is a loader that can map New() roots to some other name
605
+ type loaderWithRenamedRoots struct {
606
+ baseLoader ifc.Loader
607
+ fakeRootMap map [string ]string
608
+ }
609
+
610
+ func (l loaderWithRenamedRoots ) Repo () string {
611
+ return l .baseLoader .Repo ()
612
+ }
613
+
614
+ func (l loaderWithRenamedRoots ) Root () string {
615
+ return l .baseLoader .Root ()
616
+ }
617
+
618
+ func (l loaderWithRenamedRoots ) New (newRoot string ) (ifc.Loader , error ) {
619
+ if otherRoot , ok := l .fakeRootMap [newRoot ]; ok {
620
+ return l .baseLoader .New (otherRoot ) //nolint:wrapcheck // baseLoader's error is sufficient
621
+ }
622
+
623
+ return l .baseLoader .New (newRoot ) //nolint:wrapcheck // baseLoader's error is sufficient
624
+ }
625
+
626
+ func (l loaderWithRenamedRoots ) Load (path string ) ([]byte , error ) {
627
+ return l .baseLoader .Load (path ) //nolint:wrapcheck // baseLoader's error is sufficient
628
+ }
629
+
630
+ func (l loaderWithRenamedRoots ) Cleanup () error {
631
+ return l .baseLoader .Cleanup () //nolint:wrapcheck // baseLoader's error is sufficient
632
+ }
0 commit comments