| // Copyright (c) 2019 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| import 'package:fixnum/fixnum.dart'; |
| import 'package:flutter/material.dart'; |
| import 'package:flutter_test/flutter_test.dart'; |
| |
| import 'package:cocoon_service/protos.dart' show Agent; |
| |
| import 'package:app_flutter/agent_health_details.dart'; |
| |
| void main() { |
| group('AgentHealthDetails', () { |
| test('is healthy when everything is healthy', () { |
| final Agent agent = Agent() |
| ..healthCheckTimestamp = |
| Int64.parseInt(DateTime.now().millisecondsSinceEpoch.toString()) |
| ..isHealthy = true |
| ..healthDetails = ''' |
| ssh-connectivity: succeeded |
| Last known IP address: 192.168.1.29 |
| |
| android-device-ZY223D6B7B: succeeded |
| has-healthy-devices: succeeded |
| Found 1 healthy devices |
| |
| cocoon-authentication: succeeded |
| cocoon-connection: succeeded |
| able-to-perform-health-check: succeeded'''; |
| |
| final AgentHealthDetails healthDetails = AgentHealthDetails(agent); |
| |
| expect(healthDetails.canPerformHealthCheck, isTrue); |
| expect(healthDetails.cocoonAuthentication, isTrue); |
| expect(healthDetails.cocoonConnection, isTrue); |
| expect(healthDetails.hasHealthyDevices, isTrue); |
| expect(healthDetails.hasSshConnectivity, isTrue); |
| expect(healthDetails.pingedRecently, isTrue); |
| expect(healthDetails.isHealthy, isTrue); |
| }); |
| |
| test('is not healthy when just one metric is unhealthy', () { |
| final Agent agent = Agent() |
| ..healthCheckTimestamp = |
| Int64.parseInt(DateTime.now().millisecondsSinceEpoch.toString()) |
| ..isHealthy = false |
| ..healthDetails = ''' |
| ssh-connectivity: succeeded |
| Last known IP address: 192.168.1.226 |
| |
| ios-device-b4873f14ffb248dfcb00c8cb7fdd4cdcff87f1f0: succeeded |
| |
| cocoon-authentication: succeeded |
| cocoon-connection: succeeded |
| ios: failed |
| ERROR: Command "/Users/flutter/.cocoon/flutter/bin/flutter build ios" failed with exit code 1. |
| #0 fail (package:cocoon_agent/src/utils.dart:140:3) |
| #1 exec (package:cocoon_agent/src/utils.dart:341:5) |
| <asynchronous suspension> |
| #2 flutter (package:cocoon_agent/src/utils.dart:369:10) |
| #3 performHealthChecks.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:cocoon_agent/src/health.dart:58:34) |
| <asynchronous suspension> |
| #4 inDirectory (package:cocoon_agent/src/utils.dart:382:24) |
| <asynchronous suspension> |
| #5 performHealthChecks.<anonymous closure>.<anonymous closure> (package:cocoon_agent/src/health.dart:57:17) |
| <asynchronous suspension> |
| #6 _captureErrors (package:cocoon_agent/src/health.dart:90:47) |
| <asynchronous suspension> |
| #7 performHealthChecks.<anonymous closure> (package:cocoon_agent/src/health.dart:47:30) |
| <asynchronous suspension> |
| #8 _captureErrors (package:cocoon_agent/src/health.dart:90:47) |
| <asynchronous suspension> |
| #9 performHealthChecks (package:cocoon_agent/src/health.dart:20:51) |
| <asynchronous suspension> |
| #10 ContinuousIntegrationCommand.run.<anonymous closure> (package:cocoon_agent/src/commands/ci.dart:75:26) |
| <asynchronous suspension> |
| #11 _rootRun (dart:async/zone.dart:1124:13) |
| #12 _CustomZone.run (dart:async/zone.dart:1021:19) |
| #13 _runZoned (dart:async/zone.dart:1516:10) |
| #14 runZoned (dart:async/zone.dart:1500:12) |
| #15 ContinuousIntegrationCommand.run (package:cocoon_agent/src/commands/ci.dart:63:13) |
| <asynchronous suspension> |
| #16 main (file:///Users/flutter/cocoon/agent/bin/agent.dart:63:19) |
| <asynchronous suspension> |
| #17 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:303:32) |
| #18 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12) |
| |
| able-to-perform-health-check: succeeded'''; |
| |
| final AgentHealthDetails healthDetails = AgentHealthDetails(agent); |
| |
| expect(healthDetails.canPerformHealthCheck, isTrue); |
| expect(healthDetails.cocoonAuthentication, isTrue); |
| expect(healthDetails.cocoonConnection, isTrue); |
| expect(healthDetails.hasHealthyDevices, isFalse); |
| expect(healthDetails.hasSshConnectivity, isTrue); |
| expect(healthDetails.pingedRecently, isTrue); |
| expect(healthDetails.isHealthy, isFalse); |
| }); |
| |
| test('is not healthy when all metrics are healthy but has timed out', () { |
| final Agent agent = Agent() |
| ..healthCheckTimestamp = Int64.parseInt('10000') |
| ..isHealthy = true |
| ..healthDetails = ''' |
| ssh-connectivity: succeeded |
| Last known IP address: 192.168.1.29 |
| |
| android-device-ZY223D6B7B: succeeded |
| has-healthy-devices: succeeded |
| Found 1 healthy devices |
| |
| cocoon-authentication: succeeded |
| cocoon-connection: succeeded |
| able-to-perform-health-check: succeeded'''; |
| |
| final AgentHealthDetails healthDetails = AgentHealthDetails(agent); |
| |
| expect(healthDetails.canPerformHealthCheck, isTrue); |
| expect(healthDetails.cocoonAuthentication, isTrue); |
| expect(healthDetails.cocoonConnection, isTrue); |
| expect(healthDetails.hasHealthyDevices, isTrue); |
| expect(healthDetails.hasSshConnectivity, isTrue); |
| expect(healthDetails.pingedRecently, isFalse); |
| expect(healthDetails.isHealthy, isFalse); |
| }); |
| |
| test('is unhealthy when health details is null', () { |
| final Agent agent = Agent() |
| ..healthCheckTimestamp = Int64.parseInt('10000') |
| ..isHealthy = false; |
| |
| final AgentHealthDetails healthDetails = AgentHealthDetails(agent); |
| |
| expect(healthDetails.canPerformHealthCheck, isFalse); |
| expect(healthDetails.cocoonAuthentication, isFalse); |
| expect(healthDetails.cocoonConnection, isFalse); |
| expect(healthDetails.hasHealthyDevices, isFalse); |
| expect(healthDetails.hasSshConnectivity, isFalse); |
| expect(healthDetails.pingedRecently, isFalse); |
| expect(healthDetails.isHealthy, isFalse); |
| }); |
| }); |
| |
| group('AgentHealthDetailsBar', () { |
| testWidgets('healthy bar', (WidgetTester tester) async { |
| final Agent agent = Agent() |
| ..healthCheckTimestamp = |
| Int64.parseInt(DateTime.now().millisecondsSinceEpoch.toString()) |
| ..isHealthy = true |
| ..healthDetails = ''' |
| ssh-connectivity: succeeded |
| Last known IP address: 192.168.1.29 |
| |
| android-device-ZY223D6B7B: succeeded |
| has-healthy-devices: succeeded |
| Found 1 healthy devices |
| |
| cocoon-authentication: succeeded |
| cocoon-connection: succeeded |
| able-to-perform-health-check: succeeded'''; |
| |
| final AgentHealthDetails healthDetails = AgentHealthDetails(agent); |
| |
| await tester.pumpWidget( |
| MaterialApp( |
| home: AgentHealthDetailsBar(healthDetails), |
| ), |
| ); |
| |
| expect(find.byIcon(Icons.timer), findsNothing); |
| expect(find.byIcon(Icons.verified_user), findsOneWidget); |
| expect(find.byIcon(Icons.network_wifi), findsOneWidget); |
| expect(find.byIcon(Icons.devices), findsOneWidget); |
| }); |
| |
| testWidgets('timed out icon', (WidgetTester tester) async { |
| final Agent agent = Agent() |
| ..healthCheckTimestamp = Int64.parseInt('100') |
| ..isHealthy = true |
| ..healthDetails = ''' |
| ssh-connectivity: succeeded |
| Last known IP address: 192.168.1.29 |
| |
| android-device-ZY223D6B7B: succeeded |
| has-healthy-devices: succeeded |
| Found 1 healthy devices |
| |
| cocoon-authentication: succeeded |
| cocoon-connection: succeeded |
| able-to-perform-health-check: succeeded'''; |
| |
| final AgentHealthDetails healthDetails = AgentHealthDetails(agent); |
| |
| await tester.pumpWidget( |
| MaterialApp( |
| home: AgentHealthDetailsBar(healthDetails), |
| ), |
| ); |
| |
| expect(find.byIcon(Icons.timer), findsOneWidget); |
| expect(find.byIcon(Icons.verified_user), findsOneWidget); |
| expect(find.byIcon(Icons.network_wifi), findsOneWidget); |
| expect(find.byIcon(Icons.devices), findsOneWidget); |
| }); |
| |
| testWidgets('unhealthy bar', (WidgetTester tester) async { |
| final Agent agent = Agent() |
| ..healthCheckTimestamp = Int64.parseInt('100') |
| ..isHealthy = false |
| ..healthDetails = ''; |
| |
| final AgentHealthDetails healthDetails = AgentHealthDetails(agent); |
| |
| await tester.pumpWidget( |
| MaterialApp( |
| home: AgentHealthDetailsBar(healthDetails), |
| ), |
| ); |
| |
| expect(find.byIcon(Icons.timer), findsOneWidget); |
| expect(find.byIcon(Icons.verified_user), findsNothing); |
| expect(find.byIcon(Icons.network_wifi), findsNothing); |
| expect(find.byIcon(Icons.devices), findsNothing); |
| }); |
| }); |
| } |