Skip to content

Commit

Permalink
Fix SDK tests that had changed
Browse files Browse the repository at this point in the history
  • Loading branch information
louiszawadzki committed Sep 7, 2023
1 parent 976102d commit 9b5498c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 47 deletions.
2 changes: 1 addition & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ target 'ddSdkReactnativeExample' do
)
__apply_Xcode_12_5_M1_post_install_workaround(installer)
# Enable `DD_SDK_COMPILED_FOR_TESTING` condition when compiling `DatadogSDK` dependency:
datadog_sdk_target = installer.pods_project.targets.detect {|t| ["DatadogCore", "DatadogRUM", "DatadogLogs", "DatadogInternal", "DatadogTrace"].include?(t.name) }
datadog_sdk_target = installer.pods_project.targets.detect {|t| ["DatadogCore", "DatadogRUM", "DatadogLogs", "DatadogInternal", "DatadogTrace", "DatadogCrashReporting"].include?(t.name) }
datadog_sdk_target.build_configurations.each do |config|
config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] = '$(inherited) DD_SDK_COMPILED_FOR_TESTING'
end
Expand Down
4 changes: 2 additions & 2 deletions example/ios/PodfileForTests
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ target 'ddSdkReactnativeExample' do
# An absolute path to your application root.
:app_path => "#{Pod::Config.instance.installation_root}/.."
)

target 'ddSdkReactnativeExampleTests' do
inherit! :complete
# Pods for testing
Expand All @@ -59,7 +59,7 @@ target 'ddSdkReactnativeExample' do
)
__apply_Xcode_12_5_M1_post_install_workaround(installer)
# Enable `DD_SDK_COMPILED_FOR_TESTING` condition when compiling `DatadogSDK` dependency:
datadog_sdk_target = installer.pods_project.targets.detect {|t| t.name == "DatadogSDK" }
datadog_sdk_target = installer.pods_project.targets.detect {|t| ["DatadogCore", "DatadogRUM", "DatadogLogs", "DatadogInternal", "DatadogTrace", "DatadogCrashReporting"].include?(t.name) }
datadog_sdk_target.build_configurations.each do |config|
config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] = '$(inherited) DD_SDK_COMPILED_FOR_TESTING'
end
Expand Down
29 changes: 16 additions & 13 deletions packages/core/ios/Sources/DdSdkImplementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,26 @@ public class DdSdkImplementation: NSObject {
let consent = self.buildTrackingConsent(consent: sdkConfiguration.trackingConsent)
Datadog.initialize(with: sdkConfig, trackingConsent: consent)

let rumConfig = self.buildRUMConfiguration(configuration: sdkConfiguration)
RUM.enable(with: rumConfig)

Logs.enable(with: Logs.Configuration())

Trace.enable(with: Trace.Configuration())

if sdkConfiguration.nativeCrashReportEnabled ?? false {
CrashReporting.enable()
}

self.enableFeatures(sdkConfiguration: sdkConfiguration)
self.sendConfigurationAsTelemetry(rnConfiguration: sdkConfiguration)
self.startJSRefreshRateMonitoring(sdkConfiguration: sdkConfiguration)

resolve(nil)
}
}

func enableFeatures(sdkConfiguration: DdSdkConfiguration) {
let rumConfig = self.buildRUMConfiguration(configuration: sdkConfiguration)
RUM.enable(with: rumConfig)

Logs.enable(with: Logs.Configuration())

Trace.enable(with: Trace.Configuration())

if sdkConfiguration.nativeCrashReportEnabled ?? false {
CrashReporting.enable()
}
}

