-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1297 from kaisoz/add-k8s-events-support
Add support for Events in k8s module
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//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 TestListEventsEReturnsNilErrorWhenListingEvents(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 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" | ||
|
||
options := NewKubectlOptions("", "", "") | ||
|
||
defer DeleteNamespace(t, options, ns) | ||
CreateNamespace(t, options, ns) | ||
|
||
options.Namespace = ns | ||
events := ListEvents(t, options, v1.ListOptions{}) | ||
require.Equal(t, 0, len(events)) | ||
} |