fliptest

package module
v1.0.15 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2021 License: MIT Imports: 10 Imported by: 0

README

fliptest

Package fliptest provides a mechanism for testing internet egress in an AWS VPC by creating a VPC Lambda via Cloudformation stack to which custom test URLs can be passed.

Read the docs: https://godoc.org/github.com/GESkunkworks/fliptest

Sample usage:

package main

import (
    "fmt"
    "encoding/json"

    "github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
    
    "github.com/GESkunkworks/fliptest"
)

func main() {
    // setup session for flippage
    var sess *session.Session
    sess = session.Must(session.NewSessionWithOptions(session.Options{
        Config:  aws.Config{Region: aws.String("us-east-1")},
        Profile: "account1",
    }))

    input := fliptest.FlipTesterInput{
        Session:     sess,
        SubnetId:    "subnet-d3297188",
        VpcId:       "vpc-c8a6c3ae",
        RetainStack: true,
    }
    test, err := fliptest.New(&input)
    if err != nil {
        panic(err)
    }
    err = test.Test()
    if err != nil {
        fmt.Println(err)
        // if it was the tests failing that caused the error
        // we can see results from the test
        if body, err := json.MarshalIndent(test.TestResults, "", "    "); err == nil {
            fmt.Println(string(body))
        }
    } else {
        // see results of the tests that passed
        if body, err := json.MarshalIndent(test.TestResults, "", "    "); err == nil {
            fmt.Println(string(body))
        }
        // if desired a simple activity log can be retrieved
        fmt.Println(test.GetLog())
    }
}

sample output:

$ go run main.go
[
    {
        "Name": "gopkg.in",
        "ElapsedTimeS": 0.49933934211730957,
        "Message": "got response code from URL",
        "Success": true,
        "Url": "https://gopkg.in",
        "ResponseCode": 200
    },
    {
        "Name": "google",
        "ElapsedTimeS": 0.690319299697876,
        "Message": "got response code from URL",
        "Success": true,
        "Url": "https://www.google.com",
        "ResponseCode": 200
    },
    {
        "Name": "time",
        "ElapsedTimeS": 0.9288179874420166,
        "Message": "got response code from URL",
        "Success": true,
        "Url": "https://www.nist.gov",
        "ResponseCode": 200
    }
]
starting test
creating stack
loading template file
waiting on stack
waiting on stack
waiting on stack
waiting on stack
waiting on stack
waiting on stack
waiting on stack
calling lambda
tests passed
retaining stack
tests completed

If you want to run custom tests via the Lambda console you can create some test events in the browser. The structure for the test event is like so:

{
    "RequestType": "RunAll",
    "TestUrls": [
        {
            "Name": "test-google",
            "Url": "https://www.google.com/"
        }
    ]
}

Documentation

Overview

Package fliptest provides a mechanism for testing internet egress in an AWS VPC by creating a VPC Lambda via Cloudformation stack to which custom test URLs can be passed.

Index

Examples

Constants

View Source
const DefaultStackPrefix string = "ISS-GR-egress-tester-"

Unless overridden using FlipTesterInput the stack will be prefixed with this and a random number will be added to the end.

Variables

This section is empty.

Functions

This section is empty.

Types

type FlipTester

type FlipTester struct {

	// Holds the list of URLs that will be passed to the
	// lambda when the .Test() method is called.
	TestUrls []*TestUrl

	// Stores results (if any) from tests after the
	// .Test() method has been called
	TestResults []*TestResult

	// Indicates whether or not the tests passed. The pass
	// criteria is fixed based on whether the GET request
	// received a 200 response and it took less than 4 seconds
	Passed bool

	// Indicates whether or not the stack will be deleted after
	// the .Test() method is called.
	RetainStack bool

	// The stack name will be available here in case the tests need
	// to be resumed later.
	StackName string
	// contains filtered or unexported fields
}

FlipTester is object that is created and its methods are called in order to test internet in the VPC.

func New

func New(input *FlipTesterInput) (fliptester *FlipTester, err error)

New returns an instance of FlipTester provided a prebuilt FlipTesterInput object. If not all parameters are defined then some default values will be chosen when possible. Will return a FlipTester from which the .Test() method can be called to create a Cloudformation stack, call the resulting lambda, and populate .TestResults with the results. The .TestEvent.TestUrls slice can be modified directly with custom tests before calling .Test() any errors will be returned.

Example (Newstack)

new-stack

This example sets up a new stack from scratch and retains it for future use.

package main

import (
	"encoding/json"
	"fmt"

	"github.com/GESkunkworks/fliptest"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
)

