From 4a222391be47364c09aec02f311e2534cc823e2b Mon Sep 17 00:00:00 2001 From: Tomas Tormo Date: Wed, 24 May 2023 09:54:05 +0200 Subject: [PATCH 1/2] Add support for Events in k8s module --- modules/k8s/event.go | 34 +++++++++++++++++++++++++++++++ modules/k8s/event_test.go | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 modules/k8s/event.go create mode 100644 modules/k8s/event_test.go diff --git a/modules/k8s/event.go b/modules/k8s/event.go new file mode 100644 index 000000000..76c32e747 --- /dev/null +++ b/modules/k8s/event.go @@ -0,0 +1,34 @@ +package k8s + +import ( + "context" + + "github.com/stretchr/testify/require" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/gruntwork-io/terratest/modules/testing" +) + +// ListEvents will retrieve the Events in the given namespace that match the given filters and return them. This will fail the +// test if there is an error. +func ListEvents(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) []corev1.Event { + events, err := ListEventsE(t, options, filters) + require.NoError(t, err) + return events +} + +// ListEventsE will retrieve the Events that match the given filters and return them. +func ListEventsE(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) ([]corev1.Event, error) { + clientset, err := GetKubernetesClientFromOptionsE(t, options) + if err != nil { + return nil, err + } + + resp, err := clientset.CoreV1().Events(options.Namespace).List(context.Background(), filters) + if err != nil { + return nil, err + } + return resp.Items, nil +} diff --git a/modules/k8s/event_test.go b/modules/k8s/event_test.go new file mode 100644 index 000000000..3e5c8f0ac --- /dev/null +++ b/modules/k8s/event_test.go @@ -0,0 +1,43 @@ +//go:build kubernetes +// +build kubernetes + +// NOTE: we have build tags to differentiate kubernetes tests from non-kubernetes tests. This is done because minikube +// is heavy and can interfere with docker related tests in terratest. Specifically, many of the tests start to fail with +// `connection refused` errors from `minikube`. To avoid overloading the system, we run the kubernetes tests and helm +// tests separately from the others. This may not be necessary if you have a sufficiently powerful machine. We +// recommend at least 4 cores and 16GB of RAM if you want to run all the tests together. + +package k8s + +import ( + "testing" + + "github.com/stretchr/testify/require" + + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + _ "k8s.io/client-go/plugin/pkg/client/auth" +) + +func TestListEventsInNamespace(t *testing.T) { + t.Parallel() + + options := NewKubectlOptions("", "", "kube-system") + events, err := ListEventsE(t, options, v1.ListOptions{}) + require.Nil(t, err) + require.Greater(t, len(events), 0) +} + +func TestListEventsReturnsZeroEventsIfNoneCreated(t *testing.T) { + t.Parallel() + ns := "test-ns" + + options := NewKubectlOptions("", "", "") + + defer DeleteNamespace(t, options, ns) + CreateNamespace(t, options, ns) + + options.Namespace = ns + events, err := ListEventsE(t, options, v1.ListOptions{}) + require.Nil(t, err) + require.Equal(t, 0, len(events)) +} From 61d998551ff486b9f1fa87d7604a66cc519d9fca Mon Sep 17 00:00:00 2001 From: Tomas Tormo Date: Wed, 28 Jun 2023 23:08:14 +0200 Subject: [PATCH 2/2] Add tests for ListEvent function --- modules/k8s/event_test.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/modules/k8s/event_test.go b/modules/k8s/event_test.go index 3e5c8f0ac..55cdff7ec 100644 --- a/modules/k8s/event_test.go +++ b/modules/k8s/event_test.go @@ -18,7 +18,7 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth" ) -func TestListEventsInNamespace(t *testing.T) { +func TestListEventsEReturnsNilErrorWhenListingEvents(t *testing.T) { t.Parallel() options := NewKubectlOptions("", "", "kube-system") @@ -27,6 +27,14 @@ func TestListEventsInNamespace(t *testing.T) { require.Greater(t, len(events), 0) } +func TestListEventsInNamespace(t *testing.T) { + t.Parallel() + + options := NewKubectlOptions("", "", "kube-system") + events := ListEvents(t, options, v1.ListOptions{}) + require.Greater(t, len(events), 0) +} + func TestListEventsReturnsZeroEventsIfNoneCreated(t *testing.T) { t.Parallel() ns := "test-ns" @@ -37,7 +45,6 @@ func TestListEventsReturnsZeroEventsIfNoneCreated(t *testing.T) { CreateNamespace(t, options, ns) options.Namespace = ns - events, err := ListEventsE(t, options, v1.ListOptions{}) - require.Nil(t, err) + events := ListEvents(t, options, v1.ListOptions{}) require.Equal(t, 0, len(events)) }