public async Task WhenUsingInvalidProjectId_ThenListEventsAsyncThrowsException(
            [Credential(Role = PredefinedRole.LogsViewer)] CredentialRequest credential)
        {
            var startDate = DateTime.UtcNow.AddDays(-30);
            var request   = new ListLogEntriesRequest()
            {
                ResourceNames = new[]
                {
                    "projects/invalid"
                },
                Filter = $"resource.type=\"gce_instance\" " +
                         $"AND protoPayload.methodName:{InsertInstanceEvent.Method} " +
                         $"AND timestamp > {startDate:yyyy-MM-dd}",
                PageSize = 1000,
                OrderBy  = "timestamp desc"
            };

            var adapter = new AuditLogAdapter(await credential.GetCredentialAsync());

            AssertEx.ThrowsAggregateException <GoogleApiException>(
                () => adapter.ListEventsAsync(
                    request,
                    _ => { },
                    new Apis.Util.ExponentialBackOff(),
                    CancellationToken.None).Wait());
        }
        public async Task WhenInstanceCreated_ThenListLogEntriesReturnsInsertEvent(
            [LinuxInstance] InstanceRequest testInstance,
            [Credential(Role = PredefinedRole.LogsViewer)] CredentialRequest credential)
        {
            await testInstance.AwaitReady();

            var instanceRef = await testInstance.GetInstanceAsync();

            var startDate = DateTime.UtcNow.AddDays(-30);
            var endDate   = DateTime.UtcNow;

            var adapter = new AuditLogAdapter(await credential.GetCredentialAsync());
            var request = new ListLogEntriesRequest()
            {
                ResourceNames = new[]
                {
                    "projects/" + TestProject.ProjectId
                },
                Filter = $"resource.type=\"gce_instance\" " +
                         $"AND protoPayload.methodName:{InsertInstanceEvent.Method} " +
                         $"AND timestamp > {startDate:yyyy-MM-dd}",
                PageSize = 1000,
                OrderBy  = "timestamp desc"
            };

            var events          = new List <EventBase>();
            var instanceBuilder = new InstanceSetHistoryBuilder(startDate, endDate);

            // Creating the VM might be quicker than the logs become available.
            for (int retry = 0; retry < 4 && !events.Any(); retry++)
            {
                await adapter.ListEventsAsync(
                    request,
                    events.Add,
                    new Apis.Util.ExponentialBackOff(),
                    CancellationToken.None);

                if (!events.Any())
                {
                    await Task.Delay(20 * 1000);
                }
            }

            var insertEvent = events.OfType <InsertInstanceEvent>()
                              .First(e => e.InstanceReference == instanceRef);

            Assert.IsNotNull(insertEvent);
        }