...

Source file src/sigs.k8s.io/cli-utils/test/e2e/e2eutil/rest.go

Documentation: sigs.k8s.io/cli-utils/test/e2e/e2eutil

     1  // Copyright 2022 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package e2eutil
     5  
     6  import (
     7  	"bytes"
     8  	"context"
     9  	"fmt"
    10  	"os"
    11  	"os/exec"
    12  	"path/filepath"
    13  	"runtime"
    14  	"strings"
    15  	"time"
    16  
    17  	"github.com/onsi/gomega"
    18  )
    19  
    20  const unknown = "unknown"
    21  
    22  // UserAgent returns the a User-Agent for use with HTTP clients.
    23  // The string corresponds to the current version of the binary being executed,
    24  // using metadata from git and go.
    25  func UserAgent(suffix string) string {
    26  	return fmt.Sprintf("%s/%s (%s/%s) cli-utils/%s/%s",
    27  		adjustCommand(os.Args[0]),
    28  		adjustVersion(gitVersion()),
    29  		runtime.GOOS,
    30  		runtime.GOARCH,
    31  		adjustCommit(gitCommit()),
    32  		suffix)
    33  }
    34  
    35  // gitVersion returns the output from `git describe`
    36  func gitVersion() string {
    37  	ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
    38  	defer cancel()
    39  
    40  	cmd := exec.CommandContext(ctx, "git", "describe")
    41  
    42  	// Ginkgo sets the working directory to the current test dir
    43  	cwd, err := os.Getwd()
    44  	gomega.Expect(err).ToNot(gomega.HaveOccurred())
    45  	cmd.Dir = cwd
    46  
    47  	var outBuf bytes.Buffer
    48  	var errBuf bytes.Buffer
    49  	cmd.Stdout = &outBuf
    50  	cmd.Stderr = &errBuf
    51  
    52  	err = cmd.Run()
    53  	gomega.Expect(err).ToNot(gomega.HaveOccurred(), "STDERR:\n%v\nSTDOUT:\n%v", errBuf, outBuf)
    54  
    55  	return strings.TrimSpace(outBuf.String())
    56  }
    57  
    58  // gitCommit returns the output from `git rev-parse HEAD`
    59  func gitCommit() string {
    60  	ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
    61  	defer cancel()
    62  
    63  	cmd := exec.CommandContext(ctx, "git", "rev-parse", "HEAD")
    64  
    65  	// Ginkgo sets the working directory to the current test dir
    66  	cwd, err := os.Getwd()
    67  	gomega.Expect(err).ToNot(gomega.HaveOccurred())
    68  	cmd.Dir = cwd
    69  
    70  	var outBuf bytes.Buffer
    71  	var errBuf bytes.Buffer
    72  	cmd.Stdout = &outBuf
    73  	cmd.Stderr = &errBuf
    74  
    75  	err = cmd.Run()
    76  	gomega.Expect(err).ToNot(gomega.HaveOccurred(), "STDERR: %s", errBuf.String())
    77  
    78  	return strings.TrimSpace(outBuf.String())
    79  }
    80  
    81  // adjustCommand returns the last component of the
    82  // OS-specific command path for use in User-Agent.
    83  func adjustCommand(p string) string {
    84  	// Unlikely, but better than returning "".
    85  	if len(p) == 0 {
    86  		return unknown
    87  	}
    88  	return filepath.Base(p)
    89  }
    90  
    91  // adjustVersion strips "alpha", "beta", etc. from version in form
    92  // major.minor.patch-[alpha|beta|etc].
    93  func adjustVersion(v string) string {
    94  	if len(v) == 0 {
    95  		return unknown
    96  	}
    97  	seg := strings.SplitN(v, "-", 2)
    98  	return seg[0]
    99  }
   100  
   101  // adjustCommit returns sufficient significant figures of the commit's git hash.
   102  func adjustCommit(c string) string {
   103  	if len(c) == 0 {
   104  		return unknown
   105  	}
   106  	if len(c) > 7 {
   107  		return c[:7]
   108  	}
   109  	return c
   110  }
   111  

View as plain text