func main() {
	// setup session for flippage
	var sess *session.Session
	sess = session.Must(session.NewSessionWithOptions(session.Options{
		Config:  aws.Config{Region: aws.String("us-east-1")},
		Profile: "account1",
	}))

	input := fliptest.FlipTesterInput{
		Session:     sess,
		SubnetId:    "subnet-d3297188",
		VpcId:       "vpc-c8a6c3ae",
		RetainStack: true,
	}
	test, err := fliptest.New(&input)
	if err != nil {
		panic(err)
	}
	err = test.Test()
	if err != nil {
		fmt.Println(err)
		// if it was the tests failing that caused the error
		// we can see results from the test
		if body, err := json.MarshalIndent(test.TestResults, "", "    "); err == nil {
			fmt.Println(string(body))
		}
	} else {
		// see results of the tests that passed
		if body, err := json.MarshalIndent(test.TestResults, "", "    "); err == nil {
			fmt.Println(string(body))
		}
		// if desired a simple activity log can be retrieved
		fmt.Println(test.GetLog())
	}
}
Output:

Example (Resumestackcustomtests)

resume-stack-with-custom-tests

This example resumes an existing stack and changes the test URLs that it's calling on the lambda.

package main

import (
	"encoding/json"
	"fmt"

	"github.com/GESkunkworks/fliptest"
	"github.com/aws/aws-sdk-go/aws/session"
)

func main() {
	var sess *session.Session
	sess = session.New()
	// if you retained the stack previously you can resume it
	// if you know the name
	var tests []*fliptest.TestUrl
	t1 := fliptest.TestUrl{
		Name: "cnn",
		Url:  "https://www.cnn.com/",
	}
	t2 := fliptest.TestUrl{
		Name: "mysite",
		Url:  "http://www.mysite.org/getdata/",
	}
	tests = append(tests, &t1)
	tests = append(tests, &t2)
	input := fliptest.FlipTesterInput{
		Session:     sess,
		StackName:   "ISS-GR-egress-tester-00714632",
		TestUrls:    tests,
		RetainStack: false,
	}
	test, err := fliptest.New(&input)
	if err != nil {
		panic(err)
	}
	err = test.Test()
	if err != nil {
		panic(err)
	}
	if body, err := json.MarshalIndent(test.TestResults, "", "    "); err == nil {
		fmt.Println(string(body))
	}
}
Output:

func (*FlipTester) CreateStack

func (ft *FlipTester) CreateStack() (err error)

CreateStack takes the current fliptest session information and creates the test stack in the desired VPC/Subnet. It blocks until the stack is fully created and ready and returns any errors.

func (*FlipTester) DeleteStack

func (ft *FlipTester) DeleteStack() (err error)

DeleteStack allows you to delete the Cloudformation stack manually.

func (*FlipTester) GetLog

func (ft *FlipTester) GetLog() string

GetLog returns a string representing the log messages from the life of the FlipTester object.

func (*FlipTester) Test

func (ft *FlipTester) Test() (err error)

Test sets up the Cloudformation stack from template and then calls the created function and parses the results.

type FlipTesterInput

type FlipTesterInput struct {

	// The SubnetId in which to launch the test lambda.
	// The egress path that is provided by this subnet's
	// route table and NACLs will determine whether or not
	// the egress test is successful.
	SubnetId string

	// The VpcId in which to launch the test lambda.
	VpcId string

	// A prefix to give to the created Cloudformation
	// stack. If none is provided then a default of
	// defaultStackPrefix const will be used.
	StackPrefix string

	// If a custom Cloudformation template is desired
	// then a filename can be provided here and the
	// FlipTester will attempt to load it and create
	// the stack using the provided template instead
	// of the defaultTemplate constant.
	StackTemplateFilename string

	// The name of a previously created FlipTester
	// Cloudformation stack to resume using. Will
	// bypass any new stack creation and instead
	// simply provide the .Test() method against
	// the existing stack
	StackName string

	// A slice containing TestUrl structs
	// that the test will execute against. If
	// no test URLs are provided then a set of
	// defaults will be run.
	TestUrls []*TestUrl

	// Whether or not to retain the Cloudformation
	// stack after finishing the test. If the stack
	// is retained then the test can be run again
	// without having to wait for stack creation.
	RetainStack bool

	// The AWS session to use for this testing
	// process. If no session is provided then
	// one will be created using system defaults.
	Session *session.Session

	// The context that will be added to all log
	// messages. Generally the account name
	// or something similar
	Context string

	// How long the tester should sleep (in seconds)
	// before attempting to call the lambda after
	// it detects the stack is created. In some
	// regions this can take up to 40 seconds.
	// Default: 40 Seconds
	InitialSleepTimeSeconds int

	// How long after creating the test event
	// to sleep (in seconds). Sometimes these
	// VPC lambdas need a little extra time.
	// Default: 20 Seconds
	PostEventSleepTimeSeconds int
}

FlipTesterInput provides all of the information necessary to create a FlipTester object.

type TestResult

type TestResult struct {
	Name         string
	ElapsedTimeS float64
	Message      string
	Success      bool
	Url          string
	ResponseCode int
}

TestResult holds results from the lambda execution.

type TestUrl

type TestUrl struct {
	Name string
	Url  string
}

TestUrl holds a Name and Url. The Name is just an identifying label and a GET will be performed on the Url using the Python urllib library.

Jump to

Keyboard shortcuts

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