utils

package
v0.0.0-...-7ce9f83 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2023 License: Apache-2.0 Imports: 13 Imported by: 3

Documentation

Overview

Copyright (c) 2021 - for information on the respective copyright owner see the NOTICE file and/or the repository https://github.com/carbynestack/ephemeral.

SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadFile

func ReadFile(path string) ([]byte, error)

ReadFile reads file content for a given file location.

Types

type Commander

type Commander struct {
	Command string
	Options []string
}

Commander is a wrapper around os/exec.Command(). It returns a byte array containing the output of stdout and error. An error in command execution will land there. Stderr is scanned as well, its output is printed out.

func NewCommander

func NewCommander() *Commander

NewCommander returns a new commander.

func (*Commander) CallCMD

func (c *Commander) CallCMD(ctx context.Context, cmd []string, dir string) ([]byte, []byte, error)

CallCMD calls a specified command in sh and returns its stdout and stderr as a byte slice and potentially an error. As per os/exec doc: ``` If the command fails to run or doesn't complete successfully, the error is of type *ExitError. Other error types may be returned for I/O problems. ```

func (*Commander) Run

func (c *Commander) Run(cmd string) ([]byte, []byte, error)

Run is a facade command that runs a single command from the current directory.

type CreatePathResponse

type CreatePathResponse error

CreatePathResponse is used to define the default response returned by MockedFileIO.CreatePath calls.

type CreatePipeResponse

type CreatePipeResponse error

CreatePipeResponse is used to define the default response returned by MockedFileIO.CreatePipe calls.

type DeleteResponse

type DeleteResponse error

DeleteResponse is used to define the default response returned by MockedFileIO.Delete calls.

type Executor

type Executor interface {
	// CallCMD executes the command and returns the output's STDOUT, STDERR streams as well as any errors
	CallCMD(ctx context.Context, cmd []string, dir string) ([]byte, []byte, error)
}

Executor is an interface for calling a command and process its output.

type File

type File interface {
	io.ReadWriteCloser
	io.StringWriter
	SetWriteDeadline(t time.Time) error
}

File is an interface for basic file based io methods

type FileErrorPair

type FileErrorPair struct {
	File  File
	Error error
}

FileErrorPair is a tuple of File and error as returned by some MockedFileIO methods.

type FileIO

type FileIO interface {
	CreatePath(path string) error
	CreatePipe(path string) error
	Delete(path string) error
	OpenRead(path string) (File, error)
	OpenWriteOrCreate(name string) (File, error)
	OpenWritePipe(name string) (File, error)
	ReadLine(file File) (string, error)
}

FileIO is an interface for filesystem methods

var Fio FileIO = &OSFileIO{}

Fio is a pointer to the shared FileIO implementation

type MockedBrokenRoundTripper

type MockedBrokenRoundTripper struct {
}

MockedBrokenRoundTripper mocks http.RoundTripper for testing which will always result in an error

func (*MockedBrokenRoundTripper) RoundTrip

