Skip to content

Commit

Permalink
Rename functions ...WithOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
charlieegan3 committed Jul 25, 2023
1 parent 8229bd5 commit 5d537e9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 37 deletions.
8 changes: 4 additions & 4 deletions internal/bundle/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ func LoadWasmResolversFromStore(ctx context.Context, store storage.Store, txn st
return resolvers, nil
}

// LoadBundleFromDisk loads a previously persisted activated bundle from disk
func LoadBundleFromDisk(path string, opts *LoadOptions) (*bundle.Bundle, error) {
// LoadBundleFromDiskWithOptions loads a previously persisted activated bundle from disk
func LoadBundleFromDiskWithOptions(path string, opts *LoadOptions) (*bundle.Bundle, error) {
// if a bundle package exists, use that as it might contain the bundle etag which
// is not stored in the legacy bundle file. This can help avoid unnecessary bundle
// downloads.
Expand Down Expand Up @@ -230,9 +230,9 @@ func LoadBundleFromDisk(path string, opts *LoadOptions) (*bundle.Bundle, error)
}
}

// SaveBundleToDisk persists a bundle to disk. Bundles are wrapped in a 'bundlePackage' in order
// SaveBundleToDiskWithOptions persists a bundle to disk. Bundles are wrapped in a 'bundlePackage' in order
// to support additional metadata (e.g. etag) that is not part of the original bundle.
func SaveBundleToDisk(path string, rawBundle io.Reader, opts *SaveOptions) error {
func SaveBundleToDiskWithOptions(path string, rawBundle io.Reader, opts *SaveOptions) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
Expand Down
24 changes: 12 additions & 12 deletions internal/bundle/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestLoadBundleFromDiskLegacy(t *testing.T) {
t.Fatal("unexpected error:", err)
}

loadedBundle, err := LoadBundleFromDisk(tempDir, &LoadOptions{})
loadedBundle, err := LoadBundleFromDiskWithOptions(tempDir, &LoadOptions{})
if err != nil {
t.Fatal("unexpected error:", err)
}
Expand Down Expand Up @@ -71,12 +71,12 @@ func TestLoadBundleFromDiskBundlePackage(t *testing.T) {
t.Fatal("unexpected error:", err)
}

err = SaveBundleToDisk(tempDir, &buf, &SaveOptions{Etag: "123"})
err = SaveBundleToDiskWithOptions(tempDir, &buf, &SaveOptions{Etag: "123"})
if err != nil {
t.Fatal("unexpected error:", err)
}

loadedBundle, err := LoadBundleFromDisk(tempDir, &LoadOptions{})
loadedBundle, err := LoadBundleFromDiskWithOptions(tempDir, &LoadOptions{})
if err != nil {
t.Fatal("unexpected error:", err)
}
Expand Down Expand Up @@ -110,12 +110,12 @@ func TestSaveBundleToDisk(t *testing.T) {
t.Fatal("unexpected error:", err)
}

err = SaveBundleToDisk(tempDir, &buf, &SaveOptions{Etag: "123"})
err = SaveBundleToDiskWithOptions(tempDir, &buf, &SaveOptions{Etag: "123"})
if err != nil {
t.Fatal("unexpected error:", err)
}

loadedBundle, err := LoadBundleFromDisk(tempDir, &LoadOptions{})
loadedBundle, err := LoadBundleFromDiskWithOptions(tempDir, &LoadOptions{})
if err != nil {
t.Fatal("unexpected error:", err)
}
Expand Down Expand Up @@ -167,13 +167,13 @@ func TestSaveBundleToDiskOverwrite(t *testing.T) {
sourceBundle2Etag := "456"

// write the first version of the bundle to disk
err = SaveBundleToDisk(tempDir, &buf1, &SaveOptions{Etag: sourceBundle1ETag})
err = SaveBundleToDiskWithOptions(tempDir, &buf1, &SaveOptions{Etag: sourceBundle1ETag})
if err != nil {
t.Fatal("unexpected error:", err)
}

// load the bundle and validate it
loadedBundle1, err := LoadBundleFromDisk(tempDir, &LoadOptions{})
loadedBundle1, err := LoadBundleFromDiskWithOptions(tempDir, &LoadOptions{})
if err != nil {
t.Fatal("unexpected error:", err)
}
Expand All @@ -187,13 +187,13 @@ func TestSaveBundleToDiskOverwrite(t *testing.T) {
}

// overwrite the bundle
err = SaveBundleToDisk(tempDir, &buf2, &SaveOptions{Etag: sourceBundle2Etag})
err = SaveBundleToDiskWithOptions(tempDir, &buf2, &SaveOptions{Etag: sourceBundle2Etag})
if err != nil {
t.Fatal("unexpected error:", err)
}

// load the new bundle and validate it
loadedBundle2, err := LoadBundleFromDisk(tempDir, &LoadOptions{})
loadedBundle2, err := LoadBundleFromDiskWithOptions(tempDir, &LoadOptions{})
if err != nil {
t.Fatal("unexpected error:", err)
}
Expand Down Expand Up @@ -229,12 +229,12 @@ func TestSaveBundleToDiskNewPath(t *testing.T) {

bundlePath := filepath.Join(tempDir, "foo", "bar", "example1")

err = SaveBundleToDisk(bundlePath, &buf, &SaveOptions{Etag: "123"})
err = SaveBundleToDiskWithOptions(bundlePath, &buf, &SaveOptions{Etag: "123"})
if err != nil {
t.Fatal("unexpected error:", err)
}

loadedBundle, err := LoadBundleFromDisk(bundlePath, &LoadOptions{})
loadedBundle, err := LoadBundleFromDiskWithOptions(bundlePath, &LoadOptions{})
if err != nil {
t.Fatal("unexpected error:", err)
}
Expand All @@ -252,7 +252,7 @@ func TestSaveBundleToDiskNil(t *testing.T) {
var err error
srcDir := t.TempDir()

err = SaveBundleToDisk(srcDir, nil, &SaveOptions{})
err = SaveBundleToDiskWithOptions(srcDir, nil, &SaveOptions{})
if err == nil {
t.Fatal("expected error but got nil")
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/bundle/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (p *Plugin) loadAndActivateBundlesFromDisk(ctx context.Context) {

for name, src := range p.config.Bundles {
if p.persistBundle(name) {
b, err := bundleUtils.LoadBundleFromDisk(filepath.Join(p.bundlePersistPath, name), &bundleUtils.LoadOptions{VerificationConfig: src.Signing})
b, err := bundleUtils.LoadBundleFromDiskWithOptions(filepath.Join(p.bundlePersistPath, name), &bundleUtils.LoadOptions{VerificationConfig: src.Signing})
if err != nil {
p.log(name).Error("Failed to load bundle from disk: %v", err)
p.status[name].SetError(err)
Expand Down Expand Up @@ -506,7 +506,7 @@ func (p *Plugin) process(ctx context.Context, name string, u download.Update) {
if u.Bundle.Type() == bundle.SnapshotBundleType && p.persistBundle(name) {
p.log(name).Debug("Persisting bundle to disk in progress.")

err := bundleUtils.SaveBundleToDisk(
err := bundleUtils.SaveBundleToDiskWithOptions(
filepath.Join(p.bundlePersistPath, name),
u.Raw,
&bundleUtils.SaveOptions{Etag: u.ETag},
Expand Down
22 changes: 11 additions & 11 deletions plugins/bundle/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ func TestPluginOneShotBundlePersistence(t *testing.T) {

ensurePluginState(t, plugin, plugins.StateOK)

result, err := bundleUtils.LoadBundleFromDisk(filepath.Join(plugin.bundlePersistPath, bundleName), &bundleUtils.LoadOptions{})
result, err := bundleUtils.LoadBundleFromDiskWithOptions(filepath.Join(plugin.bundlePersistPath, bundleName), &bundleUtils.LoadOptions{})
if err != nil {
t.Fatal("unexpected error:", err)
}
Expand Down Expand Up @@ -774,7 +774,7 @@ func TestPluginOneShotSignedBundlePersistence(t *testing.T) {
ensurePluginState(t, plugin, plugins.StateOK)

// load signed bundle from disk
result, err := bundleUtils.LoadBundleFromDisk(filepath.Join(plugin.bundlePersistPath, bundleName), &bundleUtils.LoadOptions{VerificationConfig: bundles[bundleName].Signing})
result, err := bundleUtils.LoadBundleFromDiskWithOptions(filepath.Join(plugin.bundlePersistPath, bundleName), &bundleUtils.LoadOptions{VerificationConfig: bundles[bundleName].Signing})
if err != nil {
t.Fatal("unexpected error:", err)
}
Expand Down Expand Up @@ -858,7 +858,7 @@ func TestLoadAndActivateBundlesFromDisk(t *testing.T) {
t.Fatal("unexpected error:", err)
}

err = bundleUtils.SaveBundleToDisk(
err = bundleUtils.SaveBundleToDiskWithOptions(
filepath.Join(bundlePersistPath, bundleName),
&buf,
&bundleUtils.SaveOptions{},
Expand Down Expand Up @@ -974,7 +974,7 @@ is_one(x) {
t.Fatal("unexpected error:", err)
}

err = bundleUtils.SaveBundleToDisk(
err = bundleUtils.SaveBundleToDiskWithOptions(
filepath.Join(bundlePersistPath, bundleName),
&buf1,
&bundleUtils.SaveOptions{},
Expand All @@ -988,7 +988,7 @@ is_one(x) {
t.Fatal("unexpected error:", err)
}

err = bundleUtils.SaveBundleToDisk(
err = bundleUtils.SaveBundleToDiskWithOptions(
filepath.Join(bundlePersistPath, bundleNameOther),
&buf2,
&bundleUtils.SaveOptions{},
Expand Down Expand Up @@ -1061,7 +1061,7 @@ allow {
t.Fatal("unexpected error:", err)
}

err = bundleUtils.SaveBundleToDisk(
err = bundleUtils.SaveBundleToDiskWithOptions(
filepath.Join(bundlePersistPath, bundleName),
&buf,
&bundleUtils.SaveOptions{},
Expand Down Expand Up @@ -2502,7 +2502,7 @@ func TestUpgradeLegacyBundleToMuiltiBundleNewBundles(t *testing.T) {
func TestLoadBundleFromDisk(t *testing.T) {

// no bundle on disk
_, err := bundleUtils.LoadBundleFromDisk(filepath.Join("foo", "bar"), nil)
_, err := bundleUtils.LoadBundleFromDiskWithOptions(filepath.Join("foo", "bar"), nil)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
Expand All @@ -2519,7 +2519,7 @@ func TestLoadBundleFromDisk(t *testing.T) {

b := writeTestBundleToDisk(t, bundleDir, false)

result, err := bundleUtils.LoadBundleFromDisk(bundleDir, &bundleUtils.LoadOptions{})
result, err := bundleUtils.LoadBundleFromDiskWithOptions(bundleDir, &bundleUtils.LoadOptions{})
if err != nil {
t.Fatal("unexpected error:", err)
}
Expand All @@ -2532,7 +2532,7 @@ func TestLoadBundleFromDisk(t *testing.T) {
func TestLoadSignedBundleFromDisk(t *testing.T) {

// no bundle on disk
_, err := bundleUtils.LoadBundleFromDisk("foo/bar", nil)
_, err := bundleUtils.LoadBundleFromDiskWithOptions("foo/bar", nil)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
Expand All @@ -2553,7 +2553,7 @@ func TestLoadSignedBundleFromDisk(t *testing.T) {
Signing: bundle.NewVerificationConfig(map[string]*keys.Config{"foo": {Key: "secret", Algorithm: "HS256"}}, "foo", "", nil),
}

result, err := bundleUtils.LoadBundleFromDisk(bundleDir, &bundleUtils.LoadOptions{VerificationConfig: src.Signing})
result, err := bundleUtils.LoadBundleFromDiskWithOptions(bundleDir, &bundleUtils.LoadOptions{VerificationConfig: src.Signing})
if err != nil {
t.Fatal("unexpected error:", err)
}
Expand Down Expand Up @@ -2911,7 +2911,7 @@ func TestPluginReadBundleEtagFromPersistenceDir(t *testing.T) {
t.Fatal("unexpected error:", err)
}

err = bundleUtils.SaveBundleToDisk(
err = bundleUtils.SaveBundleToDiskWithOptions(
path.Join(persistenceDir, "bundles", "test"),
&buf,
&bundleUtils.SaveOptions{Etag: "foo"},
Expand Down
4 changes: 2 additions & 2 deletions plugins/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (c *Discovery) getBundlePersistPath() (string, error) {
func (c *Discovery) loadAndActivateBundleFromDisk(ctx context.Context) {

if c.config != nil && c.config.Persist {
b, err := bundleUtils.LoadBundleFromDisk(
b, err := bundleUtils.LoadBundleFromDiskWithOptions(
filepath.Join(c.bundlePersistPath, c.discoveryBundleDirName()),
&bundleUtils.LoadOptions{VerificationConfig: c.config.Signing},
)
Expand Down Expand Up @@ -309,7 +309,7 @@ func (c *Discovery) processUpdate(ctx context.Context, u download.Update) {

if c.config != nil && c.config.Persist {
c.logger.Debug("Persisting discovery bundle to disk in progress.")
err := bundleUtils.SaveBundleToDisk(
err := bundleUtils.SaveBundleToDiskWithOptions(
filepath.Join(c.bundlePersistPath, c.discoveryBundleDirName()),
u.Raw,
&bundleUtils.SaveOptions{Etag: u.ETag},
Expand Down
10 changes: 4 additions & 6 deletions plugins/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"testing"
"time"

bundleUtils "github.com/open-policy-agent/opa/internal/bundle"

"github.com/open-policy-agent/opa/ast"
bundleApi "github.com/open-policy-agent/opa/bundle"
"github.com/open-policy-agent/opa/download"
Expand Down Expand Up @@ -533,7 +531,7 @@ func TestOneShotWithBundlePersistence(t *testing.T) {

ensurePluginState(t, disco, plugins.StateOK)

result, err := bundleUtils.LoadBundleFromDisk(
result, err := bundleUtils.LoadBundleFromDiskWithOptions(
filepath.Join(disco.bundlePersistPath, disco.discoveryBundleDirName()),
&bundleUtils.LoadOptions{VerificationConfig: disco.config.Signing},
)
Expand Down Expand Up @@ -623,7 +621,7 @@ func TestLoadAndActivateBundleFromDisk(t *testing.T) {
t.Fatal("unexpected error:", err)
}

err = bundleUtils.SaveBundleToDisk(
err = bundleUtils.SaveBundleToDiskWithOptions(
filepath.Join(bundlePersistPath, "config"),
&buf,
&bundleUtils.SaveOptions{},
Expand Down Expand Up @@ -724,7 +722,7 @@ func TestLoadAndActivateSignedBundleFromDisk(t *testing.T) {
t.Fatal("unexpected error:", err)
}

err = bundleUtils.SaveBundleToDisk(
err = bundleUtils.SaveBundleToDiskWithOptions(
filepath.Join(bundlePersistPath, "config"),
&buf,
&bundleUtils.SaveOptions{},
Expand Down Expand Up @@ -819,7 +817,7 @@ func TestLoadAndActivateBundleFromDiskMaxAttempts(t *testing.T) {
t.Fatal("unexpected error:", err)
}

err = bundleUtils.SaveBundleToDisk(
err = bundleUtils.SaveBundleToDiskWithOptions(
filepath.Join(bundlePersistPath, Name),
&buf,
&bundleUtils.SaveOptions{},
Expand Down

0 comments on commit 5d537e9

Please sign in to comment.