utils

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2022 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderMarkdown added in v0.1.3

func RenderMarkdown(input string, colors []string) error

RenderMarkdown - renders markdown and outputs it to the console

Types

type Client

type Client interface {
	ImagePull(ctx context.Context, refStr string, options types.ImagePullOptions) (io.ReadCloser, error)
	ImageList(ctx context.Context, options types.ImageListOptions) ([]types.ImageSummary, error)
	ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *specs.Platform, containerName string) (container.ContainerCreateCreatedBody, error)
	CopyFromContainer(ctx context.Context, containerID string, srcPath string) (io.ReadCloser, types.ContainerPathStat, error)
	ContainerRemove(ctx context.Context, container string, options types.ContainerRemoveOptions) error
	ImageRemove(ctx context.Context, imageID string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error)
}

Client interface so that we can create a mock of the docker SDK interactions in our unit tests

type Config

type Config struct {
	BaseDir        string `hcl:"base_dir,attr"`
	StartPort      int    `hcl:"start_port,attr"`
	PortInc        int    `hcl:"port_increment,attr"`
	Alias          bool   `hcl:"alias,attr"`
	RepositoryHost string `hcl:"repository_host,attr"`
	PimsConfigDir  string `hcl:"pims_config_dir,attr"`
	PimsDir        string `hcl:"pims_dir,attr"`
}

Config object to contain the configuration details

type Copier

type Copier interface {
	CopyFiles(reader io.ReadCloser, dest string, source string) error
}

Create an interface to house the CopyFiles implementation. This will allow us to make a mock of the CopyFiles Function.

type Copy

type Copy struct {
	Source string `hcl:"source,attr"`
	Dest   string `hcl:"dest,attr"`
}

Copy object to parse the copy block in the package list

type CopyTool

type CopyTool struct{}

Create the real copy struct

func (*CopyTool) CopyFiles

func (cp *CopyTool) CopyFiles(reader io.ReadCloser, dest string, source string) error

CopyFiles implements a tar reader to copy files from the ReadCloser that the docker sdk CopyFromContainer function returns to the specified destination

type DockMock

type DockMock struct {
	//Variable to know what function to return an error from
	ErrorAt string

	//Variable to store the error message
	ErrorMsg string

	//Keep track of the values from ImagePull Function
	IPRefStr string

	//Keep track of the values from the ContainerCreate Function
	CCConfig *container.Config
	CCName   string
	CCRet    container.ContainerCreateCreatedBody

	//Keep track of the values from the CopyFromContainer Function
	CFCID     string
	CFCSource string

	//Keep track of the values from the ContainerRemove Function
	CRContainer string
	CROptions   types.ContainerRemoveOptions

	//Keep track of the values from the ImageRemove Function
	IRImgID   string
	IROptions types.ImageRemoveOptions

	//ImageList return value
	ILRet []types.ImageSummary
}

Create a Mock for the Docker client

func NewDockMock

func NewDockMock() *DockMock

Function to create a new DockMock

func (*DockMock) ContainerCreate

func (dm *DockMock) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *specs.Platform, containerName string) (container.ContainerCreateCreatedBody, error)

Mock function of the Docker SDK ContainerCreate function

func (*DockMock) ContainerRemove

func (dm *DockMock) ContainerRemove(ctx context.Context, container string, options types.ContainerRemoveOptions) error

Mock function of the Docker SDK ContainerRemove function

func (*DockMock) CopyFromContainer

func (dm *DockMock) CopyFromContainer(ctx context.Context, containerID string, srcPath string) (io.ReadCloser, types.ContainerPathStat, error)

Mock function of the Docker SDK CopyFromContainer function

func (*DockMock) ImageList

func (dm *DockMock) ImageList(ctx context.Context, options types.ImageListOptions) ([]types.ImageSummary, error)

Mock function of the Docker SDK ImagePull function

func (*DockMock) ImagePull

func (dm *DockMock) ImagePull(ctx context.Context, refStr string, options types.ImagePullOptions) (io.ReadCloser, error)

Mock function of the Docker SDK ImagePull function

func (*DockMock) ImageRemove

func (dm *DockMock) ImageRemove(ctx context.Context, imageID string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error)

Mock function of the Docker SDK ImageRemove function

