Test HTTP Error thrown by the file loader

Signed-off-by: Sylvain Rabot <sylvain@abstraction.fr>
This commit is contained in:
Sylvain Rabot
2021-12-10 09:28:54 +01:00
parent 738573b079
commit 16495c6ed7

View File

@@ -4,12 +4,15 @@
package krusty_test
import (
"fmt"
"net/http"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/kustomize/api/internal/utils"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/api/loader"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
@@ -74,6 +77,31 @@ spec:
assert.NoError(t, fSys.RemoveAll(tmpDir.String()))
}
func TestRemoteResourceWithHTTPError(t *testing.T) {
fSys := filesys.MakeFsOnDisk()
b := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
tmpDir, err := filesys.NewTmpConfirmedDir()
assert.NoError(t, err)
url404 := "https://github.com/thisisa404.yaml"
kusto := filepath.Join(tmpDir.String(), "kustomization.yaml")
assert.NoError(t, fSys.WriteFile(kusto, []byte(fmt.Sprintf(`
resources:
- %s
`, url404))))
_, err = b.Run(fSys, tmpDir.String())
if utils.IsErrTimeout(err) {
// Don't fail on timeouts.
t.SkipNow()
}
httpErr := fmt.Errorf("%w: status code %d (%s)", loader.ErrorHTTP, 404, http.StatusText(404))
accuFromErr := fmt.Errorf("accumulating resources from '%s': %w", url404, httpErr)
expectedErr := fmt.Errorf("accumulating resources: %w", accuFromErr)
assert.EqualErrorf(t, err, expectedErr.Error(), url404)
}
func TestRemoteResourceAnnoOrigin(t *testing.T) {
fSys := filesys.MakeFsOnDisk()
b := krusty.MakeKustomizer(krusty.MakeDefaultOptions())