-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add testing, and download from RUM unreleased modules (#123)
Add testing, and download from RUM unreleased modules
- Loading branch information
1 parent
9440247
commit cacad6b
Showing
23 changed files
with
1,718 additions
and
747 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,138 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestValidateInput(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
appID string | ||
site string | ||
clientToken string | ||
arch string | ||
sessionSampleRate int | ||
sessionReplaySampleRate int | ||
expectedError string | ||
}{ | ||
{ | ||
name: "Valid input", | ||
appID: "test-app", | ||
site: "test-site", | ||
clientToken: "test-token", | ||
arch: "amd64", | ||
sessionSampleRate: 50, | ||
sessionReplaySampleRate: 50, | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "Empty appID", | ||
appID: "", | ||
site: "test-site", | ||
clientToken: "test-token", | ||
arch: "amd64", | ||
sessionSampleRate: 50, | ||
sessionReplaySampleRate: 50, | ||
expectedError: "--appId is required", | ||
}, | ||
{ | ||
name: "Empty site", | ||
appID: "test-app", | ||
site: "", | ||
clientToken: "test-token", | ||
arch: "amd64", | ||
sessionSampleRate: 50, | ||
sessionReplaySampleRate: 50, | ||
expectedError: "--site is required", | ||
}, | ||
{ | ||
name: "Empty clientToken", | ||
appID: "test-app", | ||
site: "test-site", | ||
clientToken: "", | ||
arch: "amd64", | ||
sessionSampleRate: 50, | ||
sessionReplaySampleRate: 50, | ||
expectedError: "--clientToken is required", | ||
}, | ||
{ | ||
name: "Invalid sessionSampleRate (negative)", | ||
appID: "test-app", | ||
site: "test-site", | ||
clientToken: "test-token", | ||
arch: "amd64", | ||
sessionSampleRate: -1, | ||
sessionReplaySampleRate: 50, | ||
expectedError: "sessionSampleRate is required and must be between 0 and 100", | ||
}, | ||
{ | ||
name: "Invalid sessionSampleRate (over 100)", | ||
appID: "test-app", | ||
site: "test-site", | ||
clientToken: "test-token", | ||
arch: "amd64", | ||
sessionSampleRate: 101, | ||
sessionReplaySampleRate: 50, | ||
expectedError: "sessionSampleRate is required and must be between 0 and 100", | ||
}, | ||
{ | ||
name: "Invalid sessionReplaySampleRate (negative)", | ||
appID: "test-app", | ||
site: "test-site", | ||
clientToken: "test-token", | ||
arch: "amd64", | ||
sessionSampleRate: 50, | ||
sessionReplaySampleRate: -1, | ||
expectedError: "sessionReplaySampleRate is required and must be between 0 and 100", | ||
}, | ||
{ | ||
name: "Invalid sessionReplaySampleRate (over 100)", | ||
appID: "test-app", | ||
site: "test-site", | ||
clientToken: "test-token", | ||
arch: "amd64", | ||
sessionSampleRate: 50, | ||
sessionReplaySampleRate: 101, | ||
expectedError: "sessionReplaySampleRate is required and must be between 0 and 100", | ||
}, | ||
{ | ||
name: "Invalid arch", | ||
appID: "test-app", | ||
site: "test-site", | ||
clientToken: "test-token", | ||
arch: "x86", | ||
sessionSampleRate: 50, | ||
sessionReplaySampleRate: 50, | ||
expectedError: "arch must be either 'amd64' or 'arm64'", | ||
}, | ||
{ | ||
name: "Valid input with arm64 arch", | ||
appID: "test-app", | ||
site: "test-site", | ||
clientToken: "test-token", | ||
arch: "arm64", | ||
sessionSampleRate: 50, | ||
sessionReplaySampleRate: 50, | ||
expectedError: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := validateInput(tt.appID, tt.site, tt.clientToken, tt.arch, tt.sessionSampleRate, tt.sessionReplaySampleRate) | ||
|
||
if tt.expectedError == "" { | ||
if err != nil { | ||
t.Errorf("Expected no error, but got: %v", err) | ||
} | ||
} else { | ||
if err == nil { | ||
t.Errorf("Expected error containing '%s', but got no error", tt.expectedError) | ||
} else if !strings.Contains(err.Error(), tt.expectedError) { | ||
t.Errorf("Expected error containing '%s', but got: %v", tt.expectedError, err) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,82 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gotest.tools/assert" | ||
) | ||
|
||
type FileInfo struct { | ||
Filename string | ||
IsError bool | ||
} | ||
|
||
func TestTransformConfigSnapshot(t *testing.T) { | ||
const modulesPath = "/opt/datadog-nginx" | ||
const agentUri = "http://localhost:8126" | ||
const appId = "ffffffff-ffff-ffff-ffff-ffffffffffff" | ||
const clientToken = "pubffffffffffffffffffffffffffffffff" | ||
const site = "datadoghq.com" | ||
const sessionSampleRate = 49 | ||
const sessionReplaySampleRate = 51 | ||
|
||
pattern := filepath.Join("testdata", "*.conf") | ||
files, err := filepath.Glob(pattern) | ||
require.NoError(t, err, "Failed to list test cases") | ||
|
||
// List all test cases | ||
// *.conf should contain a corresponding *.conf.snap file, created if not exists | ||
// *.err.conf should fail the transformation, no corresponding snapshot file | ||
var testCases []FileInfo | ||
for _, file := range files { | ||
isError := strings.HasSuffix(filepath.Base(file), ".err.conf") | ||
testCases = append(testCases, FileInfo{ | ||
Filename: file, | ||
IsError: isError, | ||
}) | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.Filename, func(t *testing.T) { | ||
input, err := os.ReadFile(tc.Filename) | ||
require.NoError(t, err, "Failed to read input file") | ||
|
||
// Create a temporary file for the output | ||
tmpFile, err := os.CreateTemp("", "nginx-config-*.conf") | ||
require.NoError(t, err, "Failed to create temporary file") | ||
defer os.Remove(tmpFile.Name()) | ||
|
||
configurator := &NginxConfigurator{ | ||
ModulesPath: modulesPath, | ||
} | ||
|
||
transformed, err := transformConfig(configurator, input, agentUri, appId, clientToken, site, sessionSampleRate, sessionReplaySampleRate, tmpFile.Name()) | ||
if tc.IsError { | ||
require.Error(t, err, "Expected an error transforming the configuration, it succeeded instead") | ||
return | ||
} | ||
|
||
require.NoError(t, err, "Failed to transform config") | ||
|
||
snapshotFile := filepath.Join("testdata", filepath.Base(tc.Filename)+".snap") | ||
|
||
if _, err := os.Stat(snapshotFile); os.IsNotExist(err) { | ||
// If snapshot doesn't exist, create it for local use, fail the test | ||
err = os.WriteFile(snapshotFile, transformed, 0644) | ||
require.NoError(t, err, "Failed to create snapshot file") | ||
t.Logf("Created new snapshot: %s", snapshotFile) | ||
t.Fail() | ||
} else { | ||
// If snapshot exists, compare with it | ||
expected, err := os.ReadFile(snapshotFile) | ||
require.NoError(t, err, "Failed to read snapshot file") | ||
|
||
assert.Equal(t, string(expected), string(transformed), "Transformed config does not match snapshot") | ||
} | ||
}) | ||
} | ||
} |
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,31 @@ | ||
user nginx; | ||
worker_processes auto; | ||
|
||
error_log /var/log/nginx/error.log notice; | ||
pid /var/run/nginx.pid; | ||
|
||
|
||
events { | ||
worker_connections 1024; | ||
} | ||
|
||
|
||
http { | ||
include /etc/nginx/mime.types; | ||
default_type application/octet-stream; | ||
|
||
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' | ||
'$status $body_bytes_sent "$http_referer" ' | ||
'"$http_user_agent" "$http_x_forwarded_for"'; | ||
|
||
access_log /var/log/nginx/access.log main; | ||
|
||
sendfile on; | ||
#tcp_nopush on; | ||
|
||
keepalive_timeout 65; | ||
|
||
#gzip on; | ||
|
||
include /etc/nginx/conf.d/*.conf; | ||
} |
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,48 @@ | ||
load_module /opt/datadog-nginx/ngx_http_datadog_module.so; | ||
user nginx; | ||
worker_processes auto; | ||
|
||
error_log /var/log/nginx/error.log notice; | ||
pid /var/run/nginx.pid; | ||
|
||
|
||
events { | ||
worker_connections 1024; | ||
} | ||
|
||
|
||
http { | ||
datadog_agent_url http://localhost:8126; | ||
|
||
# Disable APM Tracing. Remove the next line to enable APM Tracing. | ||
datadog_disable; | ||
|
||
# Enable RUM Injection | ||
datadog_rum on; | ||
|
||
datadog_rum_config "v5" { | ||
"applicationId" "ffffffff-ffff-ffff-ffff-ffffffffffff"; | ||
"clientToken" "pubffffffffffffffffffffffffffffffff"; | ||
"site" "datadoghq.com"; | ||
"sessionSampleRate" "49"; | ||
"sessionReplaySampleRate" "51"; | ||
} | ||
|
||
include /etc/nginx/mime.types; | ||
default_type application/octet-stream; | ||
|
||
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' | ||
'$status $body_bytes_sent "$http_referer" ' | ||
'"$http_user_agent" "$http_x_forwarded_for"'; | ||
|
||
access_log /var/log/nginx/access.log main; | ||
|
||
sendfile on; | ||
#tcp_nopush on; | ||
|
||
keepalive_timeout 65; | ||
|
||
#gzip on; | ||
|
||
include /etc/nginx/conf.d/*.conf; | ||
} |
Oops, something went wrong.