Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ramenctl test command #33

Merged
merged 8 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions cmd/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var TestRunCmd = &cobra.Command{
Use: "run",
Short: "Run disaster recovery flow",
Run: func(c *cobra.Command, args []string) {
if err := test.Run(outputDir()); err != nil {
if err := test.Run(configFile, outputDir()); err != nil {
console.Fatal(err)
}
console.Completed("Test run completed successfully")
Expand All @@ -29,10 +29,10 @@ var TestCleanCmd = &cobra.Command{
Use: "clean",
Short: "Delete test artifacts",
Run: func(c *cobra.Command, args []string) {
if err := test.Clean(outputDir()); err != nil {
if err := test.Clean(configFile, outputDir()); err != nil {
console.Fatal(err)
}
console.Completed("Test cleanup completed successfully")
console.Completed("Test clean completed successfully")
},
}

Expand Down
71 changes: 70 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,78 @@ go 1.23.0
// Match kubectl-rook-ceph
toolchain go1.23.4

require github.com/spf13/cobra v1.9.1
require (
github.com/ramendr/ramen/e2e v0.0.0-20250318184549-c975f8c0be43
github.com/spf13/cobra v1.9.1
go.uber.org/zap v1.27.0
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/ramendr/ramen/api v0.0.0-20250313143647-8dd671566929 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/spf13/viper v1.19.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.31.1 // indirect
k8s.io/apimachinery v0.31.1 // indirect
k8s.io/client-go v0.31.1 // indirect
k8s.io/component-base v0.31.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240411171206-dc4e619f62f3 // indirect
k8s.io/kubectl v0.31.1 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
open-cluster-management.io/api v0.15.0 // indirect
open-cluster-management.io/multicloud-operators-channel v0.15.0 // indirect
open-cluster-management.io/multicloud-operators-subscription v0.15.0 // indirect
sigs.k8s.io/controller-runtime v0.19.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
229 changes: 229 additions & 0 deletions go.sum

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-FileCopyrightText: The RamenDR authors
// SPDX-License-Identifier: Apache-2.0

package command

import (
"fmt"

"go.uber.org/zap"

e2eenv "github.com/ramendr/ramen/e2e/env"
"github.com/ramendr/ramen/e2e/types"

"github.com/ramendr/ramenctl/pkg/config"
"github.com/ramendr/ramenctl/pkg/console"
)

// Command is a ramenctl command.
type Command struct {
// Name is the command name (e.g. "test-run")
Name string
// OutputDir contains the command log, summary, and gathered files.
OutputDir string
// Config loaded from configFile.
Config *types.Config
// Env loaded from the config.
Env *types.Env
// Logger logging to the command log.
Logger *zap.SugaredLogger
}

func New(commandName, configFile, outputDir string) (*Command, error) {
// Create the logger first so we can log early command errors to the command log.
log, err := newLogger(outputDir, commandName+".log")
if err != nil {
return nil, fmt.Errorf("failed to create logger: %w", err)
}

console.Info("Using report %q", outputDir)

cfg, err := config.ReadConfig(configFile)
if err != nil {
err := fmt.Errorf("failed to read config %q: %w", err)
log.Error(err)
return nil, err
}

console.Info("Using config %q", configFile)

env, err := e2eenv.New(cfg, log)
if err != nil {
err := fmt.Errorf("failed to create env: %w", err)
log.Error(err)
return nil, err
}

return &Command{
Name: commandName,
OutputDir: outputDir,
Config: cfg,
Env: env,
Logger: log,
}, nil
}
34 changes: 34 additions & 0 deletions pkg/command/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-FileCopyrightText: The RamenDR authors
// SPDX-License-Identifier: Apache-2.0

package command

import (
"os"
"path/filepath"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func newLogger(outputDir, commandName string) (*zap.SugaredLogger, error) {
if err := os.MkdirAll(outputDir, 0o750); err != nil {
return nil, err
}

path := filepath.Join(outputDir, commandName)
logfile, err := os.Create(path)
if err != nil {
return nil, err
}

encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
encoder := zapcore.NewConsoleEncoder(encoderConfig)

core := zapcore.NewCore(encoder, zapcore.Lock(logfile), zapcore.DebugLevel)
logger := zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel))

return logger.Sugar(), nil
}
17 changes: 17 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"fmt"
"os"
"text/template"

"github.com/ramendr/ramen/e2e/config"
"github.com/ramendr/ramen/e2e/deployers"
"github.com/ramendr/ramen/e2e/types"
"github.com/ramendr/ramen/e2e/workloads"
)

//go:embed sample.yaml
Expand All @@ -30,6 +35,18 @@ func CreateSampleConfig(filename, commandName string) error {
return nil
}

func ReadConfig(filename string) (*types.Config, error) {
options := config.Options{
Workloads: workloads.AvailableNames(),
Deployers: deployers.AvailableNames(),
}
config, err := config.ReadConfig(filename, options)
if err != nil {
return nil, fmt.Errorf("unable to read config: %w", err)
}
return config, nil
}

type Sample struct {
CommandName string
}
Expand Down
44 changes: 20 additions & 24 deletions pkg/test/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,26 @@

package test

import (
"time"

"github.com/ramendr/ramenctl/pkg/console"
)

func Clean(outputDir string) error {
hub := "hub"
primary := "dr1"
secondary := "dr2"

console.Info("Using report %q", outputDir)

console.Progress("Cleaning up cluster %q", primary)
time.Sleep(2 * time.Second)
console.Completed("Cluster %q cleaned", primary)

console.Progress("Cleaning up cluster %q", secondary)
time.Sleep(2 * time.Second)
console.Completed("Cluster %q cleaned", secondary)

console.Progress("Cleaning up cluster %q", hub)
time.Sleep(2 * time.Second)
console.Completed("Cluster %q cleaned", hub)
func Clean(configFile string, outputDir string) error {
cmd, err := newCommand("test-clean", configFile, outputDir)
if err != nil {
return err
}

// We want to run all tests in parallel, but for now lets run one test.
test := newTest(cmd.Config.Tests[0], cmd)

if err := test.Unprotect(); err != nil {
return err
}

if err := test.Undeploy(); err != nil {
return err
}

if err := cleanEnvironment(cmd); err != nil {
return err
}

return nil
}
39 changes: 39 additions & 0 deletions pkg/test/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-FileCopyrightText: The RamenDR authors
// SPDX-License-Identifier: Apache-2.0

package test

import (
e2econfig "github.com/ramendr/ramen/e2e/config"
"github.com/ramendr/ramen/e2e/types"

"github.com/ramendr/ramenctl/pkg/command"
)

// Command is a ramenctl test command.
type Command struct {
*command.Command

// NamespacePrefix is used for all namespaces created by tests.
NamespacePrefix string

// PCCSpecs maps pvscpec name to pvcspec.
PVCSpecs map[string]types.PVCSpecConfig
}

// newCommand return a new test command.
func newCommand(name, configFile, outputDir string) (*Command, error) {
cmd, err := command.New(name, configFile, outputDir)
if err != nil {
return nil, err
}

// This is not user configurable. We use the same prefix for all namespaces created by the test.
cmd.Config.Channel.Namespace = "test-gitops"

return &Command{
Command: cmd,
NamespacePrefix: "test-",
PVCSpecs: e2econfig.PVCSpecsMap(cmd.Config),
}, nil
}
69 changes: 69 additions & 0 deletions pkg/test/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-FileCopyrightText: The RamenDR authors
// SPDX-License-Identifier: Apache-2.0

package test

import (
"fmt"

"go.uber.org/zap"

"github.com/ramendr/ramen/e2e/types"
)

// Context implements types.Context interface.
type Context struct {
workload types.Workload
deployer types.Deployer
name string
logger *zap.SugaredLogger
cmd *Command
}

var _ types.Context = &Context{}

func newContext(workload types.Workload, deployer types.Deployer, cmd *Command) *Context {
name := fmt.Sprintf("%s-%s", deployer.GetName(), workload.GetName())
return &Context{
workload: workload,
deployer: deployer,
name: name,
logger: cmd.Logger.Named(name),
cmd: cmd,
}
}

func (c *Context) Deployer() types.Deployer {
return c.deployer
}

func (c *Context) Workload() types.Workload {
return c.workload
}

func (c *Context) Name() string {
return c.name
}

func (c *Context) ManagementNamespace() string {
if ns := c.deployer.GetNamespace(c); ns != "" {
return ns
}
return c.AppNamespace()
}

func (c *Context) AppNamespace() string {
return c.cmd.NamespacePrefix + c.name
}

func (c *Context) Logger() *zap.SugaredLogger {
return c.logger
}

func (c *Context) Env() *types.Env {
return c.cmd.Env
}

func (c *Context) Config() *types.Config {
return c.cmd.Config
}
Loading