Skip to content

Commit 85f21c1

Browse files
committed
Fix golint failures of e2e/framework/util.go - part2
This is a part of a series for fixing golint failures for util.go. - fixes golint failures from line 1395 to line 2353 at original util.go This fixes golint failures of the following file: - test/e2e/framework/util.go This changes following files because of change function name in above file. - test/e2e/apps/rc.go - test/e2e/apps/replica_set.go
1 parent b359b6b commit 85f21c1

File tree

3 files changed

+62
-39
lines changed

3 files changed

+62
-39
lines changed

test/e2e/apps/rc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func TestReplicationControllerServeImageOrFail(f *framework.Framework, test stri
164164
retryTimeout := 2 * time.Minute
165165
retryInterval := 5 * time.Second
166166
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))
167-
err = wait.Poll(retryInterval, retryTimeout, framework.PodProxyResponseChecker(f.ClientSet, f.Namespace.Name, label, name, true, pods).CheckAllResponses)
167+
err = wait.Poll(retryInterval, retryTimeout, framework.NewPodProxyResponseChecker(f.ClientSet, f.Namespace.Name, label, name, true, pods).CheckAllResponses)
168168
if err != nil {
169169
framework.Failf("Did not get expected responses within the timeout period of %.2f seconds.", retryTimeout.Seconds())
170170
}

test/e2e/apps/replica_set.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func testReplicaSetServeImageOrFail(f *framework.Framework, test string, image s
166166
retryTimeout := 2 * time.Minute
167167
retryInterval := 5 * time.Second
168168
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))
169-
err = wait.Poll(retryInterval, retryTimeout, framework.PodProxyResponseChecker(f.ClientSet, f.Namespace.Name, label, name, true, pods).CheckAllResponses)
169+
err = wait.Poll(retryInterval, retryTimeout, framework.NewPodProxyResponseChecker(f.ClientSet, f.Namespace.Name, label, name, true, pods).CheckAllResponses)
170170
if err != nil {
171171
framework.Failf("Did not get expected responses within the timeout period of %.2f seconds.", retryTimeout.Seconds())
172172
}

test/e2e/framework/util.go

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ func CheckInvariants(events []watch.Event, fns ...InvariantFunc) error {
13541354
return nil
13551355
}
13561356

1357-
// Waits default amount of time (PodStartTimeout) for the specified pod to become running.
1357+
// WaitForPodRunningInNamespace waits default amount of time (PodStartTimeout) for the specified pod to become running.
13581358
// Returns an error if timeout occurs first, or pod goes in to failed state.
13591359
func WaitForPodRunningInNamespace(c clientset.Interface, pod *v1.Pod) error {
13601360
if pod.Status.Phase == v1.PodRunning {
@@ -1363,19 +1363,20 @@ func WaitForPodRunningInNamespace(c clientset.Interface, pod *v1.Pod) error {
13631363
return WaitTimeoutForPodRunningInNamespace(c, pod.Name, pod.Namespace, PodStartTimeout)
13641364
}
13651365

1366-
// Waits default amount of time (PodStartTimeout) for the specified pod to become running.
1366+
// WaitForPodNameRunningInNamespace waits default amount of time (PodStartTimeout) for the specified pod to become running.
13671367
// Returns an error if timeout occurs first, or pod goes in to failed state.
13681368
func WaitForPodNameRunningInNamespace(c clientset.Interface, podName, namespace string) error {
13691369
return WaitTimeoutForPodRunningInNamespace(c, podName, namespace, PodStartTimeout)
13701370
}
13711371

1372-
// Waits an extended amount of time (slowPodStartTimeout) for the specified pod to become running.
1372+
// waitForPodRunningInNamespaceSlow waits an extended amount of time (slowPodStartTimeout) for the specified pod to become running.
13731373
// The resourceVersion is used when Watching object changes, it tells since when we care
13741374
// about changes to the pod. Returns an error if timeout occurs first, or pod goes in to failed state.
13751375
func waitForPodRunningInNamespaceSlow(c clientset.Interface, podName, namespace string) error {
13761376
return WaitTimeoutForPodRunningInNamespace(c, podName, namespace, slowPodStartTimeout)
13771377
}
13781378

1379+
// WaitTimeoutForPodRunningInNamespace waits the given timeout duration for the specified pod to become running.
13791380
func WaitTimeoutForPodRunningInNamespace(c clientset.Interface, podName, namespace string, timeout time.Duration) error {
13801381
return wait.PollImmediate(Poll, timeout, podRunning(c, podName, namespace))
13811382
}
@@ -1396,7 +1397,7 @@ func podRunning(c clientset.Interface, podName, namespace string) wait.Condition
13961397
}
13971398
}
13981399

