MuranoCLI 2.2.4 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. checksums.yaml +4 -4
  2. data/.agignore +3 -0
  3. data/.gitignore +18 -1
  4. data/.rubocop.yml +222 -0
  5. data/.trustme.sh +185 -0
  6. data/.trustme.vim +24 -0
  7. data/Gemfile +23 -4
  8. data/LICENSE.txt +1 -1
  9. data/MuranoCLI.gemspec +43 -8
  10. data/README.markdown +9 -11
  11. data/Rakefile +187 -143
  12. data/TODO.taskpaper +2 -2
  13. data/bin/murano +51 -52
  14. data/docs/basic_example.rst +436 -0
  15. data/docs/completions/murano_completion-bash +3484 -0
  16. data/docs/demo.md +32 -32
  17. data/docs/develop.rst +391 -0
  18. data/lib/MrMurano.rb +21 -7
  19. data/lib/MrMurano/Account.rb +159 -174
  20. data/lib/MrMurano/Business.rb +381 -0
  21. data/lib/MrMurano/Config-Migrate.rb +32 -26
  22. data/lib/MrMurano/Config.rb +407 -128
  23. data/lib/MrMurano/Content.rb +191 -0
  24. data/lib/MrMurano/Gateway.rb +489 -0
  25. data/lib/MrMurano/Keystore.rb +48 -0
  26. data/lib/MrMurano/Passwords.rb +103 -0
  27. data/lib/MrMurano/ProjectFile.rb +121 -79
  28. data/lib/MrMurano/ReCommander.rb +114 -10
  29. data/lib/MrMurano/Setting.rb +90 -0
  30. data/lib/MrMurano/Solution-ServiceConfig.rb +89 -45
  31. data/lib/MrMurano/Solution-Services.rb +461 -166
  32. data/lib/MrMurano/Solution-Users.rb +70 -31
  33. data/lib/MrMurano/Solution.rb +372 -13
  34. data/lib/MrMurano/SolutionId.rb +73 -0
  35. data/lib/MrMurano/SyncRoot.rb +137 -0
  36. data/lib/MrMurano/SyncUpDown.rb +594 -284
  37. data/lib/MrMurano/Webservice-Cors.rb +71 -0
  38. data/lib/MrMurano/Webservice-Endpoint.rb +234 -0
  39. data/lib/MrMurano/Webservice-File.rb +193 -0
  40. data/lib/MrMurano/Webservice.rb +51 -0
  41. data/lib/MrMurano/commands.rb +18 -15
  42. data/lib/MrMurano/commands/business.rb +300 -6
  43. data/lib/MrMurano/commands/completion-bash.erb +166 -0
  44. data/lib/MrMurano/commands/{zshcomplete.erb → completion-zsh.erb} +0 -0
  45. data/lib/MrMurano/commands/completion.rb +76 -39
  46. data/lib/MrMurano/commands/config.rb +108 -44
  47. data/lib/MrMurano/commands/content.rb +115 -72
  48. data/lib/MrMurano/commands/cors.rb +29 -14
  49. data/lib/MrMurano/commands/devices.rb +286 -0
  50. data/lib/MrMurano/commands/domain.rb +52 -12
  51. data/lib/MrMurano/commands/gb.rb +24 -9
  52. data/lib/MrMurano/commands/globals.rb +64 -0
  53. data/lib/MrMurano/commands/init.rb +377 -155
  54. data/lib/MrMurano/commands/keystore.rb +92 -82
  55. data/lib/MrMurano/commands/link.rb +300 -0
  56. data/lib/MrMurano/commands/login.rb +74 -11
  57. data/lib/MrMurano/commands/logs.rb +63 -32
  58. data/lib/MrMurano/commands/mock.rb +57 -29
  59. data/lib/MrMurano/commands/password.rb +57 -39
  60. data/lib/MrMurano/commands/postgresql.rb +127 -94
  61. data/lib/MrMurano/commands/settings.rb +203 -0
  62. data/lib/MrMurano/commands/show.rb +79 -38
  63. data/lib/MrMurano/commands/solution.rb +423 -5
  64. data/lib/MrMurano/commands/solution_picker.rb +547 -0
  65. data/lib/MrMurano/commands/status.rb +195 -61
  66. data/lib/MrMurano/commands/sync.rb +78 -39
  67. data/lib/MrMurano/commands/timeseries.rb +71 -55
  68. data/lib/MrMurano/commands/tsdb.rb +113 -87
  69. data/lib/MrMurano/commands/usage.rb +57 -15
  70. data/lib/MrMurano/hash.rb +100 -10
  71. data/lib/MrMurano/http.rb +187 -43
  72. data/lib/MrMurano/makePretty.rb +16 -14
  73. data/lib/MrMurano/optparse.rb +2178 -0
  74. data/lib/MrMurano/progress.rb +138 -0
  75. data/lib/MrMurano/schema/resource-v1.0.0.yaml +32 -0
  76. data/lib/MrMurano/template/projectFile.murano.erb +16 -13
  77. data/lib/MrMurano/verbosing.rb +166 -29
  78. data/lib/MrMurano/version.rb +30 -1
  79. data/spec/Account-Passwords_spec.rb +21 -4
  80. data/spec/Account_spec.rb +69 -146
  81. data/spec/Business_spec.rb +290 -0
  82. data/spec/ConfigFile_spec.rb +1 -0
  83. data/spec/ConfigMigrate_spec.rb +12 -8
  84. data/spec/Config_spec.rb +40 -34
  85. data/spec/Content_spec.rb +363 -0
  86. data/spec/GatewayBase_spec.rb +54 -0
  87. data/spec/GatewayDevice_spec.rb +321 -0
  88. data/spec/GatewayResource_spec.rb +266 -0
  89. data/spec/GatewaySettings_spec.rb +120 -0
  90. data/spec/Http_spec.rb +18 -8
  91. data/spec/Mock_spec.rb +2 -2
  92. data/spec/ProjectFile_spec.rb +25 -14
  93. data/spec/Setting_spec.rb +110 -0
  94. data/spec/Solution-ServiceConfig_spec.rb +44 -5
  95. data/spec/Solution-ServiceEventHandler_spec.rb +23 -14
  96. data/spec/Solution-ServiceModules_spec.rb +47 -37
  97. data/spec/Solution-UsersRoles_spec.rb +10 -8
  98. data/spec/Solution_spec.rb +17 -8
  99. data/spec/SyncRoot_spec.rb +46 -20
  100. data/spec/SyncUpDown_spec.rb +437 -201
  101. data/spec/Verbosing_spec.rb +12 -4
  102. data/spec/{Solution-Cors_spec.rb → Webservice-Cors_spec.rb} +23 -20
  103. data/spec/{Solution-Endpoint_spec.rb → Webservice-Endpoint_spec.rb} +43 -41
  104. data/spec/{Solution-File_spec.rb → Webservice-File_spec.rb} +44 -33
  105. data/spec/Webservice-Setting_spec.rb +89 -0
  106. data/spec/_workspace.rb +4 -4
  107. data/spec/cmd_business_spec.rb +9 -4
  108. data/spec/cmd_common.rb +44 -1
  109. data/spec/cmd_content_spec.rb +43 -17
  110. data/spec/cmd_cors_spec.rb +4 -4
  111. data/spec/cmd_device_spec.rb +61 -16
  112. data/spec/cmd_domain_spec.rb +29 -6
  113. data/spec/cmd_init_spec.rb +281 -126
  114. data/spec/cmd_keystore_spec.rb +3 -3
  115. data/spec/cmd_link_spec.rb +98 -0
  116. data/spec/cmd_password_spec.rb +1 -1
  117. data/spec/cmd_setting_application_spec.rb +260 -0
  118. data/spec/cmd_setting_product_spec.rb +220 -0
  119. data/spec/cmd_status_spec.rb +223 -114
  120. data/spec/cmd_syncdown_spec.rb +115 -35
  121. data/spec/cmd_syncup_spec.rb +68 -15
  122. data/spec/cmd_usage_spec.rb +35 -8
  123. data/spec/fixtures/dumped_config +6 -4
  124. data/spec/fixtures/gateway_resource_files/resources.notyaml +12 -0
  125. data/spec/fixtures/gateway_resource_files/resources.yaml +13 -0
  126. data/spec/fixtures/gateway_resource_files/resources_invalid.yaml +13 -0
  127. data/spec/fixtures/mrmuranorc_deleted_bob +0 -2
  128. data/spec/fixtures/product_spec_files/lightbulb.yaml +20 -13
  129. data/spec/fixtures/{syncable_content → syncable_conflict}/services/devdata.lua +1 -1
  130. data/spec/fixtures/{syncable_content → syncable_conflict}/services/timers.lua +0 -0
  131. data/spec/spec_helper.rb +5 -0
  132. metadata +262 -171
  133. data/bin/mr +0 -8
  134. data/lib/MrMurano/Product-1P-Device.rb +0 -145
  135. data/lib/MrMurano/Product-Resources.rb +0 -205
  136. data/lib/MrMurano/Product.rb +0 -358
  137. data/lib/MrMurano/Solution-Cors.rb +0 -47
  138. data/lib/MrMurano/Solution-Endpoint.rb +0 -191
  139. data/lib/MrMurano/Solution-File.rb +0 -166
  140. data/lib/MrMurano/commands/assign.rb +0 -57
  141. data/lib/MrMurano/commands/businessList.rb +0 -45
  142. data/lib/MrMurano/commands/product.rb +0 -14
  143. data/lib/MrMurano/commands/productCreate.rb +0 -39
  144. data/lib/MrMurano/commands/productDelete.rb +0 -33
  145. data/lib/MrMurano/commands/productDevice.rb +0 -87
  146. data/lib/MrMurano/commands/productDeviceIdCmds.rb +0 -89
  147. data/lib/MrMurano/commands/productList.rb +0 -45
  148. data/lib/MrMurano/commands/productWrite.rb +0 -27
  149. data/lib/MrMurano/commands/solutionCreate.rb +0 -41
  150. data/lib/MrMurano/commands/solutionDelete.rb +0 -34
  151. data/lib/MrMurano/commands/solutionList.rb +0 -45
  152. data/spec/ProductBase_spec.rb +0 -113
  153. data/spec/ProductContent_spec.rb +0 -162
  154. data/spec/ProductResources_spec.rb +0 -329
  155. data/spec/Product_1P_Device_spec.rb +0 -202
  156. data/spec/Product_1P_RPC_spec.rb +0 -175
  157. data/spec/Product_spec.rb +0 -153
  158. data/spec/Solution-ServiceDevice_spec.rb +0 -176
  159. data/spec/cmd_assign_spec.rb +0 -51
