mirror of
https://github.com/hauler-dev/hauler.git
synced 2026-05-06 01:08:05 +00:00
ensure content type for files is properly detected by getter, add test verifying this
This commit is contained in:
@@ -87,7 +87,7 @@ func (f *file) compute() error {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg := f.config
|
||||
cfg := f.client.Config(f.ref)
|
||||
if cfg == nil {
|
||||
cfg = f.client.Config(f.ref)
|
||||
}
|
||||
|
||||
@@ -13,34 +13,61 @@ import (
|
||||
"github.com/spf13/afero"
|
||||
|
||||
"github.com/rancherfederal/hauler/internal/getter"
|
||||
"github.com/rancherfederal/hauler/pkg/consts"
|
||||
"github.com/rancherfederal/hauler/pkg/content/file"
|
||||
)
|
||||
|
||||
func Test_file_Layers(t *testing.T) {
|
||||
filename := "myfile.yaml"
|
||||
data := []byte(`data`)
|
||||
var (
|
||||
filename = "myfile.yaml"
|
||||
data = []byte(`data`)
|
||||
)
|
||||
|
||||
mfs := afero.NewMemMapFs()
|
||||
afero.WriteFile(mfs, filename, data, 0644)
|
||||
func Test_file_Config(t *testing.T) {
|
||||
mc, s, teardown := setup()
|
||||
defer teardown()
|
||||
|
||||
mf := &mockFile{File: getter.NewFile(), fs: mfs}
|
||||
|
||||
mockHttp := getter.NewHttp()
|
||||
|
||||
mhttp := afero.NewHttpFs(mfs)
|
||||
fileserver := http.FileServer(mhttp.Dir("."))
|
||||
http.Handle("/", fileserver)
|
||||
|
||||
s := httptest.NewServer(fileserver)
|
||||
defer s.Close()
|
||||
|
||||
mc := &getter.Client{
|
||||
Options: getter.ClientOptions{},
|
||||
Getters: map[string]getter.Getter{
|
||||
"file": mf,
|
||||
"http": mockHttp,
|
||||
tests := []struct {
|
||||
name string
|
||||
ref string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "should properly type local file",
|
||||
ref: filename,
|
||||
want: consts.FileLocalConfigMediaType,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "should properly type remote file",
|
||||
ref: s.URL + "/" + filename,
|
||||
want: consts.FileHttpConfigMediaType,
|
||||
wantErr: false,
|
||||
},
|
||||
// TODO: Add directory test
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
f := file.NewFile(tt.ref, file.WithClient(mc))
|
||||
|
||||
f.MediaType()
|
||||
|
||||
m, err := f.Manifest()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got := string(m.Config.MediaType)
|
||||
if got != tt.want {
|
||||
t.Errorf("Expected mediatype %s | got %s", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_file_Layers(t *testing.T) {
|
||||
mc, s, teardown := setup()
|
||||
defer teardown()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -89,6 +116,35 @@ func Test_file_Layers(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func setup() (*getter.Client, *httptest.Server, func()) {
|
||||
mfs := afero.NewMemMapFs()
|
||||
afero.WriteFile(mfs, filename, data, 0644)
|
||||
|
||||
mf := &mockFile{File: getter.NewFile(), fs: mfs}
|
||||
|
||||
mockHttp := getter.NewHttp()
|
||||
|
||||
mhttp := afero.NewHttpFs(mfs)
|
||||
fileserver := http.FileServer(mhttp.Dir("."))
|
||||
http.Handle("/", fileserver)
|
||||
|
||||
s := httptest.NewServer(fileserver)
|
||||
|
||||
mc := &getter.Client{
|
||||
Options: getter.ClientOptions{},
|
||||
Getters: map[string]getter.Getter{
|
||||
"file": mf,
|
||||
"http": mockHttp,
|
||||
},
|
||||
}
|
||||
|
||||
teardown := func() {
|
||||
defer s.Close()
|
||||
}
|
||||
|
||||
return mc, s, teardown
|
||||
}
|
||||
|
||||
type mockFile struct {
|
||||
*getter.File
|
||||
fs afero.Fs
|
||||
|
||||
Reference in New Issue
Block a user