hive-messages 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +24 -0
  3. data/Gemfile +3 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +35 -0
  6. data/Rakefile +11 -0
  7. data/hive-messages.gemspec +33 -0
  8. data/lib/hive/messages.rb +46 -0
  9. data/lib/hive/messages/artifact.rb +17 -0
  10. data/lib/hive/messages/base.rb +18 -0
  11. data/lib/hive/messages/configuration.rb +12 -0
  12. data/lib/hive/messages/execution_variables_base.rb +27 -0
  13. data/lib/hive/messages/job.rb +100 -0
  14. data/lib/hive/messages/nil_job.rb +15 -0
  15. data/lib/hive/paths.rb +12 -0
  16. data/lib/hive/paths/artifacts.rb +20 -0
  17. data/lib/hive/paths/jobs.rb +48 -0
  18. data/lib/hive/paths/queues.rb +21 -0
  19. data/lib/hive/representers/artifact_representer.rb +13 -0
  20. data/lib/hive/representers/job_representer.rb +25 -0
  21. data/lib/roar/transport/net_http/request_patch.rb +19 -0
  22. data/lib/virtus/attribute/execution_variables.rb +20 -0
  23. data/spec/fixtures/sample.pem +31 -0
  24. data/spec/fixtures/upload_sample.log +1 -0
  25. data/spec/lib/hive/messages/artifact_spec.rb +50 -0
  26. data/spec/lib/hive/messages/configuration_spec.rb +13 -0
  27. data/spec/lib/hive/messages/execution_variables_base_spec.rb +12 -0
  28. data/spec/lib/hive/messages/job_spec.rb +233 -0
  29. data/spec/lib/hive/messages_spec.rb +30 -0
  30. data/spec/lib/hive/paths/artifacts_spec.rb +22 -0
  31. data/spec/lib/hive/paths/jobs_spec.rb +49 -0
  32. data/spec/lib/hive/paths/queues_spec.rb +35 -0
  33. data/spec/lib/hive/paths_spec.rb +10 -0
  34. data/spec/lib/hive/representers/artifact_representer_spec.rb +49 -0
  35. data/spec/lib/hive/representers/job_representer_spec.rb +96 -0
  36. data/spec/lib/roar/transport/net_http/request_patch_spec.rb +84 -0
  37. data/spec/lib/virtus/attribute/execution_variables_spec.rb +60 -0
  38. data/spec/spec_helper.rb +13 -0
  39. metadata +266 -0
