-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e.go
170 lines (154 loc) · 4.71 KB
/
e2e.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Copyright 2014 The Kubernetes 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.
*/
// User-interface for test-infra/kubetest/e2e.go
// Equivalent to go get -u k8s.io/test-infra/kubetest && kubetest "${@}"
package main
import (
"flag"
"fmt"
"go/build"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"time"
)
type flags struct {
get bool
old time.Duration
args []string
}
const (
getDefault = true
oldDefault = 24 * time.Hour
)
func parse(args []string) (flags, error) {
fs := flag.NewFlagSet(args[0], flag.ContinueOnError)
get := fs.Bool("get", getDefault, "go get -u kubetest if old or not installed")
old := fs.Duration("old", oldDefault, "Consider kubetest old if it exceeds this")
verbose := fs.Bool("v", true, "this flag is translated to kubetest's --verbose-commands")
var a []string
if err := fs.Parse(args[1:]); err == flag.ErrHelp {
os.Stderr.WriteString(" -- kubetestArgs\n")
os.Stderr.WriteString(" All flags after -- are passed to the kubetest program\n")
return flags{}, err
} else if err != nil {
log.Print("NOTICE: go run hack/e2e.go is now a shim for test-infra/kubetest")
log.Printf(" Usage: go run hack/e2e.go [--get=%v] [--old=%v] -- [KUBETEST_ARGS]", getDefault, oldDefault)
log.Print(" The separator is required to use --get or --old flags")
log.Print(" The -- flag separator also suppresses this message")
a = args[len(args)-fs.NArg()-1:]
} else {
a = append(a, fmt.Sprintf("--verbose-commands=%t", *verbose))
a = append(a, fs.Args()...)
}
return flags{*get, *old, a}, nil
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
f, err := parse(os.Args)
if err != nil {
os.Exit(2)
}
t := newTester()
k, err := t.getKubetest(f.get, f.old)
if err != nil {
log.Fatalf("err: %v", err)
}
log.Printf("Calling kubetest %v...", strings.Join(f.args, " "))
if err = t.wait(k, f.args...); err != nil {
log.Fatalf("err: %v", err)
}
log.Print("Done")
}
func wait(cmd string, args ...string) error {
sigChannel := make(chan os.Signal, 1)
signal.Notify(sigChannel, os.Interrupt)
c := exec.Command(cmd, args...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Start(); err != nil {
return err
}
go func() {
sig := <-sigChannel
if err := c.Process.Signal(sig); err != nil {
log.Fatalf("could not send %s signal %s: %v", cmd, sig, err)
}
}()
return c.Wait()
}
// Struct that allows unit tests to override functionality.
type tester struct {
// os.Stat
stat func(string) (os.FileInfo, error)
// exec.LookPath
lookPath func(string) (string, error)
// build.Default.GOPATH
goPath string
wait func(string, ...string) error
}
func newTester() tester {
return tester{os.Stat, exec.LookPath, build.Default.GOPATH, wait}
}
// Try to find kubetest, either GOPATH/bin/kubetest or PATH
func (t tester) lookKubetest() (string, error) {
// Check for kubetest in GOPATH/bin
if t.goPath != "" {
p := filepath.Join(t.goPath, "bin", "kubetest")
_, err := t.stat(p)
if err == nil {
return p, nil
}
}
// Check for kubetest in PATH
p, err := t.lookPath("kubetest")
return p, err
}
// Upgrade if kubetest does not exist or has not been updated today
func (t tester) getKubetest(get bool, old time.Duration) (string, error) {
// Find kubetest installation
p, err := t.lookKubetest()
if err == nil && !get {
return p, nil // Installed, Skip update
}
if err == nil {
// Installed recently?
if s, err := t.stat(p); err != nil {
return p, err // Cannot stat
} else if time.Since(s.ModTime()) <= old {
return p, nil // Recently updated
} else if t.goPath == "" {
log.Print("Skipping kubetest upgrade because $GOPATH is empty")
return p, nil
}
log.Printf("The kubetest binary is older than %s.", old)
}
if t.goPath == "" {
return "", fmt.Errorf("Cannot install kubetest until $GOPATH is set")
}
log.Print("Updating kubetest binary...")
cmd := []string{"go", "get", "-u", "k8s.io/test-infra/kubetest"}
if err = t.wait(cmd[0], cmd[1:]...); err != nil {
return "", fmt.Errorf("%s: %v", strings.Join(cmd, " "), err) // Could not upgrade
}
if p, err = t.lookKubetest(); err != nil {
return "", err // Cannot find kubetest
} else if err = t.wait("touch", p); err != nil {
return "", err // Could not touch
} else {
return p, nil // Updated modtime
}
}