Documentation ¶
Overview ¶
Package utils contains commonly needed utility functions for test suites inside the VM and in test workflow setup.
Index ¶
- Constants
- Variables
- func AccessSecret(ctx context.Context, client *secretmanager.Client, secretName string) (string, error)
- func CheckLinuxCmdExists(cmd string) bool
- func CheckPowershellReturnCode(command string, want int) error
- func CheckPowershellSuccess(command string) error
- func Context(t *testing.T) context.Context
- func CreateClient(user, host string, pembytes []byte) (*ssh.Client, error)
- func DownloadGCSObject(ctx context.Context, client *storage.Client, gcsPath string) ([]byte, error)
- func DownloadGCSObjectToFile(ctx context.Context, client *storage.Client, gcsPath, file string) error
- func DownloadPrivateKey(ctx context.Context, user string) ([]byte, error)
- func ExtractBaseImageName(image string) (string, error)
- func FailOnPowershellFail(command string, errorMsg string, t *testing.T)
- func FilterLoopbackTunnelingInterfaces(ifaces []net.Interface) []net.Interface
- func GetDaisyClient(ctx context.Context) (daisyCompute.Client, error)
- func GetHostKeysFileFromDisk() ([]byte, error)
- func GetHostKeysFromDisk() (map[string]string, error)
- func GetInstanceName(ctx context.Context) (string, error)
- func GetInterface(ctx context.Context, index int) (net.Interface, error)
- func GetInterfaceByMAC(mac string) (net.Interface, error)
- func GetMetadata(ctx context.Context, elem ...string) (string, error)
- func GetMetadataWithHeaders(ctx context.Context, elem ...string) (string, http.Header, error)
- func GetMountDiskPartition(diskExpectedSizeGB int) (string, error)
- func GetMountDiskPartitionSymlink(mountDiskName string) (string, error)
- func GetProjectZone(ctx context.Context) (string, string, error)
- func GetRealVMName(name string) (string, error)
- func HasFeature(img *compute.Image, feature string) bool
- func Is32BitWindows(image string) bool
- func IsCOS(image string) bool
- func IsWindows() bool
- func IsWindowsClient(image string) bool
- func LinuxOnly(t *testing.T)
- func ParseHostKey(bytes []byte) (map[string]string, error)
- func ParseInterfaceIPv4(iface net.Interface) (net.IP, error)
- func PutMetadata(ctx context.Context, path string, data string) error
- func Skip32BitWindows(t *testing.T, skipMsg string)
- func SkipWindowsClientImages(t *testing.T)
- func ValidWindowsPassword(userPwLgth int) string
- func WindowsContainersOnly(t *testing.T)
- func WindowsOnly(t *testing.T)
- type BlockDevice
- type BlockDeviceList
- type ProcessStatus
Constants ¶
const ( // GuestAttributeTestNamespace is the namespace for the guest attribute in the daisy "wait for instance" step for CIT. GuestAttributeTestNamespace = "citTest" // GuestAttributeTestKey is the key for the guest attribute in the daisy "wait for instance" step for CIT in the common case. GuestAttributeTestKey = "test-complete" // FirstBootGAKey is the key for guest attribute in the daisy "wait for instance" step in the case where it is the first boot, and we still want to wait for results from a subsequent reboot. FirstBootGAKey = "first-boot-key" )
Variables ¶
var ( // ErrMDSEntryNotFound is an error used to report 404 status code. ErrMDSEntryNotFound = errors.New("No metadata entry found: 404 error") )
Functions ¶
func AccessSecret ¶
func AccessSecret(ctx context.Context, client *secretmanager.Client, secretName string) (string, error)
AccessSecret accesses the given secret.
func CheckLinuxCmdExists ¶
CheckLinuxCmdExists checks that a command exists on the linux image, and is executable.
func CheckPowershellReturnCode ¶
CheckPowershellReturnCode returns an error if the exit code doesn't match the expected value.
func CheckPowershellSuccess ¶
CheckPowershellSuccess returns an error if the powershell command fails.
func Context ¶
Context returns a context to be used by test implementations, it handles the context cancellation based on the test's timeout(deadline), if no timeout is defined (or the deadline can't be assessed) then a plain background context is returned.
func CreateClient ¶
CreateClient create a ssh client to connect host.
func DownloadGCSObject ¶
DownloadGCSObject downloads a GCS object.
func DownloadGCSObjectToFile ¶
func DownloadGCSObjectToFile(ctx context.Context, client *storage.Client, gcsPath, file string) error
DownloadGCSObjectToFile downloads a GCS object, writing it to the specified file.
func DownloadPrivateKey ¶
DownloadPrivateKey download private key from daisy source.
func ExtractBaseImageName ¶
ExtractBaseImageName extract the base image name from full image resource.
func FailOnPowershellFail ¶
FailOnPowershellFail fails the test if the powershell command fails.
func FilterLoopbackTunnelingInterfaces ¶
FilterLoopbackTunnelingInterfaces filters the list of interfaces to contain only the actual NICs, removing the loopback and tunneling interfaces listed in skipInterfaces.
func GetDaisyClient ¶
func GetDaisyClient(ctx context.Context) (daisyCompute.Client, error)
GetDaisyClient returns a daisy compute client with the correct compute endpoint.
func GetHostKeysFileFromDisk ¶
GetHostKeysFileFromDisk read ssh host public key as bytes
func GetHostKeysFromDisk ¶
GetHostKeysFromDisk read ssh host public key and parse
func GetInstanceName ¶
GetInstanceName gets the instance name.
func GetInterface ¶
GetInterface returns the interface corresponding to the metadata interface array at the specified index.
func GetInterfaceByMAC ¶
GetInterfaceByMAC returns the interface with the specified MAC address.
func GetMetadata ¶
GetMetadata does a HTTP Get request to the metadata server, the metadata entry of interest is provided by elem as the elements of the entry path, the following example does a Get request to the entry "instance/guest-attributes":
resp, err := GetAttribute(context.Background(), "instance", "guest-attributes") ...
func GetMetadataWithHeaders ¶
GetMetadataWithHeaders is similar to GetMetadata it only differs on the return where GetMetadata returns only the response's body as a string and an error GetMetadataWithHeaders returns the response's body as a string, the headers and an error.
func GetMountDiskPartition ¶
GetMountDiskPartition runs lsblk to get the partition of the mount disk on linux, assuming the size of the mount disk is diskExpectedSizeGb.
func GetMountDiskPartitionSymlink ¶
GetMountDiskPartitionSymlink uses symlinks to get the partition of the mount disk on linux, assuming the name of the disk resource is mountDiskName.
func GetProjectZone ¶
GetProjectZone gets the project and zone of the instance.
func GetRealVMName ¶
GetRealVMName returns the real name of a VM running in the same test.
func HasFeature ¶
HasFeature reports whether a compute.Image has a GuestOsFeature tag.
func Is32BitWindows ¶
Is32BitWindows returns true if the image contains -x86.
func IsWindows ¶
func IsWindows() bool
IsWindows returns true if the detected runtime environment is Windows.
func IsWindowsClient ¶
IsWindowsClient returns true if the image is a client (non-server) Windows image.
func ParseHostKey ¶
ParseHostKey parse hostkey data from bytes.
func ParseInterfaceIPv4 ¶
ParseInterfaceIPv4 parses the interface's IPv4 address.
func PutMetadata ¶
PutMetadata does a HTTP Put request to the metadata server, the metadata entry of interest is provided by path as the section of the path after the metadata server, with the data string as the post data. The following example sets the key "instance/guest-attributes/example" to "data":
err := PutMetadata(context.Background(), path.Join("instance", "guest-attributes", "example"), "data") ...
func Skip32BitWindows ¶
Skip32BitWindows skips tests on 32-bit client images.
func SkipWindowsClientImages ¶
SkipWindowsClientImages skips tests on Windows Client Images.
func ValidWindowsPassword ¶
ValidWindowsPassword returns a random password of the given length which meets Windows complexity requirements.
func WindowsContainersOnly ¶
WindowsContainersOnly skips tests not on Windows "for Containers" images.
Types ¶
type BlockDevice ¶
type BlockDevice struct { Name string `json:"name,omitempty"` // on some OSes, size is a string, and on some OSes, size is a number. // This allows both to be parsed Size json.Number `json:"size,omitempty"` Type string `json:"type,omitempty"` // Other fields are not currently used. X map[string]any `json:"-"` }
BlockDevice defines information about a single partition or disk in the output of lsblk.
type BlockDeviceList ¶
type BlockDeviceList struct {
BlockDevices []BlockDevice `json:"blockdevices,omitempty"`
}
BlockDeviceList gives full information about blockdevices, from the output of lsblk.
type ProcessStatus ¶
ProcessStatus holds stdout, stderr and the exit code from an external command call.
func RunPowershellCmd ¶
func RunPowershellCmd(command string) (ProcessStatus, error)
RunPowershellCmd runs a powershell command and returns stdout and stderr if successful.