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,42 +107,80 @@ 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)
// dump the plugin source code to a file if err != nil {
err := os.WriteFile(testPluginPath, testPlugin, os.ModePerm) t.Fatalf("failed to make temporary directory: %v", err)
if err != nil { }
t.Fatalf("failed to write test plugin file: %v", err)
}
// execute command to build the plugin // show all paths to make sure we're using the correct ones
cmd := exec.Command("go", "build", "-buildmode=plugin", fmt.Sprintf("-o=%s/test.so", tempDir)) fmt.Printf("test directory: %v\n", testPluginDir)
if cmd.Err != nil { fmt.Printf("test plugin path: %v\n", testPluginPath)
t.Fatalf("failed to build plugin: %v", cmd.Err) fmt.Printf("test plugin source path: %v\n", testPluginSourcePath)
}
// stat the file to confirm that it was built // dump the plugin source code to a file
fileInfo, err := os.Stat(testPluginPath) err = os.WriteFile(testPluginSourcePath, testPluginSource, os.ModePerm)
if err != nil { if err != nil {
t.Fatalf("failed to stat plugin file: %v", err) t.Fatalf("failed to write test 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")
}
}) // 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 // 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) }
}
// 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 { if _, ok := gen.(interface {
GetName() string GetName() string
GetVersion() string GetVersion() string
@ -142,25 +189,7 @@ var Generator TestGenerator
}); !ok { }); !ok {
t.Error("plugin does not implement all of the generator interface") 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" { 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")
} }
}) })