|
| 1 | +# |
| 2 | +# Author:: Nuo Yan <nuo@opscode.com> |
| 3 | +# Copyright:: Copyright (c) 2010, Opscode, Inc |
| 4 | +# License:: Apache License, Version 2.0 |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper")) |
| 20 | + |
| 21 | +describe Chef::Provider::Service::Windows, "load_current_resource" do |
| 22 | + before(:each) do |
| 23 | + @init_command = "sc" |
| 24 | + @node = mock("Chef::Node", :null_object => true) |
| 25 | + @new_resource = Chef::Resource::Service.new("chef") |
| 26 | + @new_resource.stub!(:pattern).and_return("chef") |
| 27 | + @new_resource.stub!(:status_command).and_return(false) |
| 28 | + |
| 29 | + @current_resource = Chef::Resource::Service.new("chef") |
| 30 | + @provider = Chef::Provider::Service::Windows.new(@node, @new_resource) |
| 31 | + Chef::Resource::Service.stub!(:new).and_return(@current_resource) |
| 32 | + IO.stub!(:popen).with("#{@init_command} query #{@new_resource.service_name}").and_return(['','','','4']) |
| 33 | + IO.stub!(:popen).with("#{@init_command} qc #{@new_resource.service_name}").and_return(['','','','','2']) |
| 34 | + end |
| 35 | + |
| 36 | + it "should create a current resource with the name of the new resource" do |
| 37 | + Chef::Resource::Service.should_receive(:new).and_return(@current_resource) |
| 38 | + @provider.load_current_resource |
| 39 | + end |
| 40 | + |
| 41 | + it "should set the current resources service name to the new resources service name" do |
| 42 | + @current_resource.should_receive(:service_name).with(@new_resource.service_name) |
| 43 | + @provider.load_current_resource |
| 44 | + end |
| 45 | + |
| 46 | + it "should return the current resource" do |
| 47 | + @provider.load_current_resource.should eql(@current_resource) |
| 48 | + end |
| 49 | + |
| 50 | +end |
| 51 | + |
| 52 | +describe Chef::Provider::Service::Windows, "start_service" do |
| 53 | + before(:each) do |
| 54 | + @new_resource = Chef::Resource::Service.new("chef") |
| 55 | + @new_resource.start_command "sc start chef" |
| 56 | + |
| 57 | + @provider = Chef::Provider::Service::Windows.new(@node, @new_resource) |
| 58 | + Chef::Resource::Service.stub!(:new).and_return(@current_resource) |
| 59 | + end |
| 60 | + |
| 61 | + it "should call the start command if one is specified" do |
| 62 | + @new_resource.stub!(:start_command).and_return("#{@new_resource.start_command}") |
| 63 | + IO.should_receive(:popen).with("#{@new_resource.start_command}").and_return(StringIO.new("foo\nbar\nbaz\n2 START_PENDING\n")) |
| 64 | + @provider.start_service() |
| 65 | + end |
| 66 | +end |
| 67 | + |
| 68 | +describe Chef::Provider::Service::Windows, "stop_service" do |
| 69 | + before(:each) do |
| 70 | + @init_command = "sc" |
| 71 | + @new_resource = Chef::Resource::Service.new("chef") |
| 72 | + @new_resource.stop_command "sc stop chef" |
| 73 | + |
| 74 | + @provider = Chef::Provider::Service::Windows.new(@node, @new_resource) |
| 75 | + Chef::Resource::Service.stub!(:new).and_return(@current_resource) |
| 76 | + end |
| 77 | + |
| 78 | + it "should call the stop command if one is specified" do |
| 79 | + @new_resource.stub!(:stop_command).and_return("#{@new_resource.stop_command}") |
| 80 | + IO.should_receive(:popen).with("#{@new_resource.stop_command}").and_return(StringIO.new("foo\nbar\nbaz\n1 STOPPED\n")) |
| 81 | + @provider.stop_service().should be_true |
| 82 | + end |
| 83 | + |
| 84 | + it "should use the built-in command if no stop command is specified" do |
| 85 | + @new_resource.stub!(:stop_command).and_return(nil) |
| 86 | + IO.stub!(:popen).with("#{@init_command} stop #{@new_resource.service_name}").and_return(IO.new(2,'w')) |
| 87 | + IO.popen("#{@init_command} stop #{@new_resource.service_name}").stub!(:readlines).and_return(["foo\n","bar\n","baz\n","1 STOPPED\n"]) |
| 88 | + @provider.stop_service().should be_true |
| 89 | + end |
| 90 | +end |
| 91 | + |
| 92 | +describe Chef::Provider::Service::Windows, "restart_service" do |
| 93 | + before(:each) do |
| 94 | + @init_command = "sc" |
| 95 | + @new_resource = Chef::Resource::Service.new("chef") |
| 96 | + |
| 97 | + @provider = Chef::Provider::Service::Windows.new(@node, @new_resource) |
| 98 | + Chef::Resource::Service.stub!(:new).and_return(@current_resource) |
| 99 | + end |
| 100 | + |
| 101 | + it "should just call stop, then start when the resource doesn't support restart and no restart_command is specified" do |
| 102 | + IO.should_receive(:popen).with("#{@init_command} stop #{@new_resource.service_name}").and_return(StringIO.new("foo\nbar\nbaz\n1 STOPPED\n")) |
| 103 | + IO.should_receive(:popen).with("#{@init_command} start #{@new_resource.service_name}").and_return(StringIO.new("foo\nbar\nbaz\n2 START_PENDING\n")) |
| 104 | + @provider.restart_service() |
| 105 | + end |
| 106 | +end |
| 107 | + |
| 108 | +describe Chef::Provider::Service::Windows, "enable_service" do |
| 109 | + before(:each) do |
| 110 | + @init_command = "sc" |
| 111 | + @node = Chef::Node.new |
| 112 | + @new_resource = Chef::Resource::Service.new("chef") |
| 113 | + @new_resource.running false |
| 114 | + @new_resource.enabled false |
| 115 | + |
| 116 | + @provider = Chef::Provider::Service::Windows.new(@node, @new_resource) |
| 117 | + Chef::Resource::Service.stub!(:new).and_return(@current_resource) |
| 118 | + end |
| 119 | + |
| 120 | + it "should enable service and set the startup type" do |
| 121 | + @new_resource.startup_type :automatic |
| 122 | + IO.should_receive(:popen).with("sc config chef start= auto").and_return(StringIO.new("SUCCESS")) |
| 123 | + @provider.enable_service().should be_true |
| 124 | + end |
| 125 | +end |
| 126 | + |
| 127 | +describe Chef::Provider::Service::Windows, "disable_service" do |
| 128 | + before(:each) do |
| 129 | + @node = Chef::Node.new |
| 130 | + @new_resource = Chef::Resource::Service.new("chef") |
| 131 | + |
| 132 | + @provider = Chef::Provider::Service::Windows.new(@node, @new_resource) |
| 133 | + Chef::Resource::Service.stub!(:new).and_return(@current_resource) |
| 134 | + end |
| 135 | + |
| 136 | + it "should disable service" do |
| 137 | + IO.should_receive(:popen).with("sc config chef start= disabled").and_return(StringIO.new("SUCCESS")) |
| 138 | + @provider.disable_service().should be_true |
| 139 | + end |
| 140 | +end |
| 141 | + |
0 commit comments