public async Task WhenInstanceLacksPublicIp_ThenPublicAddressReturnsNull(
            [LinuxInstance(PublicIp = false)] ResourceTask <InstanceLocator> testInstance,
            [Credential(Role = PredefinedRole.ComputeViewer)] ResourceTask <ICredential> credential)
        {
            var adapter  = new ComputeEngineAdapter(await credential);
            var instance = await adapter.GetInstanceAsync(
                await testInstance,
                CancellationToken.None);

            Assert.IsNull(instance.PublicAddress());
        }
        public async Task WhenUserNotInRole_ThenGetInstanceAsyncThrowsResourceAccessDeniedException(
            [LinuxInstance] ResourceTask <InstanceLocator> testInstance,
            [Credential(Role = PredefinedRole.IapTunnelUser)] ResourceTask <ICredential> credential)
        {
            var locator = await testInstance;
            var adapter = new ComputeEngineAdapter(await credential);

            AssertEx.ThrowsAggregateException <ResourceAccessDeniedException>(
                () => adapter.GetInstanceAsync(
                    locator,
                    CancellationToken.None).Wait());
        }
        public async Task WhenConnected_ThenTryActivateReturnsTrue(
            [LinuxInstance] ResourceTask <InstanceLocator> testInstance,
            [Credential(Role = PredefinedRole.ComputeInstanceAdminV1)] ResourceTask <ICredential> credential)
        {
            var locator = await testInstance;

            using (var key = new RsaSshKey(new RSACng()))
                using (var gceAdapter = new ComputeEngineAdapter(
                           this.serviceProvider.GetService <IAuthorizationAdapter>()))
                    using (var keyAdapter = new AuthorizedKeyService(
                               this.serviceProvider.GetService <IAuthorizationAdapter>(),
                               new ComputeEngineAdapter(await credential),
                               new ResourceManagerAdapter(await credential),
                               new Mock <IOsLoginService>().Object))
                    {
                        var authorizedKey = await keyAdapter.AuthorizeKeyAsync(
                            locator,
                            key,
                            TimeSpan.FromMinutes(10),
                            null,
                            AuthorizeKeyMethods.InstanceMetadata,
                            CancellationToken.None)
                                            .ConfigureAwait(true);

                        var instance = await gceAdapter.GetInstanceAsync(
                            locator,
                            CancellationToken.None)
                                       .ConfigureAwait(true);

                        // Connect
                        var broker = new SshTerminalSessionBroker(this.serviceProvider);

                        ISshTerminalSession session = null;
                        await AssertRaisesEventAsync <SessionStartedEvent>(
                            async() => session = await broker.ConnectAsync(
                                locator,
                                new IPEndPoint(instance.PublicAddress(), 22),
                                authorizedKey,
                                null,
                                TimeSpan.FromSeconds(10)));

                        Assert.IsNull(this.ExceptionShown);

                        Assert.AreSame(session, SshTerminalPane.TryGetActivePane(this.mainForm));
                        Assert.AreSame(session, SshTerminalPane.TryGetExistingPane(this.mainForm, locator));
                        Assert.IsTrue(broker.IsConnected(locator));
                        Assert.IsTrue(broker.TryActivate(locator));

                        AssertRaisesEvent <SessionEndedEvent>(
                            () => session.Close());
                    }
        }
        public async Task WhenInstanceHasPublicIp_ThenPublicAddressReturnsNonRfc1918Ip(
            [LinuxInstance] ResourceTask <InstanceLocator> testInstance,
            [Credential(Role = PredefinedRole.ComputeViewer)] ResourceTask <ICredential> credential)
        {
            var adapter  = new ComputeEngineAdapter(await credential);
            var instance = await adapter.GetInstanceAsync(
                await testInstance,
                CancellationToken.None);

            Assert.IsNotNull(instance.PublicAddress());
            CollectionAssert.DoesNotContain(
                new byte[] { 172, 192, 10 },
                instance.PublicAddress().GetAddressBytes()[0],
                "Is not a RFC1918 address");
        }
        public async Task WhenUserNotInRole_ThenGetInstanceAsyncThrowsResourceAccessDeniedException(
            [LinuxInstance] InstanceRequest testInstance,
            [Credential(Role = PredefinedRole.IapTunnelUser)] CredentialRequest credential)
        {
            await testInstance.AwaitReady();

            var instanceRef = await testInstance.GetInstanceAsync();

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

            AssertEx.ThrowsAggregateException <ResourceAccessDeniedException>(
                () => adapter.GetInstanceAsync(
                    testInstance.Locator,
                    CancellationToken.None).Wait());
        }
        public async Task WhenInstancePopulated_ThenGetInstanceLocatorSucceeds(
            [LinuxInstance] ResourceTask <InstanceLocator> testInstance,
            [Credential(Role = PredefinedRole.ComputeViewer)] ResourceTask <ICredential> credential)
        {
            var adapter  = new ComputeEngineAdapter(await credential);
            var instance = await adapter.GetInstanceAsync(await testInstance);

            var zoneLocator     = instance.GetZoneLocator();
            var instanceLocator = instance.GetInstanceLocator();

            Assert.AreEqual(TestProject.Zone, zoneLocator.Name);
            Assert.AreEqual(TestProject.Zone, instanceLocator.Zone);

            Assert.AreEqual(await testInstance, instanceLocator);
        }