type MockCopyTool

type MockCopyTool struct {
	Error    bool
	ErrorMsg string
	Dest     string
}

CopyTool Mock

func (*MockCopyTool) CopyFiles

func (mcp *MockCopyTool) CopyFiles(reader io.ReadCloser, dest string, source string) error

Mock of the CopyFiles Utility function

type MockUtility

type MockUtility struct {
	//Keeps track of the function calls that are made during a test
	Calls []string

	//Package object that can be changed for different tests
	Pim PimHCLUtil

	//Config Object that can be changed for different tests
	Conf Config

	//HCL Body that can be changed for different tests
	HCLBody hcl.Body

	//Set if the image should exist or not for testing
	ImgExist bool

	//Set the function name that should throw an error when called
	ErrorAt string

	//Set an error message for the tests
	ErrorMsg string

	//Keep track of the directories that are created
	MadeDirs []string

	//Keep track of the files that are opened/created
	OpenedFiles []string

	//Keep track of the directories that are removed
	RemovedDirs []string

	//Keep track of the upgraded directories
	UpgradedDirs []string

	//Keep track of the HCLFiles read
	HCLFiles []string

	//Keep track of the images that are pulled
	PulledImgs []string

	//Use a fake containerID to make sure it is being used correctly
	ContainerID string

	//CreateContainer data
	CreateImages []string

	//Keep track of the CopyFromContainer data
	CopySources     []string
	CopyDests       []string
	CopyContainerID string

	//Keep track of the RemoveContainer data
	RemoveContainerID string

	//Keep track of the RunContainer data
	RunImage         string
	RunPorts         []string
	RunVolumes       []string
	RunContainerName string
	RunArgs          []string

	//Keep track of the RemoveImage data
	RemovedImgs []string

	//Keep track of the alias data
	CmdToAlias []string

	//Should the Pim Configuration file exist
	PimConfigShouldExist bool

	//Pim Config Directory passed in
	PimConfigDir string

	//List of pim names to return
	InstalledPims []string

	//List of pims fetched using FetchPimConfigs
	FetchedPims []string
}

Mock utility and its functions

func NewMockUtility

func NewMockUtility() *MockUtility

Create a new Mock Utility and set any default variables

func (*MockUtility) AddAliasUnix

func (mu *MockUtility) AddAliasUnix(name string, ed string) error

Mock of the AddAliasUnix Utility function

func (*MockUtility) AddAliasWin

func (mu *MockUtility) AddAliasWin(name string, ed string) error

Mock of the AddAliasWin Utility function

func (*MockUtility) CopyFromContainer

func (mu *MockUtility) CopyFromContainer(source string, dest string, containerID string, cli Client, cp Copier) error

Mock of the CopyFromContainer Utility function

func (*MockUtility) CreateContainer

func (mu *MockUtility) CreateContainer(image string, cli Client) (string, error)

Mock of the CreateContainer Utility function

func (*MockUtility) FetchPimConfig

func (mu *MockUtility) FetchPimConfig(baseUrl string, pimName string, savePath string) error

func (*MockUtility) FileExists

func (mu *MockUtility) FileExists(path string) bool

func (*MockUtility) GetHCLBody

func (mu *MockUtility) GetHCLBody(filepath string) (hcl.Body, error)

Mock of the GetHCLBody Utility function

func (*MockUtility) GetListOfInstalledPimConfigs

func (mu *MockUtility) GetListOfInstalledPimConfigs(pimConfigDir string) ([]string, error)

func (*MockUtility) Getwd added in v0.1.3

func (mu *MockUtility) Getwd() (dir string, err error)

Mock of the Getwd Utility function

func (*MockUtility) ImageExists

func (mu *MockUtility) ImageExists(imageID string, cli Client) (bool, error)

Mock of the ImageExists Utility function

func (*MockUtility) MakeDir

func (mu *MockUtility) MakeDir(path string) error

Mock of the MakeDir Utility function

func (*MockUtility) OpenFile

func (mu *MockUtility) OpenFile(path string) (*os.File, error)

Mock of the OpenFile Utility function

func (*MockUtility) ParseBody

func (mu *MockUtility) ParseBody(body hcl.Body, out interface{}) (interface{}, error)

Mock of the ParseBody Utility function

