github.com/coreos/mantle@v0.13.0/kola/tests/coretest/helpers.go (about)

     1  package coretest
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"crypto/sha256"
     7  	"encoding/hex"
     8  	"fmt"
     9  	"io/ioutil"
    10  	"net"
    11  	"net/http"
    12  	"os"
    13  	"os/exec"
    14  	"strings"
    15  	"time"
    16  )
    17  
    18  func CheckPort(network, address string, timeout time.Duration) error {
    19  	errc := make(chan error, 1)
    20  	go func() {
    21  		_, err := net.Dial(network, address)
    22  		errc <- err
    23  	}()
    24  	select {
    25  	case <-time.After(timeout):
    26  		return fmt.Errorf("%s:%s timed out after %s seconds.",
    27  			network, address, timeout)
    28  	case err := <-errc:
    29  		return err
    30  	}
    31  }
    32  
    33  func IsLink(f os.FileInfo) bool {
    34  	return f.Mode()&os.ModeSymlink != 0
    35  }
    36  
    37  func CheckHttpStatus(url string, timeout time.Duration) error {
    38  	errc := make(chan error, 1)
    39  	go func() {
    40  		tr := &http.Transport{}
    41  
    42  		req, err := http.NewRequest("GET", url, nil)
    43  		if err != nil {
    44  			errc <- err
    45  			return
    46  		}
    47  
    48  		resp, err := tr.RoundTrip(req)
    49  		if err != nil {
    50  			errc <- err
    51  			return
    52  		}
    53  		defer resp.Body.Close()
    54  
    55  		if resp.StatusCode != 200 {
    56  			errc <- fmt.Errorf("%s failed with status %d.", url, resp.StatusCode)
    57  			return
    58  		}
    59  		errc <- nil
    60  	}()
    61  
    62  	select {
    63  	case <-time.After(timeout):
    64  		return fmt.Errorf("%s timed out after %s seconds.",
    65  			url, timeout)
    66  	case err := <-errc:
    67  		return err
    68  	}
    69  }
    70  
    71  type MountTable struct {
    72  	Device     string
    73  	MountPoint string
    74  	FsType     string
    75  	Options    []string
    76  }
    77  
    78  func GetMountTable() (mounts []MountTable, err error) {
    79  	f, err := os.Open("/proc/mounts")
    80  	if err != nil {
    81  		return
    82  	}
    83  
    84  	scanner := bufio.NewScanner(f)
    85  	for scanner.Scan() {
    86  		line := strings.Split(scanner.Text(), " ")
    87  		if len(line) < 6 {
    88  			continue
    89  		}
    90  		mounts = append(mounts, MountTable{
    91  			Device:     line[0],
    92  			MountPoint: line[1],
    93  			FsType:     line[2],
    94  			Options:    strings.Split(line[3], ","),
    95  		})
    96  	}
    97  	return
    98  }
    99  
   100  func Sha256File(fileName string) (hash string, err error) {
   101  	f, err := os.Open(fileName)
   102  	if err != nil {
   103  		return
   104  	}
   105  
   106  	bytes, err := ioutil.ReadAll(f)
   107  	if err != nil {
   108  		return
   109  	}
   110  
   111  	fileHasher := sha256.New()
   112  	fileHasher.Write(bytes)
   113  	return hex.EncodeToString(fileHasher.Sum(nil)), nil
   114  }
   115  
   116  func Run(command string, args ...string) (string, string, error) {
   117  	var stdoutBytes, stderrBytes bytes.Buffer
   118  	cmd := exec.Command(command, args...)
   119  	cmd.Stdout = &stdoutBytes
   120  	cmd.Stderr = &stderrBytes
   121  	err := cmd.Run()
   122  	return stdoutBytes.String(), stderrBytes.String(), err
   123  }
   124  
   125  // MachineID returns the content of /etc/machine-id. It panics on any error.
   126  func MachineID() string {
   127  	f, err := os.Open("/etc/machine-id")
   128  	if err != nil {
   129  		panic(err)
   130  	}
   131  
   132  	defer f.Close()
   133  
   134  	buf, err := ioutil.ReadAll(f)
   135  	if err != nil {
   136  		panic(err)
   137  	}
   138  
   139  	return strings.TrimSpace(string(buf))
   140  }