Extensions for the melatonin test framework

melatonin-ext - Extensions for the melatonin test framework

These packages extend melatonin to provide additional test contexts for testing various 3rd party services.

Exec

The Exec extension provides a context for testing a command line application. It can be used to execute commands and check the exit code, stdout, and stderr.

Installation

go get github.com/jefflinse/melatonin-ext/exec

Usage

package main

import (
    osexec "os/exec"
    "github.com/jefflinse/melatonin/mt"
    "github.com/jefflinse/melatonin-ext/aws/exec"
)

func main() {

    mt.RunTests([]mt.TestCase{

        // Test a commmand
        exec.Run("echo").
            WithArgs("Hello, world!").
            ExpectExitCode(0).
            ExpectStdout("Hello, world!"),

        // Supply some env vars
        exec.Run("echo").
            WithArgs("Hello, world!").
            WithEnv(map[string]string{
                "FOO": "baz",
                "BAR": "buz",
            }).
            ExpectStatus(200).
            ExpectPayload("Hello, world!"),

        // Supply your own exec.Cmd to run
        exec.Cmd(osexec.Command("echo", "Hello, world!")).
            ExpectStatus(200).
            ExpectPayload("Hello, world!"),
    })
}

Custom Context

Define a custom context to customize the execution context, such as the environment:

ctx := exec.NewTestContext().
    WithInheritedEnvironment(true).
    WithEnvVars(map[string]string{
        "FIRST":  "foo",
        "SECOND": "bar",
    })

mt.RunTests([]mt.TestCase{

    ctx.Run("echo").
        ExpectStatus(200).
        ExpectPayload("Hello, world!"),
        WithEnvVars(map[string]string{
            "SECOND": "new bar",
            "THIRD": "baz",
        }),
})

// environment for test case:
//  <all inherited env vars from runtime>
//  FIRST=foo
//  SECOND=new bar
//  THIRD=baz

WithInheritedEnvironment(true) instructs the context to inherit the environment variables from the environment that launched the melatonin process. The default is false, meaning that exec test contexts (including the default context) begin with an empty environment by default.

WithEnvVars(map[string]string{}) will overwrite/append environment variables for a context or test case.

AWS Lambda

The Lambda extension provides a context for testing AWS Lambda functions. It can test Go handler functions directly as unit tests, or it can invoke deployed functions in AWS for performing E2E tests.

Installation

go get github.com/jefflinse/melatonin-ext/aws

Usage

package main

import (
    "github.com/jefflinse/melatonin/mt"
    "github.com/jefflinse/melatonin-ext/aws/lambda"
)

func myHandler(ctx context.Context, event interface{}) (interface{}, error) {
    return "Hello, world!", nil
}

func main() {

    mt.RunTests([]mt.TestCase{

        // Test a Go handler function directly
        lambda.Handle(myHandler).
            ExpectPayload("Hello, world!"),

        // Test a Lambda function by name...
        lambda.Invoke("my-lambda-function").
            ExpectStatus(200).
            ExpectPayload("Hello, world!"),

        // ...or by ARN
        lambda.Invoke("arn:aws:lambda:us-west-2:123456789012:function:my-lambda-function").
            ExpectStatus(200).
            ExpectPayload("Hello, world!"),
    })
}

Custom Context

Define a custom context to customize the AWS Lambda service, including the AWS session:

import (
    "github.com/aws/aws-sdk-go/aws/session"
    lambdasvc "github.com/aws/aws-sdk-go/service/lambda"
)

lambdaService := lambdasvc.New(aws.Must(aws.NewSession(
    aws.WithRegion("us-west-2"),
)))

ctx := lambda.NewTestContext(lambdaService)

mt.RunTests([]mt.TestCase{

    ctx.Invoke("my-lambda-function").
        ExpectStatus(200).
        ExpectPayload("Hello, world!"),
})

Use Mock Lambda APIs

A Lambda test context can be created using any type that satisfies the LambdaAPI interface, making it simple to substitute your own mock Lambda implementation for testing.

import "github.com/aws/aws-sdk-go/service/lambda"

type LambdaAPI interface {
    Invoke(input *lambda.InvokeInput) (*lambda.InvokeOutput, error)
}
Similar Resources

A little tool to create small QR code cards for vaccination/test proof.

