1 | Different ways to debug azure-xplat-cli commands
|
2 |
|
3 | ## Pre-requisites
|
4 | Ensure following things have been done, before debugging the commands or tests:
|
5 | * **Authentication**
|
6 |
|
7 | ```
|
8 | * ARM Mode
|
9 | azure login -u $username (organizational account) -p $password (token based)
|
10 |
|
11 | * ASM Mode
|
12 | azure login -u $username (organizational account) -p $password (token based)
|
13 | OR
|
14 | azure account import $path-to-publish-settings-file (Cert based)
|
15 | ```
|
16 |
|
17 | * **Setting the subscription**
|
18 | ```
|
19 | * List all the subscriptions
|
20 | azure account list
|
21 | * Set the subscription of your choice
|
22 | azure account set "$Subscription Name"
|
23 | ```
|
24 |
|
25 | ## Using the -vv option
|
26 | This provides the underlying REST requests that take place upon executing the xplat cli command. It provides more insight into the actual REST request where the error happens.
|
27 |
|
28 | For example:
|
29 | ```
|
30 | D:\sdk\xplat\azure-sdk-tools-xplat>azure site delete foozap1 -vv
|
31 | info: Executing command site delete
|
32 | verbose: Attempting to locate site foozap1
|
33 | verbose: Getting locations
|
34 | silly: Providers to register via registerResourceNamespace:
|
35 | silly: requestOptions
|
36 | silly: {
|
37 | silly: rawResponse: false,
|
38 | silly: queryString: {},
|
39 | silly: method: 'GET',
|
40 | silly: headers: { user-agent: 'WindowsAzureXplatCLI/0.8.16', x-ms-version: '2014-04-01' },
|
41 | silly: url: 'https://management.core.windows.net/06b24a43-b85a-c7t7-bd74-82f3429d998c/services/WebSpaces'
|
42 | silly: }
|
43 | silly: returnObject
|
44 | silly: {
|
45 | silly: statusCode: 200,
|
46 | silly: header: {
|
47 | silly: content-type: 'application/xml; charset=utf-8',
|
48 | silly: strict-transport-security: 'max-age=31536000; includeSubDomains',
|
49 | silly: content-length: '114',
|
50 | silly: x-ms-servedbyregion: 'ussouth2',
|
51 | silly: x-aspnet-version: '4.0.30319',
|
52 | silly: cache-control: 'private',
|
53 | silly: date: 'Wed, 18 Mar 2015 23:44:03 GMT',
|
54 | silly: x-powered-by: 'ASP.NET',
|
55 | silly: x-ms-request-id: 'c4fc8f157c76847fsda2f56732ce77a3',
|
56 | silly: server: '1.0.6198.202 (rd_rdfe_stable.150307-1902) Microsoft-HTTPAPI/2.0'
|
57 | silly: },
|
58 | silly: body: '<WebSpaces xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"/>'
|
59 | silly: }
|
60 | verbose: Getting sites
|
61 | verbose: []
|
62 | error: Unable to locate site named foozap1
|
63 | silly: {
|
64 | silly: arguments: undefined,
|
65 | silly: type: undefined,
|
66 | silly: message: 'Unable to locate site named foozap1',
|
67 | silly: __frame: {
|
68 | silly: name: 'lookupSiteNameAndWebSpace__20',
|
69 | silly: line: 871,
|
70 | silly: file: 'C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\CLI\\lib\\commands\\asm\\websites\\websitesclient.js',
|
71 | silly: prev: {
|
72 | silly: name: '__8',
|
73 | silly: line: 661,
|
74 | silly: file: 'C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\CLI\\lib\\commands\\asm\\site.js',
|
75 | silly: prev: undefined,
|
76 | silly: calls: 1,
|
77 | silly: active: false,
|
78 | silly: offset: 14,
|
79 | silly: col: 15
|
80 | silly: },
|
81 | silly: calls: 2,
|
82 | silly: active: false,
|
83 | silly: offset: 12,
|
84 | silly: col: 9
|
85 | silly: },
|
86 | silly: rawStack: 'Error: Unable to locate site named foozap1
|
87 | info: Error information has been recorded to C:\Users\amzavery\.azure\azure.err
|
88 | verbose: Error: Unable to locate site named foozap1
|
89 | ```
|
90 |
|
91 | ## Using console.log()
|
92 | This is always handy and is guaranteed to work.
|
93 | It is a good practice to use util.inspect() to dump well formatted objects.
|
94 | In your script:
|
95 | ```
|
96 | var util = require('util');
|
97 | . . .
|
98 | console.log(">>>>>>>>>>>>>> Some Identifier " + util.inspect(some_object, {depth: null}));
|
99 | . . .
|
100 | ```
|
101 | Providing **{depth: null}** is optional. By default, it will dump object upto 3 levels deep. Setting depth to null will dump the complete object.
|
102 |
|
103 | ## Using node inspector
|
104 | This will open a debugging session in a browser (chrome or opera). It is super easy to use.
|
105 | Steps to get it working:
|
106 |
|
107 | * ```npm install -g node-inspector```
|
108 | * Assuming this is being executing in the cmd prompt from the root directory of the cloned repo "azure-xplat-cli/"
|
109 | * ```node-debug bin\azure group create -l "West US" testgroup1```
|
110 | * A browser (Chrome/Opera) should pop up, or it can be manually browsed at this url - http://127.0.0.1:8080/debug?port=5858
|
111 | * Now breakpoints can be set at desired location.
|
112 | * It may happen that files with extension "._js" are not seen initially in the left pane. Please set a break point anywhere in lib\cli.js and keep on pressing "F10". After some time, files with extension "_.js" can be seen. Breakpoints can now be set at desired line in "*._js" file.
|
113 | * A breakpoint can also be set in the underlying azure-sdk-for-node which has the management clients making the REST calls to the server.
|
114 | These can be seen inside the directories starting with name "azure" or "azure-*" inside the node_modules directory.
|
115 | (node_modules will be present after executing ```npm install``` from the root directory "azure-xplat-cli" of the cloned repo.)
|
116 |
|
117 | ### For debugging tests:
|
118 | Please follow the below mentioned steps for the debugger to accept breakpoints set in your test file:
|
119 | * In the file azure-xplat-cli/scripts/unit.js
|
120 | ```
|
121 | modify the last line
|
122 | from - require('../node_modules/mocha/bin/mocha');
|
123 | to - require('../node_modules/mocha/bin/_mocha');
|
124 | ```
|
125 | * set a breakpoint at runTest() method of mocha's runner.
|
126 | In the file "azure-xplat-cli\node_modules\mocha\lib\runner.js" in the "runTest()" method around Line 378.
|
127 | * **Note**: "node_modules" directory is not a part of "azure-xplat-cli" repository. If the **"node_modules"** directory or the **"mocha"** directory inside **"node_modules"** directory is not present, then ```npm install``` needs to be executed from the root directory of the cloned "azure-xplat-cli" repo.
|
128 | ```
|
129 | Runner.prototype.runTest = function(fn){
|
130 | var test = this.test
|
131 | , self = this;
|
132 |
|
133 | if (this.asyncOnly) test.asyncOnly = true;
|
134 |
|
135 | try {
|
136 | test.on('error', function(err){
|
137 | self.fail(test, err);
|
138 | });
|
139 | test.run(fn); <<<<<<<<<<<<<<<<<<<<<<<-------- set a breakpoint here
|
140 | } catch (err) {
|
141 | fn(err);
|
142 | }
|
143 | };
|
144 | ```
|
145 | * Set a breakpoint in your test which should be located under "azure-xplat-cli/test/commands" directory
|
146 |
|
147 |
|
148 | ## Using Visual Studio
|
149 | The Visual Studio plugin for node.js can be downloaded from [here](http://nodejstools.codeplex.com/).
|
150 |
|
151 | ### Setting up the project
|
152 | * File --> New --> Project
|
153 | * On the left pane Installed --> Templates --> Javascript --> Node.js
|
154 | * From the available options Select "From Existing Node.js Code"
|
155 | * Provide a name to your project "xplat" and a name to the solution "xplat"
|
156 | * The location of the project would be the location of your cloned repo. Example - "D:\sdk\xplat\azure-sdk-tools-xplat"
|
157 | * Next --> Enter the filter to include files: In the end append the following string "; *._js"
|
158 | * Next --> Including node_modules in the project is optional. (It can always be include later, if the need arises).
|
159 | * Next --> Location for the project file - "D:\sdk\xplat\azure-sdk-tools-xplat\xplat.njsproj" --> Finish
|
160 | * In some time the solution explorer shows the source code files.
|
161 | * For better performance, it is advisable to **disable** intellisense in VisualStudio for Node.js projects by going to
|
162 | * Tools --> Options --> TextEditor --> Node.js --> Intellisense -->No Intellisense.
|
163 | * Set the Tab size and Indentation to 2 spaces by going to
|
164 | * Tools --> Options --> TextEditor --> Node.js --> Tabs --> [Tab size: 2, Indent size: 2]
|
165 | * Tools --> Options --> TextEditor --> Javascript --> Tabs --> [Tab size: 2, Indent size: 2]
|
166 |
|
167 | ### For debugging the commands:
|
168 | * Right Click the "azure-xplat-cli\bin\azure.js" file and set it as Node.js startup file.
|
169 | * From the menu bar go to Project --> Properties
|
170 | * Script arguments should contain the command to execute - group list
|
171 | * Save the changes.
|
172 | * Set breakpoints at desired locations and Press F5 for happy debugging
|
173 | * At times, files with extension "._js" do not hit the breakpoint. It is flaky and nothing can be done about it. At such times, console.log() is your best buddy :+1:
|
174 |
|
175 | ### For debugging the tests:
|
176 | * From the menu bar go to Project --> Properties and set the environment variables required for running tests.
|
177 | * The list of environment variables can be found over [here](https://github.com/Azure/azure-xplat-cli/wiki/Setting-up-environment-variables-for-running-xplat-cli-tests)
|
178 | * If the need arises to add a new environment variable for tests please update the [wiki](https://github.com/Azure/azure-xplat-cli/wiki/Setting-up-environment-variables-for-running-xplat-cli-tests) to keep the list current
|
179 | * Visual Studio can also be opened from **"Visual Studio Debugger Cmd Prompt"** usually located at "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\Shortcuts\Developer Command Prompt for VS2013.lnk" to set the environment variables. Then open the .sln file from the prompt.
|
180 | * In the Solution Explorer, click on a test that needs to be debugged. For example: "azure-xplat-cli\test\commands\cli.account.affinitygroup-tests.js"
|
181 | * In the **Properties** pane select **"Mocha"** as the Test Framework. Save All the changes.
|
182 | * The tests shall be seen in the "Test Explorer". Right Click on any Test and Select "Debug Selected Test". |
\ | No newline at end of file |