summaryrefslogtreecommitdiff
path: root/spec/functional/resource/windows_task_spec.rb
blob: 2839b1228819808a7e2208c42c845051a32ea2c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
#
# Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.com>)
# Copyright:: Copyright (c) 2016 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "spec_helper"
require "chef/provider/windows_task"

describe Chef::Resource::WindowsTask, :windows_only do
  let(:task_name) { "chef-client" }
  let(:new_resource) { Chef::Resource::WindowsTask.new(task_name) }
  let(:windows_task_provider) do
    node = Chef::Node.new
    events = Chef::EventDispatch::Dispatcher.new
    run_context = Chef::RunContext.new(node, {}, events)
    Chef::Provider::WindowsTask.new(new_resource, run_context)
  end

  describe "action :create" do
    after { delete_task }
    context "when frequency and frequency_modifier are not passed" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        # Make sure MM/DD/YYYY is accepted
        new_resource.start_day "09/20/2017"
        new_resource
      end

      it "creates a scheduled task to run every 1 hr starting on 09/20/2017" do
        subject.run_action(:create)
        task_details = windows_task_provider.send(:load_task_hash, task_name)
        expect(task_details[:TaskName]).to eq("\\chef-client")
        expect(task_details[:TaskToRun]).to eq("chef-client")
        expect(task_details[:"Repeat:Every"]).to eq("1 Hour(s), 0 Minute(s)")

        # This test will not work across locales
        expect(task_details[:StartDate]).to eq("9/20/2017")
      end

      it "does not converge the resource if it is already converged" do
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "frequency :minute" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :minute
        new_resource.frequency_modifier 15
        new_resource
      end

      it "creates a scheduled task that runs after every 15 minutes" do
        subject.run_action(:create)
        task_details = windows_task_provider.send(:load_task_hash, task_name)
        expect(task_details[:TaskName]).to eq("\\chef-client")
        expect(task_details[:TaskToRun]).to eq("chef-client")
        expect(task_details[:"Repeat:Every"]).to eq("0 Hour(s), 15 Minute(s)")
        expect(task_details[:run_level]).to eq("HighestAvailable")
      end

      it "does not converge the resource if it is already converged" do
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "frequency :hourly" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :hourly
        new_resource.frequency_modifier 3
        new_resource
      end

      it "creates a scheduled task that runs after every 3 hrs" do
        subject.run_action(:create)
        task_details = windows_task_provider.send(:load_task_hash, task_name)
        expect(task_details[:TaskName]).to eq("\\chef-client")
        expect(task_details[:TaskToRun]).to eq("chef-client")
        expect(task_details[:"Repeat:Every"]).to eq("3 Hour(s), 0 Minute(s)")
        expect(task_details[:run_level]).to eq("HighestAvailable")
      end

      it "does not converge the resource if it is already converged" do
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "frequency :daily" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :daily
        new_resource
      end

      it "creates a scheduled task to run daily" do
        subject.run_action(:create)
        task_details = windows_task_provider.send(:load_task_hash, task_name)
        expect(task_details[:TaskName]).to eq("\\chef-client")
        expect(task_details[:TaskToRun]).to eq("chef-client")
        expect(task_details[:ScheduleType]).to eq("Daily")
        expect(task_details[:Days]).to eq("Every 1 day(s)")
        expect(task_details[:run_level]).to eq("HighestAvailable")
      end

      it "does not converge the resource if it is already converged" do
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "frequency :monthly" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :monthly
        new_resource.frequency_modifier 2
        new_resource
      end

      it "creates a scheduled task to every 2 months" do
        subject.run_action(:create)
        task_details = windows_task_provider.send(:load_task_hash, task_name)
        expect(task_details[:TaskName]).to eq("\\chef-client")
        expect(task_details[:TaskToRun]).to eq("chef-client")
        expect(task_details[:ScheduleType]).to eq("Monthly")
        expect(task_details[:Months]).to eq("FEB, APR, JUN, AUG, OCT, DEC")
        expect(task_details[:run_level]).to eq("HighestAvailable")
      end

      it "does not converge the resource if it is already converged" do
        skip "This functionality needs to be handle"
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "frequency :once" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :once
        new_resource
      end

      context "when start_time is not provided" do
        it "raises argument error" do
          expect { subject.run_action(:create) }.to raise_error(Mixlib::ShellOut::ShellCommandFailed)
        end
      end

      context "when start_time is provided" do
        it "creates the scheduled task to run once at 5pm" do
          subject.start_time "17:00"
          subject.run_action(:create)
          task_details = windows_task_provider.send(:load_task_hash, task_name)
          expect(task_details[:TaskName]).to eq("\\chef-client")
          expect(task_details[:TaskToRun]).to eq("chef-client")
          expect(task_details[:ScheduleType]).to eq("One Time Only")
          expect(task_details[:StartTime]).to eq("5:00:00 PM")
          expect(task_details[:run_level]).to eq("HighestAvailable")
        end

        it "does not converge the resource if it is already converged" do
          subject.start_time "17:00"
          subject.run_action(:create)
          subject.run_action(:create)
          expect(subject).not_to be_updated_by_last_action
        end
      end
    end

    context "frequency :none" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :none
        new_resource
      end

      it "creates the scheduled task to run on demand only" do
        subject.run_action(:create)
        task_details = windows_task_provider.send(:load_task_hash, task_name)

        expect(task_details[:TaskName]).to eq("\\chef-client")
        expect(task_details[:TaskToRun]).to eq("chef-client")
        expect(task_details[:ScheduleType]).to eq("On demand only")
        expect(task_details[:StartTime]).to eq("N/A")
        expect(task_details[:StartDate]).to eq("N/A")
        expect(task_details[:NextRunTime]).to eq("N/A")
        expect(task_details[:none]).to eq(true)
        expect(task_details[:run_level]).to eq("HighestAvailable")
      end

      it "does not converge the resource if it is already converged" do
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "frequency :onstart" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :onstart
        new_resource
      end

      it "creates the scheduled task to run at system start up" do
        subject.run_action(:create)
        task_details = windows_task_provider.send(:load_task_hash, task_name)

        expect(task_details[:TaskName]).to eq("\\chef-client")
        expect(task_details[:TaskToRun]).to eq("chef-client")
        expect(task_details[:ScheduleType]).to eq("At system start up")
        expect(task_details[:StartTime]).to eq("N/A")
        expect(task_details[:StartDate]).to eq("N/A")
        expect(task_details[:NextRunTime]).to eq("N/A")
        expect(task_details[:onstart]).to eq(true)
        expect(task_details[:run_level]).to eq("HighestAvailable")
      end

      it "does not converge the resource if it is already converged" do
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "frequency :weekly" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :weekly
        new_resource
      end

      it "creates the scheduled task to run weekly" do
        subject.run_action(:create)
        task_details = windows_task_provider.send(:load_task_hash, task_name)
        expect(task_details[:TaskName]).to eq("\\chef-client")
        expect(task_details[:TaskToRun]).to eq("chef-client")
        expect(task_details[:ScheduleType]).to eq("Weekly")
        expect(task_details[:Months]).to eq("Every 1 week(s)")
        expect(task_details[:run_level]).to eq("HighestAvailable")
      end

      it "does not converge the resource if it is already converged" do
        skip "This functionality needs to be handle"
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end

      context "when days are provided" do
        it "creates the scheduled task to run on particular days" do
          subject.day "Mon, Fri"
          subject.frequency_modifier 2
          subject.run_action(:create)
          task_details = windows_task_provider.send(:load_task_hash, task_name)
          expect(task_details[:TaskName]).to eq("\\chef-client")
          expect(task_details[:TaskToRun]).to eq("chef-client")
          expect(task_details[:Days]).to eq("MON, FRI")
          expect(task_details[:ScheduleType]).to eq("Weekly")
          expect(task_details[:Months]).to eq("Every 2 week(s)")
          expect(task_details[:run_level]).to eq("HighestAvailable")
        end

        it "does not converge the resource if it is already converged" do
          subject.day "Mon, Fri"
          subject.frequency_modifier 2
          subject.run_action(:create)
          subject.run_action(:create)
          expect(subject).not_to be_updated_by_last_action
        end
      end

      context "when invalid day is passed" do
        it "raises error" do
          subject.day "abc"
          expect { subject.run_action(:create) }.to raise_error(Mixlib::ShellOut::ShellCommandFailed)
        end
      end

      context "when months are passed" do
        it "raises error that months are supported only when frequency=:monthly" do
          subject.months "Jan"
          expect { subject.run_action(:create) }.to raise_error(Mixlib::ShellOut::ShellCommandFailed)
        end
      end
    end

    context "frequency :on_logon" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :on_logon
        new_resource
      end

      it "creates the scheduled task to on logon" do
        subject.run_action(:create)
        task_details = windows_task_provider.send(:load_task_hash, task_name)
        expect(task_details[:TaskName]).to eq("\\chef-client")
        expect(task_details[:TaskToRun]).to eq("chef-client")
        expect(task_details[:ScheduleType]).to eq("At logon time")
        expect(task_details[:run_level]).to eq("HighestAvailable")
      end

      it "does not converge the resource if it is already converged" do
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "frequency :on_idle" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :on_idle
        new_resource
      end

      context "when idle_time is not passed" do
        it "raises error" do
          expect { subject.run_action(:create) }.to raise_error(Mixlib::ShellOut::ShellCommandFailed)
        end
      end

      context "when idle_time is passed" do
        it "creates the scheduled task to run when system is idle" do
          subject.idle_time 20
          subject.run_action(:create)
          task_details = windows_task_provider.send(:load_task_hash, task_name)
          expect(task_details[:TaskName]).to eq("\\chef-client")
          expect(task_details[:TaskToRun]).to eq("chef-client")
          expect(task_details[:ScheduleType]).to eq("At idle time")
          expect(task_details[:run_level]).to eq("HighestAvailable")
          expect(task_details[:idle_time]).to eq("PT20M")
        end

        it "does not converge the resource if it is already converged" do
          subject.idle_time 20
          subject.run_action(:create)
          subject.run_action(:create)
          expect(subject).not_to be_updated_by_last_action
        end
      end
    end

    context "when random_delay is passed" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource
      end

      it "sets the random_delay for frequency :minute" do
        subject.frequency :minute
        subject.random_delay "PT20M"
        subject.run_action(:create)
        task_details = windows_task_provider.send(:load_task_hash, task_name)
        expect(task_details[:TaskName]).to eq("\\chef-client")
        expect(task_details[:ScheduleType]).to eq("One Time Only, Minute")
        expect(task_details[:TaskToRun]).to eq("chef-client")
        expect(task_details[:run_level]).to eq("HighestAvailable")
        expect(task_details[:random_delay]).to eq("PT20M")
      end

      it "does not converge the resource if it is already converged" do
        subject.frequency :minute
        subject.random_delay "PT20M"
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end

      it "raises error if invalid random_delay is passed" do
        subject.frequency :minute
        subject.random_delay "abc"
        expect { subject.after_created }.to raise_error("Invalid value passed for `random_delay`. Please pass seconds as an Integer (e.g. 60) or a String with numeric values only (e.g. '60').")
      end

      it "raises error if random_delay is passed with frequency on_idle" do
        subject.frequency :on_idle
        subject.random_delay "PT20M"
        expect { subject.after_created }.to raise_error("`random_delay` property is supported only for frequency :minute, :hourly, :daily, :weekly and :monthly")
      end
    end
  end

  describe "Examples of idempotent checks for each frequency" do
    after { delete_task }
    context "For frequency :once" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :once
        new_resource.start_time "17:00"
        new_resource
      end

      it "create task by adding frequency_modifier as 1" do
        subject.frequency_modifier 1
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end

      it "create task by adding frequency_modifier as 5" do
        skip "This functionality needs to be handle"
        subject.frequency_modifier 5
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "For frequency :none" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :none
        new_resource
      end

      it "create task by adding frequency_modifier as 1" do
        subject.frequency_modifier 1
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end

      it "create task by adding frequency_modifier as 5" do
        skip "This functionality needs to be handle"
        subject.frequency_modifier 5
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "For frequency :weekly" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :weekly
        new_resource
      end

      it "create task by adding start_day" do
        skip "This functionality needs to be handle"
        subject.start_day "12/28/2018"
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end

      it "create task by adding frequency_modifier and random_delay" do
        skip "This functionality needs to be handle"
        subject.frequency_modifier 3
        subject.random_delay "60"
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "For frequency :monthly" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :once
        new_resource.start_time "17:00"
        new_resource
      end

      it "create task by adding frequency_modifier as 1" do
        subject.frequency_modifier 1
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end

      it "create task by adding frequency_modifier as 5" do
        skip "This functionality needs to be handle"
        subject.frequency_modifier 5
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "For frequency :hourly" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :hourly
        new_resource.frequency_modifier 5
        new_resource.random_delay "2400"
        new_resource
      end

      it "create task by adding frequency_modifier and random_delay" do
        skip "This functionality needs to be handle"
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "For frequency :daily" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :daily
        new_resource.frequency_modifier 2
        new_resource.random_delay "2400"
        new_resource
      end

      it "create task by adding frequency_modifier and random_delay" do
        skip "This functionality needs to be handle"
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "For frequency :on_logon" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.frequency :on_logon
        new_resource
      end

      it "create task by adding frequency_modifier and random_delay" do
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end

      it "create task by adding frequency_modifier as 5" do
        skip "This functionality needs to be handle"
        subject.frequency_modifier 5
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end

    context "For frequency :onstart" do
      subject do
        new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
        new_resource.command task_name
        new_resource.run_level :highest
        new_resource.frequency :onstart
        new_resource.frequency_modifier 20
        new_resource
      end

      it "create task by adding frequency_modifier as 20" do
        skip "This functionality needs to be handle"
        subject.run_action(:create)
        subject.run_action(:create)
        expect(subject).not_to be_updated_by_last_action
      end
    end
  end

  describe "#after_created" do
    subject do
      new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
      new_resource.command task_name
      new_resource.run_level :highest
      new_resource
    end

    context "when start_day is passed with frequency :onstart" do
      it "raises error" do
        subject.frequency :onstart
        subject.start_day "09/20/2017"
        expect { subject.after_created }.to raise_error("`start_day` property is not supported with frequency: onstart")
      end
    end

    context "when a non-system user is passed without password" do
      it "raises error" do
        subject.user "Administrator"
        subject.frequency :onstart
        expect { subject.after_created }.to raise_error(%q{Cannot specify a user other than the system users without specifying a password!. Valid passwordless users: 'NT AUTHORITY\SYSTEM', 'SYSTEM', 'NT AUTHORITY\LOCALSERVICE', 'NT AUTHORITY\NETWORKSERVICE', 'BUILTIN\USERS', 'USERS'})
      end
    end

    context "when interactive_enabled is passed for a System user without password" do
      it "raises error" do
        subject.interactive_enabled true
        subject.frequency :onstart
        expect { subject.after_created }.to raise_error("Please provide the password when attempting to set interactive/non-interactive.")
      end
    end

    context "when frequency_modifier > 1439 is passed for frequency=:minute" do
      it "raises error" do
        subject.frequency_modifier 1450
        subject.frequency :minute
        expect { subject.after_created }.to raise_error("frequency_modifier value 1450 is invalid. Valid values for :minute frequency are 1 - 1439.")
      end
    end

    context "when invalid months are passed" do
      it "raises error" do
        subject.months "xyz"
        subject.frequency :monthly
        expect { subject.after_created }.to raise_error("months property invalid. Only valid values are: JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC and *. Multiple values must be separated by a comma.")
      end
    end

    context "when idle_time > 999 is passed" do
      it "raises error" do
        subject.idle_time 1000
        subject.frequency :on_idle
        expect { subject.after_created }.to raise_error("idle_time value 1000 is invalid. Valid values for :on_idle frequency are 1 - 999.")
      end
    end

    context "when idle_time is passed for frequency=:monthly" do
      it "raises error" do
        subject.idle_time 300
        subject.frequency :monthly
        expect { subject.after_created }.to raise_error("idle_time property is only valid for tasks that run on_idle")
      end
    end
  end

  describe "action :delete" do
    subject do
      new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
      new_resource.command task_name
      new_resource
    end

    it "deletes the task if it exists" do
      subject.run_action(:create)
      delete_task
      task_details = windows_task_provider.send(:load_task_hash, task_name)
      expect(task_details).to eq(false)
    end

    it "does not converge the resource if it is already converged" do
      subject.run_action(:create)
      subject.run_action(:delete)
      subject.run_action(:delete)
      expect(subject).not_to be_updated_by_last_action
    end
  end

  describe "action :run" do
    after { delete_task }

    subject do
      new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
      new_resource.command "dir"
      new_resource.run_level :highest
      new_resource
    end

    it "runs the existing task" do
      skip "Task status is returned as Ready instead of Running randomly"
      subject.run_action(:create)
      subject.run_action(:run)
      task_details = windows_task_provider.send(:load_task_hash, task_name)
      expect(task_details[:Status]).to eq("Running")
    end
  end

  describe "action :end", :volatile do
    after { delete_task }

    subject do
      new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
      new_resource.command "dir"
      new_resource.run_level :highest
      new_resource
    end

    it "ends the running task" do
      subject.run_action(:create)
      subject.run_action(:run)
      task_details = windows_task_provider.send(:load_task_hash, task_name)
      subject.run_action(:end)
      task_details = windows_task_provider.send(:load_task_hash, task_name)
      expect(task_details[:Status]).to eq("Ready")
    end
  end

  describe "action :enable" do
    after { delete_task }

    subject do
      new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
      new_resource.command task_name
      new_resource
    end

    it "enables the disabled task" do
      subject.run_action(:create)
      subject.run_action(:disable)
      task_details = windows_task_provider.send(:load_task_hash, task_name)
      expect(task_details[:ScheduledTaskState]).to eq("Disabled")
      subject.run_action(:enable)
      task_details = windows_task_provider.send(:load_task_hash, task_name)
      expect(task_details[:ScheduledTaskState]).to eq("Enabled")
    end
  end

  describe "action :disable" do
    after { delete_task }

    subject do
      new_resource = Chef::Resource::WindowsTask.new(task_name, run_context)
      new_resource.command task_name
      new_resource
    end

    it "disables the task" do
      subject.run_action(:create)
      subject.run_action(:disable)
      task_details = windows_task_provider.send(:load_task_hash, task_name)
      expect(task_details[:ScheduledTaskState]).to eq("Disabled")
    end
  end

  def delete_task
    task_to_delete = Chef::Resource::WindowsTask.new(task_name, run_context)
    task_to_delete.run_action(:delete)
  end
end