framework

package
v1.3.0-alpha.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 16 Imported by: 5

README

Tanzu CLI Test Framework

This is a Go package that provides a test framework for Tanzu CLI plugins. It includes functions and structures for creating a main test and individual subtests. The tests run CLI commands and report on their success or failure.

Usage

To use the framework package in your CLI plugin tests, import it as follows:

import "github.com/vmware-tanzu/tanzu-plugin-runtime/test/framework"

Create a new main test using the NewMain function, providing the main test name and a CleanupFunc function to be executed at the end of the test (if necessary).

  m := framework.NewMain("my-command", myCommand, myCleanupFunc)
  • my-command is the name of your test suite.
  • myCommand is a cobra.Command object that represents the CLI command you want to test.
  • myCleanupFunc is a cleanup function that is executed at the end of the test (if specified).

The NewMain function takes a pointer to a cobra.Command as a parameter, so you need to have created this command before calling NewMain.

You can also pass in two optional bool parameters, printReport and deferDelete, which control whether a report should be printed and whether resource deletion should be deferred, respectively.

Create tests using the NewTest function, providing the test name, a slice of arguments to pass to the CLI command, and a boolean indicating whether the command should succeed or fail.

Add the tests to the main test using the AddTest function.

Run the tests using the Run method of the main test.

Here is an example usage of how to use the framework:

  • This code is a simple CLI (Command Line Interface) written in Go, using the Cobra library. The CLI provides a single command, builder, which has a sub-command called init.

  • When builder init is executed with the argument myrepo --dry-run, the CLI is expected to create a new repository named myrepo. However, since the --dry-run flag is specified, no actual action will be taken, and the CLI will simply print out what it would have done.

  • To test this functionality, the test function is defined, which creates a new instance of clitest.Main, sets up the test, and executes it. The test verifies that the myrepo string is contained in the output of the builder init command by calling t.ExecContainsString("myrepo").

  • The Cleanup function is defined to clean up any resources that may have been created during the test, although in this case it is not used.

  • The main function sets up a new plugin using the plugin.NewPlugin function, passing in the clitest.NewTestFor("builder") object as a descriptor. The p.Cmd.RunE field is set to the test function, and the plugin is executed using p.Execute().

package main

import (
    "log"
    "os"

    "github.com/spf13/cobra"

    "github.com/vmware-tanzu/tanzu-framework/cli/runtime/plugin"
    clitest "github.com/vmware-tanzu/tanzu-framework/cli/runtime/test"
)

var descriptor = clitest.NewTestFor("builder")

func main() {
    p, err := plugin.NewPlugin(descriptor)
    if err != nil {
        log.Fatal(err)
    }
    p.Cmd.RunE = test
    if err := p.Execute(); err != nil {
        os.Exit(1)
    }
}

func test(c *cobra.Command, _ []string) error {
    m := clitest.NewMain("cluster", c, Cleanup)
    defer m.Finish()

    err := m.RunTest(
        "create a repo",
        "builder init myrepo --dry-run",
        func(t *clitest.Test) error {
            err := t.ExecContainsString("myrepo")
            if err != nil {
                return err
            }
            return nil
        },
    )
    if err != nil {
        return err
    }
    return nil
}

// Cleanup the test.
func Cleanup() error {
    return nil
}

Documentation

Overview

Package framework provides a tanzu cli test framework

Index

Constants

View Source
const (
	CLIName = "tanzu"
)
View Source
const NamePrefix = "cli-test"

NamePrefix is the prefix used in generated names.

Variables

View Source
var NoCleanupFunc = func() error { return nil }

NoCleanupFunc is cleanup function that just returns nil.

Functions

func ContainsAnyString

func ContainsAnyString(stdOut *bytes.Buffer, contains []string) error

ContainsAnyString checks that the given buffer contains any of the given set of strings.

func ContainsString

func ContainsString(stdOut *bytes.Buffer, contains string) error

ContainsString checks that the given buffer contains the string.

func Exec

func Exec(command string) (stdOut, stdErr *bytes.Buffer, err error)

Exec the command, exit on error

func ExecContainsAnyString

func ExecContainsAnyString(command string, contains []string) error

ExecContainsAnyString checks that the given command output contains any of the given set of strings.

func ExecContainsErrorString

func ExecContainsErrorString(command, contains string) error

ExecContainsErrorString checks that the given command stdErr output contains the string

func ExecContainsString

func ExecContainsString(command, contains string) error

ExecContainsString checks that the given command output contains the string.

func ExecNotContainsStdErrorString

func ExecNotContainsStdErrorString(command, contains string) error

ExecNotContainsStdErrorString checks that the given command stdErr output contains the string

func FlagSet

func FlagSet() *pflag.FlagSet

FlagSet returns the default flagset values for cli tests.

func GenerateName

func GenerateName() string

GenerateName returns a name for a cli test.

func NewTestFor

func NewTestFor(pluginName string) *plugin.PluginDescriptor

