common

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2020 License: Apache-2.0 Imports: 20 Imported by: 0

README

Common

Common utilities for file access, collection manipulation (including some functional helper methods), unit tests and mocks among others. This package will be imported from multiple other packages under its parent directory and to avoid cyclical import dependencies files in this package should not import from peer packages unless absolutely necessary.

Documentation

Overview

(C) Copyright 2019 Intel Corporation.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

GOVERNMENT LICENSE RIGHTS-OPEN SOURCE SOFTWARE The Government's rights to use, modify, reproduce, release, perform, display, or disclose this software are subject to the terms of the Apache License as provided in Contract No. 8F-30005. Any reproduction of computer software, computer software documentation, or portions thereof marked with this legend must also reproduce the markings.

(C) Copyright 2019 Intel Corporation.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

GOVERNMENT LICENSE RIGHTS-OPEN SOURCE SOFTWARE The Government's rights to use, modify, reproduce, release, perform, display, or disclose this software are subject to the terms of the Apache License as provided in Contract No. 8F-30005. Any reproduction of computer software, computer software documentation, or portions thereof marked with this legend must also reproduce the markings.

Index

Constants

View Source
const DisableProxyScrubEnv = "DAOS_DISABLE_PROXY_SCRUB"
View Source
const UtilLogDepth = 4

UtilLogDepth signifies stack depth, set calldepth on calls to logger so log message context refers to caller not callee.

Variables

This section is empty.

Functions

func All

func All(ss []string, f func(string) bool) bool

All returns true if all strings returns true from f.

func Any

func Any(ss []string, f func(string) bool) bool

Any returns true if any of the strings in the slice returns true from f.

func AppendFile

func AppendFile(path string) (*os.File, error)

AppendFile appends to existing or creates new file with default options

func AssertEqual

func AssertEqual(
	t *testing.T, a interface{}, b interface{}, message string)

AssertEqual asserts b is equal to a

Whilst suitable in most situations, reflect.DeepEqual() may not be suitable for nontrivial struct element comparisons, go-cmp should then be used but will introduce a third party dep.

func AssertFalse

func AssertFalse(t *testing.T, b bool, message string)

AssertFalse asserts b is false

func AssertStringsEqual

func AssertStringsEqual(
	t *testing.T, a []string, b []string, message string)

AssertStringsEqual sorts string slices before comparing.

func AssertTrue

func AssertTrue(t *testing.T, b bool, message string)

AssertTrue asserts b is true

func CmpErr

func CmpErr(t *testing.T, want, got error)

CmpErr compares two errors for equality or at least close similarity in their messages.

func CmpErrBool added in v0.8.0

func CmpErrBool(want, got error) bool

CmpErrBool compares two errors and returns a boolean value indicating equality or at least close similarity between their messages.

func CreateTestDir

func CreateTestDir(t *testing.T) (string, func())

CreateTestDir creates a temporary test directory. It returns the path to the directory and a cleanup function.

func CreateTestSocket added in v0.9.0

func CreateTestSocket(t *testing.T, sockPath string) (*net.UnixListener, func())

CreateTestSocket creates a Unix Domain Socket that can listen for connections on a given path. It returns the listener and a cleanup function.

func DefaultCmpOpts

func DefaultCmpOpts() []cmp.Option

DefaultCmpOpts gets default go-cmp comparison options for tests.

func ExpectError

func ExpectError(t *testing.T, actualErr error, expectedMessage string, desc interface{})

ExpectError asserts error contains expected message

func Filter

func Filter(ss []string, f func(string) bool) (nss []string)

Filter returns new slice with only strings that return true from f.

func FindBinary

func FindBinary(binName string) (string, error)

FindBinary attempts to locate the named binary by checking $PATH first. If the binary is not found in $PATH, look in the directory containing the running process' binary as well as the working directory.

func GetAdjacentPath added in v0.9.0

func GetAdjacentPath(inPath string) (string, error)

GetAdjacentPath retrieves path relative to the binary used to launch the currently running process.

func GetConsent

func GetConsent(log logging.Logger) bool

GetConsent scans stdin for yes/no

func GetFilePaths

func GetFilePaths(dir string, ext string) ([]string, error)

GetFilePaths return full file paths in given directory with matching file extensions

func GetWorkingPath added in v0.9.0

func GetWorkingPath(inPath string) (string, error)

GetWorkingPath retrieves path relative to the current working directory when invoking the current process.

func HasPort added in v0.9.0

func HasPort(addr string) bool

HasPort checks if addr specifies a port. This only works with IPv4 addresses at the moment.

