Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zheng <[email protected]>
  • Loading branch information
Two-Hearts committed Dec 12, 2023
1 parent 6fb5ac6 commit 3f4aa31
Showing 1 changed file with 97 additions and 49 deletions.
146 changes: 97 additions & 49 deletions plugin/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,8 @@ func TestManager_Install(t *testing.T) {
t.Fatalf("failed to create %s: %v", newPluginDir, err)
}
defer os.RemoveAll(newPluginDir)
pluginFile, err := os.Create(newPluginFilePath)
if err != nil {
t.Fatalf("failed to create %s: %v", newPluginFilePath, err)
}
if err := pluginFile.Close(); err != nil {
t.Fatalf("failed to close %s: %v", newPluginFilePath, err)
if err := createFileAndChmod(newPluginFilePath, 0666); err != nil {
t.Fatal(err)
}
mgr := NewCLIManager(mockfs.NewSysFSWithRootMock(fstest.MapFS{}, "testdata/plugins"))

Expand Down Expand Up @@ -183,8 +179,7 @@ func TestManager_Install(t *testing.T) {
PluginPath: newPluginFilePath,
Overwrite: true,
}
_, _, err = mgr.Install(context.Background(), installOpts)
if err != nil {
if _, _, err := mgr.Install(context.Background(), installOpts); err != nil {
t.Fatalf("expecting error to be nil, but got %v", err)
}
})
Expand All @@ -196,12 +191,8 @@ func TestManager_Install(t *testing.T) {
t.Fatalf("failed to create %s: %v", newPluginDir, err)
}
defer os.RemoveAll(newPluginDir)
pluginFile, err := os.Create(newPluginFilePath)
if err != nil {
t.Fatalf("failed to create %s: %v", newPluginFilePath, err)
}
if err := pluginFile.Close(); err != nil {
t.Fatalf("failed to close %s: %v", newPluginFilePath, err)
if err := createFileAndChmod(newPluginFilePath, 0666); err != nil {
t.Fatal(err)
}
executor = testInstallCommander{
newPluginFilePath: newPluginFilePath,
Expand Down Expand Up @@ -233,8 +224,8 @@ func TestManager_Install(t *testing.T) {
installOpts := CLIInstallOptions{
PluginPath: newPluginFilePath,
}
_, _, err = mgr.Install(context.Background(), installOpts)
expectedErrorMsg := "plugin foo with version 1.0.0 already exists"
_, _, err := mgr.Install(context.Background(), installOpts)
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
}
Expand All @@ -250,8 +241,8 @@ func TestManager_Install(t *testing.T) {
installOpts := CLIInstallOptions{
PluginPath: newPluginFilePath,
}
_, _, err = mgr.Install(context.Background(), installOpts)
expectedErrorMsg := "the installing plugin version 0.1.0 is lower than the existing plugin version 1.0.0"
_, _, err := mgr.Install(context.Background(), installOpts)
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
}
Expand All @@ -264,12 +255,8 @@ func TestManager_Install(t *testing.T) {
t.Fatalf("failed to create %s: %v", newPluginDir, err)
}
defer os.RemoveAll(newPluginDir)
pluginFile, err := os.Create(newPluginFilePath)
if err != nil {
t.Fatalf("failed to create %s: %v", newPluginFilePath, err)
}
if err := pluginFile.Close(); err != nil {
t.Fatalf("failed to close %s: %v", newPluginFilePath, err)
if err := createFileAndChmod(newPluginFilePath, 0666); err != nil {
t.Fatal(err)
}
executor = testInstallCommander{
newPluginFilePath: newPluginFilePath,
Expand All @@ -278,8 +265,8 @@ func TestManager_Install(t *testing.T) {
installOpts := CLIInstallOptions{
PluginPath: newPluginFilePath,
}
_, _, err = mgr.Install(context.Background(), installOpts)
expectedErrorMsg := "failed to get plugin name from file path testdata/bar/bar: invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}, but got bar"
_, _, err := mgr.Install(context.Background(), installOpts)
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
}
Expand All @@ -292,12 +279,8 @@ func TestManager_Install(t *testing.T) {
t.Fatalf("failed to create %s: %v", newPluginDir, err)
}
defer os.RemoveAll(newPluginDir)
pluginFile, err := os.Create(newPluginFilePath)
if err != nil {
t.Fatalf("failed to create %s: %v", newPluginFilePath, err)
}
if err := pluginFile.Close(); err != nil {
t.Fatalf("failed to close %s: %v", newPluginFilePath, err)
if err := createFileAndChmod(newPluginFilePath, 0666); err != nil {
t.Fatal(err)
}
executor = testInstallCommander{
newPluginFilePath: newPluginFilePath,
Expand All @@ -306,8 +289,8 @@ func TestManager_Install(t *testing.T) {
installOpts := CLIInstallOptions{
PluginPath: newPluginFilePath,
}
_, _, err = mgr.Install(context.Background(), installOpts)
expectedErrorMsg := "invalid plugin file extension. Expecting file notation-bar, but got notation-bar.exe"
_, _, err := mgr.Install(context.Background(), installOpts)
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
}
Expand All @@ -322,8 +305,8 @@ func TestManager_Install(t *testing.T) {
installOpts := CLIInstallOptions{
PluginPath: newPluginFilePath,
}
_, _, err = mgr.Install(context.Background(), installOpts)
expectedErrorMsg := "failed to parse plugin from directory testdata/bar/notation-bar: stat testdata/bar/notation-bar: no such file or directory"
_, _, err := mgr.Install(context.Background(), installOpts)
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
}
Expand All @@ -350,8 +333,8 @@ func TestManager_Install(t *testing.T) {
installOpts := CLIInstallOptions{
PluginPath: newPluginFilePath,
}
_, _, err = mgr.Install(context.Background(), installOpts)
expectedErrorMsg := "failed to get metadata of new plugin: executable name must be \"notation-foobar\" instead of \"notation-bar\""
_, _, err = mgr.Install(context.Background(), installOpts)
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
}
Expand All @@ -367,8 +350,8 @@ func TestManager_Install(t *testing.T) {
installOpts := CLIInstallOptions{
PluginPath: newPluginFilePath,
}
_, _, err = mgr.Install(context.Background(), installOpts)
expectedErrorMsg := "failed to get metadata of existing plugin: executable name must be \"notation-bar\" instead of \"notation-foo\""
_, _, err := mgr.Install(context.Background(), installOpts)
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
}
Expand All @@ -385,7 +368,7 @@ func TestManager_Install(t *testing.T) {
PluginPath: newPluginFilePath,
Overwrite: true,
}
_, _, err = mgr.Install(context.Background(), installOpts)
_, _, err := mgr.Install(context.Background(), installOpts)
if err != nil {
t.Fatalf("expecting error to be nil, but got %v", err)
}
Expand All @@ -400,22 +383,44 @@ func TestManager_Install(t *testing.T) {
t.Fatalf("failed to create %s: %v", newPluginDir, err)
}
defer os.RemoveAll(newPluginDir)
pluginFile, err := os.Create(newPluginFilePath)
if err != nil {
t.Fatalf("failed to create %s: %v", newPluginFilePath, err)
if err := createFileAndChmod(newPluginFilePath, 0700); err != nil {
t.Fatal(err)
}
if err := pluginFile.Chmod(0700); err != nil {
t.Fatalf("failed to chmod %s: %v", newPluginFilePath, err)
if err := createFileAndChmod(newPluginLibPath, 0666); err != nil {
t.Fatal(err)
}
if err := pluginFile.Close(); err != nil {
t.Fatalf("failed to close %s: %v", newPluginFilePath, err)
executor = testInstallCommander{
existedPluginFilePath: existedPluginFilePath,
newPluginFilePath: newPluginFilePath,
existedPluginStdout: metadataJSON(validMetadata),
newPluginStdout: metadataJSON(validMetadataHigherVersion),
}
pluginLibFile, err := os.Create(newPluginLibPath)
if err != nil {
t.Fatalf("failed to create %s: %v", newPluginLibPath, err)
installOpts := CLIInstallOptions{
PluginPath: newPluginDir,
}
expectedErrorMsg := "failed to get metadata of existing plugin: executable name must be \"notation-bar\" instead of \"notation-foo\""
existingPluginMetadata, newPluginMetadata, err := mgr.Install(context.Background(), installOpts)
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
}
if existingPluginMetadata.Version != "1.0.0" {
t.Fatalf("expecting existing plugin metadata to be 1.0.0, but got %s", existingPluginMetadata.Version)
}
if newPluginMetadata.Version != "1.1.0" {
t.Fatalf("expecting new plugin metadata to be 1.1.0, but got %s", newPluginMetadata.Version)
}
if err := pluginLibFile.Close(); err != nil {
t.Fatalf("failed to close %s: %v", newPluginLibPath, err)
})

t.Run("fail to install from plugin dir due to no plugin executable file", func(t *testing.T) {
existedPluginFilePath := "testdata/plugins/foo/notation-foo"
newPluginFilePath := "testdata/foo/foo"
newPluginDir := filepath.Dir(newPluginFilePath)
if err := os.MkdirAll(newPluginDir, 0777); err != nil {
t.Fatalf("failed to create %s: %v", newPluginDir, err)
}
defer os.RemoveAll(newPluginDir)
if err := createFileAndChmod(newPluginFilePath, 0666); err != nil {
t.Fatal(err)
}
executor = testInstallCommander{
existedPluginFilePath: existedPluginFilePath,
Expand All @@ -426,9 +431,41 @@ func TestManager_Install(t *testing.T) {
installOpts := CLIInstallOptions{
PluginPath: newPluginDir,
}
_, _, err = mgr.Install(context.Background(), installOpts)
if err != nil {
t.Fatalf("expecting error to be nil, but got %v", err)
expectedErrorMsg := "no plugin executable file was found under testdata/foo"
_, _, err := mgr.Install(context.Background(), installOpts)
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
}
})

t.Run("fail to install from plugin dir due to more than one plugin executable file", func(t *testing.T) {
existedPluginFilePath := "testdata/plugins/foo/notation-foo"
newPluginFilePath := "testdata/foo/notation-foo1"
newPluginFilePath2 := "testdata/foo/notation-foo2"
newPluginDir := filepath.Dir(newPluginFilePath)
if err := os.MkdirAll(newPluginDir, 0777); err != nil {
t.Fatalf("failed to create %s: %v", newPluginDir, err)
}
defer os.RemoveAll(newPluginDir)
if err := createFileAndChmod(newPluginFilePath, 0700); err != nil {
t.Fatal(err)
}
if err := createFileAndChmod(newPluginFilePath2, 0700); err != nil {
t.Fatal(err)
}
executor = testInstallCommander{
existedPluginFilePath: existedPluginFilePath,
newPluginFilePath: newPluginFilePath,
existedPluginStdout: metadataJSON(validMetadata),
newPluginStdout: metadataJSON(validMetadataHigherVersion),
}
installOpts := CLIInstallOptions{
PluginPath: newPluginDir,
}
expectedErrorMsg := "found more than one plugin executable files under testdata/foo"
_, _, err := mgr.Install(context.Background(), installOpts)
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err)
}
})
}
Expand Down Expand Up @@ -468,3 +505,14 @@ func metadataJSON(m proto.GetMetadataResponse) []byte {
}
return d
}

func createFileAndChmod(path string, mode fs.FileMode) error {
f, err := os.Create(path)
if err != nil {
return err
}
if err := f.Chmod(mode); err != nil {
return err
}
return f.Close()
}

0 comments on commit 3f4aa31

Please sign in to comment.