func (*MockUtility) PullImage

func (mu *MockUtility) PullImage(name string, cli Client) error

Mock of the PullImage Utility function

func (*MockUtility) RemoveAliasUnix

func (mu *MockUtility) RemoveAliasUnix(name string, ed string) error

Mock of the RemoveAliasUnix Utility function

func (*MockUtility) RemoveAliasWin

func (mu *MockUtility) RemoveAliasWin(name string, ed string) error

Mock of the RemoveAliasWin Utility function

func (*MockUtility) RemoveContainer

func (mu *MockUtility) RemoveContainer(containerID string, cli Client) error

Mock of the RemoveContainer Utility function

func (*MockUtility) RemoveDir

func (mu *MockUtility) RemoveDir(path string) error

Mock of the RemoveDir Utility function

func (*MockUtility) RemoveFile

func (mu *MockUtility) RemoveFile(path string) error

func (*MockUtility) RemoveImage

func (mu *MockUtility) RemoveImage(image string, cli Client) error

Mock of the RemoveImage Utility function

func (*MockUtility) RenderErrorMarkdown added in v0.1.3

func (mu *MockUtility) RenderErrorMarkdown(input string)

Mock of the RenderErrorMarkdown utility function

func (*MockUtility) RenderInfoMarkdown added in v0.1.3

func (mu *MockUtility) RenderInfoMarkdown(input string)

Mock of the RenderInfoMarkdown utility function

func (*MockUtility) RunContainer

func (mu *MockUtility) RunContainer(image string, ports []string, volumes []string, containerName string, args []string) (string, error)

Mock of the RunContainer Utility function

func (*MockUtility) UpgradeDir

func (mu *MockUtility) UpgradeDir(path string) error

Mock of the UpgradeDir Utility function

type PackageImage

type PackageImage struct {
	Name     string    `hcl:"name,label"`
	BaseDir  string    `hcl:"base_dir,attr"`
	Versions []Version `hcl:"version,block"`
}

Package object to parse the package block in the package list

type PimHCLUtil

type PimHCLUtil struct {
	Pims []PackageImage `hcl:"pim,block"`
}

PackageHCLUtil object to contain a list of packages and all their attributes after the parsing of the package list

type Tools

type Tools interface {
	MakeDir(path string) error
	OpenFile(path string) (*os.File, error)
	RemoveDir(path string) error
	UpgradeDir(path string) error
	ParseBody(body hcl.Body, out interface{}) (interface{}, error)
	GetHCLBody(filepath string) (hcl.Body, error)
	PullImage(name string, cli Client) error
	ImageExists(imageID string, cli Client) (bool, error)
	CreateContainer(image string, cli Client) (string, error)
	CopyFromContainer(source string, dest string, containerID string, cli Client, cp Copier) error
	RemoveContainer(containerID string, cli Client) error
	RunContainer(image string, ports []string, volumes []string, containerName string, args []string) (string, error)
	RemoveImage(image string, cli Client) error
	AddAliasWin(name string, ed string) error
	RemoveAliasWin(name string, ed string) error
	AddAliasUnix(name string, ed string) error
	RemoveAliasUnix(name string, ed string) error
	FetchPimConfig(baseUrl string, pimName string, savePath string) error
	FileExists(path string) bool
	RemoveFile(path string) error
	GetListOfInstalledPimConfigs(pimConfigDir string) ([]string, error)
	Getwd() (string, error)
	RenderInfoMarkdown(input string)
	RenderErrorMarkdown(input string)
}

Tools interface so that we can create a mock of our utility functions in our unit tests

type Utility

type Utility struct{}

Utility Tool struct with its functions

func NewUtility

func NewUtility() *Utility

func (*Utility) AddAliasUnix

func (u *Utility) AddAliasUnix(name string, ed string) error

AddAlias will add the alias for the package name specified

func (*Utility) AddAliasWin

func (u *Utility) AddAliasWin(name string, ed string) error

AddAlias will add the alias for the package name specified

func (*Utility) CopyFromContainer

func (u *Utility) CopyFromContainer(source string, dest string, containerID string, cli Client, cp Copier) error

CopyFromContainer will copy files from within a Docker Container to the source location on the host

func (*Utility) CreateContainer

