Project

General

Profile

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

hammer-cli-csv / test / resources / activation_keys_test.rb @ a2c97209

1
require './test/csv_test_helper'
2
require './lib/hammer_cli_csv'
3

    
4
module Resources
5
  class TestActivationKeys < MiniTest::Unit::TestCase
6
    def test_usage
7
      start_vcr
8
      set_user 'admin'
9

    
10
      stdout,stderr = capture {
11
        hammer.run(%W{--reload-cache csv activation-keys --help})
12
      }
13
      assert_equal '', stderr
14
      assert_equal stdout, <<-HELP
15
Usage:
16
     csv activation-keys [OPTIONS]
17

18
Options:
19
 --continue-on-error           Continue processing even if individual resource error
20
 --export                      Export current data instead of importing
21
 --file FILE_NAME              CSV file (default to /dev/stdout with --export, otherwise required)
22
 --itemized-subscriptions      Export one subscription per row, only process update subscriptions on import
23
 --organization ORGANIZATION   Only process organization matching this name
24
 --search SEARCH               Only export search results
25
 -h, --help                    print help
26
 -v, --verbose                 be verbose
27
HELP
28
      stop_vcr
29
    end
30

    
31
    def test_create_and_update
32
      start_vcr
33
      set_user 'admin'
34

    
35
      name = "testakey1"
36

    
37
      file = Tempfile.new('activation_keys_test')
38
      file.write <<-EOF
39
Name,Organization,Description,Limit,Environment,Content View,Host Collections,Auto-Attach,Service Level,Release Version,Subscriptions
40
#{name},Test Corporation,,,,Default Organization View,"",No,,,
41
EOF
42
      file.rewind
