Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### Bug Fixes:

- fix(compute/serve): persist default `override_host` to `fastly.toml` for Viceroy and log when applied ([#1940](https://github.com/fastly/cli/pull/1940))

### Enhancements:

### Dependencies:
Expand Down
21 changes: 15 additions & 6 deletions pkg/commands/compute/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ func (c *ServeCommand) Exec(in io.Reader, out io.Writer) (err error) {
text.Break(out)
}

c.setBackendsWithDefaultOverrideHostIfMissing(out)

spinner, err := text.NewSpinner(out)
if err != nil {
return err
Expand Down Expand Up @@ -204,6 +202,10 @@ func (c *ServeCommand) Exec(in io.Reader, out io.Writer) (err error) {
}
}

if err := c.setBackendsWithDefaultOverrideHostIfMissing(out, manifestPath); err != nil {
return err
}

bin, err := c.GetViceroy(spinner, out, manifestPath)
if err != nil {
return err
Expand Down Expand Up @@ -338,21 +340,28 @@ func (c *ServeCommand) Build(in io.Reader, out io.Writer) error {
// local_server.backends that is missing that property. The value will only be
// set if the URL defined uses a hostname (e.g. http://127.0.0.1/ won't) so we
// can set the override_host to match the hostname.
func (c *ServeCommand) setBackendsWithDefaultOverrideHostIfMissing(out io.Writer) {
//
// Viceroy reads backend configuration from the manifest file on disk, so any
// defaults applied here are written back to manifestPath before Viceroy starts.
func (c *ServeCommand) setBackendsWithDefaultOverrideHostIfMissing(out io.Writer, manifestPath string) error {
updated := false
for k, backend := range c.Globals.Manifest.File.LocalServer.Backends {
if backend.OverrideHost == "" {
if u, err := url.Parse(backend.URL); err == nil {
segs := strings.Split(u.Host, ":") // avoid parsing IP with port
if ip := net.ParseIP(segs[0]); ip == nil {
if c.Globals.Verbose() {
text.Info(out, "[local_server.backends.%s] (%s) is configured without an `override_host`. We will use %s as a default to help avoid any unexpected errors. See https://www.fastly.com/documentation/reference/compute/fastly-toml#local-server for more details.", k, backend.URL, u.Host)
}
text.Info(out, "[local_server.backends.%s] (%s) is configured without an `override_host`. We will use %s as a default to help avoid any unexpected errors. See https://www.fastly.com/documentation/reference/compute/fastly-toml#local-server for more details.", k, backend.URL, u.Host)
backend.OverrideHost = u.Host
c.Globals.Manifest.File.LocalServer.Backends[k] = backend
updated = true
}
}
}
}
if !updated {
return nil
}
return c.Globals.Manifest.File.Write(manifestPath)
}

// GetViceroy returns the path to the installed binary.
Expand Down
52 changes: 52 additions & 0 deletions pkg/commands/compute/serve_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package compute

import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"

"github.com/fsnotify/fsnotify"

"github.com/fastly/cli/pkg/argparser"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/manifest"
)

// TestIsContentChange checks that content/existence events trigger a rebuild and
Expand All @@ -30,3 +38,47 @@ func TestIsContentChange(t *testing.T) {
})
}
}

func TestSetBackendsWithDefaultOverrideHostIfMissingWritesManifest(t *testing.T) {
dir := t.TempDir()
manifestPath := filepath.Join(dir, "fastly.toml")
const initial = `manifest_version = "3.0.0"
name = "test"

[local_server.backends.http-me]
url = "https://http-me.glitch.me"
`
if err := os.WriteFile(manifestPath, []byte(initial), 0o600); err != nil {
t.Fatal(err)
}

var file manifest.File
if err := file.Read(manifestPath); err != nil {
t.Fatal(err)
}

c := &ServeCommand{
Base: argparser.Base{
Globals: &global.Data{
Manifest: &manifest.Data{File: file},
},
},
}

var out bytes.Buffer
if err := c.setBackendsWithDefaultOverrideHostIfMissing(&out, manifestPath); err != nil {
t.Fatal(err)
}

written, err := os.ReadFile(manifestPath)
if err != nil {
t.Fatal(err)
}
body := string(written)
if !strings.Contains(body, "override_host") || !strings.Contains(body, "http-me.glitch.me") {
t.Fatalf("expected override_host persisted in manifest, got:\n%s", body)
}
if !strings.Contains(out.String(), "http-me.glitch.me") {
t.Fatalf("expected override_host info message, got: %q", out.String())
}
}
Loading