PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/unit/packagers/ips_spec.rb

https://github.com/opscode/omnibus
Ruby | 293 lines | 274 code | 19 blank | 0 comment | 1 complexity | 5beba6c5908a136bbfdfdbda258aa056 MD5 | raw file
  1. require "spec_helper"
  2. module Omnibus
  3. describe Packager::IPS do
  4. let(:project) do
  5. Project.new.tap do |project|
  6. project.name("project")
  7. project.homepage("https://example.com")
  8. project.install_dir("/opt/project")
  9. project.build_version("1.2.3+20161003185500.git.37.089ab3f")
  10. project.build_iteration("2")
  11. project.maintainer("Chef Software")
  12. end
  13. end
  14. subject { described_class.new(project) }
  15. let(:project_root) { File.join(tmp_path, "project/root") }
  16. let(:package_dir) { File.join(tmp_path, "package/dir") }
  17. let(:staging_dir) { File.join(tmp_path, "staging/dir") }
  18. let(:source_dir) { File.join(staging_dir, "proto_install") }
  19. let(:repo_dir) { File.join(staging_dir, "publish/repo") }
  20. let(:architecture) { "i86pc" }
  21. let(:shellout) { double("Mixlib::ShellOut", run_command: true, error!: nil) }
  22. before do
  23. Config.project_root(project_root)
  24. Config.package_dir(package_dir)
  25. allow(Mixlib::ShellOut).to receive(:new).and_return(shellout)
  26. allow(subject).to receive(:staging_dir).and_return(staging_dir)
  27. create_directory(staging_dir)
  28. create_directory(source_dir)
  29. create_directory(repo_dir)
  30. stub_ohai(platform: "solaris2", version: "5.11") do |data|
  31. data["kernel"]["machine"] = architecture
  32. end
  33. end
  34. describe "#publisher_prefix" do
  35. it "is a DSL method" do
  36. expect(subject).to have_exposed_method(:publisher_prefix)
  37. end
  38. it "has a default value" do
  39. expect(subject.publisher_prefix).to eq("Omnibus")
  40. end
  41. end
  42. it "#id is :IPS" do
  43. expect(subject.id).to eq(:ips)
  44. end
  45. describe "#package_name" do
  46. it "should create correct package name" do
  47. expect(subject.package_name).to eq("project-1.2.3+20161003185500.git.37.089ab3f-2.i386.p5p")
  48. end
  49. end
  50. describe "#fmri_package_name" do
  51. it "should create correct fmri package name" do
  52. expect(subject.fmri_package_name).to eq ("project@1.2.3,5.11-2")
  53. end
  54. end
  55. describe "#pkg_metadata_file" do
  56. it "is created inside the staging_dir" do
  57. expect(subject.pkg_metadata_file).to eq("#{subject.staging_dir}/gen.manifestfile")
  58. end
  59. end
  60. describe "#pkg_manifest_file" do
  61. it "is created inside the staging_dir" do
  62. expect(subject.pkg_manifest_file).to eq("#{subject.staging_dir}/#{subject.safe_base_package_name}.p5m")
  63. end
  64. end
  65. describe "#repo_dir" do
  66. it "is created inside the staging_dir" do
  67. expect(subject.repo_dir).to eq("#{subject.staging_dir}/publish/repo")
  68. end
  69. end
  70. describe "#source_dir" do
  71. it "is created inside the staging_dir" do
  72. expect(subject.source_dir).to eq("#{subject.staging_dir}/proto_install")
  73. end
  74. end
  75. describe "#safe_base_package_name" do
  76. context 'when the project name is "safe"' do
  77. it "returns the value without logging a message" do
  78. expect(subject.safe_base_package_name).to eq("project")
  79. expect(subject).to_not receive(:log)
  80. end
  81. end
  82. context "when the project name has invalid characters" do
  83. before { project.name("Pro$ject123.for-realz_2") }
  84. it "returns the value while logging a message" do
  85. output = capture_logging do
  86. expect(subject.safe_base_package_name).to eq("pro-ject123.for-realz-2")
  87. end
  88. expect(output).to include("The `name' component of IPS package names can only include")
  89. end
  90. end
  91. end
  92. describe "#safe_architecture" do
  93. context "the architecture is Intel-based" do
  94. let(:architecture) { "i86pc" }
  95. it "returns `i386`" do
  96. expect(subject.safe_architecture).to eq("i386")
  97. end
  98. end
  99. context "the architecture is SPARC-based" do
  100. let(:architecture) { "sun4v" }
  101. it "returns `sparc`" do
  102. expect(subject.safe_architecture).to eq("sparc")
  103. end
  104. end
  105. context "anything else" do
  106. let(:architecture) { "FOO" }
  107. it "returns the value from Ohai" do
  108. expect(subject.safe_architecture).to eq("FOO")
  109. end
  110. end
  111. end
  112. describe "#write_versionlock_file" do
  113. let(:versionlock_file) { File.join(staging_dir, "version-lock") }
  114. it "creates the version-lock file" do
  115. subject.write_versionlock_file
  116. versionlock_file_contents = File.read(versionlock_file)
  117. expect(versionlock_file_contents).to include("<transform pkg depend -> default facet.version-lock.*> false>")
  118. end
  119. end
  120. describe "#write_transform_file" do
  121. let(:transform_file) { File.join(staging_dir, "doc-transform") }
  122. it "creates the transform file" do
  123. subject.write_transform_file
  124. transform_file_contents = File.read(transform_file)
  125. expect(transform_file_contents).to include("<transform dir path=opt$ -> edit group bin sys>")
  126. expect(transform_file_contents).to include("<transform file depend -> edit pkg.debug.depend.file ruby env>")
  127. expect(transform_file_contents).to include("<transform file depend -> edit pkg.debug.depend.file make env>")
  128. expect(transform_file_contents).to include("<transform file depend -> edit pkg.debug.depend.file perl env>")
  129. expect(transform_file_contents).to include("<transform file depend -> edit pkg.debug.depend.path usr/local/bin usr/bin>")
  130. end
  131. end
  132. describe "#write_pkg_metadata" do
  133. let(:resources_path) { File.join(tmp_path, "resources/path") }
  134. let(:manifest_file) { File.join(staging_dir, "gen.manifestfile") }
  135. it "should create metadata correctly" do
  136. subject.write_pkg_metadata
  137. expect(File.exist?(manifest_file)).to be(true)
  138. manifest_file_contents = File.read(manifest_file)
  139. expect(manifest_file_contents).to include("set name=pkg.fmri value=developer/versioning/project@1.2.3,5.11-2")
  140. expect(manifest_file_contents).to include("set name=variant.arch value=i386")
  141. end
  142. context "when both symlinks.erb and project-symlinks.erb exists" do
  143. before do
  144. FileUtils.mkdir_p(resources_path)
  145. allow(subject).to receive(:resources_path).and_return(resources_path)
  146. File.open(File.join(resources_path, "project-symlinks.erb"), "w+") do |f|
  147. f.puts("link path=usr/bin/ohai target=<%= projectdir %>/bin/ohai")
  148. f.puts("link path=<%= projectdir %>/bin/gmake target=<%= projectdir %>/embedded/bin/make")
  149. end
  150. File.open(File.join(resources_path, "symlinks.erb"), "w+") do |f|
  151. f.puts("link path=usr/bin/knife target=<%= projectdir %>/bin/knife")
  152. f.puts("link path=<%= projectdir %>/bin/berks target=<%= projectdir %>/embedded/bin/berks")
  153. end
  154. end
  155. it "should render project-symlinks.erb and append to metadata contents" do
  156. subject.write_pkg_metadata
  157. expect(subject.symlinks_file).to eq("project-symlinks.erb")
  158. expect(File.exist?(manifest_file)).to be(true)
  159. manifest_file_contents = File.read(manifest_file)
  160. expect(manifest_file_contents).to include("link path=usr/bin/ohai target=/opt/project/bin/ohai")
  161. expect(manifest_file_contents).to include("link path=/opt/project/bin/gmake target=/opt/project/embedded/bin/make")
  162. end
  163. end
  164. context "when only symlinks.erb exists" do
  165. before do
  166. FileUtils.mkdir_p(resources_path)
  167. allow(subject).to receive(:resources_path).and_return(resources_path)
  168. File.open(File.join(resources_path, "symlinks.erb"), "w+") do |f|
  169. f.puts("link path=usr/bin/knife target=<%= projectdir %>/bin/knife")
  170. f.puts("link path=<%= projectdir %>/bin/berks target=<%= projectdir %>/embedded/bin/berks")
  171. end
  172. end
  173. it "should render symlinks.erb and append to metadata contents" do
  174. subject.write_pkg_metadata
  175. expect(subject.symlinks_file).to eq("symlinks.erb")
  176. expect(File.exist?(manifest_file)).to be(true)
  177. manifest_file_contents = File.read(manifest_file)
  178. expect(manifest_file_contents).to include("link path=usr/bin/knife target=/opt/project/bin/knife")
  179. expect(manifest_file_contents).to include("link path=/opt/project/bin/berks target=/opt/project/embedded/bin/berks")
  180. end
  181. end
  182. context "when symlinks_file does not exist" do
  183. it "#write_pkg_metadata does not include symlinks" do
  184. subject.write_pkg_metadata
  185. manifest_file = File.join(staging_dir, "gen.manifestfile")
  186. manifest_file_contents = File.read(manifest_file)
  187. expect(subject.symlinks_file).to be_nil
  188. expect(manifest_file_contents).not_to include("link path=usr/bin/ohai target=/opt/project/bin/ohai")
  189. expect(manifest_file_contents).not_to include("link path=usr/bin/knife target=/opt/project/bin/knife")
  190. end
  191. end
  192. end
  193. describe "#generate_pkg_contents" do
  194. it "uses the correct commands" do
  195. expect(subject).to receive(:shellout!)
  196. .with("pkgsend generate #{staging_dir}/proto_install | pkgfmt > #{staging_dir}/project.p5m.1")
  197. expect(subject).to receive(:shellout!)
  198. .with("pkgmogrify -DARCH=`uname -p` #{staging_dir}/project.p5m.1 #{staging_dir}/gen.manifestfile #{staging_dir}/doc-transform | pkgfmt > #{staging_dir}/project.p5m.2")
  199. subject.generate_pkg_contents
  200. end
  201. end
  202. describe "#generate_pkg_deps" do
  203. it "uses the correct commands" do
  204. expect(subject).to receive(:shellout!)
  205. .with("pkgdepend generate -md #{staging_dir}/proto_install #{staging_dir}/project.p5m.2 | pkgfmt > #{staging_dir}/project.p5m.3")
  206. expect(subject).to receive(:shellout!)
  207. .with("pkgmogrify -DARCH=`uname -p` #{staging_dir}/project.p5m.3 #{staging_dir}/doc-transform | pkgfmt > #{staging_dir}/project.p5m.4")
  208. expect(subject).to receive(:shellout!)
  209. .with("pkgdepend resolve -m #{staging_dir}/project.p5m.4")
  210. expect(subject).to receive(:shellout!)
  211. .with("pkgmogrify #{staging_dir}/project.p5m.4.res #{staging_dir}/version-lock > #{staging_dir}/project.p5m.5.res")
  212. subject.generate_pkg_deps
  213. end
  214. end
  215. describe "#validate_pkg_manifest" do
  216. it "uses the correct commands" do
  217. expect(subject).to receive(:shellout!)
  218. .with("pkglint -c /tmp/lint-cache -r http://pkg.oracle.com/solaris/release #{staging_dir}/project.p5m.5.res")
  219. subject.validate_pkg_manifest
  220. end
  221. end
  222. describe "#create_ips_repo" do
  223. it "uses the correct commands" do
  224. expect(subject).to receive(:shellout!)
  225. .with("pkgrepo create #{staging_dir}/publish/repo")
  226. subject.create_ips_repo
  227. end
  228. end
  229. describe "#publish_ips_pkg" do
  230. it "uses the correct commands" do
  231. expect(subject).to receive(:shellout!)
  232. .with("pkgrepo -s #{staging_dir}/publish/repo set publisher/prefix=Omnibus")
  233. expect(subject).to receive(:shellout!)
  234. .with("pkgsend publish -s #{staging_dir}/publish/repo -d #{staging_dir}/proto_install #{staging_dir}/project.p5m.5.res")
  235. expect(shellout).to receive(:stdout)
  236. subject.publish_ips_pkg
  237. end
  238. end
  239. describe "#export_pkg_archive_file" do
  240. it "uses the correct commands" do
  241. expect(subject).to receive(:shellout!)
  242. .with("pkgrecv -s #{staging_dir}/publish/repo -a -d #{package_dir}/project-1.2.3+20161003185500.git.37.089ab3f-2.i386.p5p project")
  243. expect(shellout).to receive(:stdout)
  244. subject.export_pkg_archive_file
  245. end
  246. end
  247. end
  248. end