1399-
// WaitTimeoutForPodEvent waits for an event to occur for a pod
1400+
// WaitTimeoutForPodEvent waits the given timeout duration for a pod event to occur.
14001401
func WaitTimeoutForPodEvent(c clientset.Interface, podName, namespace, eventSelector, msg string, timeout time.Duration) error {
14011402
return wait.PollImmediate(Poll, timeout, eventOccurred(c, podName, namespace, eventSelector, msg))
14021403
}
@@ -1417,12 +1418,13 @@ func eventOccurred(c clientset.Interface, podName, namespace, eventSelector, msg
14171418
}
14181419
}
14191420

1420-
// Waits default amount of time (DefaultPodDeletionTimeout) for the specified pod to stop running.
1421+
// WaitForPodNoLongerRunningInNamespace waits default amount of time (DefaultPodDeletionTimeout) for the specified pod to stop running.
14211422
// Returns an error if timeout occurs first.
14221423
func WaitForPodNoLongerRunningInNamespace(c clientset.Interface, podName, namespace string) error {
14231424
return WaitTimeoutForPodNoLongerRunningInNamespace(c, podName, namespace, DefaultPodDeletionTimeout)
14241425
}
14251426

1427+
// WaitTimeoutForPodNoLongerRunningInNamespace waits the given timeout duration for the specified pod to stop.
14261428
func WaitTimeoutForPodNoLongerRunningInNamespace(c clientset.Interface, podName, namespace string, timeout time.Duration) error {
14271429
return wait.PollImmediate(Poll, timeout, podCompleted(c, podName, namespace))
14281430
}
@@ -1495,9 +1497,8 @@ func waitForPodTerminatedInNamespace(c clientset.Interface, podName, reason, nam
14951497
if pod.Status.Phase == v1.PodFailed {
14961498
if pod.Status.Reason == reason { // short-circuit waitForPodCondition's loop
14971499
return true, nil
1498-
} else {
1499-
return true, fmt.Errorf("Expected pod %q in namespace %q to be terminated with reason %q, got reason: %q", podName, namespace, reason, pod.Status.Reason)
15001500
}
1501+
return true, fmt.Errorf("Expected pod %q in namespace %q to be terminated with reason %q, got reason: %q", podName, namespace, reason, pod.Status.Reason)
15011502
}
15021503
return false, nil
15031504
})
@@ -1580,6 +1581,7 @@ func WaitForRCToStabilize(c clientset.Interface, ns, name string, timeout time.D
15801581
return err
15811582
}
15821583

