common

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2019 License: Apache-2.0 Imports: 21 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.

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 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 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, then it looks in the directory containing the binary for the running process.

func GetAbsInstallPath

func GetAbsInstallPath(relPath string) (string, error)

GetAbsInstallPath retrieves absolute path of files in daos install dir

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 Include

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

Include 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 MemberResultsToPB

func MemberResultsToPB(results SystemMemberResults) []*ctlpb.SystemStopResp_Result

MemberResultsToPB converts SystemMemberResults to equivalent protobuf format.

func MembersToPB

func MembersToPB(members SystemMembers) []*ctlpb.SystemMember

MembersToPB converts internal member structs to protobuf equivalents.

func MockCheckMountOk

func MockCheckMountOk(path string) error

MockCheckMountOk mocks CheckMount and always returns nil error.

func MockControllerPB

func MockControllerPB() *ctlpb.NvmeController

MockControllerPB is a mock protobuf Controller message used in tests for multiple packages (message contains repeated namespace field).

func MockDeviceHealthPB

func MockDeviceHealthPB() *ctlpb.NvmeController_Health

MockDeviceHealthPB is a mock protobuf Health message used in tests for multiple packages.

func MockModulePB

func MockModulePB() *ctlpb.ScmModule

MockModulePB is a mock protobuf Module message used in tests for multiple packages.

func MockMountPB

func MockMountPB() *ctlpb.ScmMount

MockMountPB is a mock protobuf Mount message used in tests for multiple packages.

func MockNamespacePB

func MockNamespacePB() *ctlpb.NvmeController_Namespace

MockNamespacePB is a mock protobuf Namespace message used in tests for multiple packages.

func MockPmemDevicePB

func MockPmemDevicePB() *ctlpb.PmemDevice

MockPmemDevicePB is a mock protobuf PmemDevice used in tests for multiple packages.

func NewMockControllerPB

func NewMockControllerPB(
	pciAddr string, fwRev string, model string, serial string,
	nss []*ctlpb.NvmeController_Namespace, hs *ctlpb.NvmeController_Health) *ctlpb.NvmeController

NewMockControllerPB generates specific protobuf controller message

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 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 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 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

type Membership

type Membership struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Membership tracks details of system members.

func NewMembership

func NewMembership(log logging.Logger) *Membership

func (*Membership) Add

func (m *Membership) Add(member SystemMember) (int, error)

Add adds member to membership.

func (*Membership) GetMember

func (m *Membership) GetMember(uuid string) (*SystemMember, error)

GetMember retrieves member from membership based on UUID.

func (*Membership) GetMembers

func (m *Membership) GetMembers() SystemMembers

GetMembers returns internal member structs as a sequence.

func (*Membership) Remove

func (m *Membership) Remove(uuid string)

Remove removes member from membership, idenpotent.

Avoid taking a RW lock where possible.

type SystemMember

type SystemMember struct {
	Addr net.Addr
	Uuid string
	Rank uint32
}

SystemMember refers to a data-plane instance that is a member of this DAOS system running on host with the control-plane listening at "Addr".

func (SystemMember) String

func (sm SystemMember) String() string

type SystemMemberResult

type SystemMemberResult struct {
	ID     string
	Action string
	Err    error
}

SystemMemberResult refers to the result of an action on a SystemMember identified by string representation "address/uuid/rank".

func (SystemMemberResult) String

func (smr SystemMemberResult) String() string

type SystemMemberResults

type SystemMemberResults []*SystemMemberResult

func MemberResultsFromPB

func MemberResultsFromPB(log logging.Logger, pbResults []*ctlpb.SystemStopResp_Result) SystemMemberResults

MemberResultsFromPB converts results from member actions (protobuf format) to SystemMemberResults.

func (SystemMemberResults) String

func (smrs SystemMemberResults) String() string

type SystemMembers

type SystemMembers []*SystemMember

func MembersFromPB

func MembersFromPB(log logging.Logger, pbMembers []*ctlpb.SystemMember) SystemMembers

MembersFromPB converts to member slice from protobuf format.

Don't populate member Addr field if it can't be resolved.

func (SystemMembers) String

func (sms SystemMembers) String() string

Directories

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

Jump to

Keyboard shortcuts

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