Icedream's fancy vaccination/test certificate card tool This is just a tool I wrote to transform my vaccine certificate QR codes into something I can

Dec 16, 2021

Test-project - Lyrid Golang 1.x Chi Template

Lyrid Golang 1.x Chi Template Run locally with: go get go run ./main.go Open ht

Jan 31, 2022

Go-opera-test - EVM-compatible chain secured by the Lachesis consensus algorithm

Opera EVM-compatible chain secured by the Lachesis consensus algorithm. Building

Feb 14, 2022

Antch, a fast, powerful and extensible web crawling & scraping framework for Go

Antch Antch, inspired by Scrapy. If you're familiar with scrapy, you can quickly get started. Antch is a fast, powerful and extensible web crawling &

Jan 6, 2023

F' - A flight software and embedded systems framework

F´ (F Prime) is a component-driven framework that enables rapid development and deployment of spaceflight and other embedded software applications.

Jan 4, 2023

GoC2 - MacOS Post Exploitation C2 Framework

GoC2 - MacOS Post Exploitation C2 Framework

goc2 c2 client/server/paylod GoC2 - MacOS Post Exploitation C2 Framework Custom C2 for bypassing EDR and ease of use.

Dec 23, 2022

The High Code Framework (low-code for devs)

hof - the high code framework The hof tool tries to remove redundent development activities by using high level designs, code generation, and diff3 wh

Dec 24, 2022

A toaster component for hogosuru framework

A toaster component for hogosuru framework

Toaster component for hogosuru Toaster implementation for hogosuru How to use? Create a hogosurutoaster.Toaster or attach it to a hogosuru container a

Mar 24, 2022

Entitas-Go is a fast Entity Component System Framework (ECS) Go 1.17 port of Entitas v1.13.0 for C# and Unity.

Entitas-Go Entitas-GO is a fast Entity Component System Framework (ECS) Go 1.17 port of Entitas v1.13.0 for C# and Unity. Code Generator Install the l

Dec 26, 2022
Reactive Extensions for the Go Language

Reactive Extensions for the Go Language

Nov 1, 2021
A test repo to demonstrate the current (go1.17.2) issue when trying to use retractA test repo to demonstrate the current (go1.17.2) issue when trying to use retract

test-go-mod-retract This is a test repo to demonstrate the current (go1.17.2) issue when trying to use retract in go.mod to retract a version in a non

Oct 16, 2021
James is your butler and helps you to create, build, debug, test and run your Go projects
James is your butler and helps you to create, build, debug, test and run your Go projects

go-james James is your butler and helps you to create, build, debug, test and run your Go projects. When you often create new apps using Go, it quickl

Oct 8, 2022
Enrich `go test` outputs with text decorations.
Enrich `go test` outputs with text decorations.

richgo Rich-Go will enrich go test outputs with text decorations Installation (go get): go get -u github.com/kyoh86/richgo (homebrew): brew tap kyoh8

Dec 28, 2022
Automatically generate Go test boilerplate from your source code.
Automatically generate Go test boilerplate from your source code.

gotests gotests makes writing Go tests easy. It's a Golang commandline tool that generates table driven tests based on its target source files' functi

Jan 3, 2023
Solution to elevator test problem but this time recursive and in go

Synopsis A multi-floor building has a Lift in it. People are queued on different floors waiting for the Lift. Some people want to go up. Some people w

Nov 8, 2021
Implementation of the test task, chat in the goland language
Implementation of the test task, chat in the goland language

Implementation of the test task, chat in the goland language

Dec 5, 2021
A Golang program for a colleague to help in calculating the ratio between the points obtained in a test and the corresponding evaluation in tenths.
A Golang program for a colleague to help in calculating the ratio between the points obtained in a test and the corresponding evaluation in tenths.

A Golang program for a colleague to help in calculating the ratio between the points obtained in a test and the corresponding evaluation in tenths. If you have not the compiled file (.exe) you can build it with the Go compiler.

Jul 7, 2022
MNC Technical Test With Golang

MNC Technical Test With Golang

Nov 20, 2021
A toy repo used to test the functionality of "go mod why".

Mod Why Test Discussion From a module perspective: The main module (github.com/ejweber/mod-why-test) has a single direct dependency (github.com/ejwebe

Dec 1, 2021