Skip to content

Commit

Permalink
datadog-acceptance: flag refactoring and disable flag update
Browse files Browse the repository at this point in the history
  • Loading branch information
natemollica-nm committed Apr 18, 2024
1 parent 91abb5d commit 55f94fa
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 68 deletions.
7 changes: 1 addition & 6 deletions acceptance/framework/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ const (
HelmChartPath = "../../../charts/consul"
LicenseSecretName = "license"
LicenseSecretKey = "key"
DatadogSecretName = "datadog-secret"
DatadogAPIKey = "api-key"
DatadogAppKey = "app-key"
)

type KubeTestConfig struct {
Expand Down Expand Up @@ -73,9 +70,7 @@ type TestConfig struct {
EnableEnterprise bool
EnterpriseLicense string

EnableDatadog bool
DatadogAPIKey string
DatadogAppKey string
SkipDataDogTests bool
DatadogHelmChartVersion string

EnableOpenshift bool
Expand Down
39 changes: 0 additions & 39 deletions acceptance/framework/consul/helm_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,6 @@ func NewHelmCluster(
valuesFromConfig, err := cfg.HelmValuesFromConfig()
require.NoError(t, err)

if cfg.EnableDatadog {
datadogNamespace := helmValues["global.metrics.datadog.namespace"]
configureNamespace(t, ctx.KubernetesClient(t), cfg, datadogNamespace)

if cfg.EnableDatadog && (cfg.DatadogAPIKey != "" || cfg.DatadogAppKey != "") {
createOrUpdateDatadogSecret(t, ctx.KubernetesClient(t), cfg, datadogNamespace)
}
}

// Merge all helm values
helpers.MergeMaps(values, valuesFromConfig)
helpers.MergeMaps(values, helmValues)
Expand Down Expand Up @@ -680,14 +671,6 @@ func createOrUpdateLicenseSecret(t *testing.T, client kubernetes.Interface, cfg
CreateK8sSecret(t, client, cfg, namespace, config.LicenseSecretName, config.LicenseSecretKey, cfg.EnterpriseLicense)
}

func createOrUpdateDatadogSecret(t *testing.T, client kubernetes.Interface, cfg *config.TestConfig, namespace string) {
secretMap := map[string]string{
config.DatadogAPIKey: cfg.DatadogAPIKey,
config.DatadogAppKey: cfg.DatadogAppKey,
}
createMultiKeyK8sSecret(t, client, cfg, namespace, config.DatadogSecretName, secretMap)
}

func configureNamespace(t *testing.T, client kubernetes.Interface, cfg *config.TestConfig, namespace string) {
ctx := context.Background()

Expand Down Expand Up @@ -799,25 +782,3 @@ func CreateK8sSecret(t *testing.T, client kubernetes.Interface, cfg *config.Test
_ = client.CoreV1().Secrets(namespace).Delete(context.Background(), secretName, metav1.DeleteOptions{})
})
}

func createMultiKeyK8sSecret(t *testing.T, client kubernetes.Interface, cfg *config.TestConfig, namespace, secretName string, secretMap map[string]string) {
retry.RunWith(&retry.Counter{Wait: 2 * time.Second, Count: 15}, t, func(r *retry.R) {
_, err := client.CoreV1().Secrets(namespace).Get(context.Background(), secretName, metav1.GetOptions{})
if errors.IsNotFound(err) {
_, err := client.CoreV1().Secrets(namespace).Create(context.Background(), &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
},
StringData: secretMap,
Type: corev1.SecretTypeOpaque,
}, metav1.CreateOptions{})
require.NoError(r, err)
} else {
require.NoError(r, err)
}
})

helpers.Cleanup(t, cfg.NoCleanupOnFailure, cfg.NoCleanup, func() {
_ = client.CoreV1().Secrets(namespace).Delete(context.Background(), secretName, metav1.DeleteOptions{})
})
}
75 changes: 75 additions & 0 deletions acceptance/framework/datadog/datadog.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package datadog