func Includes added in v0.9.0

func Includes(ss []string, target string) bool

Includes returns true if string target in slice.

func Index

func Index(ss []string, target string) int

Index returns first index of target string, -1 if no match

func IsAlphabetic

func IsAlphabetic(s string) bool

IsAlphabetic checks of a string just contains alphabetic characters.

func LoadTestFiles

func LoadTestFiles(inFile string, outFile string) (
	inputs [][]string, outputs [][]string, err error)

LoadTestFiles reads inputs and outputs from file and do basic sanity checks. Both files contain entries of multiple lines separated by blank line. Return inputs and outputs, both of which are slices of string slices.

func Map

func Map(ss []string, f func(string) string) (nss []string)

Map returns new slice with f applied to each string in original.

func MockCheckMountOk

func MockCheckMountOk(path string) error

MockCheckMountOk mocks CheckMount and always returns nil error.

func ParseInts added in v0.9.1

func ParseInts(in string) (ints []uint32, err error)

ParseInts converts string of uint32s to uint32 array.

func Pluralise

func Pluralise(s string, n int) string

Pluralise appends "s" to input string unless n==1.

func PrintStructs

func PrintStructs(name string, i interface{})

PrintStructs dumps friendly YAML representation of structs to stdout proceeded with "name" identifier.

func ResolvePath added in v0.9.0

func ResolvePath(inPath string, defaultPath string) (outPath string, err error)

ResolvePath simply returns an absolute path, appends input path to current working directory if input path not empty otherwise appends default path to location of running binary (adjacent). Use case is specific to config files.

func Run

func Run(cmd string) error

Run executes command in os and builds useful error message.

func ScrubEnvironment

func ScrubEnvironment(blacklist []string)

ScrubEnvironment modifies the environment variables set for this process and any children which inherit its environment by unsetting any variables supplied in the blacklist.

func ScrubEnvironmentExcept

func ScrubEnvironmentExcept(whitelist []string)

ScrubEnvironmentExcept modifies the environment variables set for this process and any children which inherit its environment by unsetting any variables that are not supplied in the whitelist.

func ScrubProxyVariables

func ScrubProxyVariables()

ScrubProxyVariables removes proxy variables from the process environment.

func SetupTestListener added in v0.9.0

func SetupTestListener(t *testing.T, conn chan *net.UnixConn) (string, func())

SetupTestListener sets up a Unix Domain Socket in a temp directory to listen and receive one connection. The server-side connection object is sent through the conn channel when a client connects. It returns the path to the socket, to allow the client to connect, and a cleanup function.

func ShowBufferOnFailure

func ShowBufferOnFailure(t *testing.T, buf fmt.Stringer)

ShowBufferOnFailure displays captured output on test failure. Should be run via defer in the test function.

func SplitFile

func SplitFile(path string) (sections [][]string, err error)

SplitFile separates file content into contiguous sections separated by a blank line.

func SplitPort added in v0.9.0

func SplitPort(addrPattern string, defaultPort int) (string, string, error)

SplitPort separates port from host in address and can apply default port if address doesn't contain one.

func StructsToString

func StructsToString(i interface{}) (lines string, err error)

StructsToString returns yaml representation (as a list of strings) of any interface but avoids fields/lines prefixed with xxx_ such as added by protobuf boilerplate.

func SyncDir

func SyncDir(path string) (err error)

SyncDir flushes all prior modifications to a directory. This is required if one modifies a directory (e.g., by creating a new file in it) and needs to wait for this modification to become persistent.

func TruncFile

func TruncFile(path string) (*os.File, error)

TruncFile overrides existing or creates new file with default options

func WriteFileAtomic

func WriteFileAtomic(path string, data []byte, perm os.FileMode) error

WriteFileAtomic mimics ioutil.WriteFile, but it makes sure the file is either successfully written persistently or untouched.

func WriteSlice

func WriteSlice(path string, slice []string) (err error)

WriteSlice writes string slice to specified file, overwriting and creating if non-existent.

func WriteString

func WriteString(path string, s string) error

WriteString writes string to specified file, wrapper around WriteSlice.

Types

This section is empty.

Directories

Path Synopsis
(C) Copyright 2019 Intel Corporation.
(C) Copyright 2019 Intel Corporation.
convert
(C) Copyright 2019 Intel Corporation.
(C) Copyright 2019 Intel Corporation.
ctl
mgmt
(C) Copyright 2019 Intel Corporation.
(C) Copyright 2019 Intel Corporation.
srv

Jump to

Keyboard shortcuts

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