mirror of
https://github.com/hauler-dev/hauler.git
synced 2026-02-14 18:09:51 +00:00
ensure content type for files is properly detected by getter, add test verifying this
This commit is contained in:
@@ -12,6 +12,9 @@ import (
|
||||
|
||||
"github.com/opencontainers/go-digest"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/rancherfederal/hauler/pkg/artifact"
|
||||
"github.com/rancherfederal/hauler/pkg/consts"
|
||||
)
|
||||
|
||||
type directory struct {
|
||||
@@ -68,6 +71,17 @@ func (d directory) Detect(u *url.URL) bool {
|
||||
return fi.IsDir()
|
||||
}
|
||||
|
||||
func (d directory) Config(u *url.URL) artifact.Config {
|
||||
c := &directoryConfig{
|
||||
config{Reference: u.String()},
|
||||
}
|
||||
return artifact.ToConfig(c, artifact.WithConfigMediaType(consts.FileDirectoryConfigMediaType))
|
||||
}
|
||||
|
||||
type directoryConfig struct {
|
||||
config `json:",inline,omitempty"`
|
||||
}
|
||||
|
||||
func tarDir(root string, prefix string, w io.Writer, stripTimes bool) error {
|
||||
tw := tar.NewWriter(w)
|
||||
defer tw.Close()
|
||||
|
||||
@@ -7,8 +7,6 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/v1/types"
|
||||
|
||||
"github.com/rancherfederal/hauler/pkg/artifact"
|
||||
"github.com/rancherfederal/hauler/pkg/consts"
|
||||
)
|
||||
@@ -47,13 +45,9 @@ func (f File) Config(u *url.URL) artifact.Config {
|
||||
c := &fileConfig{
|
||||
config{Reference: u.String()},
|
||||
}
|
||||
return artifact.ToConfig(c)
|
||||
return artifact.ToConfig(c, artifact.WithConfigMediaType(consts.FileLocalConfigMediaType))
|
||||
}
|
||||
|
||||
type fileConfig struct {
|
||||
config `json:",inline,omitempty"`
|
||||
}
|
||||
|
||||
func (c *fileConfig) MediaType() (types.MediaType, error) {
|
||||
return consts.FileLocalConfigMediaType, nil
|
||||
}
|
||||
|
||||
@@ -9,8 +9,6 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/v1/types"
|
||||
|
||||
"github.com/rancherfederal/hauler/pkg/artifact"
|
||||
"github.com/rancherfederal/hauler/pkg/consts"
|
||||
)
|
||||
@@ -61,13 +59,9 @@ func (h *Http) Config(u *url.URL) artifact.Config {
|
||||
c := &httpConfig{
|
||||
config{Reference: u.String()},
|
||||
}
|
||||
return artifact.ToConfig(c)
|
||||
return artifact.ToConfig(c, artifact.WithConfigMediaType(consts.FileHttpConfigMediaType))
|
||||
}
|
||||
|
||||
type httpConfig struct {
|
||||
config `json:",inline,omitempty"`
|
||||
}
|
||||
|
||||
func (c *httpConfig) MediaType() (types.MediaType, error) {
|
||||
return consts.FileHttpConfigMediaType, nil
|
||||
}
|
||||
|
||||
@@ -23,8 +23,9 @@ const (
|
||||
FileLayerMediaType = "application/vnd.content.hauler.file.layer.v1"
|
||||
|
||||
// FileLocalConfigMediaType is the reserved media type for File config
|
||||
FileLocalConfigMediaType = "application/vnd.content.hauler.file.local.config.v1+json"
|
||||
FileHttpConfigMediaType = "application/vnd.content.hauler.file.http.config.v1+json"
|
||||
FileLocalConfigMediaType = "application/vnd.content.hauler.file.local.config.v1+json"
|
||||
FileDirectoryConfigMediaType = "application/vnd.content.hauler.file.directory.config.v1+json"
|
||||
FileHttpConfigMediaType = "application/vnd.content.hauler.file.http.config.v1+json"
|
||||
|
||||
// WasmArtifactLayerMediaType is the reserved media type for WASM artifact layers
|
||||
WasmArtifactLayerMediaType = "application/vnd.wasm.content.layer.v1+wasm"
|
||||
|
||||
@@ -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