NewTestFor creates a plugin descriptor for a test plugin.

func NotContainsString

func NotContainsString(stdOut *bytes.Buffer, contains string) error

NotContainsString checks that the given buffer not contains the string if contains then throws error.

Types

type CleanupFunc

type CleanupFunc func() error

CleanupFunc is executed at the end of the test.

type Main

type Main struct {
	// Name of the main test.
	Name string
	// Tests in this main.
	Tests []*Test
	// Report holds the report for the test.
	Report *Report
	// Cleanup function.
	Cleanup CleanupFunc

	// Whether to defer the resource deletion.
	DeferDelete bool
	// contains filtered or unexported fields
}

Main holds state for multiple command tests.

func NewMain

func NewMain(name string, c *cobra.Command, cleanup CleanupFunc) *Main

NewMain returns a new CLI test.

func (*Main) AddTest

func (m *Main) AddTest(t *Test)

AddTest adds a test to the main.

func (*Main) BuildReport

func (m *Main) BuildReport()

BuildReport will build the report with the current commands.

func (*Main) Finish

func (m *Main) Finish()

Finish test.

func (*Main) NewTest

func (m *Main) NewTest(name, command string, run func(t *Test) error) *Test

NewTest creates a new Test and adds it to the main.

func (*Main) PrintFailure

func (m *Main) PrintFailure()

PrintFailure prints a main test failure message.

func (*Main) PrintReport

func (m *Main) PrintReport(format string) error

PrintReport prints the report in json|yaml.

func (*Main) PrintStart

func (m *Main) PrintStart()

PrintStart prints a main test start message.

func (*Main) PrintSuccess

func (m *Main) PrintSuccess()

PrintSuccess prints a successful main test message.

func (*Main) ReportError

func (m *Main) ReportError(cmd string, err error)

ReportError adds an error result to the report.

func (*Main) ReportResult

func (m *Main) ReportResult(res *Result)

ReportResult adds a result to the report.

func (*Main) ReportSuccess

func (m *Main) ReportSuccess(cmd string)

ReportSuccess adds an error result to the report.

func (*Main) ReportTestResult

func (m *Main) ReportTestResult(t *Test)

ReportTestResult adds an error result to the report.

func (*Main) RunTest

func (m *Main) RunTest(name, command string, run func(t *Test) error) error

RunTest will create a test and run it.

type Report

type Report struct {
	// Name of the report.
	TestName string `json:"testName"`
	// TimeStart at which the test was started.
	TimeStart time.Time `json:"timeStart"`
	// TimeEnd at which the test was started.
	TimeEnd time.Time `json:"timeEnd"`
	// Pass tells whether all the tests passed.
	Pass bool `json:"pass"`
	// Results of the test.
	Results []*Result `json:"results"`
}

Report is the report generated from running a CLI test.

type Result

type Result struct {
	// Command executed.
	Command string `json:"command"`
	// Pass tells if command passed execution.
	Pass bool `json:"pass"`
	// Err holds any error produced.
	Err error `json:"err"`
}

Result is the result of an individual CLI test.

func (*Result) Error

func (r *Result) Error(err error)

Error fails a result and reports the error.

func (*Result) Success

func (r *Result) Success()

Success passes a result.

type Test

type Test struct {
	// Name of the test.
	Name string
	// Command to test.
	Command string

	// Result of command test.
	Result *Result
	// contains filtered or unexported fields
}

Test is a cli test to run.

func NewTest

func NewTest(name, command string, run func(t *Test) error) *Test

NewTest returns a new command

func (*Test) Exec

func (t *Test) Exec() (err error)

Exec will execute the test command.

func (*Test) ExecContainsAnyString

func (t *Test) ExecContainsAnyString(contains ...string) error

ExecContainsAnyString executes the command and checks if the output contains any of the given set of strings.

func (*Test) ExecContainsErrorString

func (t *Test) ExecContainsErrorString(contains string) error

ExecContainsErrorString executes the command and checks if the output contains the given string.

func (*Test) ExecContainsString

func (t *Test) ExecContainsString(contains string) error

ExecContainsString executes the command and checks if the output contains the given string.

func (*Test) ExecNotContainsStdErrorString

func (t *Test) ExecNotContainsStdErrorString(contains string) error

ExecNotContainsStdErrorString executes the command and checks if the StdError contains the given string.

func (*Test) PrintSuccess

func (t *Test) PrintSuccess()

PrintSuccess will print a success message.

func (*Test) Run

func (t *Test) Run() error

Run the 'run' function within the context of the test updating the result accordingly.

func (*Test) StdErr

func (t *Test) StdErr() *bytes.Buffer

StdErr from executing the test command.

func (*Test) StdOut

func (t *Test) StdOut() *bytes.Buffer

StdOut from executing the test command.

func (*Test) Wrap

func (t *Test) Wrap(f func(t *Test) error) error

Wrap will wrap the execution of the given function in the context of the test and update the results accordingly.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL