-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/network-plugin
- Loading branch information
Showing
11 changed files
with
432 additions
and
40 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
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,88 @@ | ||
package plugin | ||
|
||
import ( | ||
"encoding/gob" | ||
"fmt" | ||
"net" | ||
"path" | ||
|
||
hplugin "github.com/hashicorp/go-plugin" | ||
|
||
"github.com/ignite/cli/ignite/pkg/cache" | ||
) | ||
|
||
const ( | ||
cacheFileName = "ignite_plugin_cache.db" | ||
cacheNamespace = "plugin.rpc.context" | ||
) | ||
|
||
var storageCache *cache.Cache[hplugin.ReattachConfig] | ||
|
||
func init() { | ||
gob.Register(hplugin.ReattachConfig{}) | ||
gob.Register(&net.UnixAddr{}) | ||
} | ||
|
||
func writeConfigCache(pluginPath string, conf hplugin.ReattachConfig) error { | ||
if pluginPath == "" { | ||
return fmt.Errorf("provided path is invalid: %s", pluginPath) | ||
} | ||
if conf.Addr == nil { | ||
return fmt.Errorf("plugin Address info cannot be empty") | ||
} | ||
cache, err := newCache() | ||
if err != nil { | ||
return err | ||
} | ||
return cache.Put(pluginPath, conf) | ||
} | ||
|
||
func readConfigCache(pluginPath string) (hplugin.ReattachConfig, error) { | ||
if pluginPath == "" { | ||
return hplugin.ReattachConfig{}, fmt.Errorf("provided path is invalid: %s", pluginPath) | ||
} | ||
cache, err := newCache() | ||
if err != nil { | ||
return hplugin.ReattachConfig{}, err | ||
} | ||
return cache.Get(pluginPath) | ||
} | ||
|
||
func checkConfCache(pluginPath string) bool { | ||
if pluginPath == "" { | ||
return false | ||
} | ||
cache, err := newCache() | ||
if err != nil { | ||
return false | ||
} | ||
_, err = cache.Get(pluginPath) | ||
return err == nil | ||
} | ||
|
||
func deleteConfCache(pluginPath string) error { | ||
if pluginPath == "" { | ||
return fmt.Errorf("provided path is invalid: %s", pluginPath) | ||
} | ||
cache, err := newCache() | ||
if err != nil { | ||
return err | ||
} | ||
return cache.Delete(pluginPath) | ||
} | ||
|
||
func newCache() (*cache.Cache[hplugin.ReattachConfig], error) { | ||
cacheRootDir, err := PluginsPath() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if storageCache == nil { | ||
storage, err := cache.NewStorage(path.Join(cacheRootDir, cacheFileName)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c := cache.New[hplugin.ReattachConfig](storage, cacheNamespace) | ||
storageCache = &c | ||
} | ||
return storageCache, nil | ||
} |
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,109 @@ | ||
package plugin | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
hplugin "github.com/hashicorp/go-plugin" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestReadWriteConfigCache(t *testing.T) { | ||
t.Run("Should cache plugin config and read from cache", func(t *testing.T) { | ||
const path = "/path/to/awesome/plugin" | ||
unixFD, _ := net.ResolveUnixAddr("unix", "/var/folders/5k/sv4bxrs102n_6rr7430jc7j80000gn/T/plugin193424090") | ||
|
||
rc := hplugin.ReattachConfig{ | ||
Protocol: hplugin.ProtocolNetRPC, | ||
ProtocolVersion: hplugin.CoreProtocolVersion, | ||
Addr: unixFD, | ||
Pid: 24464, | ||
} | ||
|
||
err := writeConfigCache(path, rc) | ||
require.NoError(t, err) | ||
|
||
c, err := readConfigCache(path) | ||
require.NoError(t, err) | ||
require.Equal(t, rc, c) | ||
}) | ||
|
||
t.Run("Should error writing bad plugin config to cache", func(t *testing.T) { | ||
const path = "/path/to/awesome/plugin" | ||
rc := hplugin.ReattachConfig{ | ||
Protocol: hplugin.ProtocolNetRPC, | ||
ProtocolVersion: hplugin.CoreProtocolVersion, | ||
Addr: nil, | ||
Pid: 24464, | ||
} | ||
|
||
err := writeConfigCache(path, rc) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("Should error with invalid plugin path", func(t *testing.T) { | ||
const path = "" | ||
rc := hplugin.ReattachConfig{ | ||
Protocol: hplugin.ProtocolNetRPC, | ||
ProtocolVersion: hplugin.CoreProtocolVersion, | ||
Addr: nil, | ||
Pid: 24464, | ||
} | ||
|
||
err := writeConfigCache(path, rc) | ||
require.Error(t, err) | ||
}) | ||
} | ||
|
||
func TestDeleteConfCache(t *testing.T) { | ||
t.Run("Delete plugin config after write to cache should remove from cache", func(t *testing.T) { | ||
const path = "/path/to/awesome/plugin" | ||
unixFD, _ := net.ResolveUnixAddr("unix", "/var/folders/5k/sv4bxrs102n_6rr7430jc7j80000gn/T/plugin193424090") | ||
|
||
rc := hplugin.ReattachConfig{ | ||
Protocol: hplugin.ProtocolNetRPC, | ||
ProtocolVersion: hplugin.CoreProtocolVersion, | ||
Addr: unixFD, | ||
Pid: 24464, | ||
} | ||
|
||
err := writeConfigCache(path, rc) | ||
require.NoError(t, err) | ||
|
||
err = deleteConfCache(path) | ||
require.NoError(t, err) | ||
|
||
// there should be an error after deleting the config from the cache | ||
_, err = readConfigCache(path) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("Delete plugin config should return error given empty path", func(t *testing.T) { | ||
const path = "" | ||
err := deleteConfCache(path) | ||
require.Error(t, err) | ||
}) | ||
} | ||
|
||
func TestCheckConfCache(t *testing.T) { | ||
const path = "/path/to/awesome/plugin" | ||
unixFD, _ := net.ResolveUnixAddr("unix", "/var/folders/5k/sv4bxrs102n_6rr7430jc7j80000gn/T/plugin193424090") | ||
|
||
rc := hplugin.ReattachConfig{ | ||
Protocol: hplugin.ProtocolNetRPC, | ||
ProtocolVersion: hplugin.CoreProtocolVersion, | ||
Addr: unixFD, | ||
Pid: 24464, | ||
} | ||
|
||
t.Run("Cache should be hydrated", func(t *testing.T) { | ||
err := writeConfigCache(path, rc) | ||
require.NoError(t, err) | ||
require.Equal(t, true, checkConfCache(path)) | ||
}) | ||
|
||
t.Run("Cache should be empty", func(t *testing.T) { | ||
_ = deleteConfCache(path) | ||
require.Equal(t, false, checkConfCache(path)) | ||
}) | ||
} |
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
Oops, something went wrong.