tests: updated to use new API

This commit is contained in:
David Allen 2024-12-04 15:45:07 -07:00
parent 678e6b66bd
commit 31d14bcc53
Signed by: towk
GPG key ID: 793B2924A49B3A3F
2 changed files with 35 additions and 33 deletions

22
tests/generate_local.hurl Normal file
View file

@ -0,0 +1,22 @@
##
## Run these tests after starting server with `configurator serve...`
##
# Generate a `example` config with default plugin and template
GET http://127.0.0.1:3334/generate?target=example
HTTP 200
# Create a new target using the API
POST http://127.0.0.1:3334/targets
{
"name": "test",
"plugin": "example",
"templates": [{
"contents": "This is an example template used with the example plugin."
}]
}
HTTP 200
# Test the new target just add from POST above
GET http://127.0.0.1:3334/generate?target=example
HTTP 200

View file

@ -9,6 +9,7 @@ import (
"testing" "testing"
configurator "github.com/OpenCHAMI/configurator/pkg" 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/server" "github.com/OpenCHAMI/configurator/pkg/server"
"github.com/OpenCHAMI/configurator/pkg/util" "github.com/OpenCHAMI/configurator/pkg/util"
@ -22,7 +23,7 @@ func (g *TestGenerator) GetVersion() string { return "v1.0.0" }
func (g *TestGenerator) GetDescription() string { func (g *TestGenerator) GetDescription() string {
return "This is a plugin created for running tests." return "This is a plugin created for running tests."
} }
func (g *TestGenerator) Generate(config *configurator.Config, opts ...util.Option) (generator.FileMap, error) { func (g *TestGenerator) Generate(config *config.Config, params generator.Params) (generator.FileMap, error) {
// Jinja 2 template file // Jinja 2 template file
files := [][]byte{ files := [][]byte{
[]byte(` []byte(`
@ -42,28 +43,16 @@ This is another testing Jinja 2 template file using {{plugin_name}}.
"plugin_name": g.GetName(), "plugin_name": g.GetName(),
"plugin_version": g.GetVersion(), "plugin_version": g.GetVersion(),
"plugin_description": g.GetDescription(), "plugin_description": g.GetDescription(),
}, files...) }, params.Templates)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to apply templates: %v", err) return nil, fmt.Errorf("failed to apply templates: %v", err)
} }
// make sure we're able to receive certain arguments when passed
params := generator.GetParams(opts...)
if len(params) <= 0 {
return nil, fmt.Errorf("expect at least one params, but found none")
}
// make sure we have a valid config we can access // make sure we have a valid config we can access
if config == nil { if config == nil {
return nil, fmt.Errorf("invalid config (config is nil)") return nil, fmt.Errorf("invalid config (config is nil)")
} }
// make sure we're able to get a valid client as well
client := generator.GetClient(params)
if client == nil {
return nil, fmt.Errorf("invalid client (client is nil)")
}
// 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
@ -100,7 +89,7 @@ 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 { return "This is a plugin creating for running tests." }
func (g *TestGenerator) Generate(config *configurator.Config, opts ...util.Option) (generator.FileMap, error) { func (g *TestGenerator) Generate(config *configurator.Config, opts ...generator.Option) (generator.FileMap, error) {
return generator.FileMap{"test": []byte("test")}, nil return generator.FileMap{"test": []byte("test")}, nil
} }
var Generator TestGenerator var Generator TestGenerator
@ -186,7 +175,7 @@ var Generator TestGenerator
GetName() string GetName() string
GetVersion() string GetVersion() string
GetDescription() string GetDescription() string
Generate(*configurator.Config, ...util.Option) (generator.FileMap, error) Generate(*config.Config, ...util.Option) (generator.FileMap, error)
}); !ok { }); !ok {
t.Error("plugin does not implement all of the generator interface") t.Error("plugin does not implement all of the generator interface")
} }
@ -203,7 +192,7 @@ var Generator TestGenerator
GetName() string GetName() string
GetVersion() string GetVersion() string
GetDescription() string GetDescription() string
Generate(*configurator.Config, ...util.Option) (generator.FileMap, error) Generate(*config.Config, ...util.Option) (generator.FileMap, error)
}); !ok { }); !ok {
t.Error("plugin does not implement all of the generator interface") t.Error("plugin does not implement all of the generator interface")
} }
@ -314,9 +303,8 @@ var Generator InvalidGenerator
// we're not doing it here since that's not what is being tested. // we're not doing it here since that's not what is being tested.
func TestGenerateExample(t *testing.T) { func TestGenerateExample(t *testing.T) {
var ( var (
config = configurator.NewConfig() conf = config.New()
client = configurator.NewSmdClient() gen = TestGenerator{}
gen = TestGenerator{}
) )
// make sure our generator returns expected strings // make sure our generator returns expected strings
@ -333,11 +321,7 @@ func TestGenerateExample(t *testing.T) {
}) })
// try to generate a file with templating applied // try to generate a file with templating applied
fileMap, err := gen.Generate( fileMap, err := gen.Generate(&conf, generator.Params{})
&config,
generator.WithTarget("test"),
generator.WithClient(client),
)
if err != nil { if err != nil {
t.Fatalf("failed to generate file: %v", err) t.Fatalf("failed to generate file: %v", err)
} }
@ -356,8 +340,7 @@ func TestGenerateExample(t *testing.T) {
// try and load the plugin from a lib here either. // try and load the plugin from a lib here either.
func TestGenerateExampleWithServer(t *testing.T) { func TestGenerateExampleWithServer(t *testing.T) {
var ( var (
config = configurator.NewConfig() conf = config.New()
client = configurator.NewSmdClient()
gen = TestGenerator{} gen = TestGenerator{}
headers = make(map[string]string, 0) headers = make(map[string]string, 0)
) )
@ -365,13 +348,13 @@ func TestGenerateExampleWithServer(t *testing.T) {
// NOTE: Currently, the server needs a config to know where to get load plugins, // NOTE: Currently, the server needs a config to know where to get load plugins,
// and how to handle targets/templates. This will be simplified in the future to // and how to handle targets/templates. This will be simplified in the future to
// decoupled the server from required a config altogether. // decoupled the server from required a config altogether.
config.Targets["test"] = configurator.Target{ conf.Targets["test"] = configurator.Target{
TemplatePaths: []string{}, TemplatePaths: []string{},
FilePaths: []string{}, FilePaths: []string{},
} }
// 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(&conf)
server.GeneratorParams.Generators = map[string]generator.Generator{ server.GeneratorParams.Generators = map[string]generator.Generator{
"test": &gen, "test": &gen,
} }
@ -390,10 +373,7 @@ func TestGenerateExampleWithServer(t *testing.T) {
// //
// NOTE: we don't actually use the config in this plugin implementation, // NOTE: we don't actually use the config in this plugin implementation,
// but we do check that a valid config was passed. // but we do check that a valid config was passed.
fileMap, err := gen.Generate( fileMap, err := gen.Generate(&conf, generator.Params{})
&config,
generator.WithClient(client),
)
if err != nil { if err != nil {
t.Fatalf("failed to generate file: %v", err) t.Fatalf("failed to generate file: %v", err)
} }