Project

General

Profile

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

hammer-cli-csv / test / csv_test_helper.rb @ f92e695a

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

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

    
14

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

    
20
require './test/test_runner'
21

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

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

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

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

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

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

    
75
def set_user(username, password='changeme')
76
  if HammerCLI.context[:api_connection]
77
    HammerCLI.context[:api_connection].drop_all
78
  else
79
    HammerCLI::Connection.drop_all
80
    HammerCLIForeman.clear_credentials
81
  end
82
  HammerCLI::Settings.load({
83
                               :_params => {
84
                                   :username => username,
85
                                   :password => password
86
                               },
87
                               :foreman => {
88
                                   :username => username,
89
                                   :password => password
90
                               },
91
                               :csv => {
92
                                   :username => username,
93
                                   :password => password
94
                               }
95
                           })
96
end
97

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

    
111
def content_view_delete(name)
112
  org = 'Test Corporation'
113
  id = nil
114
  stdout,stderr = capture {
115
    hammer.run(%W(content-view list --search #{name}))
116
  }
117
  lines = stdout.split("\n")
118
  if lines.length == 5
119
    id = lines[3].split(" ")[0]
120
  end
121

    
122
  if id
123
    stdout,stderr = capture {
124
      hammer.run(%W(content-view remove --id #{id} --organization #{org} --environments Library))
125
    }
126

    
127
    stdout,stderr = capture {
128
      hammer.run(%W(content-view delete --id #{id}))
129
    }
130
  end
131
end
132

    
133
def content_view_filter_delete(org, cv, name)
134
  id = nil
135
  stdout,stderr = capture {
136
    hammer.run(%W(content-view filter list --search name=#{name} --content-view #{cv} --organization #{org}))
137
  }
138
  lines = stdout.split("\n")
139
  if lines.length == 5
140
    id = lines[3].split(" ")[0]
141
  end
142

    
143
  if id
144
    stdout,stderr = capture {
145
      hammer.run(%W(content-view filter delete --id #{id}))
146
    }
147
  end
148
end
149

    
150
require File.join(File.dirname(__FILE__), 'apipie_resource_mock')
151
require File.join(File.dirname(__FILE__), 'helpers/command')
152
require File.join(File.dirname(__FILE__), 'helpers/resource_disabled')