Ejemplo n.º 1
0
        private static void NextStep(Container a, Container b, int find, List<Tuple<string, int, int>> steps)
        {
            if (steps.Count >= maxSteps)
            {
                Trace.TraceWarning("Max attempts exception: {0} : {1} | {2}", find, a.Capacity, b.Capacity);
                throw new ApplicationException("Max attempts exceeded without a solution.");
            }

            Tuple<string, int, int> step;

            if (b.IsEmpty)
            {
                b.Fill();
                step = Tuple.Create(string.Format("[Fill] Container [{0}]", b.Name), a.Units, b.Units);
            }
            else if (a.IsFull)
            {
                a.Dump();
                step = Tuple.Create(string.Format("[Empty] Container [{0}]", a.Name), a.Units, b.Units);
            }
            else
            {
                a.Transfer(b);
                step = (Tuple.Create(string.Format("[Transfer] Container [{0}] to [{1}]", b.Name, a.Name), a.Units, b.Units));
            }

            Trace.TraceInformation(step.Item1);
            Trace.TraceInformation("{0}|{1}", step.Item2, step.Item3);
            steps.Add(step);

            if (a.Units != find && b.Units != find)
                NextStep(a, b, find, steps);
        }
        public WebApplicationCommand(Container container, string[] arguments, bool shouldImpersonate, ResourceLimits rlimits)
            : base(container, arguments, shouldImpersonate, rlimits)
        {
            if (arguments.IsNullOrEmpty())
            {
                throw new ArgumentException("Expected one or more arguments");
            }

            if (String.IsNullOrWhiteSpace(arguments[0]))
            {
                throw new ArgumentException("Expected port as first argument");
            }
            port = arguments[0];

            if (arguments.Length > 1)
            {
                if (arguments[1] != RuntimeVersionTwo && arguments[1] != RuntimeVersionFour)
                {
                    throw new ArgumentException("Expected runtime version value of '2.0' or '4.0', default is '4.0'.");
                }
                else
                {
                    runtimeVersion = arguments[1];
                }
            }
        }
Ejemplo n.º 3
0
        public TaskRunner(Container container, ITaskRequest request)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            this.container = container;

            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            this.request = request;

            if (this.request.Script.IsNullOrWhiteSpace())
            {
                throw new ArgumentNullException("request.Script can't be empty.");
            }

            commands = JsonConvert.DeserializeObject<TaskCommandDTO[]>(request.Script);
            if (commands.IsNullOrEmpty())
            {
                throw new ArgumentException("Expected to run at least one command.");
            }
        }
        /// <summary>
        /// Converts an XML string to a list of Container objects
        /// </summary>
        /// <param name="xml">XML string to be converted</param>
        /// <returns>A list of container objects</returns>
        public List<Container> containerReader(string xml)
        {
            var xmlDocument = new XmlDocument();
            //xmlDocument.Load("container.xml");
            xmlDocument.LoadXml(xml);

            var tmpList = new List<Container>();

            XmlNodeList nodeList = xmlDocument.GetElementsByTagName("container");

            foreach (XmlElement elm in nodeList)
            {
                var container = new Container();
                XmlNodeList titleList = elm.GetElementsByTagName("dc:title");
                container.title = titleList[0].InnerText;

                titleList = elm.GetElementsByTagName("upnp:class");
                container.upnpClass = titleList[0].InnerText;

                container.id = elm.GetAttribute("id");
                container.parentID = elm.GetAttribute("parentID");

                tmpList.Add(container);

            }
            return tmpList;
        }
Ejemplo n.º 5
0
        public TarCommand(Container container, string[] arguments)
            : base(container, arguments)
        {
            if (arguments.Length != 3)
            {
                throw new ArgumentException("tar command must have three arguments: operation (x or c), directory and file name.");
            }

            this.command = arguments[0];
            if (this.command.IsNullOrWhiteSpace() || (!(this.command == "x" || this.command == "c")))
            {
                throw new ArgumentException("tar command: first argument must be x (extract) or c (create).");
            }

            this.directory = container.ConvertToPathWithin(arguments[1]);
            if (!Directory.Exists(this.directory))
            {
                throw new ArgumentException(String.Format("tar command: second argument must be directory that exists ('{0}')", this.directory));
            }

            if (arguments[2].IsNullOrWhiteSpace())
            {
                throw new ArgumentException("tar command: third argument must be a file name.");
            }
            else
            {
                this.tarFile = container.ConvertToPathWithin(arguments[2]);
                if (this.command == "x" && !File.Exists(this.tarFile))
                {
                    throw new ArgumentException("tar command: third argument must be a file name that exists.");
                }
            }
        }
