Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions ingest/loadtest/apply_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
_ "embed"
"fmt"
"io"
"os"
Expand All @@ -23,9 +24,18 @@
"github.com/stellar/go-stellar-sdk/xdr"
)

//go:embed configs/default-apply-load.cfg
var defaultConfig []byte

// DefaultConfig returns the contents of the default apply-load configuration
// shipped with this package.
func DefaultConfig() []byte {
return defaultConfig
Comment thread
cjonas9 marked this conversation as resolved.
Outdated
}

type Options struct {
// inputs
ConfigPath string
Config []byte // raw bytes of config
OutputPath string
FixturesPath string
CoreBinaryPath string // optional, will be looked up in PATH if not set
Expand All @@ -42,8 +52,8 @@
// ApplyLoad runs stellar-core's apply-load against the supplied config and writes
// benchmark ledgers + fixture entries to OutputPath / FixturesPath.
//
// Required: ConfigPath, OutputPath, FixturesPath. Optional: CoreBinaryPath
// (looked up in PATH), Logger, WorkDirPath (temp dir if unspecified).
// Required: OutputPath, FixturesPath. Optional: CoreBinaryPath (looked up in PATH),
// Logger, WorkDirPath (temp dir if unspecified), Config (defaultConfig if unspecified).
// The supplied config's [HISTORY] commands must publish to a `history/`
// subdirectory of the work dir.
func ApplyLoad(ctx context.Context, opts Options) (Results, error) {
Expand All @@ -60,7 +70,7 @@
}
}()
}
cfg, err := parseConfig(opts.ConfigPath)
cfg, err := parseConfig(opts.Config)
if err != nil {
return Results{}, fmt.Errorf("failed to parse config: %w", err)
}
Expand Down Expand Up @@ -93,11 +103,8 @@

// resolveOptions checks that required options are set and valid, and fills in defaults for optional ones.
func resolveOptions(opts *Options) error {
if opts.ConfigPath == "" {
return fmt.Errorf("configPath is required")
}
if opts.OutputPath == "" || opts.FixturesPath == "" {
return fmt.Errorf("both outputPath and fixturesPath are required")
return fmt.Errorf("outputPath and fixturesPath are required")
}

if opts.CoreBinaryPath == "" {
Expand All @@ -118,9 +125,12 @@
if opts.Logger == nil {
opts.Logger = log.New()
}
if opts.Config == nil {
opts.Config = DefaultConfig()
opts.Logger.Infof("no config provided, using default config")
}

opts.Logger.Infof("Using stellar-core: %s %s", opts.CoreBinaryPath, coreVersion)
opts.Logger.Infof("Using config: %s", opts.ConfigPath)

if opts.WorkDirPath == "" {
var err error
Expand All @@ -136,7 +146,7 @@
func run(ctx context.Context, opts Options, cfg applyLoadConfig) (uint32, error) {
// Copy config to work dir (apply-load writes files relative to config location)
destConfigPath := filepath.Join(opts.WorkDirPath, "apply-load.cfg")
if err := copyFile(opts.ConfigPath, destConfigPath); err != nil {
if err := os.WriteFile(destConfigPath, opts.Config, 0o644); err != nil {
return 0, err
}

Expand Down Expand Up @@ -484,15 +494,12 @@
HistoryArchiveName string
}

func parseConfig(configPath string) (applyLoadConfig, error) {
data, err := os.ReadFile(configPath)
if err != nil {
return applyLoadConfig{}, err
func parseConfig(config []byte) (applyLoadConfig, error) {
if config == nil {
return applyLoadConfig{}, fmt.Errorf("config is required")
}

var raw map[string]any
err = toml.Unmarshal(data, &raw)
if err != nil {
if err := toml.Unmarshal(config, &raw); err != nil {
return applyLoadConfig{}, err
Comment thread
cjonas9 marked this conversation as resolved.
}

Expand Down Expand Up @@ -550,7 +557,7 @@
return checkpointReader, nil
}

func copyFile(src, dst string) error {

Check failure on line 560 in ingest/loadtest/apply_load.go

View workflow job for this annotation

GitHub Actions / check (ubuntu-22.04, 1.25)

func copyFile is unused (U1000)
data, err := os.ReadFile(src)
if err != nil {
return err
Expand Down
13 changes: 4 additions & 9 deletions ingest/loadtest/apply_load_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package loadtest

import (
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -59,7 +57,7 @@ func TestParsePreBenchmarkCheckpoint(t *testing.T) {
// shipped with this package. Updating default-apply-load.cfg without
// updating the expected values here will fail this test.
func TestParseConfig_DefaultCfg(t *testing.T) {
got, err := parseConfig("testdata/default-apply-load.cfg")
got, err := parseConfig(DefaultConfig())
require.NoError(t, err)
assert.Equal(t, applyLoadConfig{
NetworkPassphrase: "load test network",
Expand Down Expand Up @@ -115,10 +113,7 @@ get = "cp history/{0} {1}"
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "apply-load.cfg")
require.NoError(t, os.WriteFile(path, []byte(tt.contents), 0o644))

_, err := parseConfig(path)
_, err := parseConfig([]byte(tt.contents))
require.Error(t, err)
if tt.wantErr != "" {
assert.Contains(t, err.Error(), tt.wantErr)
Expand All @@ -127,7 +122,7 @@ get = "cp history/{0} {1}"
}
}

func TestParseConfig_FileNotFound(t *testing.T) {
_, err := parseConfig(filepath.Join(t.TempDir(), "does-not-exist.cfg"))
func TestParseConfig_NoConfigFound(t *testing.T) {
_, err := parseConfig([]byte{})
require.Error(t, err)
}
Loading