Exemplo n.º 1
0
        static int Main(string[] args)
        {
            try
            {
                var options = new Options();
                if (!ParseOptions(args, options))
                    return 1;

                //TODO stop mixing Console.Write and logger
                // Context: Console.Write is used for pretty output, ILogger for the internal stuff.
                var logger = new LoggerConfiguration()
                    .WriteTo.Console(outputTemplate: "> {Level}: {Message}{NewLine}{Exception}")
                    .CreateLogger();

                var optionTable = new TextTable();
                optionTable.Add(Strings.OptionDryRun, options.DryRun ? Strings.OptionDryRunYes
                                                                     : Strings.OptionDryRunNo);
                optionTable.Add(Strings.OptionMirror, options.Mirror ? Strings.OptionYes
                                                                     : Strings.OptionNo);
                optionTable.Add(Strings.OptionVerbose, options.Verbose ? Strings.OptionYes
                                                                       : Strings.OptionNo);

                Console.WriteLine(Strings.Options);
                Console.Write(optionTable.ToString());
                Console.WriteLine();

                try
                {
                    Console.WriteLine(Strings.StatusParsingDefinition);
                    var definedTopology = new XmlTopologyReader().Parse(options.TopologyFilename);

                    if (!definedTopology.VerifyNamespaces(logger, definedTopology.Meta.NamespacePrefix))
                        return 1;

                    Console.WriteLine(Strings.StatusConnectingRabbitMQ, options.ConnectionParams.Host, options.ConnectionParams.VirtualHost);
                    var client = Connect(options.ConnectionParams);
                    var virtualHost = client.GetVhost(options.ConnectionParams.VirtualHost);

                    Console.WriteLine(Strings.StatusReadingTopology);
                    var existingTopology = new RabbitMQTopologyReader().Parse(client, virtualHost)
                        .FilterByNamespace(definedTopology.Meta.NamespacePrefix);

                    var writer = (options.DryRun ? (ITopologyWriter)new NullTopologyWriter()
                                                 : new RabbitMQTopologyWriter(client, virtualHost));

                    var comparator = new TopologyComparator(logger, writer)
                    {
                        AllowDelete = options.Mirror,
                        AllowRecreate = options.Mirror,
                        AllowUnbind = options.Mirror
                    };

                    Console.WriteLine(Strings.StatusComparing);
                    comparator.Compare(existingTopology, definedTopology);

                    Console.WriteLine();
                    Console.WriteLine(Strings.StatusDone);
                    return 0;
                }
                catch(Exception e)
                {
                    Console.WriteLine();
                    Console.WriteLine(Strings.StatusError, e.Message);
                    return 1;
                }
            }
            finally
            {
                if (Debugger.IsAttached)
                {
                    Console.WriteLine();
                    Console.WriteLine(Strings.StatusWaitForKey);
                    Console.ReadLine();
                }
            }
        }
        private void TestCompare(IReadOnlyCollection<string> expectedActions, string prefix = null)
        {
            var writer = new MockTopologyWriter();
            var comparator = new TopologyComparator(new LoggerConfiguration().CreateLogger(), writer)
            {
                AllowDelete = true,
                AllowRecreate = true,
                AllowUnbind = true
            };

            comparator.Compare(
                existingTopology.FilterByNamespace(prefix),
                definedTopology);

            var hasUnexpectedActions = false;
            var results = new StringBuilder();

            results.AppendLine("Expected actions not executed:");
            foreach (var line in expectedActions.Except(writer.Actions))
            {
                results.AppendLine("  " + line);
                hasUnexpectedActions = true;
            }

            results.AppendLine("Executed action not expected:");
            foreach (var line in writer.Actions.Except(expectedActions))
            {
                results.AppendLine("  " + line);
                hasUnexpectedActions = true;
            }

            if (hasUnexpectedActions)
            {
                results.AppendLine("Full log:");
                foreach (var line in writer.Actions)
                    results.AppendLine("  " + line);

                Assert.Fail(results.ToString());
            }
        }