Merge pull request #9 from OpenCHAMI/unit-tests

Update testing code
This commit is contained in:
David Allen 2024-07-15 16:19:08 -06:00 committed by GitHub
commit ab3ed902ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 49 additions and 29 deletions

2
.gitignore vendored
View file

@ -1,4 +1,4 @@
**configurator** **configurator
**.yaml **.yaml
**.yml **.yml
**.so **.so

View file

@ -155,6 +155,17 @@ targets: # targets to call with --target flag
The `server` section sets the properties for running the `configurator` tool as a service and is not required if you're only using the CLI. Also note that the `jwks-uri` parameter is only needs for protecting endpoints. If it is not set, then the API is entirely public. The `smd` section tells the `configurator` tool where to find SMD to pull state management data used by the internal client. The `templates` section is where the paths are mapped to each generator plugin by its name (see the [`Creating Generator Plugins`](#creating-generator-plugins) section for details). The `plugins` is a list of paths to load generator plugins. The `server` section sets the properties for running the `configurator` tool as a service and is not required if you're only using the CLI. Also note that the `jwks-uri` parameter is only needs for protecting endpoints. If it is not set, then the API is entirely public. The `smd` section tells the `configurator` tool where to find SMD to pull state management data used by the internal client. The `templates` section is where the paths are mapped to each generator plugin by its name (see the [`Creating Generator Plugins`](#creating-generator-plugins) section for details). The `plugins` is a list of paths to load generator plugins.
## Running the Tests
The `configurator` project includes a collection of tests focused on verifying plugin behavior and generating files. The tests do not currently test fetching information from SMD (or whatever remote source). The tests can be ran with either of the following commands:
```bash
go test ./tests/generate_test.go --tags=all
# ...or alternatively with GNU make...
make test
```
## Known Issues ## Known Issues
- Adds a new `OAuthClient` with every token request - Adds a new `OAuthClient` with every token request

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 Example struct { type Example struct {

View file

@ -90,8 +90,6 @@ func TestPlugin(t *testing.T) {
package main package main
import ( import (
"fmt"
configurator "github.com/OpenCHAMI/configurator/pkg" configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/pkg/generator" "github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/configurator/pkg/util" "github.com/OpenCHAMI/configurator/pkg/util"
@ -121,7 +119,7 @@ var Generator TestGenerator
fmt.Printf("(TestPlugin) plugin source path: %v\n", testPluginSourcePath) fmt.Printf("(TestPlugin) plugin source path: %v\n", testPluginSourcePath)
// make temporary directory to test plugin // make temporary directory to test plugin
err = os.MkdirAll(testPluginDir, os.ModeDir) err = os.MkdirAll(testPluginDir, 0o777)
if err != nil { if err != nil {
t.Fatalf("failed to make temporary directory: %v", err) t.Fatalf("failed to make temporary directory: %v", err)
} }
@ -147,9 +145,21 @@ var Generator TestGenerator
t.Fatalf("failed to 'cd' to temporary directory: %v", err) t.Fatalf("failed to 'cd' to temporary directory: %v", err)
} }
// initialize the plugin directory as a Go project
cmd := exec.Command("bash", "-c", "go mod init github.com/OpenCHAMI/configurator-test-plugin")
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
cmd = exec.Command("bash", "-c", "go mod tidy")
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("failed to execute command: %v\n%s", err, string(output))
}
// execute command to build the plugin // execute command to build the plugin
cmd := exec.Command("go", "build", "-buildmode=plugin", fmt.Sprintf("-o=%s", testPluginPath), testPluginSourcePath) cmd = exec.Command("bash", "-c", "go build -buildmode=plugin -o=test-plugin.so test-plugin.go")
if output, err := cmd.Output(); err != nil { if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("failed to execute command: %v\n%s", err, string(output)) t.Fatalf("failed to execute command: %v\n%s", err, string(output))
} }
@ -166,7 +176,7 @@ var Generator TestGenerator
} }
// test loading plugins both individually and in a dir // test loading plugins both individually and in a dir
gen, err := generator.LoadPlugin(testPluginSourcePath) gen, err := generator.LoadPlugin("test-plugin.so")
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)
} }
@ -215,18 +225,11 @@ func TestPluginWithInvalidOrNoSymbol(t *testing.T) {
testPluginSource = []byte(` 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"
)
// An invalid generator that does not or partially implements // An invalid generator that does not or partially implements
// the "Generator" interface. // the "Generator" interface.
type InvalidGenerator struct{} type InvalidGenerator struct{}
var Generator TestGenerator func (g *InvalidGenerator) GetName() string { return "invalid" }
var Generator InvalidGenerator
`) `)
) )
@ -236,7 +239,7 @@ var Generator TestGenerator
} }
// 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", wd)
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)
@ -267,9 +270,21 @@ var Generator TestGenerator
t.Fatalf("failed to 'cd' to temporary directory: %v", err) t.Fatalf("failed to 'cd' to temporary directory: %v", err)
} }
// initialize the plugin directory as a Go project
cmd := exec.Command("bash", "-c", "go mod init github.com/OpenCHAMI/configurator-test-plugin")
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
cmd = exec.Command("bash", "-c", "go mod tidy")
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("failed to execute command: %v\n%s", err, string(output))
}
// execute command to build the plugin // execute command to build the plugin
cmd := exec.Command("go", "build", "-buildmode=plugin", fmt.Sprintf("-o=%s", testPluginPath), testPluginSourcePath) cmd = exec.Command("bash", "-c", "go build -buildmode=plugin -o=invalid-plugin.so invalid-plugin.go")
if output, err := cmd.Output(); err != nil { if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("failed to execute command: %v\n%s", err, string(output)) t.Fatalf("failed to execute command: %v\n%s", err, string(output))
} }
@ -312,7 +327,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() != "This is a plugin creating for running tests." { if gen.GetDescription() != "This is a plugin created for running tests." {
t.Error("test generator return unexpected description") t.Error("test generator return unexpected description")
} }
}) })
@ -355,12 +370,6 @@ func TestGenerateExampleWithServer(t *testing.T) {
FilePaths: []string{}, FilePaths: []string{},
} }
// show which targets are availabe in the config
fmt.Printf("targets:\n")
for target, _ := range config.Targets {
fmt.Printf("\t- %s\n", target)
}
// create new server, add test generator, and start in background // create new server, add test generator, and start in background
server := server.New(&config) server := server.New(&config)
server.GeneratorParams.Generators = map[string]generator.Generator{ server.GeneratorParams.Generators = map[string]generator.Generator{