-
-
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 #1354 from kirecek/k8s/kubectl-options/rest-config
add an option for passing custom k8s rest config
- Loading branch information
Showing
3 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
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,71 @@ | ||
//go:build kubeall || kubernetes | ||
// +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 test | ||
|
||
import ( | ||
"fmt" | ||
"os/user" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"k8s.io/client-go/tools/clientcmd" | ||
|
||
"github.com/gruntwork-io/terratest/modules/k8s" | ||
"github.com/gruntwork-io/terratest/modules/random" | ||
) | ||
|
||
func TestKubernetesRestConfigBasicExampleConfig(t *testing.T) { | ||
t.Parallel() | ||
|
||
// website::tag::1::Path to the Kubernetes resource config we will test | ||
kubeResourcePath, err := filepath.Abs("../examples/kubernetes-basic-example/nginx-deployment.yml") | ||
require.NoError(t, err) | ||
|
||
// To ensure we can reuse the resource config on the same cluster to test different scenarios, we setup a unique | ||
// namespace for the resources for this test. | ||
// Note that namespaces must be lowercase. | ||
namespaceName := fmt.Sprintf("kubernetes-basic-example-%s", strings.ToLower(random.UniqueId())) | ||
|
||
usr, err := user.Current() | ||
if err != nil { | ||
require.NoError(t, err) | ||
} | ||
|
||
// Construct the path to the kubeconfig file | ||
kubeconfigPath := filepath.Join(usr.HomeDir, ".kube", "config") | ||
|
||
// Generate rest.Config from kubeconfig file | ||
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
// website::tag::2:: Setup the kubectl config and context. | ||
options := k8s.NewKubectlOptionsWithRestConfig(config, namespaceName) | ||
|
||
k8s.CreateNamespace(t, options, namespaceName) | ||
// website::tag::5::Make sure to delete the namespace at the end of the test | ||
defer k8s.DeleteNamespace(t, options, namespaceName) | ||
|
||
// website::tag::6::At the end of the test, run `kubectl delete -f RESOURCE_CONFIG` to clean up any resources that were created. | ||
defer k8s.KubectlDelete(t, options, kubeResourcePath) | ||
|
||
// website::tag::3::Apply kubectl with 'kubectl apply -f RESOURCE_CONFIG' command. | ||
// This will run `kubectl apply -f RESOURCE_CONFIG` and fail the test if there are any errors | ||
k8s.KubectlApply(t, options, kubeResourcePath) | ||
|
||
// website::tag::4::Check if NGINX service was deployed successfully. | ||
// This will get the service resource and verify that it exists and was retrieved successfully. This function will | ||
// fail the test if the there is an error retrieving the service resource from Kubernetes. | ||
service := k8s.GetService(t, options, "nginx-service") | ||
require.Equal(t, service.Name, "nginx-service") | ||
} |