require_relative 'base' describe VagrantPlugins::Skytap::API::Vm do include_context "unit" include_context "rest_api" let(:json_path) { File.join(File.expand_path('..', __FILE__), 'support', 'api_responses') } let(:vm1_attrs) { read_json(json_path, 'vm1.json') } let(:vm2_attrs) { vm1_attrs.merge("id" => "6981851", "name" => "VM2") } let(:network1_attrs) { read_json(json_path, 'network1.json') } let(:empty_environment_attrs) { read_json(json_path, 'empty_environment.json') } let(:iso_env) do # We have to create a Vagrantfile so there is a root path env = isolated_environment env.vagrantfile("") env.create_vagrant_env end let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) } let(:env) {{ machine: machine, api_client: api_client }} let(:environment) do env_attrs = empty_environment_attrs VagrantPlugins::Skytap::API::Environment.new(env_attrs, env) end let(:attrs) { vm1_attrs } let(:instance) { described_class.new(attrs, environment, env) } let(:api_client) do # By default, all GET requests will return VM1 double('api_client', get: double('resp', body: JSON.dump(attrs)) ) end # Ensure tests are not affected by Skytap credential environment variables before :each do ENV.stub(:[] => nil) end describe "reload" do subject do new_attrs = attrs.merge('name' => 'VM1, renamed') client = double('api_client', get: double('resp', body: JSON.dump(new_attrs)) ) myenv = env.merge(api_client: client) described_class.new(attrs, environment, myenv) end it "updates the attrs" do expect(subject.get_api_attribute('name')).to eq 'VM1' subject.reload expect(subject.get_api_attribute('name')).to eq 'VM1, renamed' end end describe "url" do subject do instance end its("url") { should == '/vms/6981850'} end describe "busy?" do subject do instance end it "returns false when stopped" do allow(subject).to receive(:runstate).and_return('stopped') expect(subject.busy?).to eq false allow(subject).to receive(:runstate).and_call_original end it "returns false when running" do allow(subject).to receive(:runstate).and_return('running') expect(subject.busy?).to eq false allow(subject).to receive(:runstate).and_call_original end it "returns true when runstate is busy" do allow(subject).to receive(:runstate).and_return('busy') expect(subject.busy?).to eq true allow(subject).to receive(:runstate).and_call_original end end describe "running?" do subject do instance end it "returns false when stopped" do allow(subject).to receive(:runstate).and_return('stopped') expect(subject.running?).to eq false allow(subject).to receive(:runstate).and_call_original end it "returns false when suspended" do allow(subject).to receive(:runstate).and_return('suspended') expect(subject.running?).to eq false allow(subject).to receive(:runstate).and_call_original end it "returns false when runstate is busy" do allow(subject).to receive(:runstate).and_return('busy') expect(subject.running?).to eq false allow(subject).to receive(:runstate).and_call_original end it "returns true when running" do allow(subject).to receive(:runstate).and_return('running') expect(subject.running?).to eq true allow(subject).to receive(:runstate).and_call_original end end describe "region" do subject do fake_template_attrs = {'id' => '5570024', 'region' => 'EMEA'} client = double('api_client', get: double('resp', body: JSON.dump(fake_template_attrs)) ) myenv = env.merge(api_client: client) described_class.new(attrs, environment, myenv) end its("region") { should == 'EMEA' } end end