forked from hashicorp/consul-k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
64 lines (50 loc) · 2.21 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// Rename package to your test package.
package example
import (
"context"
"testing"
"github.com/hashicorp/consul-k8s/acceptance/framework/consul"
"github.com/hashicorp/consul-k8s/acceptance/framework/helpers"
"github.com/hashicorp/consul-k8s/acceptance/framework/k8s"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestExample(t *testing.T) {
// Get test configuration.
cfg := suite.Config()
// Get the default context.
ctx := suite.Environment().DefaultContext(t)
// Create Helm values for the Helm install.
helmValues := map[string]string{
"exampleFeature.enabled": "true",
}
// Generate a random name for this test.
releaseName := helpers.RandomName()
// Create a new Consul cluster object.
consulCluster := consul.NewHelmCluster(t, helmValues, ctx, cfg, releaseName)
// Create the Consul cluster with Helm.
consulCluster.Create(t)
// Make test assertions.
// To run kubectl commands, you need to get KubectlOptions from the test context.
// There are a number of kubectl commands available in the helpers/kubectl.go file.
// For example, to call 'kubectl apply' from the test write the following:
k8s.KubectlApply(t, ctx.KubectlOptions(t), "path/to/config")
// Clean up any Kubernetes resources you have created
helpers.Cleanup(t, cfg.NoCleanupOnFailure, func() {
k8s.KubectlDelete(t, ctx.KubectlOptions(t), "path/to/config")
})
// Similarly, you can obtain Kubernetes client from your test context.
// You can use it to, for example, read all services in a namespace:
k8sClient := ctx.KubernetesClient(t)
services, err := k8sClient.CoreV1().Services(ctx.KubectlOptions(t).Namespace).List(context.Background(), metav1.ListOptions{})
require.NoError(t, err)
require.NotNil(t, services.Items)
// To make Consul API calls, you can get the Consul client from the consulCluster object,
// indicating whether the client needs to be secure or not (i.e. whether TLS and ACLs are enabled on the Consul cluster):
consulClient, _ := consulCluster.SetupConsulClient(t, true)
consulServices, _, err := consulClient.Catalog().Services(nil)
require.NoError(t, err)
require.NotNil(t, consulServices)
}