import (
"context"
"fmt"
"github.com/hashicorp/consul-k8s/acceptance/framework/k8s"
"github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
"time"

"github.com/hashicorp/consul-k8s/acceptance/framework/config"
"github.com/hashicorp/consul-k8s/acceptance/framework/helpers"
Expand All @@ -20,6 +27,11 @@ const (
releaseLabel = "app.kubernetes.io/name"
OperatorReleaseName = "datadog-operator"
DefaultHelmChartVersion = "1.4.0"
datadogSecretName = "datadog-secret"
datadogAPIKey = "api-key"
datadogAppKey = "app-key"
datadogFakeAPIKey = "DD_FAKEAPIKEY"
datadogFakeAPPKey = "DD_FAKEAPPKEY"
)

type DatadogCluster struct {
Expand Down Expand Up @@ -47,6 +59,10 @@ func (d *DatadogCluster) releaseLabelSelector() string {
func NewDatadogCluster(t *testing.T, ctx environment.TestContext, cfg *config.TestConfig, releaseName string, releaseNamespace string, helmValues map[string]string) *DatadogCluster {
logger := terratestLogger.New(logger.TestLogger{})

configureNamespace(t, ctx.KubernetesClient(t), cfg, releaseNamespace)

createOrUpdateDatadogSecret(t, ctx.KubernetesClient(t), cfg, releaseNamespace)

kopts := ctx.KubectlOptionsForNamespace(releaseNamespace)

values := defaultHelmValues()
Expand Down Expand Up @@ -113,3 +129,62 @@ func defaultHelmValues() map[string]string {
"image.repository": "gcr.io/datadoghq/operator",
}
}

func configureNamespace(t *testing.T, client kubernetes.Interface, cfg *config.TestConfig, namespace string) {
ctx := context.Background()

ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
Labels: map[string]string{},
},
}
if cfg.EnableRestrictedPSAEnforcement {
ns.ObjectMeta.Labels["pod-security.kubernetes.io/enforce"] = "restricted"
ns.ObjectMeta.Labels["pod-security.kubernetes.io/enforce-version"] = "latest"
}

_, createErr := client.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
if createErr == nil {
logger.Logf(t, "Created namespace %s", namespace)
return
}

_, updateErr := client.CoreV1().Namespaces().Update(ctx, ns, metav1.UpdateOptions{})
if updateErr == nil {
logger.Logf(t, "Updated namespace %s", namespace)
return
}

require.Failf(t, "Failed to create or update namespace", "Namespace=%s, CreateError=%s, UpdateError=%s", namespace, createErr, updateErr)
}

func createOrUpdateDatadogSecret(t *testing.T, client kubernetes.Interface, cfg *config.TestConfig, namespace string) {
secretMap := map[string]string{
datadogAPIKey: datadogFakeAPIKey,
datadogAppKey: datadogFakeAPPKey,
}
createMultiKeyK8sSecret(t, client, cfg, namespace, datadogSecretName, secretMap)
}

func createMultiKeyK8sSecret(t *testing.T, client kubernetes.Interface, cfg *config.TestConfig, namespace, secretName string, secretMap map[string]string) {
retry.RunWith(&retry.Counter{Wait: 2 * time.Second, Count: 15}, t, func(r *retry.R) {
_, err := client.CoreV1().Secrets(namespace).Get(context.Background(), secretName, metav1.GetOptions{})
if errors.IsNotFound(err) {
_, err := client.CoreV1().Secrets(namespace).Create(context.Background(), &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
},
StringData: secretMap,
Type: corev1.SecretTypeOpaque,
}, metav1.CreateOptions{})
require.NoError(r, err)
} else {
require.NoError(r, err)
}
})

