def setup_state(identity = '1', mock_instance_state = true)
      cleanup_state
      flexmock(RightScale::AgentTagManager.instance).should_receive(:tags).and_yield(['foo'])
      InstanceState.const_set(:STATE_FILE, state_file_path)
      InstanceState.const_set(:BOOT_LOG_FILE, log_path)
      InstanceState.const_set(:OPERATION_LOG_FILE, log_path)
      InstanceState.const_set(:DECOMMISSION_LOG_FILE, log_path)
      CookState.const_set(:STATE_FILE, cook_state_file_path)
      RightScale::ChefState.const_set(:STATE_FILE, chef_file_path) if RightScale.const_defined?(:ChefState)
      RightScale::ChefState.const_set(:SCRIPTS_FILE, past_scripts_path) if RightScale.const_defined?(:ChefState)

      @identity = identity
      @results_factory = ResultsMock.new
      @sender = flexmock('Sender')
      flexmock(Sender).should_receive(:instance).and_return(@sender).by_default
      RightScale.module_eval("Sender = Sender") unless defined?(::RightScale::Sender)
      @sender.should_receive(:identity).and_return(@identity).by_default
      @sender.should_receive(:send_push).by_default
      @sender.should_receive(:send_push).by_default
      @sender.should_receive(:send_request).and_yield(@results_factory.success_results).by_default
      @sender.should_receive(:message_received).by_default
      flexmock(InstanceState).should_receive(:record_state).and_return(true).by_default if mock_instance_state

      InstanceState.init(@identity) if mock_instance_state
      CookState.init

      ChefState.init(@identity, secret='some secret', reset=false)

      # should yield last in case caller wants to override the defaults
      yield if block_given?
    end
def setup_state(identity = '1', mock_instance_state = true)
      cleanup_state
      flexmock(RightScale::AgentTagManager.instance).should_receive(:tags).and_yield(['foo'])
      InstanceState.const_set(:STATE_FILE, state_file_path)
      InstanceState.const_set(:BOOT_LOG_FILE, log_path)
      InstanceState.const_set(:OPERATION_LOG_FILE, log_path)
      InstanceState.const_set(:DECOMMISSION_LOG_FILE, log_path)
      CookState.const_set(:STATE_FILE, cook_state_file_path)
      RightScale::ChefState.const_set(:STATE_FILE, chef_file_path) if RightScale.const_defined?(:ChefState)
      RightScale::ChefState.const_set(:SCRIPTS_FILE, past_scripts_path) if RightScale.const_defined?(:ChefState)

      @identity = identity
      @results_factory = ResultsMock.new
      @sender = flexmock('Sender')
      flexmock(Sender).should_receive(:instance).and_return(@sender).by_default
      RightScale.module_eval("Sender = Sender") unless defined?(::RightScale::Sender)
      @sender.should_receive(:identity).and_return(@identity).by_default
      @sender.should_receive(:send_push).by_default
      @sender.should_receive(:send_push).by_default
      @sender.should_receive(:send_request).and_yield(@results_factory.success_results).by_default
      @sender.should_receive(:message_received).by_default
      flexmock(InstanceState).should_receive(:record_state).and_return(true).by_default if mock_instance_state

      InstanceState.init(@identity) if mock_instance_state
      CookState.init

      ChefState.init(@identity, secret='some secret', reset=false)

      # should yield last in case caller wants to override the defaults
      yield if block_given?
    end
Setup instance state for tests
 Use different identity to reset list of past scripts
 Override mock_instance_state if do not want to mock InstanceState#record_state
 but then must be running in EM before setup_state is called and must do own
 InstanceState.init