public static void Share_Throws_If_SqlLocalDbEception_Is_Thrown()
        {
            // Act
            var innerException = new SqlLocalDbException(
                "It broke",
                123,
                "Name");

            var mock = new Mock <ISqlLocalDbApi>();

            mock.Setup((p) => p.ShareInstance(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
            .Throws(innerException);

            ISqlLocalDbInstanceInfo instance = CreateInstance();
            ISqlLocalDbApi          api      = mock.Object;

            var target = new SqlLocalDbInstanceManager(instance, api);

            var exception = Assert.Throws <SqlLocalDbException>(() => target.Share("SharedName"));

            exception.ErrorCode.ShouldBe(123);
            exception.InstanceName.ShouldBe("Name");
            exception.Message.ShouldBe("Failed to share SQL LocalDB instance 'Name'.");
            exception.InnerException.ShouldBeSameAs(innerException);
        }
        public void Share_Throws_If_SharedName_Is_Null()
        {
            // Act
            ISqlLocalDbInstanceInfo instance = CreateInstance();
            var api    = Mock.Of <ISqlLocalDbApi>();
            var target = new SqlLocalDbInstanceManager(instance, api);

            Assert.Throws <ArgumentNullException>("sharedName", () => target.Share(null));
        }
        public static void Share_Shares_Instance()
        {
            // Act
            string sharedName = Guid.NewGuid().ToString();

            var mock = new Mock <ISqlLocalDbApi>();
            ISqlLocalDbInstanceInfo instance = CreateInstance();
            ISqlLocalDbApi          api      = mock.Object;

            var target = new SqlLocalDbInstanceManager(instance, api);

            // Act
            target.Share(sharedName);

            // Assert
            mock.Verify((p) => p.ShareInstance(It.IsNotNull <string>(), "Name", sharedName), Times.Once());
        }
        public void Manager_Shares_And_Unshares_Instance()
        {
            // Act
            string instanceName = Guid.NewGuid().ToString();
            string sharedName   = Guid.NewGuid().ToString();

            using (var api = new SqlLocalDbApi(_loggerFactory))
            {
                api.CreateInstance(instanceName);

                try
                {
                    api.StartInstance(instanceName);

                    try
                    {
                        var instance = api.GetInstanceInfo(instanceName);

                        var manager = new SqlLocalDbInstanceManager(instance, api);

                        // Act
                        manager.Share(sharedName);

                        // Assert
                        instance.IsShared.ShouldBeTrue();

                        // Act
                        manager.Unshare();

                        // Assert
                        instance.IsShared.ShouldBeFalse();
                    }
                    catch (Exception)
                    {
                        api.StopInstance(instanceName);
                        throw;
                    }
                }
                catch (Exception)
                {
                    api.DeleteInstance(instanceName, deleteFiles: true);
                    throw;
                }
            }
        }
예제 #5
0
        /// <summary>
        /// The main entry point to the application.
        /// </summary>
        /// <param name="args">The command-line arguments passed to the application.</param>
        internal static void Main(string[] args)
        {
            PrintBanner();

            var options = new SqlLocalDbOptions()
            {
                AutomaticallyDeleteInstanceFiles = true,
                StopOptions = StopInstanceOptions.NoWait,
            };

            var services      = new ServiceCollection().AddLogging((p) => p.AddConsole().SetMinimumLevel(LogLevel.Debug));
            var loggerFactory = services.BuildServiceProvider().GetRequiredService <ILoggerFactory>();

            var localDB = new SqlLocalDbApi(options, loggerFactory);

            if (!localDB.IsLocalDBInstalled())
            {
                Console.WriteLine(SR.SqlLocalDbApi_NotInstalledFormat, Environment.MachineName);
                return;
            }

            if (args?.Length == 1 &&
                (string.Equals(args[0], "/deleteuserinstances", StringComparison.OrdinalIgnoreCase) ||
                 string.Equals(args[0], "--delete-user-instances", StringComparison.OrdinalIgnoreCase)))
            {
                localDB.DeleteUserInstances(deleteFiles: true);
            }

            IReadOnlyList <ISqlLocalDbVersionInfo> versions = localDB.GetVersions();

            Console.WriteLine(Strings.Program_VersionsListHeader);
            Console.WriteLine();

            foreach (ISqlLocalDbVersionInfo version in versions)
            {
                Console.WriteLine(version.Name);
            }

            Console.WriteLine();

            IReadOnlyList <ISqlLocalDbInstanceInfo> instances = localDB.GetInstances();

            Console.WriteLine(Strings.Program_InstancesListHeader);
            Console.WriteLine();

            foreach (ISqlLocalDbInstanceInfo instanceInfo in instances)
            {
                Console.WriteLine(instanceInfo.Name);
            }

            Console.WriteLine();

            string instanceName = Guid.NewGuid().ToString();

            ISqlLocalDbInstanceInfo instance = localDB.CreateInstance(instanceName);

            var manager = new SqlLocalDbInstanceManager(instance, localDB);

            manager.Start();

            try
            {
                if (IsCurrentUserAdmin())
                {
                    manager.Share(Guid.NewGuid().ToString());
                }

                try
                {
                    using (SqlConnection connection = manager.CreateConnection())
                    {
                        connection.Open();

                        try
                        {
                            using (SqlCommand command = new SqlCommand("create database [MyDatabase]", connection))
                            {
                                command.ExecuteNonQuery();
                            }

                            using (SqlCommand command = new SqlCommand("drop database [MyDatabase]", connection))
                            {
                                command.ExecuteNonQuery();
                            }
                        }
                        finally
                        {
                            connection.Close();
                        }
                    }
                }
                finally
                {
                    if (IsCurrentUserAdmin())
                    {
                        manager.Unshare();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                manager.Stop();
                localDB.DeleteInstance(instance.Name);
            }

            Console.WriteLine();
            Console.Write(Strings.Program_ExitPrompt);
            Console.ReadKey();
        }