@objc
public func setAttributes(attributes: NSDictionary, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
Expand Down Expand Up @@ -176,7 +179,7 @@ public class DdSdkImplementation: NSObject {
uploadFrequency: configuration.uploadFrequency,
proxyConfiguration: buildProxyConfiguration(config: configuration.additionalConfig)
)
if var additionalConfiguration = configuration.additionalConfig as? [String: Any] {
if let additionalConfiguration = configuration.additionalConfig as? [String: Any] {
config._internal_mutation {
$0.additionalConfiguration = additionalConfiguration
}
Expand Down
121 changes: 90 additions & 31 deletions packages/core/ios/Tests/DdSdkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import XCTest
@testable import DatadogCore
@testable import DatadogRUM
@testable import DatadogInternal
@testable import DatadogLogs
@testable import DatadogTrace
@testable import DatadogCrashReporting
import DatadogLogs

final class DispatchQueueMock: DispatchQueueType {
Expand Down Expand Up @@ -146,6 +149,19 @@ internal class DdSdkTests: XCTestCase {

Datadog.internalFlushAndDeinitialize()
}

func testEnableAllFeatures() {
let core = MockDatadogCore()
CoreRegistry.register(default: core)
let configuration: DdSdkConfiguration = .mockAny()

DdSdkImplementation().enableFeatures(sdkConfiguration: configuration)

XCTAssertNotNil(core.features[RUMFeature.name])
XCTAssertNotNil(core.features[LogsFeature.name])
XCTAssertNotNil(core.features[TraceFeature.name])
CoreRegistry.unregisterDefault()
}

func testBuildConfigurationDefaultEndpoint() {
let configuration: DdSdkConfiguration = .mockAny()
Expand Down Expand Up @@ -255,32 +271,36 @@ internal class DdSdkTests: XCTestCase {
}

func testBuildConfigurationNoCrashReportByDefault() {
// TODO: rewrite this test
// let configuration: DdSdkConfiguration = .mockAny(nativeCrashReportEnabled: nil)
//
// let ddConfig = DdSdkImplementation().buildSDKConfiguration(configuration: configuration)
//
// XCTAssertNil(ddConfig.crashReportingPlugin)
let core = MockDatadogCore()
CoreRegistry.register(default: core)
let configuration: DdSdkConfiguration = .mockAny(nativeCrashReportEnabled: nil)

DdSdkImplementation().enableFeatures(sdkConfiguration: configuration)

XCTAssertNil(core.features[CrashReportingFeature.name])
CoreRegistry.unregisterDefault()
}

func testBuildConfigurationNoCrashReport() {
// TODO: rewrite this test
// let configuration: DdSdkConfiguration = .mockAny(nativeCrashReportEnabled: false)
//
// let ddConfig = DdSdkImplementation().buildSDKConfiguration(configuration: configuration)
//
// XCTAssertNil(ddConfig.crashReportingPlugin)
let core = MockDatadogCore()
CoreRegistry.register(default: core)
let configuration: DdSdkConfiguration = .mockAny(nativeCrashReportEnabled: false)

DdSdkImplementation().enableFeatures(sdkConfiguration: configuration)

XCTAssertNil(core.features[CrashReportingFeature.name])
CoreRegistry.unregisterDefault()
}

func testBuildConfigurationWithCrashReport() {
// TODO: rewrite this test
// let configuration: DdSdkConfiguration = .mockAny(
// nativeCrashReportEnabled: true
// )
//
// let ddConfig = DdSdkImplementation().buildSDKConfiguration(configuration: configuration)
//
// XCTAssertNotNil(ddConfig.crashReportingPlugin)
let core = MockDatadogCore()
CoreRegistry.register(default: core)
let configuration: DdSdkConfiguration = .mockAny(nativeCrashReportEnabled: true)

DdSdkImplementation().enableFeatures(sdkConfiguration: configuration)

XCTAssertNotNil(core.features[CrashReportingFeature.name])
CoreRegistry.unregisterDefault()
}

func testBuildConfigurationWithVersionSuffix() {
Expand Down Expand Up @@ -428,12 +448,17 @@ internal class DdSdkTests: XCTestCase {
]])

let ddConfig = DdSdkImplementation().buildRUMConfiguration(configuration: configuration)

var firstPartyHosts: FirstPartyHosts? = FirstPartyHosts(["example.com": [.datadog, .b3]])
firstPartyHosts += FirstPartyHosts(["datadog.com": [.b3multi, .tracecontext]])

// TODO: rewrite this test
// XCTAssertEqual(ddConfig.urlSessionTracking?.firstPartyHostsTracing, firstPartyHosts!)
let expectedFirstPartyHosts: [String: Set<TracingHeaderType>]? = ["example.com": [.datadog, .b3], "datadog.com": [.b3multi, .tracecontext]]
var actualFirstPartyHosts: [String: Set<TracingHeaderType>]?
switch ddConfig.urlSessionTracking?.firstPartyHostsTracing {
case .trace(_,_): break
case let .traceWithHeaders(hostsWithHeaders, _):
return actualFirstPartyHosts = hostsWithHeaders
case .none: break
}

XCTAssertEqual(actualFirstPartyHosts, expectedFirstPartyHosts)
}

func testBuildMalformedFirstPartyHosts() {
Expand All @@ -443,10 +468,16 @@ internal class DdSdkTests: XCTestCase {

let ddConfig = DdSdkImplementation().buildRUMConfiguration(configuration: configuration)

let firstPartyHosts: FirstPartyHosts? = FirstPartyHosts(["example.com": [.b3]])
let expectedFirstPartyHosts: [String: Set<TracingHeaderType>]? = ["example.com": [.b3]]
var actualFirstPartyHosts: [String: Set<TracingHeaderType>]?
switch ddConfig.urlSessionTracking?.firstPartyHostsTracing {
case .trace(_,_): break
case let .traceWithHeaders(hostsWithHeaders, _):
return actualFirstPartyHosts = hostsWithHeaders
case .none: break
}

// TODO: rewrite this test
// XCTAssertEqual(ddConfig.firstPartyHosts, firstPartyHosts)
XCTAssertEqual(actualFirstPartyHosts, expectedFirstPartyHosts)
}

func testBuildFirstPartyHostsWithDuplicatedMatchKey() {
Expand All @@ -457,10 +488,16 @@ internal class DdSdkTests: XCTestCase {

let ddConfig = DdSdkImplementation().buildRUMConfiguration(configuration: configuration)

var firstPartyHosts: FirstPartyHosts? = FirstPartyHosts(["example.com": [.b3, .tracecontext]])
let expectedFirstPartyHosts: [String: Set<TracingHeaderType>]? = ["example.com": [.b3, .tracecontext]]
var actualFirstPartyHosts: [String: Set<TracingHeaderType>]?
switch ddConfig.urlSessionTracking?.firstPartyHostsTracing {
case .trace(_,_): break
case let .traceWithHeaders(hostsWithHeaders, _):
return actualFirstPartyHosts = hostsWithHeaders
case .none: break
}

// TODO: rewrite this test
// XCTAssertEqual(ddConfig.firstPartyHosts, firstPartyHosts)
XCTAssertEqual(actualFirstPartyHosts, expectedFirstPartyHosts)
}

func testBuildTelemetrySampleRate() {
Expand Down Expand Up @@ -893,3 +930,25 @@ extension NSDictionary {
)
}
}

internal class MockDatadogCore: DatadogCoreProtocol {
func send(message: DatadogInternal.FeatureMessage, else fallback: @escaping () -> Void) {}

private(set) var features: [String: DatadogFeature] = [:]

func register<T>(feature: T) throws where T : DatadogFeature {
features[T.name] = feature
}

func get<T>(feature type: T.Type) -> T? where T : DatadogFeature {
return nil
}

func scope(for feature: String) -> FeatureScope? {
return nil
}

func set(feature: String, attributes: @escaping () -> FeatureBaggage) {}

func update(feature: String, attributes: @escaping () -> FeatureBaggage) {}
}

0 comments on commit 9b5498c

Please sign in to comment.