Best Inspec_ruby code snippet using InspecPlugins.Habitat.read_habitat_config
profile.rb
Source:profile.rb
...16 def create17 logger.info("Creating a Habitat artifact for '#{@path}'...")18 # Need to create working directory first so `ensure` doesn't error19 working_dir = create_working_dir20 habitat_config = read_habitat_config21 verify_habitat_setup(habitat_config)22 output_dir = @options[:output_dir] || Dir.pwd23 unless File.directory?(output_dir)24 exit_with_error("Output directory #{output_dir} is not a directory " \25 'or does not exist.')26 end27 duplicated_profile = duplicate_profile(@path, working_dir)28 prepare_profile!(duplicated_profile)29 hart_file = build_hart(working_dir, habitat_config)30 logger.debug("Copying artifact to #{output_dir}...")31 destination = File.join(output_dir, File.basename(hart_file))32 FileUtils.cp(hart_file, destination)33 logger.info("Habitat artifact '#{@destination}' created.")34 destination35 rescue => e36 logger.debug(e.backtrace.join("\n"))37 exit_with_error('Unable to create Habitat artifact.')38 ensure39 if Dir.exist?(working_dir)40 logger.debug("Deleting working directory #{working_dir}")41 FileUtils.rm_rf(working_dir)42 end43 end44 def setup(profile = profile_from_path(@path))45 path = profile.root_path46 logger.debug("Setting up #{path} for Habitat...")47 plan_file = File.join(path, 'habitat', 'plan.sh')48 logger.info("Generating Habitat plan at #{plan_file}...")49 vars = {50 profile: profile,51 habitat_origin: read_habitat_config['origin'],52 }53 create_file_from_template(plan_file, 'plan.sh.erb', vars)54 run_hook_file = File.join(path, 'habitat', 'hooks', 'run')55 logger.info("Generating a Habitat run hook at #{run_hook_file}...")56 create_file_from_template(run_hook_file, 'hooks/run.erb')57 default_toml = File.join(path, 'habitat', 'default.toml')58 logger.info("Generating a Habitat default.toml at #{default_toml}...")59 create_file_from_template(default_toml, 'default.toml.erb')60 config = File.join(path, 'habitat', 'config', 'inspec_exec_config.json')61 logger.info("Generating #{config} for `inspec exec`...")62 create_file_from_template(config, 'config/inspec_exec_config.json.erb')63 end64 def upload65 habitat_config = read_habitat_config66 if habitat_config['auth_token'].nil?67 exit_with_error(68 'Unable to determine Habitat auth token for uploading.',69 'Run `hab setup` or set the HAB_AUTH_TOKEN environment variable.',70 )71 end72 # Run create command to create habitat artifact73 hart = create74 logger.info("Uploading Habitat artifact #{hart}...")75 upload_hart(hart, habitat_config)76 logger.info("Habitat artifact #{hart} uploaded.")77 rescue => e78 logger.debug(e.backtrace.join("\n"))79 exit_with_error('Unable to upload Habitat artifact.')80 end81 private82 def create_working_dir83 working_dir = Dir.mktmpdir84 logger.debug("Generated working directory #{working_dir}")85 working_dir86 end87 def duplicate_profile(path, working_dir)88 profile = profile_from_path(path)89 copy_profile_to_working_dir(profile, working_dir)90 profile_from_path(working_dir)91 end92 def prepare_profile!(profile)93 vendored_profile = vendor_profile_dependencies!(profile)94 verify_profile(vendored_profile)95 setup(vendored_profile)96 end97 def profile_from_path(path)98 Inspec::Profile.for_target(99 path,100 backend: Inspec::Backend.create(Inspec::Config.mock),101 )102 end103 def copy_profile_to_working_dir(profile, working_dir)104 logger.debug('Copying profile contents to the working directory...')105 profile.files.each do |profile_file|106 next if File.extname(profile_file) == '.hart'107 src = File.join(profile.root_path, profile_file)108 dst = File.join(working_dir, profile_file)109 if File.directory?(profile_file)110 logger.debug("Creating directory #{dst}")111 FileUtils.mkdir_p(dst)112 else113 logger.debug("Copying file #{src} to #{dst}")114 FileUtils.cp_r(src, dst)115 end116 end117 end118 def verify_profile(profile)119 logger.debug('Checking to see if the profile is valid...')120 unless profile.check[:summary][:valid]121 exit_with_error('Profile check failed. Please fix the profile ' \122 'before creating a Habitat artifact.')123 end124 logger.debug('Profile is valid.')125 end126 def vendor_profile_dependencies!(profile)127 profile_vendor = Inspec::ProfileVendor.new(profile.root_path)128 if profile_vendor.lockfile.exist? && profile_vendor.cache_path.exist?129 logger.debug("Profile's dependencies are already vendored, skipping " \130 'vendor process.')131 else132 logger.debug("Vendoring the profile's dependencies...")133 profile_vendor.vendor!134 logger.debug('Ensuring all vendored content has read permissions...')135 profile_vendor.make_readable136 end137 # Return new profile since it has changed138 Inspec::Profile.for_target(139 profile.root_path,140 backend: Inspec::Backend.create(Inspec::Config.mock),141 )142 end143 def verify_habitat_setup(habitat_config)144 logger.debug('Checking to see if Habitat is installed...')145 cmd = Mixlib::ShellOut.new('hab --version')146 cmd.run_command147 if cmd.error?148 exit_with_error('Unable to run Habitat commands.', cmd.stderr)149 end150 if habitat_config['origin'].nil?151 exit_with_error(152 'Unable to determine Habitat origin name.',153 'Run `hab setup` or set the HAB_ORIGIN environment variable.',154 )155 end156 end157 def create_file_from_template(file, template, vars = {})158 FileUtils.mkdir_p(File.dirname(file))159 template_path = File.join(__dir__, '../../templates/habitat', template)160 contents = ERB.new(File.read(template_path))161 .result(OpenStruct.new(vars).instance_eval { binding })162 File.write(file, contents)163 end164 def build_hart(working_dir, habitat_config)165 logger.debug('Building our Habitat artifact...')166 env = {167 'TERM' => 'vt100',168 'HAB_ORIGIN' => habitat_config['origin'],169 'HAB_NONINTERACTIVE' => 'true',170 }171 env['RUST_LOG'] = 'debug' if logger.level == :debug172 # TODO: Would love to use Mixlib::ShellOut here, but it doesn't173 # seem to preserve the STDIN tty, and docker gets angry.174 Dir.chdir(working_dir) do175 unless system(env, 'hab pkg build .')176 exit_with_error('Unable to build the Habitat artifact.')177 end178 end179 hart_files = Dir.glob(File.join(working_dir, 'results', '*.hart'))180 if hart_files.length > 1181 exit_with_error('More than one Habitat artifact was created which ' \182 'was not expected.')183 elsif hart_files.empty?184 exit_with_error('No Habitat artifact was created.')185 end186 hart_files.first187 end188 def upload_hart(hart_file, habitat_config)189 logger.debug("Uploading '#{hart_file}' to the Habitat Builder Depot...")190 config = habitat_config191 env = {192 'HAB_AUTH_TOKEN' => config['auth_token'],193 'HAB_NONINTERACTIVE' => 'true',194 'HAB_ORIGIN' => config['origin'],195 'TERM' => 'vt100',196 }197 env['HAB_DEPOT_URL'] = ENV['HAB_DEPOT_URL'] if ENV['HAB_DEPOT_URL']198 cmd = Mixlib::ShellOut.new("hab pkg upload #{hart_file}", env: env)199 cmd.run_command200 if cmd.error?201 exit_with_error(202 'Unable to upload Habitat artifact to the Depot.',203 cmd.stdout,204 cmd.stderr,205 )206 end207 logger.debug('Upload complete!')208 end209 def read_habitat_config210 cli_toml = File.join(ENV['HOME'], '.hab', 'etc', 'cli.toml')211 cli_toml = '/hab/etc/cli.toml' unless File.exist?(cli_toml)212 cli_config = File.exist?(cli_toml) ? Tomlrb.load_file(cli_toml) : {}213 cli_config['origin'] ||= ENV['HAB_ORIGIN']214 cli_config['auth_token'] ||= ENV['HAB_AUTH_TOKEN']215 cli_config216 end217 def exit_with_error(*errors)218 errors.each do |error_msg|219 logger.error(error_msg)220 end221 exit 1222 end223 end...
profile_test.rb
Source:profile_test.rb
...45 # TODO: Figure out how to capture and validate `Inspec::Log.error`46 end47 def test_create48 file_count = Dir.glob(File.join(@test_profile_path, "**/*")).count49 @hab_profile.stub :read_habitat_config, @mock_hab_config do50 @hab_profile.stub :verify_habitat_setup, nil do51 @hab_profile.stub :build_hart, @fake_hart_file do52 @hab_profile.create53 end54 end55 end56 # It should not modify target profile57 new_file_count = Dir.glob(File.join(@test_profile_path, "**/*")).count58 assert_equal new_file_count, file_count59 # It should create 1 Habitat artifact60 output_files = Dir.glob(File.join(@output_dir, "**/*"))61 assert_equal 1, output_files.count62 assert_equal "fake-hart.hart", File.basename(output_files.first)63 end64 def test_create_rasies_if_habitat_is_not_installed65 cmd = Minitest::Mock.new66 cmd.expect(:error?, true)67 cmd.expect(:run_command, nil)68 Mixlib::ShellOut.stub :new, cmd, "hab --version" do69 assert_raises(SystemExit) { @hab_profile.create }70 # TODO: Figure out how to capture and validate `Inspec::Log.error`71 end72 cmd.verify73 end74 def test_upload75 @hab_profile.stub :read_habitat_config, @mock_hab_config do76 @hab_profile.stub :create, @fake_hart_file do77 @hab_profile.stub :upload_hart, nil do78 @hab_profile.upload79 # TODO: Figure out how to capture and validate `Inspec::Log.error`80 end81 end82 end83 end84 def test_upload_raises_if_no_habitat_auth_token_is_found85 @hab_profile.stub :read_habitat_config, {} do86 assert_raises(SystemExit) { @hab_profile.upload }87 # TODO: Figure out how to capture and validate `Inspec::Log.error`88 end89 end90 def test_create_working_dir91 Dir.stub :mktmpdir, "/tmp/fakedir" do92 assert_equal "/tmp/fakedir", @hab_profile.send(:create_working_dir)93 end94 end95 def test_duplicate_profile96 current_profile = @test_profile97 duplicated_profile = @hab_profile.send(:duplicate_profile,98 @test_profile_path,99 @tmpdir)...
read_habitat_config
Using AI Code Generation
1habitat = InspecPlugins::Habitat.new()2habitat.read_habitat_config("path/to/config/file")3habitat = InspecPlugins::Habitat.new()4habitat.read_habitat_config("path/to/config/file")5habitat = InspecPlugins::Habitat.new()6habitat.read_habitat_config("path/to/config/file")7habitat = InspecPlugins::Habitat.new()8habitat.read_habitat_config("path/to/config/file")9habitat = InspecPlugins::Habitat.new()10habitat.read_habitat_config("path/to/config/file")11habitat = InspecPlugins::Habitat.new()12habitat.read_habitat_config("path/to/config/file")13habitat = InspecPlugins::Habitat.new()14habitat.read_habitat_config("path/to/config/file")15habitat = InspecPlugins::Habitat.new()16habitat.read_habitat_config("path/to/config/file")17habitat = InspecPlugins::Habitat.new()18habitat.read_habitat_config("path/to/config/file")19habitat = InspecPlugins::Habitat.new()20habitat.read_habitat_config("path/to/config/file")
read_habitat_config
Using AI Code Generation
1habitat_config = InspecPlugins::Habitat.new(inspec).read_habitat_config2habitat_config = InspecPlugins::Habitat::Habitat.new(inspec).read_habitat_config3habitat_config = InspecPlugins::Habitat::Habitat::HabitatConfig.new(inspec).read_habitat_config4habitat_config = InspecPlugins::Habitat::Habitat::HabitatConfig::HabitatConfig.new(inspec).read_habitat_config5habitat_config = InspecPlugins::Habitat::Habitat::HabitatConfig::HabitatConfig::HabitatConfig.new(inspec).read_habitat_config
read_habitat_config
Using AI Code Generation
1habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')2habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')3habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')4habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')5habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')6habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')
read_habitat_config
Using AI Code Generation
1file = inspec.file('/hab/svc/myapp/config/config.toml')2habitat_config = inspec.habitat.read_habitat_config(file)3file = inspec.file('/hab/svc/myapp/config/config.json')4habitat_config = inspec.habitat.read_habitat_config(file)5file = inspec.file('/hab/svc/myapp/config/config.yaml')6habitat_config = inspec.habitat.read_habitat_config(file)7file = inspec.file('/hab/svc/myapp/config/config.yml')8habitat_config = inspec.habitat.read_habitat_config(file)9file = inspec.file('/hab/svc/myapp/config/config.properties')10habitat_config = inspec.habitat.read_habitat_config(file)11file = inspec.file('/hab/svc/myapp/config/config.ini')12habitat_config = inspec.habitat.read_habitat_config(file)13file = inspec.file('/hab/svc/myapp/config/config.xml')14habitat_config = inspec.habitat.read_habitat_config(file)15file = inspec.file('/hab/svc/myapp/config/config.conf')16habitat_config = inspec.habitat.read_habitat_config(file)
read_habitat_config
Using AI Code Generation
1config = InspecPlugins.Habitat.read_habitat_config('/hab/svc/my-service/config/config.toml')2config = InspecPlugins.Habitat.read_habitat_config('/hab/svc/my-service/config/config.toml')3config = InspecPlugins.Habitat.read_habitat_config('/hab/svc/my-service/config/config.toml')4config = InspecPlugins.Habitat.read_habitat_config('/hab/svc/my-service/config/config.toml')5config = InspecPlugins.Habitat.read_habitat_config('/hab/svc/my-service/config/config.toml')6config = InspecPlugins.Habitat.read_habitat_config('/hab/svc/my-service/config/config.toml')
read_habitat_config
Using AI Code Generation
1rebitat.read_habitat_config("path/to/config/file")2habitat = InspecPlugins::Habitat.new()3habitat.read_habitat_config("path/to/config/file")4habitat = InspecPlugins::Habitat.new()5habitat.read_habitat_config("path/to/config/file")6habitat = InspecPlugins::Habitat.new()7habitat.read_habitat_config("path/to/config/file")8habitat = InspecPlugins::Habitat.new()9habitat.read_habitat_config("path/to/config/file")10habitat = InspecPlugins::Habitat.new()11habitat.read_habitat_config("path/to/config/file")12habitat = InspecPlugins::Habitat.new()13habitat.read_habitat_config("path/to/config/file")14habitat = InspecPlugins::Habitat.new()15habitat.read_habitat_config("path/to/config/file")16habitat = InspecPlugins::Habitat.new()17habitat.read_habitat_config("path/to/config/file")18habitat = InspecPlugins::Habitat.new()19habitat.read_habitat_config("path/to/config/file")
read_habitat_config
Using AI Code Generation
1file = inspec.file('/hab/svc/myapp/config/config.toml')2habitat_config = inspec.habitat.read_habitat_config(file)3habitat_config = inspec.habitat.read_habitat_config(file)4file = inspecfile('/hab/svc/mapp/config/config.ya)5habitat_config = inspec.habitat.read_habitat_config(file6file = inspec.file('/hab/svc/myapp/config/config.yml')7habitat_config = inspec.habitat.read_habitat_config(file)8file = inspec.file('/hab/svc/myapp/config/config.properties')9habitat_config =ainsbec.habitat.read_habitat_config(file)10filn =finspec.file('/hab/svc/myapp/config/config.ini')11ig['key_config_= inspen.habitat.read_habitat_came'](file)12habitat_config = inspec.habitat.read_habitat_config(file)13file = inspec.file('/hab/svc/myapp/config/config.conf')14'value' = inspec.habitat.read_habitat_config(file)
read_habitat_config
Using AI Code Generation
1config = InspecPlugins.Habitat.read_habitat_config('/hab/svc/my-service/config/config.toml')2config = InspecPlugins.Habitat.read_habitat_config('/hab/svc/my-service/config/config.toml')3config = InspecPlugins.Habitat.read_habitat_config('/hab/svc/my-service/config/config.toml')4config = InspecPlugins.Habitat.read_habitat_config('/hab/svc/my-service/config/config.toml')5config = InspecPlugins.Habitat.read_habitat_config('/hab/svc/my-service/config/config.toml')6config = InspecPlugins.Habitat.read_habitat_config('/hab/svc/my-service/config/config.toml')7habitat_config = InspecPlugins::Habitat::Habitat::HabitatConfig.new(inspec).read_habitat_config8habitat_config = InspecPlugins::Habitat::Habitat::HabitatConfig::HabitatConfig.new(inspec).read_habitat_config9habitat_config = InspecPlugins::Habitat::Habitat::HabitatConfig::HabitatConfig::HabitatConfig.new(inspec).read_habitat_config
read_habitat_config
Using AI Code Generation
1habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')2habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')3habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')4habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')5habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')6habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')
read_habitat_config
Using AI Code Generation
1habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')2habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')3habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')4habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')5habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')6habitat_config = InspecPlugins::Habitat::InspecHabitat.new.read_habitat_config('habitat.yml')
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!