key_spec.rb 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. require 'spec_helper'
  2. describe 'apt::key', :type => :define do
  3. let(:facts) { { :lsbdistid => 'Debian' } }
  4. GPG_KEY_ID = '47B320EB4C7C375AA9DAE1A01054B7A24BD6EC30'
  5. let :title do
  6. GPG_KEY_ID
  7. end
  8. describe 'normal operation' do
  9. describe 'default options' do
  10. it 'contains the apt_key' do
  11. should contain_apt_key(title).with({
  12. :id => title,
  13. :ensure => 'present',
  14. :source => nil,
  15. :server => nil,
  16. :content => nil,
  17. :keyserver_options => nil,
  18. })
  19. end
  20. it 'contains the apt_key present anchor' do
  21. should contain_anchor("apt_key #{title} present")
  22. end
  23. end
  24. describe 'title and key =>' do
  25. let :title do
  26. 'puppetlabs'
  27. end
  28. let :params do {
  29. :key => GPG_KEY_ID,
  30. } end
  31. it 'contains the apt_key' do
  32. should contain_apt_key(title).with({
  33. :id => GPG_KEY_ID,
  34. :ensure => 'present',
  35. :source => nil,
  36. :server => nil,
  37. :content => nil,
  38. :keyserver_options => nil,
  39. })
  40. end
  41. it 'contains the apt_key present anchor' do
  42. should contain_anchor("apt_key #{GPG_KEY_ID} present")
  43. end
  44. end
  45. describe 'ensure => absent' do
  46. let :params do {
  47. :ensure => 'absent',
  48. } end
  49. it 'contains the apt_key' do
  50. should contain_apt_key(title).with({
  51. :id => title,
  52. :ensure => 'absent',
  53. :source => nil,
  54. :server => nil,
  55. :content => nil,
  56. :keyserver_options => nil,
  57. })
  58. end
  59. it 'contains the apt_key absent anchor' do
  60. should contain_anchor("apt_key #{title} absent")
  61. end
  62. end
  63. describe 'set a bunch of things!' do
  64. let :params do {
  65. :key_content => 'GPG key content',
  66. :key_source => 'http://apt.puppetlabs.com/pubkey.gpg',
  67. :key_server => 'pgp.mit.edu',
  68. :key_options => 'debug',
  69. } end
  70. it 'contains the apt_key' do
  71. should contain_apt_key(title).with({
  72. :id => title,
  73. :ensure => 'present',
  74. :source => 'http://apt.puppetlabs.com/pubkey.gpg',
  75. :server => 'pgp.mit.edu',
  76. :content => params[:key_content],
  77. :keyserver_options => 'debug',
  78. })
  79. end
  80. it 'contains the apt_key present anchor' do
  81. should contain_anchor("apt_key #{title} present")
  82. end
  83. end
  84. context "domain with dash" do
  85. let(:params) do{
  86. :key_server => 'p-gp.m-it.edu',
  87. } end
  88. it 'contains the apt_key' do
  89. should contain_apt_key(title).with({
  90. :id => title,
  91. :server => 'p-gp.m-it.edu',
  92. })
  93. end
  94. end
  95. context "url" do
  96. let :params do
  97. {
  98. :key_server => 'hkp://pgp.mit.edu',
  99. }
  100. end
  101. it 'contains the apt_key' do
  102. should contain_apt_key(title).with({
  103. :id => title,
  104. :server => 'hkp://pgp.mit.edu',
  105. })
  106. end
  107. end
  108. context "url with port number" do
  109. let :params do
  110. {
  111. :key_server => 'hkp://pgp.mit.edu:80',
  112. }
  113. end
  114. it 'contains the apt_key' do
  115. should contain_apt_key(title).with({
  116. :id => title,
  117. :server => 'hkp://pgp.mit.edu:80',
  118. })
  119. end
  120. end
  121. end
  122. describe 'validation' do
  123. context "domain begin with dash" do
  124. let(:params) do{
  125. :key_server => '-pgp.mit.edu',
  126. } end
  127. it 'fails' do
  128. expect { subject } .to raise_error(/does not match/)
  129. end
  130. end
  131. context "domain begin with dot" do
  132. let(:params) do{
  133. :key_server => '.pgp.mit.edu',
  134. } end
  135. it 'fails' do
  136. expect { subject } .to raise_error(/does not match/)
  137. end
  138. end
  139. context "domain end with dot" do
  140. let(:params) do{
  141. :key_server => "pgp.mit.edu.",
  142. } end
  143. it 'fails' do
  144. expect { subject } .to raise_error(/does not match/)
  145. end
  146. end
  147. context "exceed character url" do
  148. let :params do
  149. {
  150. :key_server => 'hkp://pgpiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii.mit.edu'
  151. }
  152. end
  153. it 'fails' do
  154. expect { subject }.to raise_error(/does not match/)
  155. end
  156. end
  157. context "incorrect port number url" do
  158. let :params do
  159. {
  160. :key_server => 'hkp://pgp.mit.edu:8008080'
  161. }
  162. end
  163. it 'fails' do
  164. expect { subject }.to raise_error(/does not match/)
  165. end
  166. end
  167. context "incorrect protocol for url" do
  168. let :params do
  169. {
  170. :key_server => 'abc://pgp.mit.edu:80'
  171. }
  172. end
  173. it 'fails' do
  174. expect { subject }.to raise_error(/does not match/)
  175. end
  176. end
  177. context "missing port number url" do
  178. let :params do
  179. {
  180. :key_server => 'hkp://pgp.mit.edu:'
  181. }
  182. end
  183. it 'fails' do
  184. expect { subject }.to raise_error(/does not match/)
  185. end
  186. end
  187. context "url ending with a dot" do
  188. let :params do
  189. {
  190. :key_server => 'hkp://pgp.mit.edu.'
  191. }
  192. end
  193. it 'fails' do
  194. expect { subject }.to raise_error(/does not match/)
  195. end
  196. end
  197. context "url begin with a dash" do
  198. let(:params) do{
  199. :key_server => "hkp://-pgp.mit.edu",
  200. } end
  201. it 'fails' do
  202. expect { subject }.to raise_error(/does not match/)
  203. end
  204. end
  205. context 'invalid key' do
  206. let :title do
  207. 'Out of rum. Why? Why are we out of rum?'
  208. end
  209. it 'fails' do
  210. expect { subject }.to raise_error(/does not match/)
  211. end
  212. end
  213. context 'invalid source' do
  214. let :params do {
  215. :key_source => 'afp://puppetlabs.com/key.gpg',
  216. } end
  217. it 'fails' do
  218. expect { subject }.to raise_error(/does not match/)
  219. end
  220. end
  221. context 'invalid content' do
  222. let :params do {
  223. :key_content => [],
  224. } end
  225. it 'fails' do
  226. expect { subject }.to raise_error(/is not a string/)
  227. end
  228. end
  229. context 'invalid server' do
  230. let :params do {
  231. :key_server => 'two bottles of rum',
  232. } end
  233. it 'fails' do
  234. expect { subject }.to raise_error(/does not match/)
  235. end
  236. end
  237. context 'invalid keyserver_options' do
  238. let :params do {
  239. :key_options => {},
  240. } end
  241. it 'fails' do
  242. expect { subject }.to raise_error(/is not a string/)
  243. end
  244. end
  245. context 'invalid ensure' do
  246. let :params do
  247. {
  248. :ensure => 'foo',
  249. }
  250. end
  251. it 'fails' do
  252. expect { subject }.to raise_error(/does not match/)
  253. end
  254. end
  255. describe 'duplication' do
  256. context 'two apt::key resources for same key, different titles' do
  257. let :pre_condition do
  258. "apt::key { 'duplicate': key => #{title}, }"
  259. end
  260. it 'contains two apt::key resources' do
  261. should contain_apt__key('duplicate').with({
  262. :key => title,
  263. :ensure => 'present',
  264. })
  265. should contain_apt__key(title).with({
  266. :key => title,
  267. :ensure => 'present',
  268. })
  269. end
  270. it 'contains only a single apt_key' do
  271. should contain_apt_key('duplicate').with({
  272. :id => title,
  273. :ensure => 'present',
  274. :source => nil,
  275. :server => nil,
  276. :content => nil,
  277. :keyserver_options => nil,
  278. })
  279. should_not contain_apt_key(title)
  280. end
  281. end
  282. context 'two apt::key resources, different ensure' do
  283. let :pre_condition do
  284. "apt::key { 'duplicate': key => #{title}, ensure => 'absent', }"
  285. end
  286. it 'informs the user of the impossibility' do
  287. expect { subject }.to raise_error(/already ensured as absent/)
  288. end
  289. end
  290. end
  291. end
  292. end