Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Monday, February 12, 2024

Compare a whole file content with rspec-puppet

 I was attempting to use the entire body of a file for a quick and dirty 'golden master' style test while I was refactoring some things.

I ran into a problem with having a multi-line string representing the full body of my output - I would run into this problem after putting in something like the following( https://github.com/puppetlabs/rspec-puppet/issues/100 ):

Failure/Error: it { is expected.to contain_file('/mock/file.sh').with_content(multiline_content) }
  expected that the catalogue would contain File[/mock/file.sh] with content set to supplied string
  Diff: 
    <The diff is empty, are your objects producing identical '#inspect' output?>

And my test looked something like this ...

my_content=<<-CONTENT.chomp
file line one
file line two
file last line
CONTENT

on_supported_os.each do |os, os_facts|
  context "on #{os}" do
  it { is_expected.to contain_file('/mock/file.sh').with_content(my_content) }
  end
end

Since I was unable to figure out a 'proper' solution (with and without the chomp) for this problem, I did the next best thing - I simply looped over each line of the file input, and made sure all the lines were in the content - this could give a false 'passing' as it doesn't prevent extra lines from being in there, or the line order isn't tracked, but it was 'good enough' for my short purpose

my_content=<<-CONTENT.chomp
file line one
file line two
file last line
CONTENT

on_supported_os.each do |os, os_facts|
  context "on #{os}" do
  it {
    my_content.each_line do |line|
      is_expected.to contain_file('/mock/file.sh').with_content(/#{Regexp.quote(line)}/) 
  }
  end
end


Hope this helps someone, or perhaps someone see's this and can tell me what I'm doing wrong!

Sunday, February 22, 2015

Managing puppet modules with vagrant

A little while ago, I started my journey learning about DevOps, starting out by diving into some of the tooling (Puppet), and adding a few DevOps podcasts into my rotation.

One problem that plagued the process from the beginning... how to handle puppet modules?   Here's the evolution of my thought process...


Stage 1... ignorance

I simple used GIT to control my /etc/puppet/modules/ directory!  Lots of redundant code in here that was already saved in other git repos I could/should just fetch, which gives me an idea...

Stage 2..... frustration
Git Submodules!  I was just manually pulling down most of my modules directly from their git repos instead of though puppet forge anyway, so how about adding them as submodules to my repo, and having git fetch them for me!

Stage 3...... starting automation
Now that I was trying to build easy to run Vagrantfiles that had my puppet configs, I need some way to set it all up....  Initial trick.... adding a 'setup' script in my vagrant directory that pre-downloads all the modules that I run before the first setup! (either by directly calling git clone on the repo, or by calling git submodule init).  This was still a 'prestep' to running vagrant-up, so that didn't quite feel right

Stage 4...... feels right!
One of the first things I did after joining twitter was to ask #puppetize what the best way to manage modules was, and they did not disappoint!  I was redirected to this blog, which shows how you can use puppet librarian to manage your modules, but as to not require everyone to have ruby-gems installed on their main machine, they pull it down inside of their vagrant instance (since most vagrant images already have ruby-gems installed since ruby is a core component of puppet and chef.



Utilizing Configuration management with Vagrant is a powerful tool to help build easily repeatable environments for  both Dev and Deploy!  I hope this helps someone!  I know it was quite a struggle initially!

Sunday, February 1, 2015

Railstutorial, first chapter

I had some spare time today, so I finally got started with the fantastic guy to Ruby on Rails at https://www.railstutorial.org.  I've tried to get started before, but since I'm a bit of a masochist, I've decided to do everything on my raspberry pi.

My last attempt to get started, I told my raspberry pi to install ruby, and it had to build from source.  I left it churn away over night, and that was eventually completed.

Today, I told it to install the rails version specified in the tutorial, and well....

This is what I saw after My wife and I woke up at 3:30 to watch the Australian Open.

While watching, I was able to work my way though the first chapter, deploy locally (to my raspberry pi), to 'production' to a heroku account.

So far it's all been about infrastructure, so I'm looking forward to chapter 2, where you start the 'toy' app so I can begin to see how ruby and rails really work together!