@@ -0,0 +1,30 @@
1
+ require "spec_helper"
2
+
3
+ describe Hive::Messages do
4
+
5
+ describe "class methods" do
6
+
7
+ describe ".configure" do
8
+
9
+ let(:base_path) { "http://localhost:3000" }
10
+
11
+ before(:each) do
12
+ Hive::Messages.configure do |config|
13
+ config.base_path = base_path
14
+ end
15
+ end
16
+
17
+ it "creates a Configuration instance" do
18
+ expect(Hive::Messages.configuration).to be_instance_of(Hive::Messages::Configuration)
19
+ end
20
+
21
+ it "set the Hive::Paths.base" do
22
+ expect(Hive::Paths.base).to eq(base_path)
23
+ end
24
+
25
+ it "set the base_path on the Configuration" do
26
+ expect(Hive::Messages.configuration.base_path).to eq(base_path)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ describe Hive::Paths::Artifacts do
4
+
5
+ describe "class methods" do
6
+
7
+ let(:base_path) { "http://hive.bbc" }
8
+
9
+ before(:each) do
10
+ Hive::Paths.base=base_path
11
+ end
12
+
13
+ describe ".create_url" do
14
+
15
+ let(:job_id) { 99 }
16
+
17
+ it "generates a valid create artifact url" do
18
+ expect(Hive::Paths::Artifacts.create_url(job_id)).to eq "#{base_path}/api/jobs/#{job_id.to_s}/artifacts"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,49 @@
1
+ require "spec_helper"
2
+
3
+ describe Hive::Paths::Jobs do
4
+
5
+ describe "class methods" do
6
+
7
+ let(:base_path) { "http://hive.bbc" }
8
+ let(:job_id) { 99 }
9
+
10
+ before(:each) do
11
+ Hive::Paths.base = base_path
12
+ end
13
+
14
+ describe ".start_url" do
15
+
16
+ it "generates a valid job start url" do
17
+ expect(Hive::Paths::Jobs.start_url(job_id)).to eq "#{base_path}/api/jobs/#{job_id.to_s}/start"
18
+ end
19
+ end
20
+
21
+ describe ".update_results_url" do
22
+
23
+ it "generates a valid update counts url" do
24
+ expect(Hive::Paths::Jobs.update_results_url(job_id)).to eq "#{base_path}/api/jobs/#{job_id.to_s}/update_results"
25
+ end
26
+ end
27
+
28
+ describe ".report_artifacts_url" do
29
+
30
+ it "generates a valid report artifacts url" do
31
+ expect(Hive::Paths::Jobs.report_artifacts_url(job_id)).to eq "#{base_path}/api/jobs/#{job_id.to_s}/report_artifacts"
32
+ end
33
+ end
34
+
35
+ describe ".end_url" do
36
+
37
+ it "generates a valid job end url" do
38
+ expect(Hive::Paths::Jobs.end_url(job_id)).to eq "#{base_path}/api/jobs/#{job_id.to_s}/end"
39
+ end
40
+ end
41
+
42
+ describe ".error_url" do
43
+
44
+ it "generates a valid job error url" do
45
+ expect(Hive::Paths::Jobs.error_url(job_id)).to eq "#{base_path}/api/jobs/#{job_id.to_s}/error"
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,35 @@
1
+ require "spec_helper"
2
+
3
+ describe Hive::Paths::Queues do
4
+
5
+ describe "class methods" do
6
+
7
+ let(:base_path) { "http://hive.bbc" }
8
+
9
+ before(:each) do
10
+ Hive::Paths.base=base_path
11
+ end
12
+
13
+ describe ".job_reservation_url" do
14
+
15
+ let(:queues) { ["queue_one", "queue_two"] }
16
+
17
+ let(:job_reservation_url) { Hive::Paths::Queues.job_reservation_url(queues) }
18
+
19
+ context "single queue name provided" do
20
+
21
+ let(:queues) { "queue_one" }
22
+
23
+ it "adds the single queue to the reservation path" do
24
+ expect(job_reservation_url).to eq "#{base_path}/api/queues/queue_one/jobs/reserve"
25
+ end
26
+ end
27
+
28
+ context "multiple queue names provided" do
29
+ it "adds the provided queues to the reservation path" do
30
+ expect(job_reservation_url).to eq "#{base_path}/api/queues/queue_one,queue_two/jobs/reserve"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,10 @@
1
+ require "spec_helper"
2
+
3
+ describe Hive::Paths do
4
+
5
+ it { should be }
6
+ it "has an attribute called 'base'" do
7
+ Hive::Paths.base = :base
8
+ expect(Hive::Paths.base).to eq :base
9
+ end
10
+ end
@@ -0,0 +1,49 @@
1
+ require "spec_helper"
2
+
3
+ describe Hive::Representers::ArtifactRepresenter do
4
+
5
+ class TestArtifact
6
+ include Virtus.model
7
+
8
+ attribute :artifact_id
9
+ attribute :job_id
10
+ attribute :asset_file_name
11
+ attribute :asset_content_type
12
+ attribute :asset_file_size
13
+ end
14
+
15
+ let(:upstream_artifact) do
16
+ artifact = TestArtifact.new(artifact_attributes)
17
+ artifact.extend(Hive::Representers::ArtifactRepresenter)
18
+ artifact
19
+ end
20
+
21
+ let(:artifact_attributes) do
22
+ {
23
+ artifact_id: 321,
24
+ job_id: 123,
25
+ asset_file_name: "screenshot1.png",
26
+ asset_content_type: "image/png",
27
+ asset_file_size: 2300
28
+ }
29
+ end
30
+
31
+ describe "serialization " do
32
+
33
+ let(:downstream_artifact) do
34
+ artifact = TestArtifact.new
35
+ artifact.extend(Hive::Representers::ArtifactRepresenter)
36
+ artifact.from_json(upstream_artifact.to_json)
37
+ artifact
38
+ end
39
+
40
+
41
+ subject { downstream_artifact }
42
+
43
+ its(:artifact_id) { should eq artifact_attributes[:artifact_id] }
44
+ its(:job_id) { should eq artifact_attributes[:job_id] }
45
+ its(:asset_file_name) { should eq artifact_attributes[:asset_file_name] }
46
+ its(:asset_content_type) { should eq artifact_attributes[:asset_content_type] }
47
+ its(:asset_file_size) { should eq artifact_attributes[:asset_file_size] }
48
+ end
49
+ end
@@ -0,0 +1,96 @@
1
+ require "spec_helper"
2
+
3
+ describe Hive::Representers::JobRepresenter do
4
+
5
+ class TestJob
6
+ include Virtus.model
7
+
8
+ attribute :command
9
+ attribute :job_id
10
+ attribute :repository
11
+ attribute :execution_directory
12
+ attribute :target
13
+ attribute :execution_variables
14
+ attribute :reservation_details
15
+ attribute :device_id
16
+ attribute :running_count
17
+ attribute :passed_count
18
+ attribute :failed_count
19
+ attribute :state
20
+ attribute :errored_count
21
+ attribute :result
22
+ attribute :exit_value
23
+ attribute :message
24
+ attribute :result_details
25
+
26
+ attribute :extra
27
+ end
28
+
29
+ let(:upstream_job) do
30
+ job = TestJob.new(job_attributes)
31
+ job.extend(Hive::Representers::JobRepresenter)
32
+ job
33
+ end
34
+
35
+ let(:target) { { "build" => "http://hive/download/99" } }
36
+ let(:execution_variables) { { "tests" => [] } }
37
+ let(:reservation_details) { { "hive" => "99", "pid" => "1024" } }
38
+
39
+
40
+ let(:job_attributes) do
41
+ {
42
+ command: "cmd",
43
+ job_id: 99,
44
+ repository: "svn://",
45
+ execution_directory: ".",
46
+ target: target,
47
+ execution_variables: execution_variables,
48
+ reservation_details: reservation_details,
49
+ device_id: 23,
50
+ running_count: 4,
51
+ passed_count: 3,
52
+ failed_count: 2,
53
+ errored_count: 1,
54
+ state: "running",
55
+ extra: "thing",
56
+ result: "test result",
57
+ exit_value: 0,
58
+ message: "Hello, mum",
59
+ result_details: "Blah!"
60
+ }
61
+ end
62
+
63
+ describe "serialization " do
64
+
65
+ let(:downstream_job) do
66
+ job = TestJob.new
67
+ job.extend(Hive::Representers::JobRepresenter)
68
+ job.from_json(upstream_job.to_json)
69
+ job
70
+ end
71
+
72
+
73
+ subject { downstream_job }
74
+
75
+ its(:command) { should eq job_attributes[:command] }
76
+ its(:job_id) { should eq job_attributes[:job_id] }
77
+ its(:repository) { should eq job_attributes[:repository] }
78
+ its(:execution_directory) { should eq job_attributes[:execution_directory] }
79
+ its(:target) { should eq job_attributes[:target] }
80
+ its(:execution_variables) { should eq execution_variables }
81
+ its(:reservation_details) { should eq reservation_details }
82
+ its(:device_id) { should eq job_attributes[:device_id] }
83
+ its(:running_count) { should eq job_attributes[:running_count] }
84
+ its(:passed_count) { should eq job_attributes[:passed_count] }
85
+ its(:failed_count) { should eq job_attributes[:failed_count] }
86
+ its(:errored_count) { should eq job_attributes[:errored_count] }
87
+ its(:state) { should eq job_attributes[:state] }
88
+ its(:result) { should eq job_attributes[:result] }
89
+ its(:exit_value) { should eq job_attributes[:exit_value] }
90
+ its(:message) { should eq job_attributes[:message] }
91
+ its(:result_details) { should eq job_attributes[:result_details] }
92
+
93
+ # Attribute that should be ignored
94
+ its(:extra) { should be_nil }
95
+ end
96
+ end
@@ -0,0 +1,84 @@
1
+ require "spec_helper"
2
+
3
+ # Monkey patch == so that our expectations below work correctly
4
+ class OpenSSL::X509::Certificate
5
+ def ==(other)
6
+ self.to_s == other.to_s
7
+ end
8
+ end
9
+
10
+ class OpenSSL::PKey::RSA
11
+ def ==(other)
12
+ self.to_s == other.to_s
13
+ end
14
+ end
15
+
16
+ describe Roar::Transport::NetHTTP::Request do
17
+
18
+ describe "class methods" do
19
+
20
+ describe ".new" do
21
+
22
+ let(:options) { { uri: "http://www.bbc.co.uk" } }
23
+ let(:net_http_double) { double(Net::HTTP, "use_ssl=".to_sym => true, "cert=".to_sym => true, "key=".to_sym => true, "verify_mode=".to_sym => true) }
24
+
25
+ before(:each) do
26
+ Hive::Messages.configure do |config|
27
+ configuration_options.each do |option_key, option_value|
28
+ config.send("#{option_key}=", option_value)
29
+ end
30
+ end
31
+ Net::HTTP.stub(new: net_http_double)
32
+ Roar::Transport::NetHTTP::Request.new(options)
33
+ end
34
+
35
+ context "when Hive::Messages is configured to use a client pem_file" do
36
+
37
+ let(:pem_file) { File.expand_path("spec/fixtures/sample.pem", Hive::Messages.root) }
38
+
39
+ let(:pem) { File.read(pem_file) }
40
+ let(:cert) { OpenSSL::X509::Certificate.new(pem) }
41
+ let(:key) { OpenSSL::PKey::RSA.new(pem) }
42
+
43
+ let(:configuration_options) { { pem_file: pem_file, ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE } }
44
+
45
+ it "turned ssl on" do
46
+ expect(net_http_double).to have_received(:use_ssl=).with(true)
47
+ end
48
+
49
+ it "provided a client cert" do
50
+ expect(net_http_double).to have_received(:cert=).with(cert)
51
+ end
52
+
53
+ it "provided a client key" do
54
+ expect(net_http_double).to have_received(:key=).with(key)
55
+ end
56
+
57
+ it "set the ssl verify mode" do
58
+ expect(net_http_double).to have_received(:verify_mode=).with(configuration_options[:ssl_verify_mode])
59
+ end
60
+ end
61
+
62
+ context "when Hive::Messages is NOT configured to use a client pem_file" do
63
+
64
+ let(:configuration_options) { {} }
65
+
66
+ it "did not turn ssl on" do
67
+ expect(net_http_double).to_not have_received(:use_ssl=)
68
+ end
69
+
70
+ it "did not provide a cert" do
71
+ expect(net_http_double).to_not have_received(:cert=)
72
+ end
73
+
74
+ it "did not provide a key" do
75
+ expect(net_http_double).to_not have_received(:key=)
76
+ end
77
+
78
+ it "did not set an ssl verify mode" do
79
+ expect(net_http_double).to_not have_received(:verify_mode=)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,60 @@
1
+ require "spec_helper"
2
+
3
+ describe Virtus::Attribute::ExecutionVariables do
4
+
5
+ let(:model_klass) do
6
+ klass = Class.new do
7
+ include Virtus.model
8
+
9
+ attribute :execution_variables, Virtus::Attribute::ExecutionVariables
10
+ end
11
+ end
12
+
13
+ let(:model) { model_klass.new(execution_variables: values) }
14
+ let(:execution_variables) { model.execution_variables }
15
+
16
+ context "values are empty" do
17
+
18
+ let(:values) { {} }
19
+
20
+ it "returned nil for the execution_variables" do
21
+ expect(execution_variables).to be_nil
22
+ end
23
+ end
24
+
25
+ context "values just contain job_ib and version" do
26
+
27
+ let(:job_id) { 99 }
28
+ let(:version) { "one" }
29
+ let(:values) { { job_id: job_id, version: version } }
30
+
31
+ subject { execution_variables }
32
+
33
+ it { should be_a(Hive::Messages::ExecutionVariablesBase) }
34
+ its(:job_id) { should eq job_id }
35
+ its(:version) { should eq version }
36
+ end
37
+
38
+ context "values contain extra attributes" do
39
+
40
+ let(:job_id) { 99 }
41
+ let(:version) { "one" }
42
+ let(:foo) { "foo_value" }
43
+ let(:values) { { job_id: job_id, version: version, foo: foo } }
44
+ subject { execution_variables }
45
+
46
+ it { should be_a(Hive::Messages::ExecutionVariablesBase) }
47
+ its(:job_id) { should eq job_id }
48
+ its(:version) { should eq version }
49
+
50
+ it "created a new 'foo' attribute on the anonymous class that was not set on the base ExecutionVariables class" do
51
+ attributes = execution_variables.class.attribute_set.collect(&:name)
52
+ expect(attributes).to include(:foo)
53
+ end
54
+
55
+ it "set the value of the 'foo' variable" do
56
+ expect(execution_variables.foo).to eq foo
57
+ end
58
+ end
59
+ end
60
+
@@ -0,0 +1,13 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require "rspec"
4
+
5
+ require "hive/messages"
6
+
7
+ Dir["./spec/support/**/*.rb"].each {|f| require f}
8
+
9
+ RSpec.configure do |config|
10
+ config.color_enabled = true
11
+ config.filter_run focus: true
12
+ config.run_all_when_everything_filtered = true
13
+ end