-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
290 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fake-gcs-server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2019 Francisco Souza. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package config | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"math" | ||
|
||
"github.com/fsouza/fake-gcs-server/fakestorage" | ||
) | ||
|
||
const ( | ||
filesystemBackend = "filesystem" | ||
memoryBackend = "memory" | ||
) | ||
|
||
type Config struct { | ||
Seed string | ||
publicHost string | ||
externalURL string | ||
host string | ||
port uint | ||
backend string | ||
fsRoot string | ||
} | ||
|
||
// Load parses the given arguments list and return a config object (and/or an | ||
// error in case of failures). | ||
func Load(args []string) (Config, error) { | ||
var cfg Config | ||
fs := flag.NewFlagSet("gcs-emulator", flag.ContinueOnError) | ||
fs.StringVar(&cfg.backend, "backend", filesystemBackend, "storage backend (memory or filesystem)") | ||
fs.StringVar(&cfg.fsRoot, "filesystem-root", "/storage", "filesystem root (required for the filesystem backend). folder will be created if it doesn't exist") | ||
fs.StringVar(&cfg.publicHost, "public-host", "storage.googleapis.com", "Optional URL for public host") | ||
fs.StringVar(&cfg.externalURL, "external-url", "", "optional external URL, returned in the Location header for uploads. Defaults to the address where the server is running") | ||
fs.StringVar(&cfg.host, "host", "0.0.0.0", "host to bind to") | ||
fs.StringVar(&cfg.Seed, "data", "/data", "where to load data from (provided that the directory exists)") | ||
fs.UintVar(&cfg.port, "port", 4443, "port to bind to") | ||
err := fs.Parse(args) | ||
if err != nil { | ||
return cfg, err | ||
} | ||
return cfg, cfg.validate() | ||
} | ||
|
||
func (c *Config) validate() error { | ||
if c.backend != memoryBackend && c.backend != filesystemBackend { | ||
return fmt.Errorf(`invalid backend %q, must be either "memory" or "filesystem"`, c.backend) | ||
} | ||
if c.backend == filesystemBackend && c.fsRoot == "" { | ||
return fmt.Errorf("backend %q requires the filesystem-root to be defined", c.backend) | ||
} | ||
if c.port > math.MaxUint16 { | ||
return fmt.Errorf("port %d is too high, maximum value is %d", c.port, math.MaxUint16) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Config) ToFakeGcsOptions() fakestorage.Options { | ||
storageRoot := c.fsRoot | ||
if c.backend == memoryBackend { | ||
storageRoot = "" | ||
} | ||
return fakestorage.Options{ | ||
StorageRoot: storageRoot, | ||
Host: c.host, | ||
Port: uint16(c.port), | ||
PublicHost: c.publicHost, | ||
ExternalURL: c.externalURL, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Copyright 2019 Francisco Souza. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/fsouza/fake-gcs-server/fakestorage" | ||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
args []string | ||
expectedConfig Config | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "all parameters", | ||
args: []string{ | ||
"-backend", "memory", | ||
"-filesystem-root", "/tmp/something", | ||
"-public-host", "127.0.0.1.nip.io:8443", | ||
"-external-url", "https://myhost.example.com:8443", | ||
"-host", "127.0.0.1", | ||
"-port", "443", | ||
"-data", "/var/gcs", | ||
}, | ||
expectedConfig: Config{ | ||
Seed: "/var/gcs", | ||
backend: "memory", | ||
fsRoot: "/tmp/something", | ||
publicHost: "127.0.0.1.nip.io:8443", | ||
externalURL: "https://myhost.example.com:8443", | ||
host: "127.0.0.1", | ||
port: 443, | ||
}, | ||
}, | ||
{ | ||
name: "default parameters", | ||
expectedConfig: Config{ | ||
Seed: "/data", | ||
backend: "filesystem", | ||
fsRoot: "/storage", | ||
publicHost: "storage.googleapis.com", | ||
externalURL: "", | ||
host: "0.0.0.0", | ||
port: 4443, | ||
}, | ||
}, | ||
{ | ||
name: "invalid port value type", | ||
args: []string{"-port", "not-a-number"}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "invalid port value", | ||
args: []string{"-port", "65536"}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "invalid backend", | ||
args: []string{"-backend", "in-memory"}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "filesystem backend with no root", | ||
args: []string{"-backend", "filesystem", "-filesystem-root", ""}, | ||
expectErr: true, | ||
}, | ||
} | ||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
cfg, err := Load(test.args) | ||
if err != nil && !test.expectErr { | ||
t.Fatalf("uexpected non-nil error: %v", err) | ||
} else if err == nil && test.expectErr { | ||
t.Fatal("unexpected <nil> error") | ||
} | ||
if diff := cmp.Diff(cfg, test.expectedConfig, cmp.AllowUnexported(Config{})); !test.expectErr && diff != "" { | ||
t.Errorf("wrong config returned\nwant %#v\ngot %#v\ndiff: %v", test.expectedConfig, cfg, diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestToFakeGcsOptions(t *testing.T) { | ||
t.Parallel() | ||
var tests = []struct { | ||
name string | ||
config Config | ||
expected fakestorage.Options | ||
}{ | ||
{ | ||
"filesystem", | ||
Config{ | ||
backend: "filesystem", | ||
fsRoot: "/tmp/something", | ||
publicHost: "127.0.0.1.nip.io:8443", | ||
externalURL: "https://myhost.example.com:8443", | ||
host: "0.0.0.0", | ||
port: 443, | ||
}, | ||
fakestorage.Options{ | ||
StorageRoot: "/tmp/something", | ||
PublicHost: "127.0.0.1.nip.io:8443", | ||
ExternalURL: "https://myhost.example.com:8443", | ||
Host: "0.0.0.0", | ||
Port: 443, | ||
}, | ||
}, | ||
{ | ||
"memory", | ||
Config{ | ||
backend: "memory", | ||
fsRoot: "/tmp/something", | ||
publicHost: "127.0.0.1.nip.io:8443", | ||
externalURL: "https://myhost.example.com:8443", | ||
host: "0.0.0.0", | ||
port: 443, | ||
}, | ||
fakestorage.Options{ | ||
StorageRoot: "", | ||
PublicHost: "127.0.0.1.nip.io:8443", | ||
ExternalURL: "https://myhost.example.com:8443", | ||
Host: "0.0.0.0", | ||
Port: 443, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
opts := test.config.ToFakeGcsOptions() | ||
if diff := cmp.Diff(opts, test.expected); diff != "" { | ||
t.Errorf("wrong set of options returned\nwant %#v\ngot %#v\ndiff: %v", test.expected, opts, diff) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.