Ejemplo n.º 6
0
 public PowershellCommand(Container container, string[] arguments, bool shouldImpersonate, ResourceLimits rlimits)
     : base(container, arguments, shouldImpersonate, rlimits)
 {
     if (base.arguments.IsNullOrEmpty())
     {
         throw new ArgumentException("powershell: command must have at least one argument.");
     }
 }
Ejemplo n.º 7
0
 public TaskCommandFactory(Container container, bool shouldImpersonate, ResourceLimits rlimits)
 {
     if (container == null)
     {
         throw new ArgumentNullException("container");
     }
     this.container = container;
     this.shouldImpersonate = shouldImpersonate;
     this.rlimits = rlimits;
 }
Ejemplo n.º 8
0
        internal static List<Tuple<string, int, int>> FindQuickestSolution(Container a, Container b, int find)
        {
            var steps = new List<Tuple<string, int, int>>();

            if (find % a.Capacity == 0)
                ContainerSolver.NextStep(b, a, find, steps);
            else
                ContainerSolver.NextStep(a, b, find, steps);

            return steps;
        }
Ejemplo n.º 9
0
        public void FindBucketSixModThree()
        {
            int find = 6;

            Container a = new Container('a', 3);
            Container b = new Container('b', 9);

            var steps = ContainerSolver.FindQuickestSolution(a, b, find);

            Assert.IsTrue(b.Units == find);
            Assert.IsTrue(steps.Count == 4);
            Trace.TraceInformation("Solution Successful in {0} steps", steps.Count);
        }
Ejemplo n.º 10
0
        public void CanAddBinding()
        {
            // Act
            Container.AddConfig(new Config <ITest1, Test1>());
            Container.AddConfig(new Config <ITest2>(new Test2()));

            // Assert
            var test1 = Container.GetBinding <ITest1>();

            Assert.IsNotNull(test1);
            Assert.IsFalse(string.IsNullOrWhiteSpace(test1.TestString));
            Assert.IsNotNull(Container.GetBinding <ITest2>());
        }
Ejemplo n.º 11
0
        public void ABitTougherSolution()
        {
            int find = 4;
            int counter = 0;

            Container a = new Container('a', 3);
            Container b = new Container('b', 5);

            counter = ContainerSolver.NextStep(a, b, find, counter);

            Assert.IsTrue(b.Units == find);
            Assert.IsTrue(counter == 6);
            Trace.TraceInformation("Solution Successful in {0} steps", counter);
        }
Ejemplo n.º 12
0
        public void CanOverwriteConfig()
        {
            // Arrange
            Container.AddConfig(new Config <ITest1, Test1>());

            // Act
            Container.OverwriteConfig(new Config <ITest1, Test1A>());

            // Assert
            var test1 = Container.GetBinding <ITest1>();
            var expectedTestString = new Test1A().TestString;

            Assert.AreEqual(expectedTestString, test1.TestString);
        }
Ejemplo n.º 13
0
        public void SolutionStupidSimple()
        {
            int find = 4;

            Container a = new Container('a', 3);
            Container b = new Container('b', 5);

            Assert.IsTrue(a.Capacity == 3);
            Assert.IsTrue(b.Capacity == 5);
            Trace.TraceInformation("a: {0}/{1}", a.Units, a.Capacity);
            Trace.TraceInformation("b: {0}/{1}", b.Units, b.Capacity);
            //0/0

            b.Fill();
            Trace.TraceInformation("a: {0}/{1}", a.Units, a.Capacity);
            Trace.TraceInformation("b: {0}/{1}", b.Units, b.Capacity);
            Assert.IsTrue(b.Units == 5);
            //0/5

            a.Transfer(b);
            //3/2
            Trace.TraceInformation("a: {0}/{1}", a.Units, a.Capacity);
            Trace.TraceInformation("b: {0}/{1}", b.Units, b.Capacity);
            Assert.IsTrue(a.Units <= a.Capacity);

            a.Dump();
            Assert.IsTrue(a.Units == 0);
            Trace.TraceInformation("a: {0}/{1}", a.Units, a.Capacity);
            Trace.TraceInformation("b: {0}/{1}", b.Units, b.Capacity);
            //0/2

            a.Transfer(b);
            Trace.TraceInformation("a: {0}/{1}", a.Units, a.Capacity);
            Trace.TraceInformation("b: {0}/{1}", b.Units, b.Capacity);
            Assert.IsTrue(a.Units == 2, "Expected 2");
            Assert.IsTrue(b.Units == 0, "Expected 0");
            //2/0

            b.Fill();
            //2/5

            a.Transfer(b);
            //3/4
            Trace.TraceInformation("a: {0}/{1}", a.Units, a.Capacity);
            Trace.TraceInformation("b: {0}/{1}", b.Units, b.Capacity);

            Assert.IsTrue(b.Units == find);
            Trace.TraceInformation("You Win!");
        }
