-
-
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.
Update functionalities in k8s module to take in options
- Loading branch information
1 parent
c7853c2
commit 4f285bf
Showing
8 changed files
with
158 additions
and
22 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
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
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
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
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,66 @@ | ||
package k8s | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// CreateNamespace will create a new Kubernetes namespace on the cluster targeted by the provided options. This will | ||
// fail the test if there is an error in creating the namespace. | ||
func CreateNamespace(t *testing.T, options *KubectlOptions, namespaceName string) { | ||
require.NoError(t, CreateNamespaceE(t, options, namespaceName)) | ||
} | ||
|
||
// CreateNamespaceE will create a new Kubernetes namespace on the cluster targeted by the provided options. | ||
func CreateNamespaceE(t *testing.T, options *KubectlOptions, namespaceName string) error { | ||
clientset, err := GetKubernetesClientFromOptionsE(t, options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
namespace := corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: namespaceName, | ||
}, | ||
} | ||
_, err = clientset.CoreV1().Namespaces().Create(&namespace) | ||
return err | ||
} | ||
|
||
// GetNamespace will query the Kubernetes cluster targeted by the provided options for the requested namespace. This will | ||
// fail the test if there is an error in creating the namespace. | ||
func GetNamespace(t *testing.T, options *KubectlOptions, namespaceName string) corev1.Namespace { | ||
namespace, err := GetNamespaceE(t, options, namespaceName) | ||
require.NoError(t, err) | ||
return namespace | ||
} | ||
|
||
// GetNamespaceE will query the Kubernetes cluster targeted by the provided options for the requested namespace. | ||
func GetNamespaceE(t *testing.T, options *KubectlOptions, namespaceName string) (corev1.Namespace, error) { | ||
clientset, err := GetKubernetesClientFromOptionsE(t, options) | ||
if err != nil { | ||
return corev1.Namespace{}, err | ||
} | ||
|
||
namespace, err := clientset.CoreV1().Namespaces().Get(namespaceName, metav1.GetOptions{}) | ||
return *namespace, err | ||
} | ||
|
||
// DeleteNamespace will delete the requested namespace from the Kubernetes cluster targeted by the provided options. This will | ||
// fail the test if there is an error in creating the namespace. | ||
func DeleteNamespace(t *testing.T, options *KubectlOptions, namespaceName string) { | ||
require.NoError(t, DeleteNamespaceE(t, options, namespaceName)) | ||
} | ||
|
||
// DeleteNamespaceE will delete the requested namespace from the Kubernetes cluster targeted by the provided options. | ||
func DeleteNamespaceE(t *testing.T, options *KubectlOptions, namespaceName string) error { | ||
clientset, err := GetKubernetesClientFromOptionsE(t, options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return clientset.CoreV1().Namespaces().Delete(namespaceName, &metav1.DeleteOptions{}) | ||
} |
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,28 @@ | ||
package k8s | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/gruntwork-io/terratest/modules/random" | ||
) | ||
|
||
func TestNamespaces(t *testing.T) { | ||
t.Parallel() | ||
|
||
uniqueId := random.UniqueId() | ||
namespaceName := strings.ToLower(uniqueId) | ||
options := NewKubectlOptions("", "") | ||
CreateNamespace(t, options, namespaceName) | ||
defer func() { | ||
DeleteNamespace(t, options, namespaceName) | ||
namespace := GetNamespace(t, options, namespaceName) | ||
require.Equal(t, namespace.Status.Phase, corev1.NamespaceTerminating) | ||
}() | ||
|
||
namespace := GetNamespace(t, options, namespaceName) | ||
require.Equal(t, namespace.Name, namespaceName) | ||
} |
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
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