forked from sigstore/rekor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
151 lines (132 loc) · 3.47 KB
/
util.go
File metadata and controls
151 lines (132 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// Copyright 2021 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build e2e
// +build e2e
package e2e
import (
"encoding/base64"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"strings"
"testing"
"time"
"github.com/sigstore/rekor/pkg/generated/models"
)
const (
cli = "../rekor-cli"
server = "../rekor-server"
nodeDataDir = "node"
)
func outputContains(t *testing.T, output, sub string) {
t.Helper()
if !strings.Contains(output, sub) {
t.Errorf("Expected [%s] in response, got %s", sub, output)
}
}
func run(t *testing.T, stdin, cmd string, arg ...string) string {
t.Helper()
c := exec.Command(cmd, arg...)
if stdin != "" {
c.Stdin = strings.NewReader(stdin)
}
if os.Getenv("REKORTMPDIR") != "" {
// ensure that we use a clean state.json file for each run
c.Env = append(c.Env, "HOME="+os.Getenv("REKORTMPDIR"))
}
b, err := c.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}
return string(b)
}
func runCli(t *testing.T, arg ...string) string {
t.Helper()
arg = append(arg, rekorServerFlag())
// use a blank config file to ensure no collision
if os.Getenv("REKORTMPDIR") != "" {
arg = append(arg, "--config="+os.Getenv("REKORTMPDIR")+".rekor.yaml")
}
return run(t, "", cli, arg...)
}
func runCliErr(t *testing.T, arg ...string) string {
t.Helper()
arg = append(arg, rekorServerFlag())
// use a blank config file to ensure no collision
if os.Getenv("REKORTMPDIR") != "" {
arg = append(arg, "--config="+os.Getenv("REKORTMPDIR")+".rekor.yaml")
}
cmd := exec.Command(cli, arg...)
b, err := cmd.CombinedOutput()
if err == nil {
t.Log(string(b))
t.Fatalf("expected error, got %s", string(b))
}
return string(b)
}
func rekorServerFlag() string {
return fmt.Sprintf("--rekor_server=%s", rekorServer())
}
func rekorServer() string {
if s := os.Getenv("REKOR_SERVER"); s != "" {
return s
}
return "http://localhost:3000"
}
func readFile(t *testing.T, p string) string {
b, err := ioutil.ReadFile(p)
if err != nil {
t.Fatal(err)
}
return strings.TrimSpace(string(b))
}
func randomData(t *testing.T, n int) []byte {
t.Helper()
rand.Seed(time.Now().UnixNano())
data := make([]byte, n)
if _, err := rand.Read(data[:]); err != nil {
t.Fatal(err)
}
return data
}
func createArtifact(t *testing.T, artifactPath string) string {
t.Helper()
// First let's generate some random data so we don't have to worry about dupes.
data := randomData(t, 100)
artifact := base64.StdEncoding.EncodeToString(data[:])
// Write this to a file
write(t, artifact, artifactPath)
return artifact
}
func extractLogEntry(t *testing.T, le models.LogEntry) models.LogEntryAnon {
t.Helper()
if len(le) != 1 {
t.Fatal("expected length to be 1, is actually", len(le))
}
for _, v := range le {
return v
}
// this should never happen
return models.LogEntryAnon{}
}
func write(t *testing.T, data string, path string) {
t.Helper()
if err := ioutil.WriteFile(path, []byte(data), 0644); err != nil {
t.Fatal(err)
}
}