locked
running a script in test agent from a unit test

    שאלה

  • i have a test environment as below :

    test agent  ---->  tfs <------- system-x-develper(visual studio host)

    as can be seen i am able to connect the test agent before the tests are scheduled to run and i can run the setup script for the new build copying the new build from the drop folder to the deployment folder to the test agent.

    what i need now is to that after running a couple of tests in an ordered test list i want in middle of the list a unit test where i want to run a script to do some specific task in the test agent to change some of the environment variables again as i did before triggering the whole test run. i have tried running a powershell script from inside the code using the code:

    namespace PowerScripts
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Management.Automation;
        using System.Collections.ObjectModel;
        using System.Management.Automation.Runspaces;
        public static class Base
        {
            //executes the script which is sent as a text and returns the string output if any
            public static string RunScript(string scriptText)
            {
                // create Powershell runspace
                Runspace runspace = RunspaceFactory.CreateRunspace();
                // open it
                runspace.Open();
                // create a pipeline and feed it the script text
                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript(scriptText);
    
                // add an extra command to transform the script
                // output objects into nicely formatted strings
    
                // remove this line to get the actual objects
                // that the script returns. For example, the script
    
                // "Get-Process" returns a collection
                // of System.Diagnostics.Process instances.
    
                pipeline.Commands.Add("Out-String");
    
                // execute the script
    
                Collection<PSObject> results = pipeline.Invoke();
    
                // close the runspace
    
                runspace.Close();
    
                // convert the script result into a single string
    
                StringBuilder stringBuilder = new StringBuilder();
                foreach (PSObject obj in results)
                {
                    stringBuilder.AppendLine(obj.ToString());
                }
    
                return stringBuilder.ToString();
            }
            
        }
    }
    

    i call the static base method of this particular dll in the unit test but it fails with an error :

    Test method ODWUnitTests.CSVTests.CSVIdentity threw exception:
    System.Management.Automation.CmdletInvocationException: Access to the path 'C:\Users\Administrator\AppData\Local\VSEQT\QTAgent\5981ceaa-6785-4187-aeb1-cf7086906c2a\TESTBUILDSERVER\Deployment' is denied. ---> System.UnauthorizedAccessException: Access to the path 'C:\Users\Administrator\AppData\Local\VSEQT\QTAgent\5981ceaa-6785-4187-aeb1-cf7086906c2a\TESTBUILDSERVER\Deployment' is denied.

    stack trace :

    System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
    System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
    System.Management.Automation.PathUtils.MasterStreamOpen(PSCmdlet cmdlet, String filePath, String encoding, Boolean defaultEncoding, Boolean Append, Boolean Force, Boolean NoClobber, FileStream& fileStream, StreamWriter& streamWriter, FileInfo& readOnlyFileInfo)
    System.Management.Automation.RedirectionNode.BuildRedirectionPipeline(String path, ExecutionContext context)
    System.Management.Automation.RedirectionNode.GetRedirectionPipe(ExecutionContext context)
    System.Management.Automation.CommandNode.BindRedirectionPipes(CommandProcessorBase commandProcessor, PipelineProcessor pipeline, ExecutionContext context)
    System.Management.Automation.CommandNode.AddToPipeline(PipelineProcessor pipeline, ExecutionContext context)
    System.Management.Automation.PipelineNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
    System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)

    I would like to know if its possible to do what i want to do and if not is there any workaround (remoting is my best guess).


    mohit narang

    יום שלישי 13 מרץ 2012 15:07

תשובות

  • Hi @vicky

    i just solved the above problem which has nothing to do with the access permissions as it says in the exception. Its actually about an extra '\' in the path which i have to suplly alongwith this powershell scripting class.

    the path (a string) i supplied was : "c:\newfile.txt"

    how it should look like : "c:\\newfile.txt"

    but we can see a very strong point here and that point is we don't supply useful error messages in unit tests and testing labs which i think makes very difficult to handle the things like this. If the error message is very simple like "couldn't find path" i could atleast think that there is something wrong with the path i supplied but what happens now is i was thinking totally of firewall and user access and domain trusts kind of things. I think this issue must brought some light into to the develpment.


    mohit narang

    • סומן כתשובה על-ידי mohit123narang יום חמישי 15 מרץ 2012 08:42
    יום חמישי 15 מרץ 2012 08:42

כל התגובות

  • Hi mohit,

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.

    Thanks.


    Vicky Song [MSFT]
    MSDN Community Support | Feedback to us

    יום חמישי 15 מרץ 2012 06:28
  • Hi @vicky

    i just solved the above problem which has nothing to do with the access permissions as it says in the exception. Its actually about an extra '\' in the path which i have to suplly alongwith this powershell scripting class.

    the path (a string) i supplied was : "c:\newfile.txt"

    how it should look like : "c:\\newfile.txt"

    but we can see a very strong point here and that point is we don't supply useful error messages in unit tests and testing labs which i think makes very difficult to handle the things like this. If the error message is very simple like "couldn't find path" i could atleast think that there is something wrong with the path i supplied but what happens now is i was thinking totally of firewall and user access and domain trusts kind of things. I think this issue must brought some light into to the develpment.


    mohit narang

    • סומן כתשובה על-ידי mohit123narang יום חמישי 15 מרץ 2012 08:42
    יום חמישי 15 מרץ 2012 08:42