helpers.Cleanup(t, cfg.NoCleanupOnFailure, cfg.NoCleanup, func() {
_ = client.CoreV1().Secrets(namespace).Delete(context.Background(), secretName, metav1.DeleteOptions{})
})
}
29 changes: 7 additions & 22 deletions acceptance/framework/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ type TestFlags struct {
flagEnableEnterprise bool
flagEnterpriseLicense string

flagEnableDatadog bool
flagDatadogAPIKey string
flagDatadogAppKey string

flagEnableOpenshift bool

flagSkipDatadogTests bool

flagEnablePodSecurityPolicies bool

flagEnableCNI bool
Expand Down Expand Up @@ -113,14 +111,6 @@ func (t *TestFlags) init() {
flag.StringVar(&t.flagEnterpriseLicense, "enterprise-license", "",
"The enterprise license for Consul.")

flag.BoolVar(&t.flagEnableDatadog, "enable-datadog", false,
"If true, the test suite will run tests for datadog integration features. ")

flag.StringVar(&t.flagDatadogAPIKey, "dd-api-key", "DD_FAKEAPIKEY",
"The Datadog Agent API Key used for datadog metrics tests.")
flag.StringVar(&t.flagDatadogAppKey, "dd-app-key", "DD_FAKEAPPKEY",
"The Datadog Agent Application Key used for datadog metrics tests.")

flag.BoolVar(&t.flagEnableOpenshift, "enable-openshift", false,
"If true, the tests will automatically add Openshift Helm value for each Helm install.")

Expand Down Expand Up @@ -167,15 +157,12 @@ func (t *TestFlags) init() {
flag.BoolVar(&t.flagDisablePeering, "disable-peering", false,
"If true, the peering tests will not run.")

flag.BoolVar(&t.flagSkipDatadogTests, "skip-datadog", false,
"If true, datadog acceptance tests will not run.")

if t.flagEnterpriseLicense == "" {
t.flagEnterpriseLicense = os.Getenv("CONSUL_ENT_LICENSE")
}
if ddAPIKeyEnv, exists := os.LookupEnv("DATADOG_API_KEY"); exists {
t.flagDatadogAPIKey = ddAPIKeyEnv
}
if ddAPPKeyEnv, exists := os.LookupEnv("DATADOG_APP_KEY"); exists {
t.flagDatadogAppKey = ddAPPKeyEnv
}
}

func (t *TestFlags) Validate() error {
Expand Down Expand Up @@ -222,15 +209,13 @@ func (t *TestFlags) TestConfigFromFlags() *config.TestConfig {
EnableEnterprise: t.flagEnableEnterprise,
EnterpriseLicense: t.flagEnterpriseLicense,

EnableDatadog: t.flagEnableDatadog,
DatadogAPIKey: t.flagDatadogAPIKey,
DatadogAppKey: t.flagDatadogAppKey,

KubeEnvs: kubeEnvs,
EnableMultiCluster: t.flagEnableMultiCluster,

EnableOpenshift: t.flagEnableOpenshift,

SkipDataDogTests: t.flagSkipDatadogTests,

EnablePodSecurityPolicies: t.flagEnablePodSecurityPolicies,

EnableCNI: t.flagEnableCNI,
Expand Down
17 changes: 16 additions & 1 deletion acceptance/tests/datadog/datadog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

const (
consulDogstatsDMetricQuery = "consul.memberlist.gossip"
// consulOTLPMetricQuery = `otelcol_process_runtime_heap_alloc_bytes`.
)

// TODO: Refactor test cases into single function that accepts Helm Values, Fixture Name, and Validation Callback
Expand All @@ -34,6 +33,10 @@ func TestDatadogDogstatsDUnixDomainSocket(t *testing.T) {
cfg := suite.Config()
ctx := env.DefaultContext(t)

if cfg.SkipDataDogTests {
t.Skipf("skipping this test because -skip-datadog is set")
}

acceptanceTestingTags := "acceptance_test:unix_domain_sockets"
helmValues := map[string]string{
"global.datacenter": "dc1",
Expand Down Expand Up @@ -98,6 +101,10 @@ func TestDatadogDogstatsDUDP(t *testing.T) {
cfg := suite.Config()
ctx := env.DefaultContext(t)

if cfg.SkipDataDogTests {
t.Skipf("skipping this test because -skip-datadog is set")
}

acceptanceTestingTags := "acceptance_test:dogstatsd_udp"
helmValues := map[string]string{
"global.datacenter": "dc1",
Expand Down Expand Up @@ -163,6 +170,10 @@ func TestDatadogConsulChecks(t *testing.T) {
cfg := suite.Config()
ctx := env.DefaultContext(t)

if cfg.SkipDataDogTests {
t.Skipf("skipping this test because -skip-datadog is set")
}

helmValues := map[string]string{
"global.datacenter": "dc1",
"global.metrics.enabled": "true",
Expand Down Expand Up @@ -226,6 +237,10 @@ func TestDatadogOpenmetrics(t *testing.T) {
cfg := suite.Config()
ctx := env.DefaultContext(t)

if cfg.SkipDataDogTests {
t.Skipf("skipping this test because -skip-datadog is set")
}

helmValues := map[string]string{
"global.datacenter": "dc1",
"global.metrics.enabled": "true",
Expand Down

0 comments on commit 55f94fa

Please sign in to comment.