Ejemplo n.º 14
0
        public void CanChainAddBindings()
        {
            Container.AddConfig(new Config <ITest1, Test1>());
            Container.AddConfig(new Config <ITest2>(new Test2()));
            Container.AddConfig(new Config <ITest3, Test3>().AsSingleton());
            Container.AddConfig(new Config <ITest4>(new Test4()).AsSingleton());

            var test1 = Container.GetBinding <ITest1>();

            Assert.IsNotNull(test1);
            Assert.IsFalse(string.IsNullOrWhiteSpace(test1.TestString));
            Assert.IsNotNull(Container.GetBinding <ITest2>());
            Assert.IsNotNull(Container.GetBinding <ITest3>());
            Assert.IsNotNull(Container.GetBinding <ITest4>());
        }
Ejemplo n.º 15
0
 public void Transfer(Container source)
 {
     Trace.TraceInformation("Transfering...");
     if (source.Units + Units < Capacity)
     {
         Units += source.Units;
         source.Units = 0;
     }
     else
     {
         int movableUnits = source.Units - (source.Units - (Capacity - Units));
         Units += movableUnits;
         source.Units -= movableUnits;
     }
 }
Ejemplo n.º 16
0
        public void ABitTougherSolution()
        {
            int find = 4;

            Container a = new Container('a', 3);
            Container b = new Container('b', 5);

            var steps = ContainerSolver.FindQuickestSolution(a, b, find);

            //Sample tracesfor review
            steps.ForEach(x => Trace.TraceInformation(x.ToString()));

            Assert.IsTrue(b.Units == find);
            Assert.IsTrue(steps.Count == 6);
            Trace.TraceInformation("Solution Successful in {0} steps", steps.Count);
        }
Ejemplo n.º 17
0
        public void FindBucketSixModThree()
        {
            int find = 6;
            int counter = 0;

            Container a = new Container('a', 3);
            Container b = new Container('b', 9);

            if (find % a.Capacity == 0)
                counter = ContainerSolver.NextStep(b, a, find, counter);
            else
                counter = ContainerSolver.NextStep(a, b, find, counter);

            Assert.IsTrue(b.Units == find);
            Assert.IsTrue(counter == 4);
            Trace.TraceInformation("Solution Successful in {0} steps", counter);
        }
Ejemplo n.º 18
0
        public override Task<Response> HandleAsync()
        {
            return Task.Run<Response>(() =>
                {
                    // before

                    // do
                    var container = new Container();
                    containerManager.AddContainer(container);

                    // after
                    container.AfterCreate();

                    ProcessBindMounds(request.BindMounts, container.User);

                    return new CreateResponse { Handle = container.Handle };
                });
        }
Ejemplo n.º 19
0
        public void CanDetermineBetweenTwoImplementations()
        {
            // Arrange
            var configs = new List <IConfig>
            {
                new Config <ITest1, Test1>().WithName("Test1"),
                new Config <ITest1, Test1A>().WithName("asdf")
            };

            // Act
            Container.Initialize(configs);

            // Assert
            var test1  = Container.GetBinding <ITest1>("Test1");
            var test1A = Container.GetBinding <ITest1>("asdf");

            Assert.AreEqual("Hello World", test1.TestString);
            Assert.AreEqual("Hello World From Test1 A", test1A.TestString);
        }
Ejemplo n.º 20
0
        public void BindingDictionaryReturnsImplementationOfPassedInConfig()
        {
            // Arrange
            var configs = new List <IConfig>
            {
                new Config <ITest1, Test1>(),
                new Config <ITest2>(new Test2()),
                new Config <ITest3, Test3>().AsSingleton(),
                new Config <ITest4>(new Test4()).AsSingleton()
            };

            // Act
            Container.Initialize(configs);

            // Assert
            var test1 = Container.GetBinding <ITest1>();

            Assert.IsNotNull(test1);
            Assert.IsFalse(string.IsNullOrWhiteSpace(test1.TestString));
            Assert.IsNotNull(Container.GetBinding <ITest2>());
            Assert.IsNotNull(Container.GetBinding <ITest3>());
            Assert.IsNotNull(Container.GetBinding <ITest4>());
        }
