Skip to content

Commit

Permalink
Fix the MTRDeviceTests. generate a log file and update darwin.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
nivi-apple committed Nov 2, 2023
1 parent 426d697 commit 69f3dd9
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/darwin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ jobs:
# target versions instead?
run: |
mkdir -p /tmp/darwin/framework-tests
../../../out/debug/chip-all-clusters-app --interface-id -1 > >(tee /tmp/darwin/framework-tests/all-cluster-app.log) 2> >(tee /tmp/darwin/framework-tests/all-cluster-app-err.log >&2) &
../../../out/debug/chip-all-clusters-app --interface-id -1 --end_user_support_log /tmp/endusersupportlog.txt --network_diagnostics_log /tmp/networkdiagnosticslog.txt --crash_log /tmp/crashlog.txt > >(tee /tmp/darwin/framework-tests/all-cluster-app.log) 2> >(tee /tmp/darwin/framework-tests/all-cluster-app-err.log >&2) &
../../../out/debug/chip-all-clusters-app --interface-id -1 --dac_provider ../../../credentials/development/commissioner_dut/struct_cd_origin_pid_vid_correct/test_case_vector.json --product-id 32768 --discriminator 3839 --secured-device-port 5539 --KVS /tmp/chip-all-clusters-app-kvs2 > >(tee /tmp/darwin/framework-tests/all-cluster-app-origin-vid.log) 2> >(tee /tmp/darwin/framework-tests/all-cluster-app-origin-vid-err.log >&2) &
# Disable BLE because the app does not have the permission to use
# it and that may crash the CI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include <lib/support/ScopedBuffer.h>
#include <messaging/ExchangeContext.h>

#include <fstream>

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters::DiagnosticLogs;
Expand Down
4 changes: 2 additions & 2 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1393,8 +1393,8 @@ - (NSURL * _Nullable)_temporaryFileURLForDownload:(MTRDiagnosticLogType)type

- (bool)_isErrorResponse:(MTRDiagnosticLogsClusterRetrieveLogsResponseParams * _Nullable)response
{
// TODO: fix the comparision with kNoLogs and kExhausted
return (response == nil || (response.status != nil && [response.status intValue] != 0 && [response.status intValue] != 1) || response.logContent.length == 0);
chip::app::Clusters::DiagnosticLogs::StatusEnum statusValue = static_cast<chip::app::Clusters::DiagnosticLogs::StatusEnum>(response.status.intValue);
return (response == nil || (response.status != nil && statusValue != chip::app::Clusters::DiagnosticLogs::StatusEnum::kNoLogs && statusValue != chip::app::Clusters::DiagnosticLogs::StatusEnum::kExhausted) || response.logContent.length == 0);
}

- (void)_invokeCompletion:(void (^)(NSURL * _Nullable logResult, NSError * error))completion
Expand Down
11 changes: 5 additions & 6 deletions src/darwin/Framework/CHIP/MTRDiagnosticLogsTransferHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@
mFileHandle = [NSFileHandle fileHandleForWritingToURL:mFileURL error:&error];

if (mFileHandle == nil || error != nil) {
// TODO: Map NSError to BDX error
CHIP_ERROR error = CHIP_ERROR_INCORRECT_STATE;
LogErrorOnFailure(mTransfer.AbortTransfer(GetBdxStatusCodeFromChipError(error)));
return error;
CHIP_ERROR err = [MTRError errorToCHIPErrorCode:error];
LogErrorOnFailure(mTransfer.AbortTransfer(GetBdxStatusCodeFromChipError(err)));
return err;
}

TransferSession::TransferAcceptData acceptData;
Expand Down Expand Up @@ -118,8 +117,8 @@
[mFileHandle writeData:AsData(blockData) error:&error];

if (error != nil) {
// TODO: Map NSError to BDX error
return mTransfer.AbortTransfer(GetBdxStatusCodeFromChipError(CHIP_ERROR_INCORRECT_STATE));
CHIP_ERROR err = [MTRError errorToCHIPErrorCode:error];
return mTransfer.AbortTransfer(GetBdxStatusCodeFromChipError(err));
}
} else {
return mTransfer.AbortTransfer(GetBdxStatusCodeFromChipError(CHIP_ERROR_INCORRECT_STATE));
Expand Down
233 changes: 151 additions & 82 deletions src/darwin/Framework/CHIPTests/MTRDeviceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#import <XCTest/XCTest.h>

static const uint16_t kPairingTimeoutInSeconds = 10;
static const uint16_t kDownloadLogTimeoutInSeconds = 70;
static const uint16_t kDownloadLogTimeoutInSeconds = 120;
static const uint16_t kTimeoutInSeconds = 3;
static const uint64_t kDeviceId = 0x12344321;
static NSString * kOnboardingPayload = @"MT:-24J0AFN00KA0648G00";
Expand Down Expand Up @@ -2623,7 +2623,155 @@ - (void)test028_TimeZoneAndDST
#endif // MTR_ENABLE_PROVISIONAL
}

