Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix object store TOML definitions, add test data #715

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/commands/compute/testdata/init/fastly-viceroy-update.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,14 @@ name = "Default Rust template"
foo = "bar"
baz = """
qux"""

[local_server.object_store]
store_one = [{key = "first", data = "This is some data"}, {key = "second", path = "strings.json"}]

[[local_server.object_store.store_two]]
key = "first"
data = "This is some data"

[[local_server.object_store.store_two]]
key = "second"
path = "strings.json"
28 changes: 17 additions & 11 deletions pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ type SetupLogger struct {

// LocalServer represents a list of mocked Viceroy resources.
type LocalServer struct {
Backends map[string]LocalBackend `toml:"backends"`
Dictionaries map[string]LocalDictionary `toml:"dictionaries,omitempty"`
ObjectStore map[string]LocalObjectStore `toml:"object_stores,omitempty"`
Backends map[string]LocalBackend `toml:"backends"`
Dictionaries map[string]LocalDictionary `toml:"dictionaries,omitempty"`
ObjectStore map[string][]LocalObjectStore `toml:"object_store,omitempty"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrispoole643 were you documenting the ObjectStore in https://developer.fastly.com/reference/compute/fastly-toml/ ? I thought I recalled you doing so but couldn't see it in there at the moment. At any rate, we'll want to ensure that's kept consistent with what's here ☝🏻

}

// LocalBackend represents a backend to be mocked by the local testing server.
Expand All @@ -272,8 +272,8 @@ type LocalDictionary struct {
// LocalObjectStore represents an object_store to be mocked by the local testing server.
type LocalObjectStore struct {
Key string `toml:"key"`
Path string `toml:"path"`
Data string `toml:"data"`
Path string `toml:"path,omitempty"`
Data string `toml:"data,omitempty"`
}

// Exists yields whether the manifest exists.
Expand Down Expand Up @@ -307,6 +307,12 @@ func (f *File) SetOutput(output io.Writer) {
f.output = output
}

func (f *File) logErr(err error) {
if f.errLog != nil {
f.errLog.Add(err)
}
}

// AutoMigrateVersion updates the manifest_version value to
// ManifestLatestVersion if the current version is less than the latest
// supported and only if there is no [setup] configuration defined.
Expand Down Expand Up @@ -427,7 +433,7 @@ func (f *File) Read(path string) (err error) {
/* #nosec */
data, err := os.ReadFile(path)
if err != nil {
f.errLog.Add(err)
f.logErr(err)
return err
}

Expand All @@ -442,14 +448,14 @@ func (f *File) Read(path string) (err error) {
// structure otherwise we'll see errors from the toml library.
manifestSection, err := containsManifestSection(data)
if err != nil {
f.errLog.Add(err)
f.logErr(err)
return fmt.Errorf("failed to parse the fastly.toml manifest: %w", err)
}

if manifestSection {
buf, err := stripManifestSection(bytes.NewReader(data), path)
if err != nil {
f.errLog.Add(err)
f.logErr(err)
return fsterr.ErrInvalidManifestVersion
}
data = buf.Bytes()
Expand All @@ -460,13 +466,13 @@ func (f *File) Read(path string) (err error) {
// version supported by the Fastly CLI.
data, err = f.AutoMigrateVersion(data, path)
if err != nil {
f.errLog.Add(err)
f.logErr(err)
return err
}

err = toml.Unmarshal(data, f)
if err != nil {
f.errLog.Add(err)
f.logErr(err)
return fsterr.ErrParsingManifest
}

Expand All @@ -481,7 +487,7 @@ func (f *File) Read(path string) (err error) {
}
err = f.Write(path)
if err != nil {
f.errLog.Add(err)
f.logErr(err)
return fmt.Errorf("unable to save fastly.toml manifest change: %w", err)
}
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/testutil"
"github.com/google/go-cmp/cmp"
toml "github.com/pelletier/go-toml"
)

Expand Down Expand Up @@ -295,7 +296,8 @@ func TestManifestPersistsLocalServerSection(t *testing.T) {
t.Fatal("expected [local_server] block to exist in fastly.toml but is missing")
}

if lt.(*toml.Tree).String() != ot.(*toml.Tree).String() {
t.Fatal("testing section between original and updated fastly.toml do not match")
got, want := lt.(*toml.Tree).String(), ot.(*toml.Tree).String()
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("testing section between original and updated fastly.toml do not match (-want +got):\n%s", diff)
}
}
11 changes: 11 additions & 0 deletions pkg/manifest/testdata/fastly-viceroy-update.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,14 @@ name = "Default Rust template"
foo = "bar"
baz = """
qux"""

[local_server.object_store]
store_one = [{key = "first", data = "This is some data"}, {key = "second", path = "strings.json"}]

[[local_server.object_store.store_two]]
key = "first"
data = "This is some data"

[[local_server.object_store.store_two]]
key = "second"
path = "strings.json"