chore: cleanup

Signed-off-by: Keith Zantow <kzantow@gmail.com>
This commit is contained in:
Keith Zantow 2023-10-13 09:13:37 -04:00
parent 6c3755fbbe
commit 42aca2d7ad
No known key found for this signature in database
GPG Key ID: 735988DA57708682

View File

@ -4,7 +4,6 @@ import (
"bytes"
"flag"
"fmt"
"math"
"os"
"os/exec"
"path"
@ -31,39 +30,6 @@ func logOutputOnFailure(t testing.TB, cmd *exec.Cmd, stdout, stderr string) {
}
}
func setupPKI(t *testing.T, pw string) func() {
err := os.Setenv("COSIGN_PASSWORD", pw)
if err != nil {
t.Fatal(err)
}
cosignPath := filepath.Join(repoRoot(t), ".tmp/cosign")
cmd := exec.Command(cosignPath, "generate-key-pair")
stdout, stderr, _ := runCommand(cmd, nil)
if cmd.ProcessState.ExitCode() != 0 {
t.Log("STDOUT", stdout)
t.Log("STDERR", stderr)
t.Fatalf("could not generate keypair")
}
return func() {
err := os.Unsetenv("COSIGN_PASSWORD")
if err != nil {
t.Fatal(err)
}
err = os.Remove("cosign.key")
if err != nil {
t.Fatalf("could not cleanup cosign.key")
}
err = os.Remove("cosign.pub")
if err != nil {
t.Fatalf("could not cleanup cosign.key")
}
}
}
func getFixtureImage(t testing.TB, fixtureImageName string) string {
t.Logf("obtaining fixture image for %s", fixtureImageName)
imagetest.GetFixtureImage(t, "docker-archive", fixtureImageName)
@ -81,7 +47,7 @@ func pullDockerImage(t testing.TB, image string) {
}
// docker run -v $(pwd)/sbom:/sbom cyclonedx/cyclonedx-cli:latest validate --input-format json --input-version v1_4 --input-file /sbom
func runCycloneDXInDocker(t testing.TB, env map[string]string, image string, f *os.File, args ...string) (*exec.Cmd, string, string) {
func runCycloneDXInDocker(_ testing.TB, env map[string]string, image string, f *os.File, args ...string) (*exec.Cmd, string, string) {
allArgs := append(
[]string{
"run",
@ -222,25 +188,6 @@ func runCommandObj(t testing.TB, cmd *exec.Cmd, env map[string]string, expectErr
return stdout, stderr
}
func runCosign(t testing.TB, env map[string]string, args ...string) (*exec.Cmd, string, string) {
cmd := getCommand(t, ".tmp/cosign", args...)
if env == nil {
env = make(map[string]string)
}
stdout, stderr, err := runCommand(cmd, env)
if err != nil {
t.Errorf("error running cosign: %+v", err)
}
return cmd, stdout, stderr
}
func getCommand(t testing.TB, location string, args ...string) *exec.Cmd {
return exec.Command(filepath.Join(repoRoot(t), location), args...)
}
func runCommand(cmd *exec.Cmd, env map[string]string) (string, string, error) {
if env != nil {
cmd.Env = append(os.Environ(), envMapToSlice(env)...)
@ -396,40 +343,3 @@ func repoRoot(t testing.TB) string {
}
return absRepoRoot
}
func testRetryIntervals(done <-chan struct{}) <-chan time.Duration {
return exponentialBackoffDurations(250*time.Millisecond, 4*time.Second, 2, done)
}
func exponentialBackoffDurations(minDuration, maxDuration time.Duration, step float64, done <-chan struct{}) <-chan time.Duration {
sleepDurations := make(chan time.Duration)
go func() {
defer close(sleepDurations)
retryLoop:
for attempt := 0; ; attempt++ {
duration := exponentialBackoffDuration(minDuration, maxDuration, step, attempt)
select {
case sleepDurations <- duration:
break
case <-done:
break retryLoop
}
if duration == maxDuration {
break
}
}
}()
return sleepDurations
}
func exponentialBackoffDuration(minDuration, maxDuration time.Duration, step float64, attempt int) time.Duration {
duration := time.Duration(float64(minDuration) * math.Pow(step, float64(attempt)))
if duration < minDuration {
return minDuration
} else if duration > maxDuration {
return maxDuration
}
return duration
}