- (void)test900_SubscribeAllAttributes
/**
* Given a path relative to the Matter root, create an absolute path to the file.
*/
- (NSString *)absolutePathFor:(NSString *)matterRootRelativePath
{
// Find the right absolute path to our file. PWD should
// point to our src/darwin/Framework.
NSString * pwd = [[NSProcessInfo processInfo] environment][@"PWD"];
NSMutableArray * pathComponents = [[NSMutableArray alloc] init];
[pathComponents addObject:[pwd substringToIndex:(pwd.length - @"src/darwin/Framework".length)]];
[pathComponents addObjectsFromArray:[matterRootRelativePath pathComponents]];
return [NSString pathWithComponents:pathComponents];
}

/**
* Create a task given a path relative to the Matter root.
*/
- (NSTask *)createTaskForPath:(NSString *)path
{
NSTask * task = [[NSTask alloc] init];
[task setLaunchPath:[self absolutePathFor:path]];
return task;
}

- (NSError *)generateLogFile:(NSString *)outFile
{
NSTask * appTask = [self createTaskForPath:@"out/debug/all-clusters-app/chip-all-clusters-app"];

// Remove the file if one exists
[[NSFileManager defaultManager] removeItemAtPath:outFile error:nil];

// Create the file
[[NSFileManager defaultManager] createFileAtPath:outFile contents:nil attributes:nil];

NSFileHandle * handle = [NSFileHandle fileHandleForUpdatingAtPath:outFile];
appTask.standardOutput = handle;
NSError * error = nil;

[appTask launchAndReturnError:&error];
return error;
}

- (void)test029_DownloadEndUserSupportLog_NoTimeout
{
MTRDeviceController * controller = sController;
XCTAssertNotNil(controller);
XCTestExpectation * expectation = [self expectationWithDescription:@"End User Support Log Transfer Complete"];

dispatch_queue_t queue = dispatch_get_main_queue();

dispatch_async(queue, ^{
MTRDevice * device = [MTRDevice deviceWithNodeID:@(kDeviceId) controller:controller];
XCTAssertNotNil(device);

NSString * outFile = [NSString stringWithFormat:@"/tmp/endusersupportlog.txt"];
NSError * error = [self generateLogFile:outFile];

if (error != nil) {
NSLog(@"Failed to generate log file");
return;
}

[device downloadLogOfType:MTRDiagnosticLogTypeEndUserSupport timeout:0 queue:queue completion:^(NSURL * _Nullable logResult, NSError * error) {
XCTAssertNil(error);
XCTAssertNotNil(logResult);

NSError * attributesError = nil;
NSDictionary * fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[logResult path] error:&attributesError];

size_t fileSize = [fileAttributes fileSize];
XCTAssertTrue(fileSize > 0);
NSLog(@"test901_DownloadEndUserSupportLog_NoTimeout expectation fulfill");
[expectation fulfill];
}];
});
[self waitForExpectations:[NSArray arrayWithObject:expectation] timeout:kDownloadLogTimeoutInSeconds];
}

- (void)test030_DownloadNetworkDiagnosticsLog_NoTimeout
{
MTRDeviceController * controller = sController;
XCTAssertNotNil(controller);
XCTestExpectation * expectation = [self expectationWithDescription:@"Network Diagnostics Log Transfer Complete"];

dispatch_queue_t queue = dispatch_get_main_queue();

dispatch_async(queue, ^{
MTRDevice * device = [MTRDevice deviceWithNodeID:@(kDeviceId) controller:controller];
XCTAssertNotNil(device);

NSString * outFile = [NSString stringWithFormat:@"/tmp/networkdiagnosticslog.txt"];
NSError * error = [self generateLogFile:outFile];

if (error != nil) {
NSLog(@"Failed to generate log file");
return;
}

[device downloadLogOfType:MTRDiagnosticLogTypeNetworkDiagnostics timeout:0 queue:queue completion:^(NSURL * _Nullable logResult, NSError * error) {
XCTAssertNil(error);
XCTAssertNotNil(logResult);

NSError * attributesError = nil;
NSDictionary * fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[logResult path] error:&attributesError];

size_t fileSize = [fileAttributes fileSize];
XCTAssertTrue(fileSize > 0);
[expectation fulfill];
}];
});
[self waitForExpectations:[NSArray arrayWithObject:expectation] timeout:kDownloadLogTimeoutInSeconds];
}