func (u *Utility) CreateContainer(image string, cli Client) (string, error)

CreateContainer - Create a Docker Container from a Docker Image. Returns the containerID and any errors

func (*Utility) FetchPimConfig

func (u *Utility) FetchPimConfig(baseUrl string, pimName string, savePath string) error

FetchPimConfig will get download the latest pim configuration for specified pim

func (*Utility) FileExists

func (u *Utility) FileExists(path string) bool

FileExists - checks to see if a file exists

func (*Utility) GetHCLBody

func (u *Utility) GetHCLBody(filepath string) (hcl.Body, error)

GetHCLBody gets the HCL Body from a given filepath

func (*Utility) GetListOfInstalledPimConfigs

func (u *Utility) GetListOfInstalledPimConfigs(pimConfigDir string) ([]string, error)

GetListOfInstalledPimConfigs - returns a string array of the names of the pims with configuration files currently in the pim configuration directory

func (*Utility) Getwd added in v0.1.3

func (u *Utility) Getwd() (string, error)

Getwd returns a rooted path name corresponding to the current directory

func (*Utility) ImageExists

func (u *Utility) ImageExists(imageID string, cli Client) (bool, error)

ImageExists - Function to check and see if Docker has the image downloaded

func (*Utility) MakeDir

func (u *Utility) MakeDir(path string) error

MakeDir makes a directory if it does not exist

func (*Utility) OpenFile

func (u *Utility) OpenFile(path string) (*os.File, error)

OpenFile opens the specified file, creating it if it does not exist

func (*Utility) OverwriteFile

func (u *Utility) OverwriteFile(path string) (*os.File, error)

OverwriteFile opens the specified file with overwrite mode, creating it if it does not exist

func (*Utility) ParseBody

func (u *Utility) ParseBody(body hcl.Body, out interface{}) (interface{}, error)

Parse function to parse the HCL body given

func (*Utility) PullImage

func (u *Utility) PullImage(name string, cli Client) error

PullImage - This function pulls a Docker Image from the packageless organization in Docker Hub

func (*Utility) RemoveAliasUnix

func (u *Utility) RemoveAliasUnix(name string, ed string) error

Remove Alias will remove the alias for the specified package name from the corresponding files

func (*Utility) RemoveAliasWin

func (u *Utility) RemoveAliasWin(name string, ed string) error

Remove Alias will remove the alias for the specified package name from the corresponding files

func (*Utility) RemoveContainer

func (u *Utility) RemoveContainer(containerID string, cli Client) error

RemoveContainer is used to remove a container Docker given the container ID

func (*Utility) RemoveDir

func (u *Utility) RemoveDir(path string) error

RemoveDir removes the specified directory

func (*Utility) RemoveFile

func (u *Utility) RemoveFile(path string) error

RemoveFile - will delete the file at the specified path

func (*Utility) RemoveImage

func (u *Utility) RemoveImage(image string, cli Client) error

RemoveImage removes the image with the given name from local Docker

func (*Utility) RenderErrorMarkdown added in v0.1.3

func (u *Utility) RenderErrorMarkdown(input string)

RenderErrorMarkdown - Renders markdown and outputs it with a color scheme specific to error messages

func (*Utility) RenderInfoMarkdown added in v0.1.3

func (u *Utility) RenderInfoMarkdown(input string)

RenderInfoMarkdown - Renders markdown and outputs it with a color scheme specific to info messages

func (*Utility) RunContainer

func (u *Utility) RunContainer(image string, ports []string, volumes []string, containerName string, args []string) (string, error)

RunContainer - Runs a container for the specified package

func (*Utility) UpgradeDir

func (u *Utility) UpgradeDir(path string) error

UpgradeDir resets the directory by removing it if it exists and then recreating it

type Version

type Version struct {
	Version string   `hcl:"version,label"`
	Image   string   `hcl:"image,attr"`
	Volumes []Volume `hcl:"volume,block"`
	Copies  []*Copy  `hcl:"copy,block"`
	Port    string   `hcl:"port,optional"`
}

type Volume

type Volume struct {
	Path  string `hcl:"path,optional"`
	Mount string `hcl:"mount,attr"`
}

Volume object to parse the volume block in the package list

Jump to

Keyboard shortcuts

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