Skip to content

Commit

Permalink
feat: use billy.Filesystem and test server get
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Jul 22, 2023
1 parent 914366c commit c2d3b2c
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 163 deletions.
48 changes: 30 additions & 18 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ func (s *Server) RegisterController(source string, cntl Controller) {
s.mux.Get(prefix, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := s.fs.View(r.Context(), source, s.rev, func(f controller.FSConfig) error {
resources, err := cntl.List(r.Context(), &controller.ListRequest{
FSConfig: f,
Group: def.Spec.Group,
Version: version,
Kind: def.Names.Kind,
Namespace: chi.URLParamFromCtx(r.Context(), "ns"),
Request: controller.Request{
FSConfig: f,
Group: def.Spec.Group,
Version: version,
Kind: def.Names.Kind,
Namespace: chi.URLParamFromCtx(r.Context(), "ns"),
},
})
if err != nil {
return err
Expand All @@ -138,12 +140,14 @@ func (s *Server) RegisterController(source string, cntl Controller) {
s.mux.Get(named, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := s.fs.View(r.Context(), source, s.rev, func(f controller.FSConfig) error {
resource, err := cntl.Get(r.Context(), &controller.GetRequest{
FSConfig: f,
Group: def.Spec.Group,
Version: version,
Kind: def.Names.Kind,
Namespace: chi.URLParamFromCtx(r.Context(), "ns"),
Name: chi.URLParamFromCtx(r.Context(), "name"),
Request: controller.Request{
FSConfig: f,
Group: def.Spec.Group,
Version: version,
Kind: def.Names.Kind,
Namespace: chi.URLParamFromCtx(r.Context(), "ns"),
},
Name: chi.URLParamFromCtx(r.Context(), "name"),
})
if err != nil {
return err
Expand All @@ -165,7 +169,13 @@ func (s *Server) RegisterController(source string, cntl Controller) {
}

return cntl.Put(r.Context(), &controller.PutRequest{
FSConfig: f,
Request: controller.Request{
FSConfig: f,
Group: def.Spec.Group,
Version: version,
Kind: def.Names.Kind,
Namespace: chi.URLParamFromCtx(r.Context(), "ns"),
},
Resource: &resource,
})
})
Expand All @@ -184,12 +194,14 @@ func (s *Server) RegisterController(source string, cntl Controller) {
s.mux.Delete(named, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
result, err := s.fs.Update(r.Context(), source, s.rev, func(f controller.FSConfig) error {
return cntl.Delete(r.Context(), &controller.DeleteRequest{
FSConfig: f,
Group: def.Spec.Group,
Version: version,
Kind: def.Names.Kind,
Namespace: chi.URLParamFromCtx(r.Context(), "ns"),
Name: chi.URLParamFromCtx(r.Context(), "name"),
Request: controller.Request{
FSConfig: f,
Group: def.Spec.Group,
Version: version,
Kind: def.Names.Kind,
Namespace: chi.URLParamFromCtx(r.Context(), "ns"),
},
Name: chi.URLParamFromCtx(r.Context(), "name"),
})
})
if err != nil {
Expand Down
44 changes: 41 additions & 3 deletions pkg/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"net/http/httptest"
"testing"

"github.com/go-git/go-billy/v5/osfs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.flipt.io/cup/pkg/api"
"go.flipt.io/cup/pkg/api/core"
"go.flipt.io/cup/pkg/controllers/simple"
"go.flipt.io/cup/pkg/controllers/template"
"go.flipt.io/cup/pkg/fs/mem"
)

Expand Down Expand Up @@ -39,7 +40,7 @@ func Test_Server_Source(t *testing.T) {
server, err := api.NewServer(fss)
require.NoError(t, err)

cntrl := simple.New(testDef)
cntrl := template.New(testDef)
server.RegisterController("cup", cntrl)

srv := httptest.NewServer(server)
Expand All @@ -61,7 +62,7 @@ func Test_Server_SourceDefinitions(t *testing.T) {
server, err := api.NewServer(fss)
require.NoError(t, err)

cntrl := simple.New(testDef)
cntrl := template.New(testDef)
server.RegisterController("cup", cntrl)

srv := httptest.NewServer(server)
Expand All @@ -79,3 +80,40 @@ func Test_Server_SourceDefinitions(t *testing.T) {
"test.cup.flipt.io/v1alpha1/Resource": testDef,
}, definitions)
}

func Test_Server_Get(t *testing.T) {
fss := mem.New()
fss.AddFS("cup", "main", osfs.New("testdata"))

server, err := api.NewServer(fss)
require.NoError(t, err)

cntrl := template.New(testDef)
server.RegisterController("cup", cntrl)

srv := httptest.NewServer(server)
t.Cleanup(srv.Close)

path := "/apis/cup/test.cup.flipt.io/v1alpha1/resources/namespaces/default/foo"
resp, err := http.Get(srv.URL + path)
require.NoError(t, err)

defer resp.Body.Close()

var resource *core.Resource
require.NoError(t, json.NewDecoder(resp.Body).Decode(&resource))

assert.Equal(t, &core.Resource{
APIVersion: "test.cup.flipt.io",
Kind: "Resource",
Metadata: core.NamespacedMetadata{
Namespace: "default",
Name: "foo",
Labels: map[string]string{
"bar": "baz",
},
Annotations: map[string]string{},
},
Spec: []byte(`{}`),
}, resource)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"apiVersion": "test.cup.flipt.io",
"kind": "Resource",
"metadata": {
"namespace": "default",
"name": "foo",
"labels": {
"bar": "baz"
},
"annotations": {}
},
"spec": {}
}
23 changes: 13 additions & 10 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@ import "go.flipt.io/cup/pkg/api/core"

type Controller struct{}

type GetRequest struct {
type Request struct {
FSConfig
Group string
Version string
Kind string
Namespace string
Name string
}

type GetRequest struct {
Request
Name string
}

type ListRequest struct {
FSConfig
Group string
Version string
Kind string
Namespace string
Labels [][2]string
Request
Labels [][2]string
}

type PutRequest struct {
FSConfig
Request
Resource *core.Resource
}

type DeleteRequest GetRequest
type DeleteRequest struct {
Request
Name string
}
12 changes: 6 additions & 6 deletions pkg/controller/fs.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package controller

import (
"io/fs"
"os"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/osfs"
)

// FSConfig encapsulates the configuration required to establish the root
// directory of the wazero runtime when performing controller actions.
type FSConfig struct {
fs fs.FS
fs billy.Filesystem
dir *string
}

// NewFSConfig constructs an FSConfig which wraps an implementation of fs.FS (read-only).
func NewFSConfig(fs fs.FS) FSConfig {
func NewFSConfig(fs billy.Filesystem) FSConfig {
return FSConfig{fs: fs}
}

Expand All @@ -28,9 +28,9 @@ func NewDirFSConfig(dir string) FSConfig {
// ToFS returns either the configured fs.FS implementation or it
// adapts the desired directory into an fs.FS using os.DirFS
// depending on how the config was configured
func (c *FSConfig) ToFS() fs.FS {
func (c *FSConfig) ToFS() billy.Filesystem {
if c.dir != nil {
return os.DirFS(*c.dir)
return osfs.New(*c.dir)
}

return c.fs
Expand Down
118 changes: 0 additions & 118 deletions pkg/controllers/simple/simple.go

This file was deleted.

Loading

0 comments on commit c2d3b2c

Please sign in to comment.