Project

General

Profile

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

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

1 ee942928 Tom McKay
require 'simplecov'
2
require 'pathname'
3 7b00acc6 Tom McKay
require 'stringio'
4
require 'tempfile'
5 863472b5 Tom McKay
require 'fileutils'
6 ee942928 Tom McKay
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 b50ceaa2 Tom McKay
require './test/test_runner'
21
22 bf81b9cf Tom McKay
require 'hammer_cli'
23
require 'hammer_cli_foreman/commands'
24
25 b50ceaa2 Tom McKay
VCR.insert_cassette("apipie", {})
26 ee942928 Tom McKay
require 'hammer_cli_csv'
27 bfc065ce Tom McKay
require 'hammer_cli_foreman'
28 bf81b9cf Tom McKay
require 'hammer_cli_katello'
29 b50ceaa2 Tom McKay
VCR.eject_cassette
30 ee942928 Tom McKay
31 561a8ac9 Tom McKay
module HammerCLIForeman
32
  def self.clear_credentials
33
    @credentials = nil
34
  end
35 bfc065ce Tom McKay
end
36
37 dcfb98fd Tom McKay
def hammer(context=nil)
38 561a8ac9 Tom McKay
  HammerCLI::MainCommand.new("", context || HammerCLI::Settings.dump)
39 bfc065ce Tom McKay
end
40
41 fd57822a Tom McKay
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 bfc065ce Tom McKay
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 a6c1d857 Tomas Strachota
  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 bfc065ce Tom McKay
  HammerCLI::Settings.load({
83 561a8ac9 Tom McKay
                               :_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 ee942928 Tom McKay
end
97
98 c06f1783 Tom McKay
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 afc8539f Tom McKay
    id = lines[3].split(" ")[0]
105 c06f1783 Tom McKay
    stdout,stderr = capture {
106
      hammer.run(%W(host delete --id #{id}))
107
    }
108
  end
109
end
110
111 ebb00443 Andrew Kofink
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 f92e695a David Davis
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 ee942928 Tom McKay
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')