1584+
// WaitForPodToDisappear waits the given timeout duration for the specified pod to disappear.
15831585
func WaitForPodToDisappear(c clientset.Interface, ns, podName string, label labels.Selector, interval, timeout time.Duration) error {
15841586
return wait.PollImmediate(interval, timeout, func() (bool, error) {
15851587
Logf("Waiting for pod %s to disappear", podName)
@@ -1708,6 +1710,7 @@ func countEndpointsNum(e *v1.Endpoints) int {
17081710
return num
17091711
}
17101712

1713+
// WaitForEndpoint waits for the specified endpoint to be ready.
17111714
func WaitForEndpoint(c clientset.Interface, ns, name string) error {
17121715
for t := time.Now(); time.Since(t) < EndpointRegisterTimeout; time.Sleep(Poll) {
17131716
endpoint, err := c.CoreV1().Endpoints(ns).Get(name, metav1.GetOptions{})
@@ -1726,9 +1729,9 @@ func WaitForEndpoint(c clientset.Interface, ns, name string) error {
17261729
return fmt.Errorf("Failed to get endpoints for %s/%s", ns, name)
17271730
}
17281731

1729-
// Context for checking pods responses by issuing GETs to them (via the API
1732+
// PodProxyResponseChecker is a context for checking pods responses by issuing GETs to them (via the API
17301733
// proxy) and verifying that they answer with their own pod name.
1731-
type podProxyResponseChecker struct {
1734+
type PodProxyResponseChecker struct {
17321735
c clientset.Interface
17331736
ns string
17341737
label labels.Selector
@@ -1737,13 +1740,14 @@ type podProxyResponseChecker struct {
17371740
pods *v1.PodList
17381741
}
17391742

1740-
func PodProxyResponseChecker(c clientset.Interface, ns string, label labels.Selector, controllerName string, respondName bool, pods *v1.PodList) podProxyResponseChecker {
1741-
return podProxyResponseChecker{c, ns, label, controllerName, respondName, pods}
1743+
// NewPodProxyResponseChecker returns a context for checking pods responses.
1744+
func NewPodProxyResponseChecker(c clientset.Interface, ns string, label labels.Selector, controllerName string, respondName bool, pods *v1.PodList) PodProxyResponseChecker {
1745+
return PodProxyResponseChecker{c, ns, label, controllerName, respondName, pods}
17421746
}
17431747

17441748
// CheckAllResponses issues GETs to all pods in the context and verify they
17451749
// reply with their own pod name.
1746-
func (r podProxyResponseChecker) CheckAllResponses() (done bool, err error) {
1750+
func (r PodProxyResponseChecker) CheckAllResponses() (done bool, err error) {
17471751
successes := 0
17481752
options := metav1.ListOptions{LabelSelector: r.label.String()}
17491753
currentPods, err := r.c.CoreV1().Pods(r.ns).List(options)
@@ -1835,17 +1839,20 @@ func KubectlVersion() (*utilversion.Version, error) {
18351839
return utilversion.ParseSemantic(matches[1])
18361840
}
18371841

1842+
// PodsResponding waits for the pods to response.
18381843
func PodsResponding(c clientset.Interface, ns, name string, wantName bool, pods *v1.PodList) error {
18391844
By("trying to dial each unique pod")
18401845
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))
1841-
return wait.PollImmediate(Poll, podRespondingTimeout, PodProxyResponseChecker(c, ns, label, name, wantName, pods).CheckAllResponses)
1846+
return wait.PollImmediate(Poll, podRespondingTimeout, NewPodProxyResponseChecker(c, ns, label, name, wantName, pods).CheckAllResponses)
18421847
}
18431848

1849+
// PodsCreated returns a pod list matched by the given name.
18441850
func PodsCreated(c clientset.Interface, ns, name string, replicas int32) (*v1.PodList, error) {
18451851
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))
18461852
return PodsCreatedByLabel(c, ns, name, replicas, label)
18471853
}
18481854

1855+
// PodsCreatedByLabel returns a created pod list matched by the given label.
18491856
func PodsCreatedByLabel(c clientset.Interface, ns, name string, replicas int32, label labels.Selector) (*v1.PodList, error) {
18501857
timeout := 2 * time.Minute
18511858
for start := time.Now(); time.Since(start) < timeout; time.Sleep(5 * time.Second) {
@@ -1879,16 +1886,16 @@ func podsRunning(c clientset.Interface, pods *v1.PodList) []error {
18791886
// are running so non-running pods cause a timeout for this test.
18801887
By("ensuring each pod is running")
18811888
e := []error{}
1882-
error_chan := make(chan error)
1889+
errorChan := make(chan error)
18831890

18841891
for _, pod := range pods.Items {
18851892
go func(p v1.Pod) {
1886-
error_chan <- WaitForPodRunningInNamespace(c, &p)
1893+
errorChan <- WaitForPodRunningInNamespace(c, &p)
18871894
}(pod)
18881895
}
18891896

18901897
for range pods.Items {
1891-
err := <-error_chan
1898+
err := <-errorChan
18921899
if err != nil {
18931900
e = append(e, err)
18941901
}
@@ -1897,10 +1904,12 @@ func podsRunning(c clientset.Interface, pods *v1.PodList) []error {
18971904
return e
18981905
}
18991906

1907+
// VerifyPods checks if the specified pod is responding.
19001908
func VerifyPods(c clientset.Interface, ns, name string, wantName bool, replicas int32) error {
19011909
return podRunningMaybeResponding(c, ns, name, wantName, replicas, true)
19021910
}
19031911

1912+
// VerifyPodsRunning checks if the specified pod is running.
19041913
func VerifyPodsRunning(c clientset.Interface, ns, name string, wantName bool, replicas int32) error {
19051914
return podRunningMaybeResponding(c, ns, name, wantName, replicas, false)
19061915
}
@@ -1923,6 +1932,7 @@ func podRunningMaybeResponding(c clientset.Interface, ns, name string, wantName
19231932
return nil
19241933
}
19251934

1935+
// ServiceResponding waits for the service to be responding.
19261936
func ServiceResponding(c clientset.Interface, ns, name string) error {
19271937
By(fmt.Sprintf("trying to dial the service %s.%s via the proxy", ns, name))
19281938

@@ -1959,6 +1969,7 @@ func ServiceResponding(c clientset.Interface, ns, name string) error {
19591969
})
19601970
}
19611971

1972+
// RestclientConfig returns a config holds the information needed to build connection to kubernetes clusters.
19621973
func RestclientConfig(kubeContext string) (*clientcmdapi.Config, error) {
19631974
Logf(">>> kubeConfig: %s", TestContext.KubeConfig)
19641975
if TestContext.KubeConfig == "" {
@@ -1975,8 +1986,10 @@ func RestclientConfig(kubeContext string) (*clientcmdapi.Config, error) {
19751986
return c, nil
19761987
}
19771988

1989+
// ClientConfigGetter is a func that returns getter to return a config.
19781990
type ClientConfigGetter func() (*restclient.Config, error)
19791991

1992+
// LoadConfig returns a config for a rest client.
19801993
func LoadConfig() (*restclient.Config, error) {
19811994
if TestContext.NodeE2E {
19821995
// This is a node e2e test, apply the node e2e configuration
@@ -1986,14 +1999,14 @@ func LoadConfig() (*restclient.Config, error) {
19861999
if err != nil {
19872000
if TestContext.KubeConfig == "" {
19882001
return restclient.InClusterConfig()
1989-
} else {
1990-
return nil, err
19912002
}
2003+
return nil, err
19922004
}
19932005

19942006
return clientcmd.NewDefaultClientConfig(*c, &clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: TestContext.Host}}).ClientConfig()
19952007
}
19962008

2009+
// LoadClientset returns clientset for connecting to kubernetes clusters.
19972010
func LoadClientset() (*clientset.Clientset, error) {
19982011
config, err := LoadConfig()
19992012
if err != nil {
@@ -2002,7 +2015,7 @@ func LoadClientset() (*clientset.Clientset, error) {
20022015
return clientset.NewForConfig(config)
20032016
}
20042017

2005-
// randomSuffix provides a random string to append to pods,services,rcs.
2018+
// RandomSuffix provides a random string to append to pods,services,rcs.
20062019
// TODO: Allow service names to have the same form as names
20072020
// for pods and replication controllers so we don't
20082021
// need to use such a function and can instead
@@ -2012,6 +2025,7 @@ func RandomSuffix() string {
20122025
return strconv.Itoa(r.Int() % 10000)
20132026
}
20142027

2028+
// ExpectNoError checks if "err" is set, and if so, fails assertion while logging the error.
20152029
func ExpectNoError(err error, explain ...interface{}) {
20162030
ExpectNoErrorWithOffset(1, err, explain...)
20172031
}
@@ -2025,6 +2039,7 @@ func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{}) {
20252039
ExpectWithOffset(1+offset, err).NotTo(HaveOccurred(), explain...)
20262040
}
20272041

2042+
// ExpectNoErrorWithRetries checks if an error occurs with the given retry count.
20282043
func ExpectNoErrorWithRetries(fn func() error, maxRetries int, explain ...interface{}) {
20292044
var err error
20302045
for i := 0; i < maxRetries; i++ {
@@ -2037,7 +2052,7 @@ func ExpectNoErrorWithRetries(fn func() error, maxRetries int, explain ...interf
20372052
ExpectWithOffset(1, err).NotTo(HaveOccurred(), explain...)
20382053
}
20392054

2040-
// Stops everything from filePath from namespace ns and checks if everything matching selectors from the given namespace is correctly stopped.
2055+
// Cleanup stops everything from filePath from namespace ns and checks if everything matching selectors from the given namespace is correctly stopped.
20412056
func Cleanup(filePath, ns string, selectors ...string) {
20422057
By("using delete to clean up resources")
20432058
var nsArg string
@@ -2048,7 +2063,7 @@ func Cleanup(filePath, ns string, selectors ...string) {
20482063
AssertCleanup(ns, selectors...)
20492064
}
20502065

2051-
// Asserts that cleanup of a namespace wrt selectors occurred.
2066+
// AssertCleanup asserts that cleanup of a namespace wrt selectors occurred.
20522067
func AssertCleanup(ns string, selectors ...string) {
20532068
var nsArg string
20542069
if ns != "" {
@@ -2112,40 +2127,46 @@ func KubectlCmd(args ...string) *exec.Cmd {
21122127
return cmd
21132128
}
21142129

2115-
// kubectlBuilder is used to build, customize and execute a kubectl Command.
2130+
// KubectlBuilder is used to build, customize and execute a kubectl Command.
21162131
// Add more functions to customize the builder as needed.
2117-
type kubectlBuilder struct {
2132+
type KubectlBuilder struct {
21182133
cmd *exec.Cmd
21192134
timeout <-chan time.Time
21202135
}
21212136

2122-
func NewKubectlCommand(args ...string) *kubectlBuilder {
2123-
b := new(kubectlBuilder)
2137+
// NewKubectlCommand returns a KubectlBuilder for running kubectl.
2138+
func NewKubectlCommand(args ...string) *KubectlBuilder {
2139+
b := new(KubectlBuilder)
21242140
b.cmd = KubectlCmd(args...)
21252141
return b
21262142
}
21272143

2128-
func (b *kubectlBuilder) WithEnv(env []string) *kubectlBuilder {
2144+
// WithEnv sets the given environment and returns itself.
2145+
func (b *KubectlBuilder) WithEnv(env []string) *KubectlBuilder {
21292146
b.cmd.Env = env
21302147
return b
21312148
}
21322149

2133-
func (b *kubectlBuilder) WithTimeout(t <-chan time.Time) *kubectlBuilder {
2150+
// WithTimeout sets the given timeout and returns itself.
2151+
func (b *KubectlBuilder) WithTimeout(t <-chan time.Time) *KubectlBuilder {
21342152
b.timeout = t
21352153
return b
21362154
}
21372155

2138-
func (b kubectlBuilder) WithStdinData(data string) *kubectlBuilder {
2156+
// WithStdinData sets the given data to stdin and returns itself.
2157+
func (b KubectlBuilder) WithStdinData(data string) *KubectlBuilder {
21392158
b.cmd.Stdin = strings.NewReader(data)
21402159
return &b
21412160
}
21422161

2143-
func (b kubectlBuilder) WithStdinReader(reader io.Reader) *kubectlBuilder {
2162+
// WithStdinReader sets the given reader and returns itself.
2163+
func (b KubectlBuilder) WithStdinReader(reader io.Reader) *KubectlBuilder {
21442164
b.cmd.Stdin = reader
21452165
return &b
21462166
}
21472167

2148-
func (b kubectlBuilder) ExecOrDie() string {
2168+
// ExecOrDie runs the kubectl executable or dies if error occurs.
2169+
func (b KubectlBuilder) ExecOrDie() string {
21492170
str, err := b.Exec()
21502171
// In case of i/o timeout error, try talking to the apiserver again after 2s before dying.
21512172
// Note that we're still dying after retrying so that we can get visibility to triage it further.
@@ -2174,14 +2195,15 @@ func isTimeout(err error) bool {
21742195
return false
21752196
}
21762197

2177-
func (b kubectlBuilder) Exec() (string, error) {
2198+
// Exec runs the kubectl executable.
2199+
func (b KubectlBuilder) Exec() (string, error) {
21782200
var stdout, stderr bytes.Buffer
21792201
cmd := b.cmd
21802202
cmd.Stdout, cmd.Stderr = &stdout, &stderr
21812203

21822204
Logf("Running '%s %s'", cmd.Path, strings.Join(cmd.Args[1:], " ")) // skip arg[0] as it is printed separately
21832205
if err := cmd.Start(); err != nil {
2184-
return "", fmt.Errorf("error starting %v:\nCommand stdout:\n%v\nstderr:\n%v\nerror:\n%v\n", cmd, cmd.Stdout, cmd.Stderr, err)
2206+
return "", fmt.Errorf("error starting %v:\nCommand stdout:\n%v\nstderr:\n%v\nerror:\n%v", cmd, cmd.Stdout, cmd.Stderr, err)
21852207
}
21862208
errCh := make(chan error, 1)
21872209
go func() {
@@ -2190,19 +2212,19 @@ func (b kubectlBuilder) Exec() (string, error) {
21902212
select {
21912213
case err := <-errCh:
21922214
if err != nil {
2193-
var rc int = 127
2215+
var rc = 127
21942216
if ee, ok := err.(*exec.ExitError); ok {
21952217
rc = int(ee.Sys().(syscall.WaitStatus).ExitStatus())
21962218
Logf("rc: %d", rc)
21972219
}
21982220
return "", uexec.CodeExitError{
2199-
Err: fmt.Errorf("error running %v:\nCommand stdout:\n%v\nstderr:\n%v\nerror:\n%v\n", cmd, cmd.Stdout, cmd.Stderr, err),
2221+
Err: fmt.Errorf("error running %v:\nCommand stdout:\n%v\nstderr:\n%v\nerror:\n%v", cmd, cmd.Stdout, cmd.Stderr, err),
22002222
Code: rc,
22012223
}
22022224
}
22032225
case <-b.timeout:
22042226
b.cmd.Process.Kill()
2205-
return "", fmt.Errorf("timed out waiting for command %v:\nCommand stdout:\n%v\nstderr:\n%v\n", cmd, cmd.Stdout, cmd.Stderr)
2227+
return "", fmt.Errorf("timed out waiting for command %v:\nCommand stdout:\n%v\nstderr:\n%v", cmd, cmd.Stdout, cmd.Stderr)
22062228
}
22072229
Logf("stderr: %q", stderr.String())
22082230
Logf("stdout: %q", stdout.String())
@@ -2242,13 +2264,14 @@ func RunKubemciWithKubeconfig(args ...string) (string, error) {
22422264
func RunKubemciCmd(args ...string) (string, error) {
22432265
// kubemci is assumed to be in PATH.
22442266
kubemci := "kubemci"
2245-
b := new(kubectlBuilder)
2267+
b := new(KubectlBuilder)
22462268
args = append(args, "--gcp-project="+TestContext.CloudConfig.ProjectID)
22472269

22482270
b.cmd = exec.Command(kubemci, args...)
22492271
return b.Exec()
22502272
}
22512273

2274+
// StartCmdAndStreamOutput returns stdout and stderr after starting the given cmd.
22522275
func StartCmdAndStreamOutput(cmd *exec.Cmd) (stdout, stderr io.ReadCloser, err error) {
22532276
stdout, err = cmd.StdoutPipe()
22542277
if err != nil {
@@ -2263,7 +2286,7 @@ func StartCmdAndStreamOutput(cmd *exec.Cmd) (stdout, stderr io.ReadCloser, err e
22632286
return
22642287
}
22652288

2266-
// Rough equivalent of ctrl+c for cleaning up processes. Intended to be run in defer.
2289+
// TryKill is rough equivalent of ctrl+c for cleaning up processes. Intended to be run in defer.
22672290
func TryKill(cmd *exec.Cmd) {
22682291
if err := cmd.Process.Kill(); err != nil {
22692292
Logf("ERROR failed to kill command %v! The process may leak", cmd)

0 commit comments

Comments
 (0)