mirror of
https://github.com/fluent/fluentd-ui.git
synced 2025-08-20 05:51:11 +02:00
Merge pull request #138 from fluent/refactor_download_info
Refactor #download_info of MiscController
This commit is contained in:
commit
a85f605f3b
@ -39,30 +39,39 @@ class MiscController < ApplicationController
|
||||
|
||||
Zip::File.open(path, Zip::File::CREATE) do |zip|
|
||||
zip.get_output_stream('fluentd.log') {|f| f.puts fluentd.agent.log }
|
||||
if ENV["FLUENTD_UI_LOG_PATH"].present?
|
||||
zip.add("fluentd-ui.log", ENV["FLUENTD_UI_LOG_PATH"])
|
||||
else
|
||||
zip.add("fluentd-ui.log", Rails.root.join("log/#{Rails.env}.log"))
|
||||
end
|
||||
zip.get_output_stream('env.txt') do |f|
|
||||
ENV.to_a.each do |(key, value)|
|
||||
f.puts "#{key}=#{value}"
|
||||
end
|
||||
end
|
||||
zip.get_output_stream('versions.txt') do |f|
|
||||
f.puts "ruby: #{RUBY_DESCRIPTION}"
|
||||
f.puts "fluentd: #{FluentdUI.fluentd_version}"
|
||||
f.puts "fluentd-ui: #{FluentdUI::VERSION}"
|
||||
f.puts
|
||||
f.puts "# OS Information"
|
||||
f.puts "uname -a: #{`uname -a`.strip}"
|
||||
end
|
||||
zip.add("fluentd-ui.log", log_path)
|
||||
|
||||
add_env_file_to(zip)
|
||||
add_version_file_to(zip)
|
||||
end
|
||||
send_file path
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def log_path
|
||||
ENV["FLUENTD_UI_LOG_PATH"] || Rails.root.join("log/#{Rails.env}.log")
|
||||
end
|
||||
|
||||
def add_env_file_to(zip)
|
||||
zip.get_output_stream('env.txt') do |f|
|
||||
ENV.to_a.each do |(key, value)|
|
||||
f.puts "#{key}=#{value}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def add_version_file_to(zip)
|
||||
zip.get_output_stream('versions.txt') do |f|
|
||||
f.puts "ruby: #{RUBY_DESCRIPTION}"
|
||||
f.puts "fluentd: #{FluentdUI.fluentd_version}"
|
||||
f.puts "fluentd-ui: #{FluentdUI::VERSION}"
|
||||
f.puts
|
||||
f.puts "# OS Information"
|
||||
f.puts "uname -a: #{`uname -a`.strip}"
|
||||
end
|
||||
end
|
||||
|
||||
def update!
|
||||
FluentdUiRestart.new.async.perform
|
||||
end
|
||||
|
68
spec/controllers/misc_controller_spec.rb
Normal file
68
spec/controllers/misc_controller_spec.rb
Normal file
@ -0,0 +1,68 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe MiscController do
|
||||
let(:instance) { Fluentd.new(id: nil, variant: "fluentd_gem", log_file: "dummy.log", pid_file: "dummy.pid", config_file: "dummy.conf") }
|
||||
|
||||
class DummyAagent
|
||||
def log
|
||||
"dummy_log_content"
|
||||
end
|
||||
|
||||
def version
|
||||
"dummy version"
|
||||
end
|
||||
end
|
||||
|
||||
let!(:dummy_agent) { DummyAagent.new }
|
||||
let!(:fluentd_log_content) { dummy_agent.log }
|
||||
let!(:fluentd_version) { dummy_agent.version }
|
||||
let(:fluentd_ui_log_content) { <<-LOG.strip_heredoc }
|
||||
log1
|
||||
log2
|
||||
log3
|
||||
LOG
|
||||
|
||||
let!(:dummy_log_path) { "tmp/dummy.log" }
|
||||
let!(:expand_dir) { Rails.root.join("tmp/system_info") }
|
||||
|
||||
before do
|
||||
allow(controller).to receive(:current_user).and_return true
|
||||
|
||||
#dummy log for fluentd-ui.log
|
||||
File.open(dummy_log_path, 'w') { |file| file.write(fluentd_ui_log_content) }
|
||||
controller.stub(:log_path) { dummy_log_path }
|
||||
|
||||
instance.stub(:agent).and_return(dummy_agent)
|
||||
Fluentd.stub(:instance).and_return(instance)
|
||||
end
|
||||
|
||||
describe 'download_info' do
|
||||
before do
|
||||
get 'download_info'
|
||||
|
||||
#expand files in zip
|
||||
Zip::File.open(Rails.root.join("tmp/system_info.zip")) do |zip_file|
|
||||
FileUtils.mkdir_p(expand_dir)
|
||||
|
||||
zip_file.each do |f|
|
||||
f_path = File.join(expand_dir, f.name)
|
||||
zip_file.extract(f, f_path) unless File.exist?(f_path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#remove all temp files for this spec
|
||||
after do
|
||||
FileUtils.rm Rails.root.join("tmp/system_info.zip")
|
||||
FileUtils.rm_r expand_dir
|
||||
FileUtils.rm dummy_log_path
|
||||
end
|
||||
|
||||
it 'write files' do
|
||||
expect(File.read(File.join(expand_dir, "fluentd.log"))).to eq "#{fluentd_log_content}\n"
|
||||
expect(File.read(File.join(expand_dir, "fluentd-ui.log"))).to eq "#{fluentd_ui_log_content}"
|
||||
expect(File.read(File.join(expand_dir, "env.txt"))).to match "RAILS_ENV=test"
|
||||
expect(File.read(File.join(expand_dir, "versions.txt"))).to match "fluentd: #{fluentd_version}"
|
||||
end
|
||||
end
|
||||
end
|
@ -1,5 +0,0 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe PluginsController do
|
||||
|
||||
end
|
@ -1,5 +0,0 @@
|
||||
require 'spec_helper'
|
||||
|
||||
RSpec.describe TutorialsController, :type => :controller do
|
||||
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user