Skip to content

Commit

Permalink
add request-timeout options for kubectl
Browse files Browse the repository at this point in the history
  • Loading branch information
james03160927 committed Jan 1, 2025
1 parent a746562 commit 125eb01
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
3 changes: 3 additions & 0 deletions modules/k8s/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func RunKubectlAndGetOutputE(t testing.TestingT, options *KubectlOptions, args .
if options.Namespace != "" {
cmdArgs = append(cmdArgs, "--namespace", options.Namespace)
}
if options.RequestTimeout > 0 {
cmdArgs = append(cmdArgs, "--request-timeout", options.RequestTimeout.String())
}
cmdArgs = append(cmdArgs, args...)
command := shell.Command{
Command: "kubectl",
Expand Down
17 changes: 10 additions & 7 deletions modules/k8s/kubectl_options.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package k8s

import (
"time"

"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/testing"
"k8s.io/client-go/rest"
)

// KubectlOptions represents common options necessary to specify for all Kubectl calls
type KubectlOptions struct {
ContextName string
ConfigPath string
Namespace string
Env map[string]string
InClusterAuth bool
RestConfig *rest.Config
Logger *logger.Logger
ContextName string
ConfigPath string
Namespace string
Env map[string]string
InClusterAuth bool
RestConfig *rest.Config
Logger *logger.Logger
RequestTimeout time.Duration
}

// NewKubectlOptions will return a pointer to new instance of KubectlOptions with the configured options
Expand Down
63 changes: 63 additions & 0 deletions modules/k8s/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ package k8s

import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/gruntwork-io/terratest/modules/random"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -26,3 +30,62 @@ func TestRunKubectlAndGetOutputReturnsOutput(t *testing.T) {
require.NoError(t, err)
require.Equal(t, output, "yes")
}

func TestKubectlRequestTimeout(t *testing.T) {
t.Parallel()

var parsedTimeout time.Duration
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
parsedTimeout, _ = time.ParseDuration(r.URL.Query().Get("timeout"))
select {
case <-time.After(3 * time.Second):
case <-r.Context().Done():
}
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("dummy-error"))
}))

config := fmt.Sprintf(`
apiVersion: v1
kind: Config
clusters:
- name: dummy-cluster
cluster:
server: %s
users:
- name: dummy-user
user:
token: dummy-token
contexts:
- name: dummy-context
context:
cluster: dummy-cluster
user: dummy-user
current-context: dummy-context
`, server.URL)

t.Run("WithoutTimeout", func(t *testing.T) {
options := &KubectlOptions{
ContextName: "dummy-context",
ConfigPath: StoreConfigToTempFile(t, config),
}
_, err := RunKubectlAndGetOutputE(t, options, "get", "pods")
require.Error(t, err)
assert.Contains(t, err.Error(), "dummy-error")
assert.NotContains(t, err.Error(), "Client.Timeout exceeded while awaiting headers")
})

t.Run("WithTimeout", func(t *testing.T) {
options := &KubectlOptions{
ContextName: "dummy-context",
ConfigPath: StoreConfigToTempFile(t, config),
RequestTimeout: time.Second,
}
_, err := RunKubectlAndGetOutputE(t, options, "get", "pods")
require.Error(t, err)
assert.Equal(t, options.RequestTimeout, parsedTimeout)
assert.NotContains(t, err.Error(), "dummy-error")
assert.Contains(t, err.Error(), "Client.Timeout exceeded while awaiting headers")
})

}

0 comments on commit 125eb01

Please sign in to comment.