public void GetBatchNodeFileByComputeNodeParametersTest()
        {
            // Setup cmdlet without required parameters
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();

            cmdlet.BatchContext  = context;
            cmdlet.PoolId        = null;
            cmdlet.ComputeNodeId = null;
            cmdlet.Name          = null;
            cmdlet.ComputeNode   = null;
            cmdlet.Filter        = null;

            // Build a NodeFile instead of querying the service on a List NodeFile call
            NodeFileListResponse response    = BatchTestHelpers.CreateNodeFileListResponse(new string[] { cmdlet.Name });
            RequestInterceptor   interceptor = BatchTestHelpers.CreateNoOpInterceptor <NodeFileListParameters, NodeFileListResponse>(response);

            cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>()
            {
                interceptor
            };

            Assert.Throws <ArgumentException>(() => cmdlet.ExecuteCmdlet());

            cmdlet.PoolId        = "pool";
            cmdlet.ComputeNodeId = "computeNode1";

            // Verify no exceptions occur
            cmdlet.ExecuteCmdlet();
        }
        public void ListNodeFilesByComputeNodeMaxCountTest()
        {
            // Verify default max count
            Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);

            // Setup cmdlet to list vm files and a max count.
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();

            cmdlet.BatchContext  = context;
            cmdlet.PoolId        = "pool";
            cmdlet.ComputeNodeId = "computeNode1";
            cmdlet.Name          = null;
            cmdlet.Filter        = null;
            int maxCount = 2;

            cmdlet.MaxCount = maxCount;

            string[] namesOfConstructedNodeFiles = new[] { "startup", "workitems", "shared" };

            // Build some NodeFiles instead of querying the service on a List NodeFiles call
            RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
            {
                BatchRequest <NodeFileListParameters, NodeFileListResponse> request =
                    (BatchRequest <NodeFileListParameters, NodeFileListResponse>)baseRequest;

                request.ServiceRequestFunc = (cancellationToken) =>
                {
                    NodeFileListResponse response    = BatchTestHelpers.CreateNodeFileListResponse(namesOfConstructedNodeFiles);
                    Task <NodeFileListResponse> task = Task.FromResult(response);
                    return(task);
                };
            });

            cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>()
            {
                interceptor
            };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List <PSNodeFile> pipeline = new List <PSNodeFile>();

            commandRuntimeMock.Setup(r =>
                                     r.WriteObject(It.IsAny <PSNodeFile>()))
            .Callback <object>(f => pipeline.Add((PSNodeFile)f));

            cmdlet.ExecuteCmdlet();

            // Verify that the max count was respected
            Assert.Equal(maxCount, pipeline.Count);

            // Verify setting max count <= 0 doesn't return nothing
            cmdlet.MaxCount = -5;
            pipeline.Clear();
            cmdlet.ExecuteCmdlet();

            Assert.Equal(namesOfConstructedNodeFiles.Length, pipeline.Count);
        }
        public void ListBatchNodeFilesByComputeNodeWithoutFiltersTest()
        {
            // Setup cmdlet to list vm files without filters.
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();

            cmdlet.BatchContext  = context;
            cmdlet.PoolId        = "pool";
            cmdlet.ComputeNodeId = "computeNode1";
            cmdlet.Name          = null;
            cmdlet.Filter        = null;

            string[] namesOfConstructedNodeFiles = new[] { "startup", "workitems", "shared" };

            // Build some NodeFiles instead of querying the service on a List NodeFiles call
            RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
            {
                BatchRequest <NodeFileListParameters, NodeFileListResponse> request =
                    (BatchRequest <NodeFileListParameters, NodeFileListResponse>)baseRequest;

                request.ServiceRequestFunc = (cancellationToken) =>
                {
                    NodeFileListResponse response    = BatchTestHelpers.CreateNodeFileListResponse(namesOfConstructedNodeFiles);
                    Task <NodeFileListResponse> task = Task.FromResult(response);
                    return(task);
                };
            });

            cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>()
            {
                interceptor
            };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List <PSNodeFile> pipeline = new List <PSNodeFile>();

            commandRuntimeMock.Setup(r =>
                                     r.WriteObject(It.IsAny <PSNodeFile>()))
            .Callback <object>(f => pipeline.Add((PSNodeFile)f));

            cmdlet.ExecuteCmdlet();

            // Verify that the cmdlet wrote the constructed node files to the pipeline
            Assert.Equal(3, pipeline.Count);
            int taskCount = 0;

            foreach (PSNodeFile f in pipeline)
            {
                Assert.True(namesOfConstructedNodeFiles.Contains(f.Name));
                taskCount++;
            }
            Assert.Equal(namesOfConstructedNodeFiles.Length, taskCount);
        }
        public void ListNodeFilesByTaskMaxCountTest()
        {
            // Verify default max count
            Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);

            // Setup cmdlet to list node files and a max count.
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();

            cmdlet.BatchContext = context;
            cmdlet.JobId        = "job-1";
            cmdlet.TaskId       = "task";
            cmdlet.Name         = null;
            cmdlet.Filter       = null;
            int maxCount = 2;

            cmdlet.MaxCount = maxCount;

            string[] namesOfConstructedNodeFiles = new[] { "stdout.txt", "stderr.txt", "wd" };

            // Build some NodeFiles instead of querying the service on a List NodeFiles call
            NodeFileListResponse response    = BatchTestHelpers.CreateNodeFileListResponse(namesOfConstructedNodeFiles);
            RequestInterceptor   interceptor = BatchTestHelpers.CreateNoOpInterceptor <NodeFileListParameters, NodeFileListResponse>(response);

            cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>()
            {
                interceptor
            };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List <PSNodeFile> pipeline = new List <PSNodeFile>();

            commandRuntimeMock.Setup(r =>
                                     r.WriteObject(It.IsAny <PSNodeFile>()))
            .Callback <object>(f => pipeline.Add((PSNodeFile)f));

            cmdlet.ExecuteCmdlet();

            // Verify that the max count was respected
            Assert.Equal(maxCount, pipeline.Count);

            // Verify setting max count <= 0 doesn't return nothing
            cmdlet.MaxCount = -5;
            pipeline.Clear();
            cmdlet.ExecuteCmdlet();

            Assert.Equal(namesOfConstructedNodeFiles.Length, pipeline.Count);
        }
        public void GetBatchNodeFileByTaskParametersTest()
        {
            // Setup cmdlet without required parameters
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();

            cmdlet.BatchContext = context;
            cmdlet.JobId        = null;
            cmdlet.TaskId       = null;
            cmdlet.Name         = null;
            cmdlet.Task         = null;
            cmdlet.Filter       = null;

            // Build a NodeFile instead of querying the service on a List NodeFile call
            RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
            {
                BatchRequest <NodeFileListParameters, NodeFileListResponse> request =
                    (BatchRequest <NodeFileListParameters, NodeFileListResponse>)baseRequest;

                request.ServiceRequestFunc = (cancellationToken) =>
                {
                    NodeFileListResponse response    = BatchTestHelpers.CreateNodeFileListResponse(new string[] { cmdlet.Name });
                    Task <NodeFileListResponse> task = Task.FromResult(response);
                    return(task);
                };
            });

            cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>()
            {
                interceptor
            };

            Assert.Throws <ArgumentException>(() => cmdlet.ExecuteCmdlet());

            cmdlet.JobId  = "job-1";
            cmdlet.TaskId = "task";

            // Verify no exceptions occur
            cmdlet.ExecuteCmdlet();

            cmdlet.JobId  = null;
            cmdlet.TaskId = null;
            cmdlet.Task   = new PSCloudTask("task", "cmd /c dir /s");

            // Verify that we don't get an argument exception. We should get an InvalidOperationException though since the task is unbound
            Assert.Throws <InvalidOperationException>(() => cmdlet.ExecuteCmdlet());
        }
        public void ListBatchNodeFilesByComputeNodeByODataFilterTest()
        {
            // Setup cmdlet to list vm files using an OData filter.
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();

            cmdlet.BatchContext  = context;
            cmdlet.PoolId        = "pool";
            cmdlet.ComputeNodeId = "computeNode1";
            cmdlet.Name          = null;
            cmdlet.Filter        = "startswith(name,'startup')";

            string[] namesOfConstructedNodeFiles = new[] { "startup\\stdout.txt", "startup\\stderr.txt" };

            // Build some NodeFiles instead of querying the service on a List NodeFiles call
            NodeFileListResponse response    = BatchTestHelpers.CreateNodeFileListResponse(namesOfConstructedNodeFiles);
            RequestInterceptor   interceptor = BatchTestHelpers.CreateNoOpInterceptor <NodeFileListParameters, NodeFileListResponse>(response);

            cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>()
            {
                interceptor
            };

            // Setup the cmdlet to write pipeline output to a list that can be examined later
            List <PSNodeFile> pipeline = new List <PSNodeFile>();

            commandRuntimeMock.Setup(r =>
                                     r.WriteObject(It.IsAny <PSNodeFile>()))
            .Callback <object>(f => pipeline.Add((PSNodeFile)f));

            cmdlet.ExecuteCmdlet();

            // Verify that the cmdlet wrote the constructed node files to the pipeline
            Assert.Equal(2, pipeline.Count);
            int taskCount = 0;

            foreach (PSNodeFile f in pipeline)
            {
                Assert.True(namesOfConstructedNodeFiles.Contains(f.Name));
                taskCount++;
            }
            Assert.Equal(namesOfConstructedNodeFiles.Length, taskCount);
        }
        public void GetBatchNodeFileByComputeNodeParametersTest()
        {
            // Setup cmdlet without required parameters
            BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();

            cmdlet.BatchContext  = context;
            cmdlet.PoolId        = null;
            cmdlet.ComputeNodeId = null;
            cmdlet.Name          = null;
            cmdlet.ComputeNode   = null;
            cmdlet.Filter        = null;

            // Build a NodeFile instead of querying the service on a List NodeFile call
            RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
            {
                BatchRequest <NodeFileListParameters, NodeFileListResponse> request =
                    (BatchRequest <NodeFileListParameters, NodeFileListResponse>)baseRequest;

                request.ServiceRequestFunc = (cancellationToken) =>
                {
                    NodeFileListResponse response    = BatchTestHelpers.CreateNodeFileListResponse(new string[] { cmdlet.Name });
                    Task <NodeFileListResponse> task = Task.FromResult(response);
                    return(task);
                };
            });

            cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>()
            {
                interceptor
            };

            Assert.Throws <ArgumentException>(() => cmdlet.ExecuteCmdlet());

            cmdlet.PoolId        = "pool";
            cmdlet.ComputeNodeId = "computeNode1";

            // Verify no exceptions occur
            cmdlet.ExecuteCmdlet();
        }