-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add replicaset helper functions #804
Merged
+136
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,49 @@ | ||
package k8s | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/stretchr/testify/require" | ||
appsv1 "k8s.io/api/apps/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/gruntwork-io/terratest/modules/testing" | ||
) | ||
|
||
// ListReplicaSets will look for replicasets in the given namespace that match the given filters and return them. This will | ||
// fail the test if there is an error. | ||
func ListReplicaSets(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) []appsv1.ReplicaSet { | ||
replicaset, err := ListReplicaSetsE(t, options, filters) | ||
require.NoError(t, err) | ||
return replicaset | ||
} | ||
|
||
// ListReplicaSetsE will look for replicasets in the given namespace that match the given filters and return them. | ||
func ListReplicaSetsE(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) ([]appsv1.ReplicaSet, error) { | ||
clientset, err := GetKubernetesClientFromOptionsE(t, options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
replicasets, err := clientset.AppsV1().ReplicaSets(options.Namespace).List(context.Background(), filters) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return replicasets.Items, nil | ||
} | ||
|
||
// GetReplicaSet returns a Kubernetes replicaset resource in the provided namespace with the given name. This will | ||
// fail the test if there is an error. | ||
func GetReplicaSet(t testing.TestingT, options *KubectlOptions, replicaSetName string) *appsv1.ReplicaSet { | ||
replicaset, err := GetReplicaSetE(t, options, replicaSetName) | ||
require.NoError(t, err) | ||
return replicaset | ||
} | ||
|
||
// GetReplicaSetE returns a Kubernetes replicaset resource in the provided namespace with the given name. | ||
func GetReplicaSetE(t testing.TestingT, options *KubectlOptions, replicaSetName string) (*appsv1.ReplicaSet, error) { | ||
clientset, err := GetKubernetesClientFromOptionsE(t, options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return clientset.AppsV1().ReplicaSets(options.Namespace).Get(context.Background(), replicaSetName, metav1.GetOptions{}) | ||
} |
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,87 @@ | ||
// +build kubeall 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 ( | ||
"fmt" | ||
|
||
"strings" | ||
"testing" | ||
|
||
"github.com/gruntwork-io/terratest/modules/random" | ||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestGetReplicaSetEReturnsError(t *testing.T) { | ||
t.Parallel() | ||
|
||
options := NewKubectlOptions("", "", "") | ||
_, err := GetReplicaSetE(t, options, "sample-rs") | ||
require.Error(t, err) | ||
} | ||
|
||
func TestGetReplicaSets(t *testing.T) { | ||
t.Parallel() | ||
|
||
uniqueID := strings.ToLower(random.UniqueId()) | ||
options := NewKubectlOptions("", "", uniqueID) | ||
configData := fmt.Sprintf(EXAMPLE_REPLICASET_YAML_TEMPLATE, uniqueID, uniqueID) | ||
KubectlApplyFromString(t, options, configData) | ||
defer KubectlDeleteFromString(t, options, configData) | ||
|
||
replicaSet := GetReplicaSet(t, options, "sample-rs") | ||
require.Equal(t, replicaSet.Name, "sample-rs") | ||
require.Equal(t, replicaSet.Namespace, uniqueID) | ||
} | ||
|
||
func TestListReplicaSets(t *testing.T) { | ||
t.Parallel() | ||
|
||
uniqueID := strings.ToLower(random.UniqueId()) | ||
options := NewKubectlOptions("", "", uniqueID) | ||
configData := fmt.Sprintf(EXAMPLE_REPLICASET_YAML_TEMPLATE, uniqueID, uniqueID) | ||
KubectlApplyFromString(t, options, configData) | ||
defer KubectlDeleteFromString(t, options, configData) | ||
|
||
replicaSets := ListReplicaSets(t, options, metav1.ListOptions{}) | ||
require.Equal(t, len(replicaSets), 1) | ||
|
||
replicaSet := replicaSets[0] | ||
require.Equal(t, replicaSet.Name, "sample-rs") | ||
require.Equal(t, replicaSet.Namespace, uniqueID) | ||
} | ||
|
||
const EXAMPLE_REPLICASET_YAML_TEMPLATE = `--- | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: %s | ||
--- | ||
apiVersion: apps/v1 | ||
kind: ReplicaSet | ||
metadata: | ||
name: sample-rs | ||
namespace: %s | ||
labels: | ||
app: sample-rs | ||
spec: | ||
selector: | ||
matchLabels: | ||
name: sample-rs | ||
template: | ||
metadata: | ||
labels: | ||
name: sample-rs | ||
spec: | ||
containers: | ||
- name: alpine | ||
image: alpine:3.8 | ||
command: ['sh', '-c', 'echo Hello Terratest! && sleep 99999'] | ||
` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: the delete defer should be before the apply, so that it always runs even if the apply fails with error (which could partially deploy things).