func (m *MockedBrokenRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip mocks the execution of a single HTTP request and returns an error for each request

type MockedFileIO

type MockedFileIO struct {
	CreatePathResponse        CreatePathResponse
	CreatePathCalls           []string
	CreatePipeResponse        CreatePipeResponse
	CreatePipeCalls           []string
	DeleteResponse            DeleteResponse
	DeleteCalls               []string
	OpenReadResponse          OpenReadResponse
	OpenReadCalls             []string
	OpenWriteOrCreateResponse OpenWriteOrCreateResponse
	OpenWriteOrCreateCalls    []string
	OpenWritePipeResponse     OpenWritePipeResponse
	OpenWritePipeCalls        []string
	ReadLineResponse          ReadLineResponse
	ReadLineCalls             []File
}

MockedFileIO implements fileIO as a mock for testing

func (*MockedFileIO) CreatePath

func (mfio *MockedFileIO) CreatePath(path string) error

CreatePath mocks the creation of a directory. Returns MockedFileIO.CreatePathResponse.

func (*MockedFileIO) CreatePipe

func (mfio *MockedFileIO) CreatePipe(path string) error

CreatePipe mocks the creation of a named pipe. Returns MockedFileIO.CreatePipeResponse.

func (*MockedFileIO) Delete

func (mfio *MockedFileIO) Delete(path string) error

Delete mocks the deletion of a file. Returns MockedFileIO.DeleteResponse.

func (*MockedFileIO) OpenRead

func (mfio *MockedFileIO) OpenRead(path string) (File, error)

OpenRead mocks opening a file for reading. Returns the attributes from MockedFileIO.OpenReadResponse.

func (*MockedFileIO) OpenWriteOrCreate

func (mfio *MockedFileIO) OpenWriteOrCreate(path string) (File, error)

OpenWriteOrCreate mocks opening a file for write access. Returns the attributes from MockedFileIO.OpenWriteOrCreateResponse.

func (*MockedFileIO) OpenWritePipe

func (mfio *MockedFileIO) OpenWritePipe(path string) (File, error)

OpenWritePipe mocks opening a named pipe for write access. Returns the attributes from MockedFileIO.OpenWritePipeResponse.

func (*MockedFileIO) ReadLine

func (mfio *MockedFileIO) ReadLine(file File) (string, error)

ReadLine mocks reading a string from a file. Returns the attributes from // MockedFileIO.ReadStringResponse.

type MockedRoundTripper

type MockedRoundTripper struct {
	ExpectedPath         string
	ReturnJSON           []byte
	ExpectedResponseCode int
}

MockedRoundTripper mocks http.RoundTripper for testing which always returns successful

func (*MockedRoundTripper) RoundTrip

func (m *MockedRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip mocks the execution of a single HTTP request and returns the ExpectedResponseCode

type OSFileIO

type OSFileIO struct{}

OSFileIO implements fileIO backed by default os methods

func (OSFileIO) CreatePath

func (OSFileIO) CreatePath(path string) error

CreatePath creates a directory and all parents if required. Returns nil on success or an error otherwise. This implementation is backed by os.MkdirAll.

func (OSFileIO) CreatePipe

func (OSFileIO) CreatePipe(path string) error

CreatePipe creates a named pipe. Returns nil on success or an error otherwise. This implementation is backed by unix.Mkfifo.

func (OSFileIO) Delete

func (OSFileIO) Delete(path string) error

Delete deletes a single file or directory with all contained elements. Returns nil on success or an error otherwise. This implementation is backed by os.Remove.

func (OSFileIO) OpenRead

func (OSFileIO) OpenRead(path string) (File, error)

OpenRead opens a file for reading. Returns a file which can be accessed for further processing. If opening the file fails, an error is returned instead. This implementation is backed by os.Open.

func (OSFileIO) OpenWriteOrCreate

func (OSFileIO) OpenWriteOrCreate(path string) (File, error)

OpenWriteOrCreate opens a file for write access. The given file is created in case it does not exist. On success, a file is returned for further interaction. Otherwise, an error is returned. This implementation is backed by os.OpenFile.

func (OSFileIO) OpenWritePipe

func (OSFileIO) OpenWritePipe(path string) (File, error)

OpenWritePipe opens a named pipe for write access. Returns a file which can be accessed for further processing. Otherwise, an error is returned. This implementation is backed by os.OpenFile.

func (OSFileIO) ReadLine

func (OSFileIO) ReadLine(file File) (string, error)

ReadLine reads a line from a file. Returns the line read on success. If an error occurred before finding end of line, an error is returned. This can also include io.EOF. This implementation is backed by bufio.Reader.

type OpenReadResponse

type OpenReadResponse FileErrorPair

OpenReadResponse is used to define the default response returned by MockedFileIO.OpenRead calls.

type OpenWriteOrCreateResponse

type OpenWriteOrCreateResponse FileErrorPair

OpenWriteOrCreateResponse is used to define the default response returned by MockedFileIO.OpenWriteOrCreate calls.

type OpenWritePipeResponse

type OpenWritePipeResponse FileErrorPair

OpenWritePipeResponse is used to define the default response returned by MockedFileIO.OpenWritePipe calls.

type ReadLineResponse

type ReadLineResponse struct {
	Line  string
	Error error
}

ReadLineResponse is a tuple of a string and an error as returned by MockedFileIO.ReadLine.

type SimpleFileMock

type SimpleFileMock struct {
	ReadWriteCountResponse int
	IOError                error
	SetDeadlineError       error
	CloseError             error
	WrittenData            []byte
	WriteDeadline          time.Time
}

SimpleFileMock mocks os.File i/o for testing.

The error given in IOError is returned on all methods. Data written is appended to IOData on with each call an the length of the passed data returned as number of bytes written.

func (*SimpleFileMock) Close

func (sfm *SimpleFileMock) Close() error

Close mocks the close call and returns defined CloseError

func (*SimpleFileMock) Read

func (sfm *SimpleFileMock) Read(t []byte) (int, error)

Read mocks read calls. Data stored in WrittenData is copied to the given target array 't' at max of length of 't'. Returns length of copied data and IOError.

func (*SimpleFileMock) SetWriteDeadline

func (sfm *SimpleFileMock) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets WriteDeadline

func (*SimpleFileMock) Write

func (sfm *SimpleFileMock) Write(d []byte) (int, error)

Write mocks the function Write call, appends given data to WriteData and returns defined CloseError.

func (*SimpleFileMock) WriteString

func (sfm *SimpleFileMock) WriteString(d string) (int, error)

WriteString mocks the WriteString call, appends given data to WriteData and returns defined CloseError.

Jump to

Keyboard shortcuts

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