diff --git a/cmd/config.go b/cmd/config.go index 6596988..bd26e7c 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -5,8 +5,8 @@ import ( "github.com/spf13/cobra" - configurator "github.com/OpenCHAMI/configurator/internal" - "github.com/OpenCHAMI/configurator/internal/util" + configurator "github.com/OpenCHAMI/configurator/pkg" + "github.com/OpenCHAMI/configurator/pkg/util" ) var configCmd = &cobra.Command{ diff --git a/cmd/inspect.go b/cmd/inspect.go index 0a543c3..a2b0adf 100644 --- a/cmd/inspect.go +++ b/cmd/inspect.go @@ -5,7 +5,7 @@ import ( "maps" "strings" - "github.com/OpenCHAMI/configurator/internal/generator" + "github.com/OpenCHAMI/configurator/pkg/generator" "github.com/spf13/cobra" ) diff --git a/cmd/root.go b/cmd/root.go index 353523e..cbbfab7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,8 +4,8 @@ import ( "fmt" "os" - configurator "github.com/OpenCHAMI/configurator/internal" - "github.com/OpenCHAMI/configurator/internal/util" + configurator "github.com/OpenCHAMI/configurator/pkg" + "github.com/OpenCHAMI/configurator/pkg/util" "github.com/spf13/cobra" ) diff --git a/pkg/client.go b/pkg/client.go index f5de764..bee15d8 100644 --- a/pkg/client.go +++ b/pkg/client.go @@ -12,7 +12,7 @@ import ( "os" "time" - "github.com/OpenCHAMI/configurator/internal/util" + "github.com/OpenCHAMI/configurator/pkg/util" ) type ClientOption func(*SmdClient) diff --git a/pkg/generator/generator.go b/pkg/generator/generator.go index 1ed6c9d..34cb25b 100644 --- a/pkg/generator/generator.go +++ b/pkg/generator/generator.go @@ -8,8 +8,8 @@ import ( "path/filepath" "plugin" - configurator "github.com/OpenCHAMI/configurator/internal" - "github.com/OpenCHAMI/configurator/internal/util" + configurator "github.com/OpenCHAMI/configurator/pkg" + "github.com/OpenCHAMI/configurator/pkg/util" "github.com/nikolalohinski/gonja/v2" "github.com/nikolalohinski/gonja/v2/exec" "github.com/sirupsen/logrus" diff --git a/pkg/generator/plugins/conman/conman.go b/pkg/generator/plugins/conman/conman.go index b324238..2f04978 100644 --- a/pkg/generator/plugins/conman/conman.go +++ b/pkg/generator/plugins/conman/conman.go @@ -3,9 +3,9 @@ package main import ( "fmt" - configurator "github.com/OpenCHAMI/configurator/internal" - "github.com/OpenCHAMI/configurator/internal/generator" - "github.com/OpenCHAMI/configurator/internal/util" + configurator "github.com/OpenCHAMI/configurator/pkg" + "github.com/OpenCHAMI/configurator/pkg/generator" + "github.com/OpenCHAMI/configurator/pkg/util" ) type Conman struct{} diff --git a/pkg/generator/plugins/dnsmasq/dnsmasq.go b/pkg/generator/plugins/dnsmasq/dnsmasq.go index 6483fd1..c5e0ede 100644 --- a/pkg/generator/plugins/dnsmasq/dnsmasq.go +++ b/pkg/generator/plugins/dnsmasq/dnsmasq.go @@ -4,9 +4,9 @@ import ( "fmt" "strings" - configurator "github.com/OpenCHAMI/configurator/internal" - "github.com/OpenCHAMI/configurator/internal/generator" - "github.com/OpenCHAMI/configurator/internal/util" + configurator "github.com/OpenCHAMI/configurator/pkg" + "github.com/OpenCHAMI/configurator/pkg/generator" + "github.com/OpenCHAMI/configurator/pkg/util" ) type DnsMasq struct{} diff --git a/pkg/server/server.go b/pkg/server/server.go index 0e63a24..6b969cd 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -9,8 +9,8 @@ import ( "net/http" "time" - configurator "github.com/OpenCHAMI/configurator/internal" - "github.com/OpenCHAMI/configurator/internal/generator" + configurator "github.com/OpenCHAMI/configurator/pkg" + "github.com/OpenCHAMI/configurator/pkg/generator" "github.com/OpenCHAMI/jwtauth/v5" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" diff --git a/tests/generate_test.go b/tests/generate_test.go index 37ca89e..9a21d1e 100644 --- a/tests/generate_test.go +++ b/tests/generate_test.go @@ -7,10 +7,10 @@ import ( "os/exec" "testing" - configurator "github.com/OpenCHAMI/configurator/internal" - "github.com/OpenCHAMI/configurator/internal/generator" - "github.com/OpenCHAMI/configurator/internal/server" - "github.com/OpenCHAMI/configurator/internal/util" + configurator "github.com/OpenCHAMI/configurator/pkg" + "github.com/OpenCHAMI/configurator/pkg/generator" + "github.com/OpenCHAMI/configurator/pkg/server" + "github.com/OpenCHAMI/configurator/pkg/util" ) // A valid test generator that implements the `Generator` interface. @@ -81,11 +81,20 @@ type InvalidGenerator struct{} // Test building and loading plugins func TestPlugin(t *testing.T) { var ( - tempDir = t.TempDir() - testPluginPath = fmt.Sprintf("%s/testplugin.go", tempDir) - testPlugin = []byte(` + testPluginDir = t.TempDir() + testPluginPath = fmt.Sprintf("%s/testplugin.so", testPluginDir) + testPluginSourcePath = fmt.Sprintf("%s/testplugin.go", testPluginDir) + testPluginSource = []byte(` package main +import ( + "fmt" + + configurator "github.com/OpenCHAMI/configurator/pkg" + "github.com/OpenCHAMI/configurator/pkg/generator" + "github.com/OpenCHAMI/configurator/pkg/util" +) + type TestGenerator struct{} func (g *TestGenerator) GetName() string { return "test" } @@ -98,42 +107,80 @@ var Generator TestGenerator `) ) - // build a test plugin - t.Run("build", func(t *testing.T) { - // dump the plugin source code to a file - err := os.WriteFile(testPluginPath, testPlugin, os.ModePerm) - if err != nil { - t.Fatalf("failed to write test plugin file: %v", err) - } + // make temporary directory to test plugin + err := os.MkdirAll(testPluginDir, os.ModeDir) + if err != nil { + t.Fatalf("failed to make temporary directory: %v", err) + } - // execute command to build the plugin - cmd := exec.Command("go", "build", "-buildmode=plugin", fmt.Sprintf("-o=%s/test.so", tempDir)) - if cmd.Err != nil { - t.Fatalf("failed to build plugin: %v", cmd.Err) - } + // show all paths to make sure we're using the correct ones + fmt.Printf("test directory: %v\n", testPluginDir) + fmt.Printf("test plugin path: %v\n", testPluginPath) + fmt.Printf("test plugin source path: %v\n", testPluginSourcePath) - // stat the file to confirm that it was built - fileInfo, err := os.Stat(testPluginPath) - if err != nil { - t.Fatalf("failed to stat plugin file: %v", err) - } - if fileInfo.IsDir() { - t.Fatalf("directory file but a file was expected") - } - if fileInfo.Size() <= 0 { - t.Fatal("found an empty file or file with size of 0 bytes") - } + // dump the plugin source code to a file + err = os.WriteFile(testPluginSourcePath, testPluginSource, os.ModePerm) + if err != nil { + t.Fatalf("failed to write test plugin file: %v", err) + } - }) + // make sure the source file was actually written + fileInfo, err := os.Stat(testPluginSourcePath) + if err != nil { + t.Fatalf("failed to stat path: %v", err) + } + if fileInfo.IsDir() { + t.Fatalf("expected file but found directory") + } + + // change to testing directory to run command + // err = os.Chdir(testPluginDir) + // if err != nil { + // t.Fatalf("failed to 'cd' to temporary directory: %v", err) + // } + + // execute command to build the plugin + cmd := exec.Command("go", "build", "-buildmode=plugin", fmt.Sprintf("-o=%s", testPluginPath), testPluginSourcePath) + if output, err := cmd.Output(); err != nil { + t.Fatalf("failed to execute command: %v\n%s", err, string(output)) + } + + // stat the file to confirm that the plugin was built + fileInfo, err = os.Stat(testPluginPath) + if err != nil { + t.Fatalf("failed to stat plugin file: %v", err) + } + if fileInfo.IsDir() { + t.Fatalf("directory file but a file was expected") + } + if fileInfo.Size() <= 0 { + t.Fatal("found an empty file or file with size of 0 bytes") + } // test loading plugins both individually and in a dir - t.Run("load", func(t *testing.T) { - gen, err := generator.LoadPlugin(testPluginPath) - if err != nil { - t.Fatalf("failed to load the test plugin: %v", err) - } + gen, err := generator.LoadPlugin(testPluginSourcePath) + if err != nil { + t.Fatalf("failed to load the test plugin: %v", err) + } - // test that we have all expected methods with type assertions + // test that we have all expected methods with type assertions + if _, ok := gen.(interface { + GetName() string + GetVersion() string + GetDescription() string + Generate(*configurator.Config, ...util.Option) (generator.FileMap, error) + }); !ok { + t.Error("plugin does not implement all of the generator interface") + } + + // test loading plugins from a directory (should just load a single one) + gens, err := generator.LoadPlugins(testPluginDir) + if err != nil { + t.Fatalf("failed to load plugins in '%s': %v", testPluginDir, err) + } + + // test all of the plugins loaded from a directory (should expect same result as above) + for _, gen := range gens { if _, ok := gen.(interface { GetName() string GetVersion() string @@ -142,25 +189,7 @@ var Generator TestGenerator }); !ok { t.Error("plugin does not implement all of the generator interface") } - - // test loading plugins from a directory (should just load a single one) - gens, err := generator.LoadPlugins(tempDir) - if err != nil { - t.Fatalf("failed to load plugins in '%s': %v", tempDir, err) - } - - // test all of the plugins loaded from a directory (should expect same result as above) - for _, gen := range gens { - if _, ok := gen.(interface { - GetName() string - GetVersion() string - GetDescription() string - Generate(*configurator.Config, ...util.Option) (generator.FileMap, error) - }); !ok { - t.Error("plugin does not implement all of the generator interface") - } - } - }) + } } @@ -184,7 +213,7 @@ func TestGenerateExample(t *testing.T) { if gen.GetVersion() != "v1.0.0" { t.Error("test generator return unexpected version") } - if gen.GetDescription() != "test" { + if gen.GetDescription() != "This is a plugin creating for running tests." { t.Error("test generator return unexpected description") } })