bot_detector_test.dart 6.68 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:file/memory.dart';
6
import 'package:flutter_tools/src/base/bot_detector.dart';
7
import 'package:flutter_tools/src/base/io.dart';
8
import 'package:flutter_tools/src/base/logger.dart';
9
import 'package:flutter_tools/src/base/platform.dart';
10
import 'package:flutter_tools/src/persistent_tool_state.dart';
11

12
import '../../src/common.dart';
13
import '../../src/fake_http_client.dart';
14
import '../../src/fakes.dart';
15

16 17
final Uri azureUrl = Uri.parse('http://169.254.169.254/metadata/instance');

18 19
void main() {
  group('BotDetector', () {
20 21 22
    late FakePlatform fakePlatform;
    late FakeStdio fakeStdio;
    late PersistentToolState persistentToolState;
23 24 25

    setUp(() {
      fakePlatform = FakePlatform()..environment = <String, String>{};
26
      fakeStdio = FakeStdio();
27 28 29 30
      persistentToolState = PersistentToolState.test(
        directory: MemoryFileSystem.test().currentDirectory,
        logger: BufferLogger.test(),
      );
31 32 33
    });

    group('isRunningOnBot', () {
34
      testWithoutContext('returns false unconditionally if BOT=false is set', () async {
35 36
        fakePlatform.environment['BOT'] = 'false';
        fakePlatform.environment['TRAVIS'] = 'true';
37

38 39 40 41 42 43
        final BotDetector botDetector = BotDetector(
          platform: fakePlatform,
          httpClientFactory: () => FakeHttpClient.any(),
          persistentToolState: persistentToolState,
        );

44
        expect(await botDetector.isRunningOnBot, isFalse);
45
        expect(persistentToolState.isRunningOnBot, isFalse);
46 47
      });

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
      testWithoutContext('does not cache BOT environment variable', () async {
        fakePlatform.environment['BOT'] = 'true';

        final BotDetector botDetector = BotDetector(
          platform: fakePlatform,
          httpClientFactory: () => FakeHttpClient.any(),
          persistentToolState: persistentToolState,
        );

        expect(await botDetector.isRunningOnBot, isTrue);
        expect(persistentToolState.isRunningOnBot, isTrue);

        fakePlatform.environment['BOT'] = 'false';

        expect(await botDetector.isRunningOnBot, isFalse);
        expect(persistentToolState.isRunningOnBot, isFalse);
      });

66
      testWithoutContext('returns false unconditionally if FLUTTER_HOST is set', () async {
67 68
        fakePlatform.environment['FLUTTER_HOST'] = 'foo';
        fakePlatform.environment['TRAVIS'] = 'true';
69

70 71 72 73 74 75
        final BotDetector botDetector = BotDetector(
          platform: fakePlatform,
          httpClientFactory: () => FakeHttpClient.any(),
          persistentToolState: persistentToolState,
        );

76
        expect(await botDetector.isRunningOnBot, isFalse);
77
        expect(persistentToolState.isRunningOnBot, isFalse);
78 79
      });

80
      testWithoutContext('returns false with and without a terminal attached', () async {
81 82 83 84 85 86 87 88
        final BotDetector botDetector = BotDetector(
          platform: fakePlatform,
          httpClientFactory: () => FakeHttpClient.list(<FakeRequest>[
            FakeRequest(azureUrl, responseError: const SocketException('HTTP connection timed out')),
          ]),
          persistentToolState: persistentToolState,
        );

89
        fakeStdio.stdout.hasTerminal = true;
90
        expect(await botDetector.isRunningOnBot, isFalse);
91
        fakeStdio.stdout.hasTerminal = false;
92 93 94 95
        expect(await botDetector.isRunningOnBot, isFalse);
        expect(persistentToolState.isRunningOnBot, isFalse);
      });

96
      testWithoutContext('can test analytics outputs on bots when outputting to a file', () async {
97 98
        fakePlatform.environment['TRAVIS'] = 'true';
        fakePlatform.environment['FLUTTER_ANALYTICS_LOG_FILE'] = '/some/file';
99 100 101 102 103 104 105

        final BotDetector botDetector = BotDetector(
          platform: fakePlatform,
          httpClientFactory: () => FakeHttpClient.any(),
          persistentToolState: persistentToolState,
        );

106
        expect(await botDetector.isRunningOnBot, isFalse);
107
        expect(persistentToolState.isRunningOnBot, isFalse);
108
      });
109 110

      testWithoutContext('returns true when azure metadata is reachable', () async {
111 112 113 114 115
        final BotDetector botDetector = BotDetector(
          platform: fakePlatform,
          httpClientFactory: () => FakeHttpClient.any(),
          persistentToolState: persistentToolState,
        );
116

117
        expect(await botDetector.isRunningOnBot, isTrue);
118 119 120 121
        expect(persistentToolState.isRunningOnBot, isTrue);
      });

      testWithoutContext('caches azure bot detection results across instances', () async {
122 123 124 125 126
        final BotDetector botDetector = BotDetector(
          platform: fakePlatform,
          httpClientFactory: () => FakeHttpClient.any(),
          persistentToolState: persistentToolState,
        );
127 128 129 130

        expect(await botDetector.isRunningOnBot, isTrue);
        expect(await BotDetector(
          platform: fakePlatform,
131
          httpClientFactory: () => FakeHttpClient.list(<FakeRequest>[]),
132 133
          persistentToolState: persistentToolState,
        ).isRunningOnBot, isTrue);
134
      });
135 136 137

      testWithoutContext('returns true when running on borg', () async {
        fakePlatform.environment['BORG_ALLOC_DIR'] = 'true';
138

139 140 141 142 143 144
        final BotDetector botDetector = BotDetector(
          platform: fakePlatform,
          httpClientFactory: () => FakeHttpClient.any(),
          persistentToolState: persistentToolState,
        );

145
        expect(await botDetector.isRunningOnBot, isTrue);
146
        expect(persistentToolState.isRunningOnBot, isTrue);
147
      });
148 149 150 151 152
    });
  });

  group('AzureDetector', () {
    testWithoutContext('isRunningOnAzure returns false when connection times out', () async {
153 154 155 156 157
      final AzureDetector azureDetector = AzureDetector(
        httpClientFactory: () => FakeHttpClient.list(<FakeRequest>[
          FakeRequest(azureUrl, responseError: const SocketException('HTTP connection timed out')),
        ],
      ));
158

159 160 161
      expect(await azureDetector.isRunningOnAzure, isFalse);
    });

162
    testWithoutContext('isRunningOnAzure returns false when OsError is thrown', () async {
163 164 165 166 167
      final AzureDetector azureDetector = AzureDetector(
        httpClientFactory: () => FakeHttpClient.list(<FakeRequest>[
          FakeRequest(azureUrl, responseError: const OSError('Connection Refused', 111)),
        ],
      ));
168 169 170 171

      expect(await azureDetector.isRunningOnAzure, isFalse);
    });

172
    testWithoutContext('isRunningOnAzure returns true when azure metadata is reachable', () async {
173 174 175 176 177
      final AzureDetector azureDetector = AzureDetector(
        httpClientFactory: () => FakeHttpClient.list(<FakeRequest>[
          FakeRequest(azureUrl),
        ],
      ));
178

179
      expect(await azureDetector.isRunningOnAzure, isTrue);
180 181 182
    });
  });
}