forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestInfo.go
146 lines (131 loc) · 3.37 KB
/
testInfo.go
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
// Copyright 2017 Istio 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.
package framework
import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/google/uuid"
"istio.io/istio/pkg/log"
"istio.io/istio/tests/util"
)
var (
testLogsPath = flag.String("test_logs_path", "", "Local path to store logs in")
)
const (
tmpPrefix = "istio.e2e."
idMaxLength = 36
)
// TestInfo gathers Test Information
type testInfo struct {
RunID string
TestID string
TempDir string
}
// Test information
type testStatus struct {
TestID string `json:"test_id"`
Status int `json:"status"`
RunID string `json:"run_id"`
Date time.Time `json:"date"`
}
// NewTestInfo creates a TestInfo given a test id.
func newTestInfo(testID string) (*testInfo, error) {
id, err := generateRunID(testID)
if err != nil {
return nil, err
}
var tmpDir string
// testLogsPath will be used when called by Prow.
// Bootstrap already gather stdout and stdin so we don't need to keep the logs from glog.
if *testLogsPath != "" {
tmpDir = path.Join(*testLogsPath, id)
if err = os.MkdirAll(tmpDir, 0777); err != nil {
return nil, err
}
} else {
tmpDir, err = ioutil.TempDir(os.TempDir(), tmpPrefix)
if err != nil {
return nil, err
}
}
log.Infof("Using temp dir %s", tmpDir)
if err != nil {
return nil, errors.New("could not create a temporary dir")
}
// Need to setup logging here
return &testInfo{
TestID: testID,
RunID: id,
TempDir: tmpDir,
}, nil
}
func (t testInfo) Setup() error {
return nil
}
// Update sets the test status.
func (t testInfo) Update(r int) error {
return t.createStatusFile(r)
}
func (t testInfo) FetchAndSaveClusterLogs(namespace string, kubeconfig string) error {
return util.FetchAndSaveClusterLogs(namespace, t.TempDir, kubeconfig)
}
func (t testInfo) createStatusFile(r int) error {
log.Info("Creating status file")
ts := testStatus{
Status: r,
Date: time.Now(),
TestID: t.TestID,
RunID: t.RunID,
}
fp := filepath.Join(t.TempDir, fmt.Sprintf("%s.json", t.TestID))
f, err := os.Create(fp)
if err != nil {
log.Errorf("Could not create %s. Error %s", fp, err)
return err
}
w := bufio.NewWriter(f)
e := json.NewEncoder(w)
e.SetIndent("", " ")
if err = e.Encode(ts); err == nil {
if err = w.Flush(); err == nil {
if err = f.Close(); err == nil {
log.Infof("Created Status file %s", fp)
}
}
}
return err
}
func (t testInfo) Teardown() error {
return nil
}
func generateRunID(t string) (string, error) {
u := uuid.New().String()
u = strings.Replace(u, "-", "", -1)
t = strings.Replace(t, "_", "-", -1)
// We want at least 6 characters of uuid padding
padding := idMaxLength - len(t)
if padding < 6 {
return "", fmt.Errorf("test name should be less that %d characters", idMaxLength-6)
}
return fmt.Sprintf("%s-%s", t, u[0:padding]), nil
}