@@ -0,0 +1,54 @@
1
+ require 'fileutils'
2
+ require 'MrMurano/version'
3
+ require 'MrMurano/SyncRoot'
4
+ require 'MrMurano/Gateway'
5
+ require '_workspace'
6
+
7
+ RSpec.describe MrMurano::Gateway::GweBase do
8
+ include_context "WORKSPACE"
9
+ before(:example) do
10
+ MrMurano::SyncRoot.instance.reset
11
+ $cfg = MrMurano::Config.new
12
+ $cfg.load
13
+ $cfg['net.host'] = 'bizapi.hosted.exosite.io'
14
+ $cfg['product.id'] = 'XYZ'
15
+
16
+ @gw = MrMurano::Gateway::GweBase.new
17
+ allow(@gw).to receive(:token).and_return("TTTTTTTTTT")
18
+ end
19
+
20
+ it "initializes" do
21
+ uri = @gw.endpoint('/')
22
+ expect(uri.to_s).to eq("https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/")
23
+ end
24
+
25
+ it "gets info" do
26
+ body = {
27
+ :name=>"XXXXXXXX",
28
+ :protocol=>{:name=>"onep", :auth_type=>"cik"},
29
+ :description=>"XXXXXXXX",
30
+ :identity_format=> {
31
+ :prefix=>"", :type=>"opaque", :options=>{:casing=>"mixed", :length=>0}},
32
+ :fqdn=>"XXXXXXXX.m2.exosite-staging.io",
33
+ :provisioning=> {
34
+ :enabled=>true,
35
+ :generate_identity=>true,
36
+ :presenter_identity=>true,
37
+ :ip_whitelisting=>{:enabled=>false, :allowed=>[]}},
38
+ :resources=> {
39
+ :bob=>{:format=>"string", :unit=>"c", :settable=>true},
40
+ :fuzz=>{:format=>"string", :unit=>"c", :settable=>true},
41
+ :gruble=>{:format=>"string", :unit=>"bits", :settable=>true}
42
+ }
43
+ }
44
+ stub_request(:get, "https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2").
45
+ with(:headers => {'Authorization'=>'token TTTTTTTTTT', 'Content-Type'=>'application/json'}).
46
+ to_return(:status => 200, :body => body.to_json, :headers => {})
47
+
48
+ ret = @gw.info
49
+ expect(ret).to eq(body)
50
+ end
51
+
52
+ end
53
+
54
+ # vim: set ai et sw=2 ts=2 :
@@ -0,0 +1,321 @@
1
+ require 'fileutils'
2
+ require 'MrMurano/version'
3
+ require 'MrMurano/Gateway'
4
+ require 'MrMurano/SyncRoot'
5
+ require '_workspace'
6
+
7
+ RSpec.describe MrMurano::Gateway::Device do
8
+ include_context "WORKSPACE"
9
+ before(:example) do
10
+ MrMurano::SyncRoot.instance.reset
11
+ $cfg = MrMurano::Config.new
12
+ $cfg.load
13
+ $cfg['net.host'] = 'bizapi.hosted.exosite.io'
14
+ $cfg['product.id'] = 'XYZ'
15
+
16
+ @gw = MrMurano::Gateway::Device.new
17
+ allow(@gw).to receive(:token).and_return("TTTTTTTTTT")
18
+ end
19
+
20
+ it "initializes" do
21
+ uri = @gw.endpoint('/')
22
+ expect(uri.to_s).to eq("https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identity/")
23
+ end
24
+
25
+ context "listing" do
26
+ it "lists" do
27
+ body = {
28
+ :mayLoadMore=>false,
29
+ :devices=>
30
+ [{:identity=>"58",
31
+ :auth=>{:type=>"cik"},
32
+ :state=>{},
33
+ :locked=>false,
34
+ :reprovision=>false,
35
+ :devmode=>false,
36
+ :lastip=>"",
37
+ :lastseen=>1487021743864000,
38
+ :status=>"provisioned",
39
+ :online=>false},
40
+ {:identity=>"56",
41
+ :auth=>{:type=>"cik"},
42
+ :state=>{},
43
+ :locked=>false,
44
+ :reprovision=>false,
45
+ :devmode=>false,
46
+ :lastip=>"",
47
+ :lastseen=>1487021650584000,
48
+ :status=>"provisioned",
49
+ :online=>false},
50
+ ]}
51
+ stub_request(:get, "https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identity/").
52
+ to_return(:body=>body.to_json)
53
+
54
+ ret = @gw.list
55
+ expect(ret).to eq(body)
56
+ end
57
+
58
+ it "lists with limit" do
59
+ body = {
60
+ :mayLoadMore=>false,
61
+ :devices=>
62
+ [{:identity=>"58",
63
+ :auth=>{:type=>"cik"},
64
+ :state=>{},
65
+ :locked=>false,
66
+ :reprovision=>false,
67
+ :devmode=>false,
68
+ :lastip=>"",
69
+ :lastseen=>1487021743864000,
70
+ :status=>"provisioned",
71
+ :online=>false},
72
+ ]}
73
+ stub_request(:get, "https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identity/").
74
+ with(:query=>{:limit=>'1'}).
75
+ to_return(:body=>body.to_json)
76
+
77
+ ret = @gw.list(1)
78
+ expect(ret).to eq(body)
79
+ end
80
+
81
+ it "lists with before" do
82
+ body = {
83
+ :mayLoadMore=>false,
84
+ :devices=>
85
+ [{:identity=>"58",
86
+ :auth=>{:type=>"cik"},
87
+ :state=>{},
88
+ :locked=>false,
89
+ :reprovision=>false,
90
+ :devmode=>false,
91
+ :lastip=>"",
92
+ :lastseen=>1487021743864000,
93
+ :status=>"provisioned",
94
+ :online=>false},
95
+ ]}
96
+ stub_request(:get, "https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identity/").
97
+ with(:query=>{:limit=>'1', :before=>'1487021743864000'}).
98
+ to_return(:body=>body.to_json)
99
+
100
+ ret = @gw.list(1, 1487021743864000)
101
+ expect(ret).to eq(body)
102
+ end
103
+ end
104
+
105
+ it "fetches one" do
106
+ body = {
107
+ :identity=>"58",
108
+ :auth=>{:type=>"cik"},
109
+ :state=>{},
110
+ :locked=>false,
111
+ :reprovision=>false,
112
+ :devmode=>false,
113
+ :lastip=>"",
114
+ :lastseen=>1487021743864000,
115
+ :status=>"provisioned",
116
+ :online=>false}
117
+ stub_request(:get, "https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identity/58").
118
+ to_return(:body=>body.to_json)
119
+
120
+ ret = @gw.fetch(58)
121
+ expect(ret).to eq(body)
122
+ end
123
+
124
+ it "enables one" do
125
+ body = {
126
+ :identity=>"58",
127
+ :auth=>{:type=>"cik"},
128
+ :state=>{},
129
+ :locked=>false,
130
+ :reprovision=>false,
131
+ :devmode=>false,
132
+ :lastip=>"",
133
+ :lastseen=>1487021743864000,
134
+ :status=>"provisioned",
135
+ :online=>false}
136
+ stub_request(:put, "https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identity/58").
137
+ to_return(:body=>body.to_json)
138
+
139
+ ret = @gw.enable(58)
140
+ expect(ret).to eq(body)
141
+ end
142
+
143
+ it "enables with options" do
144
+ body = {
145
+ :identity=>"58",
146
+ :auth=>{:type=>"cik"},
147
+ :state=>{},
148
+ :locked=>false,
149
+ :reprovision=>false,
150
+ :devmode=>false,
151
+ :lastip=>"",
152
+ :lastseen=>1487021743864000,
153
+ :status=>"provisioned",
154
+ :online=>false}
155
+ stub_request(:put, "https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identity/58").
156
+ with(:body=>{:type=>:certificate,:expire=>123456}.to_json).
157
+ to_return(:body=>body.to_json)
158
+
159
+ ret = @gw.enable(58, :type=>:certificate, :expire=>123456)
160
+ expect(ret).to eq(body)
161
+ end
162
+
163
+ it "enables with extra options" do
164
+ body = {
165
+ :identity=>"58",
166
+ :auth=>{:type=>"cik"},
167
+ :state=>{},
168
+ :locked=>false,
169
+ :reprovision=>false,
170
+ :devmode=>false,
171
+ :lastip=>"",
172
+ :lastseen=>1487021743864000,
173
+ :status=>"provisioned",
174
+ :online=>false}
175
+ stub_request(:put, "https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identity/58").
176
+ with(:body=>{:type=>:certificate,:expire=>123456}.to_json).
177
+ to_return(:body=>body.to_json)
178
+
179
+ ret = @gw.enable(58, :go=>:blueteam, :type=>:certificate, :expire=>123456, :bob=>:built)
180
+ expect(ret).to eq(body)
181
+ end
182
+
183
+ it "removes one" do
184
+ body = {
185
+ :identity=>"58",
186
+ :auth=>{:type=>"cik"},
187
+ :state=>{},
188
+ :locked=>false,
189
+ :reprovision=>false,
190
+ :devmode=>false,
191
+ :lastip=>"",
192
+ :lastseen=>1487021743864000,
193
+ :status=>"provisioned",
194
+ :online=>false}
195
+ stub_request(:delete, "https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identity/58").
196
+ to_return(:body=>body.to_json)
197
+
198
+ ret = @gw.remove(58)
199
+ expect(ret).to eq(body)
200
+ end
201
+
202
+ context "activates" do
203
+ before(:example) do
204
+ @bgw = MrMurano::Gateway::GweBase.new
205
+ allow(@bgw).to receive(:token).and_return("TTTTTTTTTT")
206
+ expect(MrMurano::Gateway::GweBase).to receive(:new).and_return(@bgw)
207
+ allow(@gw).to receive(:token).and_return("TTTTTTTTTT")
208
+ stub_request(:get, "https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2").
209
+ to_return(:body=>{:fqdn=>"xxxxx.m2.exosite-staging.io"}.to_json)
210
+ end
211
+ it "succeeds" do
212
+ stub_request(:post, "https://xxxxx.m2.exosite-staging.io/provision/activate").
213
+ to_return(:body=>'XXXXXXXX')
214
+
215
+ ret = @gw.activate(58)
216
+ expect(ret).to eq('XXXXXXXX')
217
+ end
218
+
219
+ it "was already activated" do
220
+ stub_request(:post, "https://xxxxx.m2.exosite-staging.io/provision/activate").
221
+ to_return(:status => 409)
222
+
223
+ saved = $stderr
224
+ $stderr = StringIO.new
225
+
226
+ #@gw.activate(58)
227
+ #expect($stderr.string).to eq("\e[31mRequest Failed: 409: nil\e[0m\n")
228
+ expect {
229
+ @gw.activate(58)
230
+ }.to raise_error(SystemExit).and output("\e[31mThe specified device is already activated.\e[0m\n").to_stderr
231
+ $stderr = saved
232
+ end
233
+
234
+ it "wasn't enabled" do
235
+ stub_request(:post, "https://xxxxx.m2.exosite-staging.io/provision/activate").
236
+ to_return(:status => 404)
237
+
238
+ saved = $stderr
239
+ $stderr = StringIO.new
240
+
241
+ @gw.activate(58)
242
+ expect($stderr.string).to eq("\e[31mRequest Failed: 404: nil\e[0m\n")
243
+ $stderr = saved
244
+ end
245
+ end
246
+
247
+ context "enables batch" do
248
+ it "enables from cvs" do
249
+ File.open('ids.csv', 'w') {|io| io << "ID\n1\n2\n3\n4\n5"}
250
+ stub_request(:post, 'https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identities').
251
+ with(:headers=>{'Content-Type'=>%r{^multipart/form-data.*}}) do |request|
252
+ request.body.to_s =~ %r{Content-Type: text/csv\r\n\r\nID\r?\n1\r?\n2\r?\n3\r?\n4\r?\n5}
253
+ end
254
+ @gw.enable_batch('ids.csv')
255
+ end
256
+
257
+ it "but file is missing" do
258
+ expect{@gw.enable_batch('ids.csv')}.to raise_error(Errno::ENOENT)
259
+ end
260
+
261
+ it "but file is not text" do
262
+ File.open('ids.csv', 'wb') {|io| io << "\0\0\0\0"}
263
+ stub_request(:post, 'https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identities').
264
+ to_return(:status=>400, :body => "CSV file format invalid")
265
+ saved = $stderr
266
+ $stderr = StringIO.new
267
+ @gw.enable_batch('ids.csv')
268
+ expect($stderr.string).to eq(%{\e[31mRequest Failed: 400: CSV file format invalid\e[0m\n})
269
+ $stderr = saved
270
+ end
271
+
272
+ it "but file is missing header" do
273
+ File.open('ids.csv', 'w') {|io| io << "1\n2\n3\n4\n5"}
274
+ stub_request(:post, 'https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identities').
275
+ with(:headers=>{'Content-Type'=>%r{^multipart/form-data.*}}) do |request|
276
+ request.body.to_s =~ %r{Content-Type: text/csv\r\n\r\n1\r?\n2\r?\n3\r?\n4\r?\n5}
277
+ end.to_return(:status=>400, :body => "CSV file format invalid")
278
+ saved = $stderr
279
+ $stderr = StringIO.new
280
+ @gw.enable_batch('ids.csv')
281
+ expect($stderr.string).to eq(%{\e[31mRequest Failed: 400: CSV file format invalid\e[0m\n})
282
+ $stderr = saved
283
+ end
284
+ end
285
+
286
+ it "reads state" do
287
+ body = {:bob=>{:reported=>"9", :set=>"9", :timestamp=>1487021046160363}}
288
+ stub_request(:get, 'https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identity/56/state').
289
+ to_return(:body=>body.to_json)
290
+
291
+ ret = @gw.read(56)
292
+ expect(ret).to eq(body)
293
+ end
294
+
295
+ context "writes state" do
296
+ it "succeeds" do
297
+ body = {:bob=>"fuzz"}
298
+ stub_request(:patch, 'https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identity/56/state').
299
+ with(:body=>body.to_json)
300
+
301
+ @gw.write(56, :bob=>'fuzz')
302
+ end
303
+
304
+ it "fails" do
305
+ body = {:bob=>"fuzz"}
306
+ stub_request(:patch, 'https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/identity/56/state').
307
+ with(:body=>body.to_json).
308
+ to_return(:status=> 400, :body => 'Value is not settable')
309
+
310
+ saved = $stderr
311
+ $stderr = StringIO.new
312
+
313
+ @gw.write(56, :bob=>'fuzz')
314
+ expect($stderr.string).to eq("\e[31mRequest Failed: 400: Value is not settable\e[0m\n")
315
+ $stderr = saved
316
+ end
317
+ end
318
+
319
+ end
320
+
321
+ # vim: set ai et sw=2 ts=2 :
@@ -0,0 +1,266 @@
1
+ require 'fileutils'
2
+ require 'MrMurano/version'
3
+ require 'MrMurano/Gateway'
4
+ require 'MrMurano/ProjectFile'
5
+ require 'MrMurano/SyncRoot'
6
+ require '_workspace'
7
+
8
+ RSpec.describe MrMurano::Gateway::Resources do
9
+ include_context "WORKSPACE"
10
+ before(:example) do
11
+ MrMurano::SyncRoot.instance.reset
12
+ $cfg = MrMurano::Config.new
13
+ $cfg.load
14
+ $cfg['net.host'] = 'bizapi.hosted.exosite.io'
15
+ $cfg['product.id'] = 'XYZ'
16
+ $project = MrMurano::ProjectFile.new
17
+ $project.load
18
+
19
+ @gw = MrMurano::Gateway::Resources.new
20
+ allow(@gw).to receive(:token).and_return("TTTTTTTTTT")
21
+ end
22
+
23
+ it "initializes" do
24
+ uri = @gw.endpoint('/')
25
+ expect(uri.to_s).to eq("https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2/")
26
+ end
27
+
28
+ it "lists" do
29
+ resources = [
30
+ {:format=>"string", :unit=>"c", :settable=>true, :alias=>"bob"},
31
+ {:format=>"string", :unit=>"c", :settable=>true, :alias=>"fuzz"},
32
+ {:format=>"string", :unit=>"bits", :settable=>true, :alias=>"gruble"}
33
+ ]
34
+ body = { :resources => {
35
+ :bob=>{:format=>"string", :unit=>"c", :settable=>true},
36
+ :fuzz=>{:format=>"string", :unit=>"c", :settable=>true},
37
+ :gruble=>{:format=>"string", :unit=>"bits", :settable=>true}
38
+ }}
39
+ stub_request(:get, 'https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2').
40
+ to_return(:body=>body.to_json)
41
+
42
+ ret = @gw.list
43
+ expect(ret).to eq(resources)
44
+ end
45
+
46
+ it "uploads all" do
47
+ res_before = [
48
+ {:format=>"string", :unit=>"c", :settable=>true, :alias=>"bob"},
49
+ {:format=>"string", :unit=>"c", :settable=>true, :alias=>"fuzz"},
50
+ {:format=>"string", :unit=>"bits", :settable=>true, :alias=>"gruble"}
51
+ ]
52
+ res_after = {
53
+ :bob=>{:format=>"string", :unit=>"c", :settable=>true},
54
+ :fuzz=>{:format=>"string", :unit=>"c", :settable=>true},
55
+ :gruble=>{:format=>"string", :unit=>"bits", :settable=>true}
56
+ }
57
+ stub_request(:patch, 'https://bizapi.hosted.exosite.io/api:1/service/XYZ/device2').
58
+ with(:body=>{:resources=>res_after}.to_json)
59
+
60
+ ret = @gw.upload_all(res_before)
61
+ expect(ret).to eq({})
62
+ end
63
+
64
+ context "compares" do
65
+ before(:example) do
66
+ @iA = {:format=>"string", :unit=>"c", :settable=>true, :alias=>"bob"}
67
+ @iB = {:format=>"string", :unit=>"c", :settable=>true, :alias=>"bob"}
68
+ end
69
+ it "Are equal" do
70
+ ret = @gw.docmp(@iA, @iB)
71
+ expect(ret).to eq(false)
72
+ end
73
+ end
74
+
75
+ context "Lookup functions" do
76
+ it "local path is into" do
77
+ ret = @gw.tolocalpath('bob', 'rich')
78
+ expect(ret).to eq('bob')
79
+ end
80
+
81
+ it "gets synckey" do
82
+ ret = @gw.synckey({ :alias=>'bob' })
83
+ expect(ret).to eq("bob")
84
+ end
85
+ end
86
+
87
+ context "local items" do
88
+ it "succeeds" do
89
+ resfile = Pathname.new('resources.yaml')
90
+ src = File.join(@testdir, 'spec', 'fixtures', 'gateway_resource_files', 'resources.yaml')
91
+ FileUtils.copy(src, resfile.to_path)
92
+ ret = @gw.localitems(resfile)
93
+ expect(ret).to eq([
94
+ {:format=>"string", :unit=>"c", :settable=>true, :alias=>"bob"},
95
+ {:format=>"string", :unit=>"c", :settable=>true, :alias=>"fuzz"},
96
+ {:format=>"string", :unit=>"bits", :settable=>true, :alias=>"gruble"}
97
+ ])
98
+ end
99
+
100
+ it "missing file" do
101
+ resfile = Pathname.new('resources.yaml')
102
+ saved = $stderr
103
+ $stderr = StringIO.new
104
+ ret = @gw.localitems(resfile)
105
+ expect(ret).to eq([])
106
+ expect($stderr.string).to eq("\e[33mSkipping missing resources.yaml\e[0m\n")
107
+ $stderr = saved
108
+ end
109
+
110
+ it "isn't a file" do
111
+ resfile = Pathname.new('resources.yaml')
112
+ FileUtils.mkpath('resources.yaml')
113
+ saved = $stderr
114
+ $stderr = StringIO.new
115
+ ret = @gw.localitems(resfile)
116
+ expect(ret).to eq([])
117
+ expect($stderr.string).to eq("\e[33mCannot read from resources.yaml\e[0m\n")
118
+ $stderr = saved
119
+ end
120
+
121
+ it "isn't yaml" do
122
+ resfile = Pathname.new('resources.yaml')
123
+ src = File.join(@testdir, 'spec', 'fixtures', 'gateway_resource_files', 'resources.notyaml')
124
+ FileUtils.copy(src, resfile.to_path)
125
+ #expect{ @gw.localitems(resfile) }.to raise_error(JSON::Schema::ValidationError)
126
+ expect {
127
+ @gw.localitems(resfile)
128
+ }.to raise_error(SystemExit).and output("\e[31mThere is an error in the config file, resources.yaml\e[0m\n\e[31m\"The property '#/' of type Array did not match the following type: object\"\e[0m\n").to_stderr
129
+ end
130
+
131
+ it "isn't valid" do
132
+ resfile = Pathname.new('resources.yaml')
133
+ src = File.join(@testdir, 'spec', 'fixtures', 'gateway_resource_files', 'resources.notyaml')
134
+ FileUtils.copy(src, resfile.to_path)
135
+ #expect{ @gw.localitems(resfile) }.to raise_error(JSON::Schema::ValidationError)
136
+ expect {
137
+ @gw.localitems(resfile)
138
+ }.to raise_error(SystemExit).and output("\e[31mThere is an error in the config file, resources.yaml\e[0m\n\e[31m\"The property '#/' of type Array did not match the following type: object\"\e[0m\n").to_stderr
139
+ end
140
+ end
141
+
142
+ context "syncup" do
143
+ before(:example) do
144
+ expect(@gw).to receive(:list).once.and_return([
145
+ {:format=>"string", :unit=>"c", :settable=>true, :alias=>"bob"},
146
+ {:format=>"string", :unit=>"c", :settable=>true, :alias=>"fuzz"},
147
+ {:format=>"string", :unit=>"bits", :settable=>true, :alias=>"gruble"}
148
+ ])
149
+ end
150
+ it "removes keys" do
151
+ expect(@gw).to receive(:upload_all).with([
152
+ {:format=>"string", :unit=>"c", :settable=>true, :alias=>"bob"},
153
+ {:format=>"string", :unit=>"bits", :settable=>true, :alias=>"gruble"}
154
+ ])
155
+
156
+ @gw.syncup_before
157
+ @gw.remove('fuzz')
158
+ @gw.syncup_after
159
+ end
160
+
161
+ it "adds keys" do
162
+ expect(@gw).to receive(:upload_all).with([
163
+ {:format=>"string", :unit=>"c", :settable=>true, :alias=>"bob"},
164
+ {:format=>"string", :unit=>"c", :settable=>true, :alias=>"fuzz"},
165
+ {:format=>"string", :unit=>"bits", :settable=>true, :alias=>"gruble"},
166
+ {:format=>"number", :unit=>"bibs", :settable=>false, :alias=>"greeble"},
167
+ ])
168
+
169
+ @gw.syncup_before
170
+ @gw.upload(nil, {:format=>"number", :unit=>"bibs", :settable=>false, :alias=>"greeble"}, nil)
171
+ @gw.syncup_after
172
+ end
173
+
174
+ it "replaces keys" do
175
+ expect(@gw).to receive(:upload_all).with([
176
+ {:format=>"string", :unit=>"c", :settable=>true, :alias=>"bob"},
177
+ {:format=>"string", :unit=>"bits", :settable=>true, :alias=>"gruble"},
178
+ {:format=>"number", :unit=>"bibs", :settable=>false, :alias=>"fuzz"},
179
+ ])
180
+
181
+ @gw.syncup_before
182
+ @gw.upload(nil, {:format=>"number", :unit=>"bibs", :settable=>false, :alias=>"fuzz"}, nil)
183
+ @gw.syncup_after
184
+ end
185
+ end
186
+
187
+ context "syncdown" do
188
+ before(:example) do
189
+ # 2017-07-05: [lb] not sure what I had to add this just now;
190
+ # this test had been working fine... I added a check for
191
+ # duplicate local resource in locallist(), but this smells
192
+ # really funny. I cannot explain how this test used to work,
193
+ # nor why it started failing; but I had to add a mkpath on
194
+ # 'specs', and I changed the copy to go into the specs/
195
+ # directory, and I removed the extra empty array in the
196
+ # and_return check.
197
+ FileUtils.mkpath('specs')
198
+ resfile = Pathname.new(File.join('specs', 'resources.yaml'))
199
+ src = File.join(@testdir, 'spec', 'fixtures', 'gateway_resource_files', 'resources.yaml')
200
+ FileUtils.copy(src, resfile.to_path)
201
+
202
+ expect(@gw).to receive(:locallist).once.and_return(
203
+ [
204
+ {:format=>"string", :unit=>"c", :settable=>true, :alias=>"bob"},
205
+ {:format=>"string", :unit=>"c", :settable=>true, :alias=>"fuzz"},
206
+ {:format=>"string", :unit=>"bits", :settable=>true, :alias=>"gruble"}
207
+ ],
208
+ # 2017-07-05: [lb] also had to remove this check.
209
+ # locallist returns a single array, not two of them!
210
+ #[],
211
+ )
212
+
213
+ @io = instance_double("IO")
214
+ @p = instance_double('Pathname')
215
+ expect(@p).to receive(:dirname).and_return(resfile.dirname)
216
+ expect(@p).to receive(:extname).and_return(resfile.extname)
217
+ expect(@p).to receive(:open).and_yield(@io)
218
+ end
219
+ it "removes keys" do
220
+ expect(@io).to receive(:write) do |args|
221
+ fy = YAML.load(args)
222
+ expect(fy).to eq({
223
+ 'bob'=>{'format'=>"string", 'unit'=>"c", 'settable'=>true},
224
+ 'gruble'=>{'format'=>"string", 'unit'=>"bits", 'settable'=>true}
225
+ })
226
+ end
227
+
228
+ @gw.syncdown_before
229
+ @gw.removelocal(@p, {:alias=>'fuzz'})
230
+ @gw.syncdown_after(@p)
231
+ end
232
+
233
+ it "adds keys" do
234
+ expect(@io).to receive(:write) do |args|
235
+ fy = YAML.load(args)
236
+ expect(fy).to eq({
237
+ 'bob'=>{'format'=>"string", 'unit'=>"c", 'settable'=>true},
238
+ 'gruble'=>{'format'=>"string", 'unit'=>"bits", 'settable'=>true},
239
+ 'fuzz'=>{'format'=>"string", 'unit'=>"c", 'settable'=>true},
240
+ 'greeble'=>{'format'=>"number", 'unit'=>"bibs", 'settable'=>false},
241
+ })
242
+ end
243
+
244
+ @gw.syncdown_before
245
+ @gw.download(@p, {:format=>"number", :unit=>"bibs", :settable=>false, :alias=>"greeble"})
246
+ @gw.syncdown_after(@p)
247
+ end
248
+
249
+ it "replaces keys" do
250
+ expect(@io).to receive(:write) do |args|
251
+ fy = YAML.load(args)
252
+ expect(fy).to eq({
253
+ 'bob'=>{'format'=>"string", 'unit'=>"c", 'settable'=>true},
254
+ 'gruble'=>{'format'=>"string", 'unit'=>"bits", 'settable'=>true},
255
+ 'fuzz'=>{'format'=>"number", 'unit'=>"bibs", 'settable'=>false},
256
+ })
257
+ end
258
+
259
+ @gw.syncdown_before
260
+ @gw.download(@p, {:format=>"number", :unit=>"bibs", :settable=>false, :alias=>"fuzz"})
261
+ @gw.syncdown_after(@p)
262
+ end
263
+ end
264
+ end
265
+
266
+ # vim: set ai et sw=2 ts=2 :