github.com/naphatkrit/deis@v1.12.3/tests/utils/utils.go (about)

     1  // Package utils contains commonly useful functions from Deis testing.
     2  package utils
     3  
     4  import (
     5  	"bufio"
     6  	"bytes"
     7  	"crypto/rand"
     8  	"fmt"
     9  	"io"
    10  	"net"
    11  	"os"
    12  	"os/exec"
    13  	"path/filepath"
    14  	"strings"
    15  	"syscall"
    16  )
    17  
    18  // BuildTag returns the $BUILD_TAG environment variable or `git rev-parse` output.
    19  func BuildTag() string {
    20  	var tag string
    21  	tag = os.Getenv("BUILD_TAG")
    22  	if tag == "" {
    23  		out, err := exec.Command("git", "rev-parse", "--short", "HEAD").Output()
    24  		if err != nil {
    25  			tag = "NOTAG"
    26  		}
    27  		tag = "git-" + string(out)
    28  	}
    29  	return strings.TrimSpace(tag)
    30  }
    31  
    32  // ImagePrefix returns the $IMAGE_PREFIX environment variable or `deis/`
    33  func ImagePrefix() string {
    34  	var prefix string
    35  	prefix = os.Getenv("IMAGE_PREFIX")
    36  	if prefix != "" {
    37  		return prefix
    38  	}
    39  	return "deis/"
    40  }
    41  
    42  // Chdir sets the current working directory to the relative path specified.
    43  func Chdir(app string) error {
    44  	var wd, _ = os.Getwd()
    45  	dir, _ := filepath.Abs(filepath.Join(wd, app))
    46  	err := os.Chdir(dir)
    47  	fmt.Println(dir)
    48  	return err
    49  }
    50  
    51  // CreateFile creates an empty file at the specified path.
    52  func CreateFile(path string) error {
    53  	fo, err := os.Create(path)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	defer fo.Close()
    58  	return nil
    59  }
    60  
    61  // HostAddress returns the host IP for accessing etcd and Deis services.
    62  func HostAddress() string {
    63  	IP := os.Getenv("HOST_IPADDR")
    64  	if IP == "" {
    65  		IP = "172.17.8.100"
    66  	}
    67  	return IP
    68  }
    69  
    70  // Hostname returns the hostname of the machine running the container, *not* the local machine
    71  // We infer the hostname because we don't necessarily know how to log in.
    72  func Hostname() string {
    73  	switch HostAddress() {
    74  	case "172.17.8.100":
    75  		return "deis-1"
    76  	case "172.17.8.101":
    77  		return "deis-2"
    78  	case "172.17.8.102":
    79  		return "deis-3"
    80  	case "172.21.12.100":
    81  		return "docker-registry"
    82  	default:
    83  		return "boot2docker"
    84  	}
    85  }
    86  
    87  // NewID returns the first part of a random RFC 4122 UUID
    88  // See http://play.golang.org/p/4FkNSiUDMg
    89  func NewID() string {
    90  	uuid := make([]byte, 16)
    91  	io.ReadFull(rand.Reader, uuid)
    92  	// variant bits; see section 4.1.1
    93  	uuid[8] = uuid[8]&^0xc0 | 0x80
    94  	// version 4 (pseudo-random); see section 4.1.3
    95  	uuid[6] = uuid[6]&^0xf0 | 0x40
    96  	return fmt.Sprintf("%x", uuid[0:4])
    97  }
    98  
    99  // RandomPort returns an unused TCP listen port on the host.
   100  func RandomPort() string {
   101  	l, _ := net.Listen("tcp", "127.0.0.1:0") // listen on localhost
   102  	defer l.Close()
   103  	port := l.Addr()
   104  	return strings.Split(port.String(), ":")[1]
   105  }
   106  
   107  // Rmdir removes a directory and its contents.
   108  func Rmdir(app string) error {
   109  	var wd, _ = os.Getwd()
   110  	dir, _ := filepath.Abs(filepath.Join(wd, app))
   111  	err := os.RemoveAll(dir)
   112  	fmt.Println(dir)
   113  	return err
   114  }
   115  
   116  // streamOutput from a source to a destination buffer while also printing
   117  func streamOutput(src io.Reader, dst *bytes.Buffer, out io.Writer) error {
   118  
   119  	s := bufio.NewReader(src)
   120  
   121  	for {
   122  		var line []byte
   123  		line, err := s.ReadSlice('\n')
   124  		if err == io.EOF && len(line) == 0 {
   125  			break // done
   126  		}
   127  		if err == io.EOF {
   128  			return fmt.Errorf("Improper termination: %v", line)
   129  		}
   130  		if err != nil {
   131  			return err
   132  		}
   133  
   134  		// append to the buffer
   135  		dst.Write(line)
   136  
   137  		// write to stdout/stderr also
   138  		out.Write(line)
   139  	}
   140  
   141  	return nil
   142  }
   143  
   144  // RunCommandWithStdoutStderr execs a command and returns its output.
   145  func RunCommandWithStdoutStderr(cmd *exec.Cmd) (bytes.Buffer, bytes.Buffer, error) {
   146  	var stdout, stderr bytes.Buffer
   147  	stderrPipe, err := cmd.StderrPipe()
   148  	stdoutPipe, err := cmd.StdoutPipe()
   149  
   150  	cmd.Env = os.Environ()
   151  	if err != nil {
   152  		fmt.Println("error at io pipes")
   153  	}
   154  
   155  	err = cmd.Start()
   156  	if err != nil {
   157  		fmt.Println("error at command start")
   158  	}
   159  
   160  	go func() {
   161  		streamOutput(stdoutPipe, &stdout, os.Stdout)
   162  	}()
   163  	go func() {
   164  		streamOutput(stderrPipe, &stderr, os.Stderr)
   165  	}()
   166  	err = cmd.Wait()
   167  	if err != nil {
   168  		fmt.Println("error at command wait")
   169  	}
   170  	return stdout, stderr, err
   171  }
   172  
   173  func getExitCode(err error) (int, error) {
   174  	exitCode := 0
   175  	if exiterr, ok := err.(*exec.ExitError); ok {
   176  		if procExit := exiterr.Sys().(syscall.WaitStatus); ok {
   177  			return procExit.ExitStatus(), nil
   178  		}
   179  	}
   180  	return exitCode, fmt.Errorf("failed to get exit code")
   181  }