Ejemplo n.º 21
0
        public void CanChangeInstanceOfInterfaceUsedDependingOnConfig()
        {
            // Arrange
            var configs = new List <IConfig>
            {
                new Config <ITest1, Test1>().WithName("Test1"),
                new Config <ITest1, Test1A>().WithName("asdf"),
                new SelfConfig(typeof(Test5))
                .ResolveConstructorArgumentUsing <Test1>("test1")
                .ResolveConstructorArgumentUsing <Test1A>("test1A"),
                new Config <ITest2, Test2>()
            };

            // Act
            Container.Initialize(configs);

            var test5 = Container.GetBinding <Test5>();

            // Assert
            Assert.IsInstanceOfType(test5.Test1, typeof(Test1));
            Assert.IsInstanceOfType(test5.Test1A, typeof(Test1A));
            Assert.IsNotNull(test5.Test2);
        }
Ejemplo n.º 22
0
        internal static int NextStep(Container a, Container b, int find, int counter)
        {
            int maxSteps = 100;
            if (counter >= maxSteps)
            {
                Trace.TraceWarning("Max attempts exception: {0} : {1} | {2}", find, a.Capacity, b.Capacity);
                throw new ApplicationException("Max attempts exceeded without a solution.");
            }
            if (b.Units == 0)
                b.Fill();
            else if (a.Units == a.Capacity)
                a.Dump();
            else
                a.Transfer(b);

            a.Report();
            b.Report();
            counter++;

            if (a.Units != find && b.Units != find)
                counter = NextStep(a, b, find, counter);

            return counter;
        }
Ejemplo n.º 23
0
        public void TestMaxStepsCounterException()
        {
            int find = 149;
            int counter = 0;

            Container a = new Container('a', 1);
            Container b = new Container('b', 150);

            counter = ContainerSolver.NextStep(b, a, find, counter);
        }
Ejemplo n.º 24
0
        public void TestMaxStepsCounterException()
        {
            int find = 149;

            Container a = new Container('a', 1);
            Container b = new Container('b', 150);

            ContainerSolver.FindQuickestSolution(a, b, find);
        }
Ejemplo n.º 25
0
 public void Setup()
 {
     Container.Initialize(new List <IConfig>());
 }
Ejemplo n.º 26
0
 public ProcessCommand(Container container, string[] arguments, bool shouldImpersonate, ResourceLimits rlimits)
     : base(container, arguments)
 {
     this.shouldImpersonate = shouldImpersonate;
     this.rlimits = rlimits;
 }
Ejemplo n.º 27
0
 public ReplaceTokensCommand(Container container, string[] arguments)
     : base(container, arguments)
 {
     tokenReplacer = (line) => line.Replace("@ROOT@", container.Directory).ToWinPathString();
 }
Ejemplo n.º 28
0
		public void SetParentContainer(Container c) {
			parentContainer = c;
		}
Ejemplo n.º 29
0
        public void TestMaxCapacityException()
        {
            int capacity = 1;
            Container a = new Container('a', capacity);

            a.Units = 2;
        }
Ejemplo n.º 30
0
        public void TestOneHundredVariationsWithExceptions()
        {
            Random rand = new Random();

            List<Exception> exceptions = new List<Exception>();

            for (int i = 0; i < 100; i++)
            {
                int min = 0;
                int max = 25;
                int find = rand.Next(min, max);
                int aC = rand.Next(min, max);
                int bC = rand.Next(min, max);
                int counter = 0;

                try
                {
                    if (ContainerSolver.ValidateProblemVariables(find, aC, bC))
                    {
                        Trace.TraceInformation("Starting: {0} : {1} / {2}", find, aC, bC);
                        Container a = new Container('a', aC < bC ? aC : bC);
                        Container b = new Container('b', aC < bC ? bC : aC); // Container "b" should be the largest

                        if (find % a.Capacity == 0)
                            counter = ContainerSolver.NextStep(b, a, find, counter);
                        else
                            counter = ContainerSolver.NextStep(a, b, find, counter);

                        Assert.IsTrue(a.Units == find || b.Units == find);
                        Trace.TraceInformation("Finished: {0} : {1} / {2} in {3} steps", find, aC, bC, counter);
                    }
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
            }

            Trace.TraceWarning("Execptions caught:{0}", exceptions.Count);
            exceptions.ForEach(x => Trace.TraceWarning(x.Message));
        }