- (void)test031_DownloadCrashLog_NoTimeout
{
MTRDeviceController * controller = sController;
XCTAssertNotNil(controller);
XCTestExpectation * expectation = [self expectationWithDescription:@"Crash Log Transfer Complete"];

dispatch_queue_t queue = dispatch_get_main_queue();

dispatch_async(queue, ^{
MTRDevice * device = [MTRDevice deviceWithNodeID:@(kDeviceId) controller:controller];
XCTAssertNotNil(device);

NSString * outFile = [NSString stringWithFormat:@"/tmp/crashlog.txt"];
NSError * error = [self generateLogFile:outFile];

if (error != nil) {
NSLog(@"Failed to generate log file");
return;
}

[device downloadLogOfType:MTRDiagnosticLogTypeCrash timeout:0 queue:queue completion:^(NSURL * _Nullable logResult, NSError * error) {
XCTAssertNil(error);
XCTAssertNotNil(logResult);

NSError * attributesError = nil;
NSDictionary * fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[logResult path] error:&attributesError];

size_t fileSize = [fileAttributes fileSize];
XCTAssertTrue(fileSize > 0);
[expectation fulfill];
}];
});
[self waitForExpectations:[NSArray arrayWithObject:expectation] timeout:kDownloadLogTimeoutInSeconds];
}

/*- (void)test900_SubscribeAllAttributes
{
MTRBaseDevice * device = GetConnectedDevice();
dispatch_queue_t queue = dispatch_get_main_queue();
Expand Down Expand Up @@ -2794,86 +2942,7 @@ - (void)test900_SubscribeAllAttributes
// Wait for report
[self waitForExpectations:[NSArray arrayWithObject:reportExpectation] timeout:kTimeoutInSeconds];
}

- (void)test901_DownloadEndUserSupportLog_NoTimeout
{
MTRDeviceController * controller = sController;
XCTAssertNotNil(controller);
XCTestExpectation * expectation = [self expectationWithDescription:@"End User Support Log Transfer Complete"];

dispatch_queue_t queue = dispatch_get_main_queue();

dispatch_async(queue, ^{
MTRDevice * device = [MTRDevice deviceWithNodeID:@(kDeviceId) controller:controller];
XCTAssertNotNil(device);
[device downloadLogOfType:MTRDiagnosticLogTypeEndUserSupport timeout:0 queue:queue completion:^(NSURL * _Nullable logResult, NSError * error) {
XCTAssertNil(error);
XCTAssertNotNil(logResult);

NSError * attributesError = nil;
NSDictionary * fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[logResult path] error:&attributesError];

size_t fileSize = [fileAttributes fileSize];
XCTAssertTrue(fileSize > 0);
NSLog(@"test901_DownloadEndUserSupportLog_NoTimeout expectation fulfill");
[expectation fulfill];
}];
});
[self waitForExpectations:[NSArray arrayWithObject:expectation] timeout:kDownloadLogTimeoutInSeconds];
}

- (void)test902_DownloadNetworkDiagnosticsLog_NoTimeout
{
MTRDeviceController * controller = sController;
XCTAssertNotNil(controller);
XCTestExpectation * expectation = [self expectationWithDescription:@"Network Diagnostics Log Transfer Complete"];

dispatch_queue_t queue = dispatch_get_main_queue();

dispatch_async(queue, ^{
MTRDevice * device = [MTRDevice deviceWithNodeID:@(kDeviceId) controller:controller];
XCTAssertNotNil(device);
[device downloadLogOfType:MTRDiagnosticLogTypeNetworkDiagnostics timeout:0 queue:queue completion:^(NSURL * _Nullable logResult, NSError * error) {
XCTAssertNil(error);
XCTAssertNotNil(logResult);

NSError * attributesError = nil;
NSDictionary * fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[logResult path] error:&attributesError];

size_t fileSize = [fileAttributes fileSize];
XCTAssertTrue(fileSize > 0);
[expectation fulfill];
}];
});
[self waitForExpectations:[NSArray arrayWithObject:expectation] timeout:kDownloadLogTimeoutInSeconds];
}

- (void)test903_DownloadCrashLog_NoTimeout
{
MTRDeviceController * controller = sController;
XCTAssertNotNil(controller);
XCTestExpectation * expectation = [self expectationWithDescription:@"Crash Log Transfer Complete"];

dispatch_queue_t queue = dispatch_get_main_queue();

dispatch_async(queue, ^{
MTRDevice * device = [MTRDevice deviceWithNodeID:@(kDeviceId) controller:controller];
XCTAssertNotNil(device);
[device downloadLogOfType:MTRDiagnosticLogTypeCrash timeout:0 queue:queue completion:^(NSURL * _Nullable logResult, NSError * error) {
XCTAssertNil(error);
XCTAssertNotNil(logResult);

NSError * attributesError = nil;
NSDictionary * fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[logResult path] error:&attributesError];

size_t fileSize = [fileAttributes fileSize];
XCTAssertTrue(fileSize > 0);
[expectation fulfill];
}];
});
[self waitForExpectations:[NSArray arrayWithObject:expectation] timeout:kDownloadLogTimeoutInSeconds];
}
}*/

- (void)test999_TearDown
{
Expand Down

0 comments on commit 69f3dd9

Please sign in to comment.