43

    
44
      stdout,stderr = capture {
45
        hammer.run(%W{--reload-cache csv activation-keys --verbose --file #{file.path}})
46
      }
47
      assert_equal '', stderr
48
      assert_equal stdout[0..-2], "Creating activation key '#{name}'...done"
49

    
50
      file.rewind
51

    
52
      stdout,stderr = capture {
53
        hammer.run(%W{--reload-cache csv activation-keys --verbose --file #{file.path}})
54
      }
55
      assert_equal '', stderr
56
      assert_equal stdout[0..-2], "Updating activation key '#{name}'...done"
57
      file.unlink
58

    
59
      activation_key_delete(name)
60

    
61
      stop_vcr
62
    end
63

    
64
    def test_itemized_create_and_update
65
      start_vcr
66
      set_user 'admin'
67

    
68
      name = "testakey2"
69
      sub_name = "Red Hat Enterprise Linux Server, Standard (Physical or Virtual Nodes)"
70
      quantity = 1
71
      activation_key_create(name)
72

    
73
      file = Tempfile.new('activation_keys_test')
74
      # rubocop:disable LineLength
75
      file.write <<-EOF
76
Name,Organization,Description,Limit,Environment,Content View,Host Collections,Auto-Attach,Service Level,Release Version,Subscription Name,Subscription Type,Subscription Quantity,Subscription SKU,Subscription Contract,Subscription Account,Subscription Start,Subscription End
77
#{name},Test Corporation,,,,Default Organization View,\"\",Yes,,,\"#{sub_name}\",Red Hat,#{quantity},RH00004,,1583473,2016-11-10T05:00:00.000+0000,2017-11-10T04:59:59.000+0000
78
EOF
79
      # rubocop:enable LineLength
80
      file.rewind
81

    
82
      # Attaching an integer quantity of a subscription
83
      stdout,stderr = capture {
84
        hammer.run(%W{--reload-cache csv activation-keys --verbose --itemized-subscriptions --file #{file.path}})
85
      }
86
      assert_equal '', stderr
87
      assert_equal stdout[0..-2], "Updating subscriptions for activation key '#{name}'... attaching #{quantity} of '#{sub_name}'...done"
88

    
89
      file.rewind
90

    
91
      # Attaching already-attached subscription
92
      stdout,stderr = capture {
93
        hammer.run(%W{--reload-cache csv activation-keys --verbose --itemized-subscriptions --file #{file.path}})
94
      }
95
      assert_equal '', stderr
96
      assert_equal stdout[0..-2], "Updating subscriptions for activation key '#{name}'... '#{sub_name}' already attached...done"
97

    
98
      activation_key_delete(name)
99

    
100
      stop_vcr
101
    end
102

    
103
    def test_itemized_update_automatic_quantity
104
      start_vcr
105
      set_user 'admin'
106

    
107
      name = "testakey3"
108
      sub_name = "Red Hat Enterprise Linux Server, Standard (Physical or Virtual Nodes)"
109
      quantity = "Automatic"
110
      activation_key_create(name)
111

    
112
      file = Tempfile.new('activation_keys_test')
113
      # rubocop:disable LineLength
114
      file.write <<-EOF
115
Name,Organization,Description,Limit,Environment,Content View,Host Collections,Auto-Attach,Service Level,Release Version,Subscription Name,Subscription Type,Subscription Quantity,Subscription SKU,Subscription Contract,Subscription Account,Subscription Start,Subscription End
116
#{name},Test Corporation,,,,Default Organization View,\"\",Yes,,,\"#{sub_name}\",Red Hat,#{quantity},RH00004,,1583473,2016-11-10T05:00:00.000+0000,2017-11-10T04:59:59.000+0000
117
EOF
118
      # rubocop:enable LineLength
119
      file.rewind
120

    
121
      stdout,stderr = capture {
122
        hammer.run(%W{--reload-cache csv activation-keys --verbose --itemized-subscriptions --file #{file.path}})
123
      }
124
      assert_equal '', stderr
125
      assert_equal stdout[0..-2], "Updating subscriptions for activation key '#{name}'... attaching -1 of '#{sub_name}'...done"
126

    
127
      activation_key_delete(name)
128

    
129
      stop_vcr
130
    end
131

    
132
    def test_itemized_update_blank_quantity
133
      start_vcr
134
      set_user 'admin'
135

    
136
      name = "testakey4"
137
      sub_name = "Red Hat Enterprise Linux Server, Standard (Physical or Virtual Nodes)"
138
      quantity = ""
139
      activation_key_create(name)
140

    
141
      file = Tempfile.new('activation_keys_test')
142
      # rubocop:disable LineLength
143
      file.write <<-EOF
144
Name,Organization,Description,Limit,Environment,Content View,Host Collections,Auto-Attach,Service Level,Release Version,Subscription Name,Subscription Type,Subscription Quantity,Subscription SKU,Subscription Contract,Subscription Account,Subscription Start,Subscription End
145
#{name},Test Corporation,,,,Default Organization View,\"\",Yes,,,\"#{sub_name}\",Red Hat,#{quantity},RH00004,,1583473,2016-11-10T05:00:00.000+0000,2017-11-10T04:59:59.000+0000
146
EOF
147
      # rubocop:enable LineLength
148
      file.rewind
149

    
150
      stdout,stderr = capture {
151
        hammer.run(%W{--reload-cache csv activation-keys --verbose --itemized-subscriptions --file #{file.path}})
152
      }
153
      assert_equal '', stderr
154
      assert_equal stdout[0..-2], "Updating subscriptions for activation key '#{name}'... attaching -1 of '#{sub_name}'...done"
155

    
156
      activation_key_delete(name)
157

    
158
      stop_vcr
159
    end
160

    
161
    def activation_key_create(name)
162
      file = Tempfile.new('activation_keys_test')
163
      # rubocop:disable LineLength
164
      file.write <<-EOF
165
Name,Organization,Description,Limit,Environment,Content View,Host Collections,Auto-Attach,Service Level,Release Version,Subscriptions
166
#{name},Test Corporation,,,,Default Organization View,"",Yes,,,
167
EOF
168
      # rubocop:enable LineLength
169
      file.rewind
170
      stdout,stderr = capture {
171
        hammer.run(%W{--reload-cache csv activation-keys --verbose --file #{file.path}})
172
      }
173
      assert_equal '', stderr
174
      assert_equal stdout[0..-2], "Creating activation key '#{name}'...done"
175
    end
176

    
177
    def activation_key_delete(name)
178
      stdout,stderr = capture {
179
        hammer.run(%W(activation-key list --organization Test\ Corporation --search name=#{name}))
180
      }
181
      lines = stdout.split("\n")
182
      if lines.length == 5
183
        id = lines[3].split(" ")[0]
184
        stdout,stderr = capture {
185
          hammer.run(%W(activation-key delete --organization Test\ Corporation --id #{id}))
186
        }
187
      end
188
    end
189
  end
190
end