tests: updated tests to use local packages

This commit is contained in:
David Allen 2024-12-10 15:09:43 -07:00
parent c8aa4aae93
commit 0fc81ac67c
Signed by: towk
GPG key ID: 793B2924A49B3A3F

View file

@ -3,9 +3,11 @@ package tests
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"testing" "testing"
configurator "github.com/OpenCHAMI/configurator/pkg" configurator "github.com/OpenCHAMI/configurator/pkg"
@ -15,6 +17,12 @@ import (
"github.com/OpenCHAMI/configurator/pkg/util" "github.com/OpenCHAMI/configurator/pkg/util"
) )
var (
workDir string
replaceDir string
err error
)
// A valid test generator that implements the `Generator` interface. // A valid test generator that implements the `Generator` interface.
type TestGenerator struct{} type TestGenerator struct{}
@ -56,47 +64,59 @@ This is another testing Jinja 2 template file using {{plugin_name}}.
// TODO: make sure we can get a target // TODO: make sure we can get a target
// make sure we have the same number of files in file list // make sure we have the same number of files in file list
if len(files) != len(fileMap) { var (
return nil, fmt.Errorf("file list output count is not the same as the input") fileInputCount = len(files)
fileOutputCount = len(fileMap)
)
if fileInputCount != fileOutputCount {
return nil, fmt.Errorf("file output count (%d) is not the same as the input (%d)", fileOutputCount, fileInputCount)
} }
return fileMap, nil return fileMap, nil
} }
func init() {
workDir, err = os.Getwd()
if err != nil {
log.Fatalf("failed to get working directory: %v", err)
}
replaceDir = fmt.Sprintf("%s", filepath.Dir(workDir))
}
// Test building and loading plugins // Test building and loading plugins
func TestPlugin(t *testing.T) { func TestPlugin(t *testing.T) {
var ( var (
testPluginDir = t.TempDir() testPluginDir = t.TempDir()
testPluginPath = fmt.Sprintf("%s/test-plugin.so", testPluginDir) testPluginPath = fmt.Sprintf("%s/test-plugin.so", testPluginDir)
testPluginSourcePath = fmt.Sprintf("%s/test-plugin.go", testPluginDir) testPluginSourcePath = fmt.Sprintf("%s/test-plugin.go", testPluginDir)
testPluginSource = []byte(` testPluginSource = []byte(
package main `package main
import ( import (
configurator "github.com/OpenCHAMI/configurator/pkg" "github.com/OpenCHAMI/configurator/pkg/config"
"github.com/OpenCHAMI/configurator/pkg/generator" "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" }
func (g *TestGenerator) GetVersion() string { return "v1.0.0" } func (g *TestGenerator) GetVersion() string { return "v1.0.0" }
func (g *TestGenerator) GetDescription() string { return "This is a plugin creating for running tests." } func (g *TestGenerator) GetDescription() string {
func (g *TestGenerator) Generate(config *configurator.Config, opts ...generator.Option) (generator.FileMap, error) { return "This is a plugin creating for running tests."
}
func (g *TestGenerator) Generate(config *config.Config, params generator.Params) (generator.FileMap, error) {
return generator.FileMap{"test": []byte("test")}, nil return generator.FileMap{"test": []byte("test")}, nil
} }
var Generator TestGenerator
`) var Generator TestGenerator`)
) )
wd, err := os.Getwd() // get directory to replace remote pkg with local
if err != nil { // _, filename, _, _ := runtime.Caller(0)
t.Errorf("failed to get working directory: %v", err) // replaceDir := fmt.Sprintf("%s", filepath.Dir(workDir))
}
// show all paths to make sure we're using the correct ones // show all paths to make sure we're using the correct ones
fmt.Printf("(TestPlugin) working directory: %v\n", wd) fmt.Printf("(TestPlugin) working directory: %v\n", workDir)
fmt.Printf("(TestPlugin) plugin directory: %v\n", testPluginDir) fmt.Printf("(TestPlugin) plugin directory: %v\n", testPluginDir)
fmt.Printf("(TestPlugin) plugin path: %v\n", testPluginPath) fmt.Printf("(TestPlugin) plugin path: %v\n", testPluginPath)
fmt.Printf("(TestPlugin) plugin source path: %v\n", testPluginSourcePath) fmt.Printf("(TestPlugin) plugin source path: %v\n", testPluginSourcePath)
@ -134,6 +154,12 @@ var Generator TestGenerator
t.Fatalf("failed to execute command: %v\n%s", err, string(output)) t.Fatalf("failed to execute command: %v\n%s", err, string(output))
} }
// use the local `pkg` instead of the release one
cmd = exec.Command("bash", "-c", fmt.Sprintf("go mod edit -replace=github.com/OpenCHAMI/configurator=%s", replaceDir))
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("failed to execute command: %v\n%s", err, string(output))
}
// run `go mod tidy` for dependencies // run `go mod tidy` for dependencies
cmd = exec.Command("bash", "-c", "go mod tidy") cmd = exec.Command("bash", "-c", "go mod tidy")
if output, err := cmd.CombinedOutput(); err != nil { if output, err := cmd.CombinedOutput(); err != nil {
@ -216,16 +242,15 @@ var Generator InvalidGenerator
`) `)
) )
wd, err := os.Getwd()
if err != nil {
t.Errorf("failed to get working directory: %v", err)
}
// show all paths to make sure we're using the correct ones // show all paths to make sure we're using the correct ones
fmt.Printf("(TestPluginWithInvalidOrNoSymbol) working directory: %v\n", wd) fmt.Printf("(TestPluginWithInvalidOrNoSymbol) working directory: %v\n", workDir)
fmt.Printf("(TestPluginWithInvalidOrNoSymbol) plugin directory: %v\n", testPluginDir) fmt.Printf("(TestPluginWithInvalidOrNoSymbol) plugin directory: %v\n", testPluginDir)
fmt.Printf("(TestPluginWithInvalidOrNoSymbol) plugin path: %v\n", testPluginPath) fmt.Printf("(TestPluginWithInvalidOrNoSymbol) plugin path: %v\n", testPluginPath)
fmt.Printf("(TestPluginWithInvalidOrNoSymbol) plugin source path: %v\n", testPluginSourcePath) fmt.Printf("(TestPluginWithInvalidOrNoSymbol) plugin source path: %v\n", testPluginSourcePath)
// get directory to replace remote pkg with local
// _, filename, _, _ := runtime.Caller(0)
// make temporary directory to test plugin // make temporary directory to test plugin
err = os.MkdirAll(testPluginDir, os.ModeDir) err = os.MkdirAll(testPluginDir, os.ModeDir)
if err != nil { if err != nil {
@ -259,6 +284,12 @@ var Generator InvalidGenerator
t.Fatalf("failed to execute command: %v\n%s", err, string(output)) t.Fatalf("failed to execute command: %v\n%s", err, string(output))
} }
// use the local `pkg` instead of the release one
cmd = exec.Command("bash", "-c", fmt.Sprintf("go mod edit -replace=github.com/OpenCHAMI/configurator=%s", replaceDir))
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("failed to execute command: %v\n%s", err, string(output))
}
// run `go mod tidy` for dependencies // run `go mod tidy` for dependencies
cmd = exec.Command("bash", "-c", "go mod tidy") cmd = exec.Command("bash", "-c", "go mod tidy")
if output, err := cmd.CombinedOutput(); err != nil { if output, err := cmd.CombinedOutput(); err != nil {