-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/clusteragent/admission: add unit tests (#15044)
- Loading branch information
Showing
8 changed files
with
329 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
//go:build kubeapiserver | ||
// +build kubeapiserver | ||
|
||
package common | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func TestLibConfig_ToEnvs(t *testing.T) { | ||
type fields struct { | ||
ServiceName *string | ||
Env *string | ||
Tracing *bool | ||
LogInjection *bool | ||
HealthMetrics *bool | ||
RuntimeMetrics *bool | ||
TracingSamplingRate *float64 | ||
TracingRateLimit *int | ||
TracingTags []string | ||
|
||
/* | ||
TracingServiceMapping []TracingServiceMapEntry | ||
TracingAgentTimeout *int | ||
TracingHeaderTags []TracingHeaderTagEntry | ||
TracingPartialFlushMinSpans *int | ||
TracingDebug *bool | ||
TracingLogLevel *string | ||
TracingMethods []string | ||
TracingPropagationStyleInject []string | ||
TracingPropagationStyleExtract []string | ||
*/ | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want []corev1.EnvVar | ||
}{ | ||
{ | ||
name: "all", | ||
fields: fields{ | ||
ServiceName: ptr("svc"), | ||
Env: ptr("dev"), | ||
Tracing: ptr(true), | ||
LogInjection: ptr(true), | ||
HealthMetrics: ptr(true), | ||
RuntimeMetrics: ptr(true), | ||
TracingSamplingRate: ptr(0.5), | ||
TracingRateLimit: ptr(50), | ||
TracingTags: []string{"k1:v1", "k2:v2"}, | ||
}, | ||
want: []corev1.EnvVar{ | ||
{ | ||
Name: "DD_SERVICE", | ||
Value: "svc", | ||
}, | ||
{ | ||
Name: "DD_ENV", | ||
Value: "dev", | ||
}, | ||
{ | ||
Name: "DD_TRACE_ENABLED", | ||
Value: "true", | ||
}, | ||
{ | ||
Name: "DD_LOGS_INJECTION", | ||
Value: "true", | ||
}, | ||
{ | ||
Name: "DD_TRACE_HEALTH_METRICS_ENABLED", | ||
Value: "true", | ||
}, | ||
{ | ||
Name: "DD_RUNTIME_METRICS_ENABLED", | ||
Value: "true", | ||
}, | ||
{ | ||
Name: "DD_TRACE_SAMPLE_RATE", | ||
Value: "0.50", | ||
}, | ||
{ | ||
Name: "DD_TRACE_RATE_LIMIT", | ||
Value: "50", | ||
}, | ||
{ | ||
Name: "DD_TAGS", | ||
Value: "k1:v1,k2:v2", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "only service and env", | ||
fields: fields{ | ||
ServiceName: ptr("svc"), | ||
Env: ptr("dev"), | ||
}, | ||
want: []corev1.EnvVar{ | ||
{ | ||
Name: "DD_SERVICE", | ||
Value: "svc", | ||
}, | ||
{ | ||
Name: "DD_ENV", | ||
Value: "dev", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "empty", | ||
fields: fields{}, | ||
want: nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
lc := LibConfig{ | ||
ServiceName: tt.fields.ServiceName, | ||
Env: tt.fields.Env, | ||
Tracing: tt.fields.Tracing, | ||
LogInjection: tt.fields.LogInjection, | ||
HealthMetrics: tt.fields.HealthMetrics, | ||
RuntimeMetrics: tt.fields.RuntimeMetrics, | ||
TracingSamplingRate: tt.fields.TracingSamplingRate, | ||
TracingRateLimit: tt.fields.TracingRateLimit, | ||
TracingTags: tt.fields.TracingTags, | ||
|
||
/* | ||
TracingServiceMapping: tt.fields.TracingServiceMapping, | ||
TracingAgentTimeout: tt.fields.TracingAgentTimeout, | ||
TracingHeaderTags: tt.fields.TracingHeaderTags, | ||
TracingPartialFlushMinSpans: tt.fields.TracingPartialFlushMinSpans, | ||
TracingDebug: tt.fields.TracingDebug, | ||
TracingLogLevel: tt.fields.TracingLogLevel, | ||
TracingMethods: tt.fields.TracingMethods, | ||
TracingPropagationStyleInject: tt.fields.TracingPropagationStyleInject, | ||
TracingPropagationStyleExtract: tt.fields.TracingPropagationStyleExtract, | ||
*/ | ||
} | ||
require.EqualValues(t, tt.want, lc.ToEnvs()) | ||
}) | ||
} | ||
} | ||
|
||
func ptr[T int | bool | string | float64](val T) *T { | ||
return &val | ||
} |
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,93 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
//go:build kubeapiserver | ||
// +build kubeapiserver | ||
|
||
package patch | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/DataDog/datadog-agent/pkg/clusteragent/admission/common" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPatchRequestValidate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
LibConfig common.LibConfig | ||
K8sTarget K8sTarget | ||
clusterName string | ||
valid bool | ||
}{ | ||
{ | ||
name: "valid", | ||
LibConfig: common.LibConfig{Language: "lang", Version: "latest"}, | ||
K8sTarget: K8sTarget{Cluster: "cluster", Kind: "deployment", Name: "name", Namespace: "ns"}, | ||
clusterName: "cluster", | ||
valid: true, | ||
}, | ||
{ | ||
name: "empty version", | ||
LibConfig: common.LibConfig{Language: "lang"}, | ||
K8sTarget: K8sTarget{Cluster: "cluster", Kind: "deployment", Name: "name", Namespace: "ns"}, | ||
clusterName: "cluster", | ||
valid: false, | ||
}, | ||
{ | ||
name: "empty language", | ||
LibConfig: common.LibConfig{Version: "latest"}, | ||
K8sTarget: K8sTarget{Cluster: "cluster", Kind: "deployment", Name: "name", Namespace: "ns"}, | ||
clusterName: "cluster", | ||
valid: false, | ||
}, | ||
{ | ||
name: "empty cluster", | ||
LibConfig: common.LibConfig{Language: "lang", Version: "latest"}, | ||
K8sTarget: K8sTarget{Kind: "deployment", Name: "name", Namespace: "ns"}, | ||
clusterName: "cluster", | ||
valid: false, | ||
}, | ||
{ | ||
name: "wrong cluster", | ||
LibConfig: common.LibConfig{Language: "lang", Version: "latest"}, | ||
K8sTarget: K8sTarget{Cluster: "wrong-cluster", Kind: "deployment", Name: "name", Namespace: "ns"}, | ||
clusterName: "cluster", | ||
valid: false, | ||
}, | ||
{ | ||
name: "empty kind", | ||
LibConfig: common.LibConfig{Language: "lang", Version: "latest"}, | ||
K8sTarget: K8sTarget{Cluster: "cluster", Name: "name", Namespace: "ns"}, | ||
clusterName: "cluster", | ||
valid: false, | ||
}, | ||
{ | ||
name: "empty name", | ||
LibConfig: common.LibConfig{Language: "lang", Version: "latest"}, | ||
K8sTarget: K8sTarget{Cluster: "cluster", Kind: "deployment", Namespace: "ns"}, | ||
clusterName: "cluster", | ||
valid: false, | ||
}, | ||
{ | ||
name: "empty namesapce", | ||
LibConfig: common.LibConfig{Language: "lang", Version: "latest"}, | ||
K8sTarget: K8sTarget{Cluster: "cluster", Kind: "deployment", Name: "name"}, | ||
clusterName: "cluster", | ||
valid: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
pr := PatchRequest{ | ||
LibConfig: tt.LibConfig, | ||
K8sTarget: tt.K8sTarget, | ||
} | ||
err := pr.Validate(tt.clusterName) | ||
require.True(t, (err == nil) == tt.valid) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.