Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed get_file method to receive a resource as parameter. #17406

Merged
merged 1 commit into from
May 17, 2018

Conversation

h-kataria
Copy link
Contributor

This change returns settings for passed in resource region/zone/server.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1536524

@Fryguy @gtanzillo please review.

Copy link
Member

@Fryguy Fryguy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, can you create a second test instead of modifying the original? The reason is because the original tests that MiqServer.my_server is used as the default, whereas this tests shows passing a resource.
Alternately, if get_file is not used anywhere else, and you are only ever going to pass a resource, then we can remove the defaulting in this method.

expect(VMDB::Config.get_file).to eq(
"---\n:http_proxy:\n :default:\n :host: proxy.example.com\n :user: user\n :password: #{enc_pass}\n :port: 80\n"
expect(VMDB::Config.get_file(resource)).to eq(
"--- !ruby/object:Config::Options\ntable:\n :http_proxy: !ruby/object:Config::Options\n table:\n :default: !ruby/object:Config::Options\n table:\n :host: proxy.example.com\n :user: user\n :password: #{enc_pass}\n :port: 80\n modifiable: true\n modifiable: true\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that is the result of running your test, then something is broken, because we don't want yaml with Config::Options in it

Copy link
Member

@Fryguy Fryguy May 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try .to_hash.to_yaml instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fryguy fixed that, please re-review

@h-kataria h-kataria force-pushed the advanced_config_settings branch 2 times, most recently from ca80e4c to d5d0f09 Compare May 10, 2018 21:21
stub_settings(:http_proxy => {:default => {:host => "proxy.example.com", :user => "user", :password => password, :port => 80}})

expect(VMDB::Config.get_file).to eq(
"---\n:http_proxy:\n :default:\n :host: proxy.example.com\n :user: user\n :password: #{enc_pass}\n :port: 80\n"
)
end

it ".get_file for sepecified resource" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo sepecified -> specified

@@ -2,14 +2,24 @@
let(:password) { "pa$$w0rd" }
let(:enc_pass) { MiqPassword.encrypt(password) }

it ".get_file" do
it ".get_file for current server" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you reorgnize this like

describe ".get_file" do
  it "for current server" do
  end

  it "for specified resource" do
  end
end

stub_settings(:http_proxy => {:default => {:host => "proxy.example.com", :user => "user", :password => password, :port => 80}})

expect(VMDB::Config.get_file).to eq(
"---\n:http_proxy:\n :default:\n :host: proxy.example.com\n :user: user\n :password: #{enc_pass}\n :port: 80\n"
)
end

it ".get_file for sepecified resource" do
resource = FactoryGirl.create(:miq_server)
stub_settings(:http_proxy => {:default => {:host => "proxy.example.com", :user => "user", :password => password, :port => 80}})
Copy link
Member

@Fryguy Fryguy May 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stub_settings should probably not be used here because the way it works is by stubbing things globally.

Instead, I think it's better to maybe create some changes, save them, then see if they come back? This should be done in both tests.

Maybe something like...

resource = FactoryGirl.create(:miq_server)
Vmdb::Settings.save!(resource, {:http_proxy => {:default => {:host => "proxy.example.com", :user => "user", :password => password, :port => 80}}})
resource.reload

yaml = VMDB::Config.get_file(resource)

yaml = YAML.load(yaml)
expect(yaml.fetch_path(:http_proxy, :default, :host)).to eq "proxy.example.com"
expect(yaml.fetch_path(:http_proxy, :default, :user)).to eq "user"
expect(yaml.fetch_path(:http_proxy, :default, :port)).to eq 80
expect(yaml.fetch_path(:http_proxy, :default, :password)).to be_encrypted

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h-kataria I edited the snippet above to be more proper

@h-kataria h-kataria force-pushed the advanced_config_settings branch 2 times, most recently from a96e47f to f6331b6 Compare May 10, 2018 22:05
@h-kataria
Copy link
Contributor Author

@Fryguy made requested changes

def self.get_file
Vmdb::Settings.encrypt_passwords!(::Settings.to_hash).to_yaml
def self.get_file(resource = MiqServer.my_server)
Vmdb::Settings.encrypt_passwords!(resource.settings_for_resource).to_hash.to_yaml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h-kataria You have to put the .to_hash inside of the encrypt_passwords, otherwise it modifies the passwords of the resource's settings).

Vmdb::Settings.encrypt_passwords!(resource.settings_for_resource.to_hash).to_yaml

@Fryguy
Copy link
Member

Fryguy commented May 11, 2018

@h-kataria I think you missed my changes as requested in #17406 (review) . The code is still using stub_settings which is not testing this properly.

@h-kataria
Copy link
Contributor Author

@Fryguy updated once again!

@h-kataria h-kataria force-pushed the advanced_config_settings branch 3 times, most recently from 0eda305 to 7972878 Compare May 14, 2018 22:23
@Fryguy
Copy link
Member

Fryguy commented May 15, 2018

@h-kataria

  • Fix the trailing whitespace issue from the bot
  • Please don't put the assertions in an after block. after blocks are meant for cleanup, and should rarely be used. Instead prefer a regular method like assert_setttings_values or something if you're trying to DRY it up.

bdunne
bdunne previously requested changes May 15, 2018
Copy link
Member

@bdunne bdunne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want shared examples or a method where the expectations can live instead since the expectations shouldn't be in the after block. Then you can also expect the hash to have_attributes so that it is a single expectation.


after do
yaml = YAML.load(@yaml)
expect(yaml.fetch_path(:http_proxy, :default, :host)).to eq "proxy.example.com"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are the expectations in the after block? Shouldn't these be in the test or a shared example instead?

This change returns settings for passed in resource region/zone/server.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1536524
@miq-bot
Copy link
Member

miq-bot commented May 15, 2018

Checked commit h-kataria@91b7996 with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0
2 files checked, 2 offenses detected

spec/lib/vmdb/config_spec.rb

@h-kataria
Copy link
Contributor Author

@Fryguy @bdunne please review. I am running into error when using safe_load instead of YAML.load in the tests, addressed all other comments. Getting following error when using YAML.safe_load

 Failure/Error: yaml = YAML.safe_load(yaml)
     
     Psych::DisallowedClass:
       Tried to load unspecified class: Symbol

@Fryguy Fryguy dismissed bdunne’s stale review May 17, 2018 14:07

Talk to him and he is good 👍

@Fryguy Fryguy merged commit 9d5ed08 into ManageIQ:master May 17, 2018
@Fryguy Fryguy added this to the Sprint 86 Ending May 21, 2018 milestone May 17, 2018
@h-kataria h-kataria deleted the advanced_config_settings branch April 14, 2020 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants