Project

General

Profile

Download (3.13 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / test / csv_test_helper.rb @ 400832c6

1
require 'simplecov'
2
require 'pathname'
3
require 'stringio'
4
require 'tempfile'
5

    
6
SimpleCov.use_merging true
7
SimpleCov.start do
8
  command_name 'MiniTest'
9
  add_filter 'test'
10
end
11
SimpleCov.root Pathname.new(File.dirname(__FILE__) + "../../../")
12

    
13

    
14
require 'minitest/autorun'
15
require 'minitest/spec'
16
require "minitest-spec-context"
17
require "mocha/setup"
18

    
19
require './test/test_runner'
20

    
21
require 'hammer_cli'
22
require 'hammer_cli_foreman/commands'
23

    
24
VCR.insert_cassette("apipie", {})
25
require 'hammer_cli_csv'
26
require 'hammer_cli_foreman'
27
require 'hammer_cli_katello'
28
VCR.eject_cassette
29

    
30
module HammerCLIForeman
31
  def self.clear_credentials
32
    @credentials = nil
33
  end
34
end
35

    
36
def hammer(context=nil)
37
  HammerCLI::MainCommand.new("", context || HammerCLI::Settings.dump)
38
end
39

    
40
require 'apipie-bindings'
41
def api
42
  @server = HammerCLI::Settings.settings[:_params][:host] ||
43
    HammerCLI::Settings.get(:csv, :host) ||
44
    HammerCLI::Settings.get(:katello, :host) ||
45
    HammerCLI::Settings.get(:foreman, :host)
46
  @username = HammerCLI::Settings.settings[:_params][:username] ||
47
    HammerCLI::Settings.get(:csv, :username) ||
48
    HammerCLI::Settings.get(:katello, :username) ||
49
    HammerCLI::Settings.get(:foreman, :username)
50
  @password = HammerCLI::Settings.settings[:_params][:password] ||
51
    HammerCLI::Settings.get(:csv, :password) ||
52
    HammerCLI::Settings.get(:katello, :password) ||
53
    HammerCLI::Settings.get(:foreman, :password)
54
  @api = ApipieBindings::API.new({
55
                                   :uri => @server,
56
                                   :username => @username,
57
                                   :password => @password,
58
                                   :api_version => 2
59
                                 })
60
end
61

    
62
def capture
63
  old_stdout = $stdout
64
  old_stderr = $stderr
65
  $stdout = stdout = StringIO.new
66
  $stderr = stderr = StringIO.new
67
  yield
68
  [stdout.string, stderr.string]
69
ensure
70
  $stdout = old_stdout
71
  $stderr = old_stderr
72
end
73

    
74
def set_user(username, password='changeme')
75
  HammerCLI::Connection.drop_all
76
  HammerCLIForeman.clear_credentials
77
  HammerCLI::Settings.load({
78
                               :_params => {
79
                                   :username => username,
80
                                   :password => password
81
                               },
82
                               :foreman => {
83
                                   :username => username,
84
                                   :password => password
85
                               },
86
                               :csv => {
87
                                   :username => username,
88
                                   :password => password
89
                               }
90
                           })
91
end
92

    
93
def host_delete(hostname)
94
  stdout,stderr = capture {
95
    hammer.run(%W(host list --search #{hostname}))
96
  }
97
  lines = stdout.split("\n")
98
  if lines.length == 5
99
    id = stdout.split("\n")[3].split(" ")[0]
100
    stdout,stderr = capture {
101
      hammer.run(%W(host delete --id #{id}))
102
    }
103
  end
104
end
105

    
106
require File.join(File.dirname(__FILE__), 'apipie_resource_mock')
107
require File.join(File.dirname(__FILE__), 'helpers/command')
108
require File.join(File.dirname(__FILE__), 'helpers/resource_disabled')