utils

package
v0.0.0-...-918ecbf Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Overview

Package utils contains commonly needed utility functions for test suites inside the VM and in test workflow setup.

Index

Constants

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

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

func CheckLinuxCmdExists(cmd string) bool

CheckLinuxCmdExists checks that a command exists on the linux image, and is executable.

func CheckPowershellReturnCode

func CheckPowershellReturnCode(command string, want int) error

CheckPowershellReturnCode returns an error if the exit code doesn't match the expected value.

func CheckPowershellSuccess

func CheckPowershellSuccess(command string) error

CheckPowershellSuccess returns an error if the powershell command fails.

func Context

func Context(t *testing.T) context.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

func CreateClient(user, host string, pembytes []byte) (*ssh.Client, error)

CreateClient create a ssh client to connect host.

func DownloadGCSObject

func DownloadGCSObject(ctx context.Context, client *storage.Client, gcsPath string) ([]byte, error)

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

func DownloadPrivateKey(ctx context.Context, user string) ([]byte, error)

DownloadPrivateKey download private key from daisy source.

func ExtractBaseImageName

func ExtractBaseImageName(image string) (string, error)

ExtractBaseImageName extract the base image name from full image resource.

func FailOnPowershellFail

func FailOnPowershellFail(command string, errorMsg string, t *testing.T)

FailOnPowershellFail fails the test if the powershell command fails.

func FilterLoopbackTunnelingInterfaces

func FilterLoopbackTunnelingInterfaces(ifaces []net.Interface) []net.Interface

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

func GetHostKeysFileFromDisk() ([]byte, error)

GetHostKeysFileFromDisk read ssh host public key as bytes

func GetHostKeysFromDisk

func GetHostKeysFromDisk() (map[string]string, error)

GetHostKeysFromDisk read ssh host public key and parse

func GetInstanceName

func GetInstanceName(ctx context.Context) (string, error)

GetInstanceName gets the instance name.

func GetInterface

func GetInterface(ctx context.Context, index int) (net.Interface, error)

GetInterface returns the interface corresponding to the metadata interface array at the specified index.

func GetInterfaceByMAC

func GetInterfaceByMAC(mac string) (net.Interface, error)

GetInterfaceByMAC returns the interface with the specified MAC address.

func GetMetadata

func GetMetadata(ctx context.Context, elem ...string) (string, error)

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

func GetMetadataWithHeaders(ctx context.Context, elem ...string) (string, http.Header, error)

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

func GetMountDiskPartition(diskExpectedSizeGB int) (string, error)

GetMountDiskPartition runs lsblk to get the partition of the mount disk on linux, assuming the size of the mount disk is diskExpectedSizeGb.

func GetMountDiskPartitionSymlink(mountDiskName string) (string, error)

GetMountDiskPartitionSymlink uses symlinks to get the partition of the mount disk on linux, assuming the name of the disk resource is mountDiskName.

func GetProjectZone

func GetProjectZone(ctx context.Context) (string, string, error)

GetProjectZone gets the project and zone of the instance.

func GetRealVMName

func GetRealVMName(name string) (string, error)

GetRealVMName returns the real name of a VM running in the same test.

func HasFeature

func HasFeature(img *compute.Image, feature string) bool

HasFeature reports whether a compute.Image has a GuestOsFeature tag.

func Is32BitWindows

func Is32BitWindows(image string) bool

Is32BitWindows returns true if the image contains -x86.

func IsCOS

func IsCOS(image string) bool

IsCOS returns true if the image is cos.

func IsWindows

func IsWindows() bool

IsWindows returns true if the detected runtime environment is Windows.

func IsWindowsClient

func IsWindowsClient(image string) bool

IsWindowsClient returns true if the image is a client (non-server) Windows image.

func LinuxOnly

func LinuxOnly(t *testing.T)

LinuxOnly skips tests not on Linux.

func ParseHostKey

func ParseHostKey(bytes []byte) (map[string]string, error)

ParseHostKey parse hostkey data from bytes.

func ParseInterfaceIPv4

func ParseInterfaceIPv4(iface net.Interface) (net.IP, error)

ParseInterfaceIPv4 parses the interface's IPv4 address.

func PutMetadata

func PutMetadata(ctx context.Context, path string, data string) error

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

func Skip32BitWindows(t *testing.T, skipMsg string)

Skip32BitWindows skips tests on 32-bit client images.

func SkipWindowsClientImages

func SkipWindowsClientImages(t *testing.T)

SkipWindowsClientImages skips tests on Windows Client Images.

func ValidWindowsPassword

func ValidWindowsPassword(userPwLgth int) string

ValidWindowsPassword returns a random password of the given length which meets Windows complexity requirements.

func WindowsContainersOnly

func WindowsContainersOnly(t *testing.T)

WindowsContainersOnly skips tests not on Windows "for Containers" images.

func WindowsOnly

func WindowsOnly(t *testing.T)

WindowsOnly skips tests not on Windows.

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

type ProcessStatus struct {
	Stdout   string
	Stderr   string
	Exitcode int
}

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.

Jump to

Keyboard shortcuts

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