Updated imports to use pkg/ instead of internal/

This commit is contained in:
David Allen 2024-07-10 12:19:27 -06:00
parent 7361ec739f
commit 7115954cf1
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
9 changed files with 102 additions and 73 deletions

View file

@ -5,8 +5,8 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
configurator "github.com/OpenCHAMI/configurator/internal" configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/internal/util" "github.com/OpenCHAMI/configurator/pkg/util"
) )
var configCmd = &cobra.Command{ var configCmd = &cobra.Command{

View file

@ -5,7 +5,7 @@ import (
"maps" "maps"
"strings" "strings"
"github.com/OpenCHAMI/configurator/internal/generator" "github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )

View file

@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"os" "os"
configurator "github.com/OpenCHAMI/configurator/internal" configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/internal/util" "github.com/OpenCHAMI/configurator/pkg/util"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )

View file

@ -12,7 +12,7 @@ import (
"os" "os"
"time" "time"
"github.com/OpenCHAMI/configurator/internal/util" "github.com/OpenCHAMI/configurator/pkg/util"
) )
type ClientOption func(*SmdClient) type ClientOption func(*SmdClient)

View file

@ -8,8 +8,8 @@ import (
"path/filepath" "path/filepath"
"plugin" "plugin"
configurator "github.com/OpenCHAMI/configurator/internal" configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/internal/util" "github.com/OpenCHAMI/configurator/pkg/util"
"github.com/nikolalohinski/gonja/v2" "github.com/nikolalohinski/gonja/v2"
"github.com/nikolalohinski/gonja/v2/exec" "github.com/nikolalohinski/gonja/v2/exec"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"

View file

@ -3,9 +3,9 @@ package main
import ( import (
"fmt" "fmt"
configurator "github.com/OpenCHAMI/configurator/internal" configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/internal/generator" "github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/configurator/internal/util" "github.com/OpenCHAMI/configurator/pkg/util"
) )
type Conman struct{} type Conman struct{}

View file

@ -4,9 +4,9 @@ import (
"fmt" "fmt"
"strings" "strings"
configurator "github.com/OpenCHAMI/configurator/internal" configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/internal/generator" "github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/configurator/internal/util" "github.com/OpenCHAMI/configurator/pkg/util"
) )
type DnsMasq struct{} type DnsMasq struct{}

View file

@ -9,8 +9,8 @@ import (
"net/http" "net/http"
"time" "time"
configurator "github.com/OpenCHAMI/configurator/internal" configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/internal/generator" "github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/jwtauth/v5" "github.com/OpenCHAMI/jwtauth/v5"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware" "github.com/go-chi/chi/v5/middleware"

View file

@ -7,10 +7,10 @@ import (
"os/exec" "os/exec"
"testing" "testing"
configurator "github.com/OpenCHAMI/configurator/internal" configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/internal/generator" "github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/configurator/internal/server" "github.com/OpenCHAMI/configurator/pkg/server"
"github.com/OpenCHAMI/configurator/internal/util" "github.com/OpenCHAMI/configurator/pkg/util"
) )
// A valid test generator that implements the `Generator` interface. // A valid test generator that implements the `Generator` interface.
@ -81,11 +81,20 @@ type InvalidGenerator struct{}
// Test building and loading plugins // Test building and loading plugins
func TestPlugin(t *testing.T) { func TestPlugin(t *testing.T) {
var ( var (
tempDir = t.TempDir() testPluginDir = t.TempDir()
testPluginPath = fmt.Sprintf("%s/testplugin.go", tempDir) testPluginPath = fmt.Sprintf("%s/testplugin.so", testPluginDir)
testPlugin = []byte(` testPluginSourcePath = fmt.Sprintf("%s/testplugin.go", testPluginDir)
testPluginSource = []byte(`
package main 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{} type TestGenerator struct{}
func (g *TestGenerator) GetName() string { return "test" } func (g *TestGenerator) GetName() string { return "test" }
@ -98,22 +107,46 @@ var Generator TestGenerator
`) `)
) )
// build a test plugin // make temporary directory to test plugin
t.Run("build", func(t *testing.T) { err := os.MkdirAll(testPluginDir, os.ModeDir)
if err != nil {
t.Fatalf("failed to make temporary directory: %v", 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)
// dump the plugin source code to a file // dump the plugin source code to a file
err := os.WriteFile(testPluginPath, testPlugin, os.ModePerm) err = os.WriteFile(testPluginSourcePath, testPluginSource, os.ModePerm)
if err != nil { if err != nil {
t.Fatalf("failed to write test plugin file: %v", err) t.Fatalf("failed to write test plugin file: %v", err)
} }
// execute command to build the plugin // make sure the source file was actually written
cmd := exec.Command("go", "build", "-buildmode=plugin", fmt.Sprintf("-o=%s/test.so", tempDir)) fileInfo, err := os.Stat(testPluginSourcePath)
if cmd.Err != nil { if err != nil {
t.Fatalf("failed to build plugin: %v", cmd.Err) t.Fatalf("failed to stat path: %v", err)
}
if fileInfo.IsDir() {
t.Fatalf("expected file but found directory")
} }
// stat the file to confirm that it was built // change to testing directory to run command
fileInfo, err := os.Stat(testPluginPath) // 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 { if err != nil {
t.Fatalf("failed to stat plugin file: %v", err) t.Fatalf("failed to stat plugin file: %v", err)
} }
@ -124,11 +157,8 @@ var Generator TestGenerator
t.Fatal("found an empty file or file with size of 0 bytes") t.Fatal("found an empty file or file with size of 0 bytes")
} }
})
// test loading plugins both individually and in a dir // test loading plugins both individually and in a dir
t.Run("load", func(t *testing.T) { gen, err := generator.LoadPlugin(testPluginSourcePath)
gen, err := generator.LoadPlugin(testPluginPath)
if err != nil { if err != nil {
t.Fatalf("failed to load the test plugin: %v", err) t.Fatalf("failed to load the test plugin: %v", err)
} }
@ -144,9 +174,9 @@ var Generator TestGenerator
} }
// test loading plugins from a directory (should just load a single one) // test loading plugins from a directory (should just load a single one)
gens, err := generator.LoadPlugins(tempDir) gens, err := generator.LoadPlugins(testPluginDir)
if err != nil { if err != nil {
t.Fatalf("failed to load plugins in '%s': %v", tempDir, err) 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) // test all of the plugins loaded from a directory (should expect same result as above)
@ -160,7 +190,6 @@ var Generator TestGenerator
t.Error("plugin does not implement all of the generator interface") 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" { if gen.GetVersion() != "v1.0.0" {
t.Error("test generator return unexpected version") 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") t.Error("test generator return unexpected description")
} }
}) })