lock_manager 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0a235cfae8f73825fbc5b738d4f2adeac031f1b2
4
+ data.tar.gz: 1440406b654d29de1949e76b4f04f80b3117df91
5
+ SHA512:
6
+ metadata.gz: 884bc3e33046cf124843fc424b22d51f5affff5063886d690243a510b3b4d3927988c95906cbd1dfac82de04486e78ec64188631585655de741b8115a66a28b2
7
+ data.tar.gz: 6b77f05acfb5f783121bf345c0d93c7c1b96f64af4555cd372999a0071d141ff1a8f226dde7aa9d176a5e5a122cae1eb213a5937f36e021d72ae748ec4483967
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2014-2015 Puppet Labs
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # Lock Manager
2
+
3
+ Lock manager is designed to be a small helper gem that provides common methods
4
+ for locking and unlocking hardware.
5
+
6
+ ## About
7
+
8
+ Lock manager is a simple gem designed to place a lock on a host via a
9
+ json value in redis.
10
+
11
+ The format for the lock is
12
+
13
+ {"user":"stahnma","time":"2015-11-20 05:35:40 +0000",
14
+ "reason":"Because reasons"}
15
+
16
+ ## Usage
17
+
18
+ Create a lock manager object
19
+
20
+ a = LockManager.new(type: 'redis', server: 'localhost')
21
+
22
+ Check to see if the node is locked.
23
+
24
+ a.locked? 'pe-aix-72-builder'
25
+ => false
26
+
27
+ Lock the node
28
+
29
+ a.lock 'pe-aix-72-builder', 'stahnma'
30
+ => true
31
+
32
+ See the lock
33
+
34
+ a.show
35
+ => "{\"user\":\"stahnma\",\"time\":\"2015-11-20 05:55:52 +0000\",\"reason\":null}"
36
+
37
+ Unlock the node
38
+
39
+ a.unlock 'pe-aix-72-builder', 'stahnma'
40
+ => true
41
+
42
+ Lock with a purpose
43
+
44
+ a.lock 'pe-aix-72-builder', 'stahnma', "Because I'm looking into a failure"
45
+ => true
46
+
47
+ Ask for a lock, if node is already locked, keep trying and wait until you get one
48
+
49
+ a.polling_lock('pe-aix-72-builder', 'stahnma')
50
+ pe-aix-72-builder is locked...waiting 1 seconds.
51
+ pe-aix-72-builder is locked...waiting 2 seconds.
52
+ pe-aix-72-builder is locked...waiting 3 seconds.
53
+ pe-aix-72-builder is locked...waiting 5 seconds.
54
+ # Another operations elsewhere unlocked the node
55
+ => true
56
+
57
+ # Testing
58
+
59
+ Lock Manager now has tests and rubocop setup. There are basic tests for
60
+ the functionality of lock manager.
61
+
62
+ Tests assume you have redis running on localhost on the default port.
63
+
64
+ bundle exec rake
65
+
66
+ # License
67
+ Apache Sofware License 2.0
68
+
69
+ # Maintainers
70
+ Michael Stahnke <stahnma@puppetlabs.com>
71
+ Rick Bradley <rick@puppetlabs.com>
@@ -0,0 +1,10 @@
1
+ require 'minitest/autorun'
2
+ require 'json'
3
+ require 'lock_manager'
4
+
5
+ class LockManager
6
+ class Worker
7
+ def log(*)
8
+ end
9
+ end
10
+ end
data/test/test_lock.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+
3
+ class TestLockManagerLock < MiniTest::Test
4
+ def setup
5
+ @rlm = LockManager.new(type: 'redis', server: 'localhost')
6
+ @rlm.unlock 'pe-aix-72-builder', 'stahnma' # clear out any previously set locks
7
+ end
8
+
9
+ def test_lock_method
10
+ assert_equal true, @rlm.lock('pe-aix-72-builder', 'stahnma')
11
+ end
12
+
13
+ def test_check_lock
14
+ @rlm.lock('pe-aix-72-builder', 'stahnma', 'because reasons')
15
+ assert_equal true, @rlm.locked?('pe-aix-72-builder')
16
+ end
17
+
18
+ def test_lock_a_locked_item
19
+ @rlm.lock('pe-aix-72-builder', 'stahnma')
20
+ assert_equal false, @rlm.lock('pe-aix-72-builder', 'stahnma')
21
+ end
22
+
23
+ def test_lock_with_ip4
24
+ assert_raises(ArgumentError) do
25
+ @rlm.lock('192.168.1.1', 'stahnma')
26
+ end
27
+ end
28
+
29
+ def test_lock_with_ip6
30
+ assert_raises(ArgumentError) do
31
+ @rlm.lock('fe80::12c3:7bff:fe90:dc60', 'stahnma')
32
+ end
33
+ end
34
+ end
data/test/test_poll.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ class TestLockManagerPoll < MiniTest::Test
4
+ def setup
5
+ @rlm = LockManager.new(type: 'redis', server: 'localhost')
6
+ @rlm.unlock 'pe-aix-72-builder', 'stahnma' # clear out any previously set locks
7
+ end
8
+
9
+ def test_polling_lock
10
+ assert @rlm.polling_lock('pe-aix-72-builder', 'stahnma')
11
+ end
12
+
13
+ def test_polling_lock_when_locked_already
14
+ skip "I don't know how to test the case where it is locked, since it just blocks"
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class TestLockManagerRedisConnect < MiniTest::Test
4
+ def test_connect_good
5
+ @rlm = LockManager.new(type: 'redis', server: 'localhost')
6
+ assert_instance_of(LockManager, @rlm)
7
+ @rlm.lock('pe-aix-72-builder', 'stahnma')
8
+ lock_data = @rlm.show('pe-aix-72-builder')
9
+ assert_equal 'stahnma', lock_data['user'], 'Expected user to be properly set'
10
+ @rlm.unlock('pe-aix-72-builder', 'stahnma')
11
+ end
12
+
13
+ def test_bogus_connect
14
+ assert_raises(SocketError) do
15
+ @rlm = LockManager.new(type: 'redis', server: 'nopehost')
16
+ @rlm.locked?('pe-aix-72-builder')
17
+ end
18
+ end
19
+ end
data/test/test_show.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ class TestLockManagerShow < MiniTest::Test
4
+ def setup
5
+ @rlm = LockManager.new(type: 'redis', server: 'localhost')
6
+ @rlm.unlock 'pe-aix-72-builder', 'stahnma' # clear out any previously set locks
7
+ end
8
+
9
+ def test_show_works_with_lock
10
+ assert @rlm.lock('pe-aix-72-builder', 'stahnma'), 'Expected to be able to lock'
11
+ lock_data = @rlm.show('pe-aix-72-builder')
12
+ assert_equal 'stahnma', lock_data['user'], 'Expected user to be properly set'
13
+ assert_nil lock_data['reason'], 'Expected no reason to be set'
14
+ end
15
+
16
+ def test_show_works_with_lock_with_reason
17
+ assert @rlm.lock('pe-aix-72-builder', 'stahnma', 'because'), 'Expected to be able to lock'
18
+ lock_data = @rlm.show('pe-aix-72-builder')
19
+ assert_equal 'stahnma', lock_data['user'], 'Expected user to be set'
20
+ assert_equal 'because', lock_data['reason'], 'Expected reason to be set'
21
+ end
22
+
23
+ def test_show_returns_nothing_with_no_lock
24
+ @rlm.unlock 'pe-aix-72-builder', 'stahnma'
25
+ lock_data = @rlm.show('pe-aix-72-builder')
26
+ assert_nil lock_data, 'Expected no lock to be set'
27
+ end
28
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ class TestLockManagerUnlock < MiniTest::Test
4
+ def setup
5
+ @rlm = LockManager.new(type: 'redis', server: 'localhost')
6
+ @rlm.unlock 'pe-aix-72-builder', 'stahnma' # clear out any previously set locks
7
+ @rlm.lock 'pe-aix-72-builder', 'stahnma' # clear out any previously set locks
8
+ end
9
+
10
+ def test_unlock_method
11
+ assert @rlm.unlock('pe-aix-72-builder', 'stahnma')
12
+ end
13
+
14
+ def test_check_locked?
15
+ @rlm.unlock 'pe-aix-72-builder', 'stahnma'
16
+ assert_equal(false, @rlm.locked?('pe-aix-72-builder'))
17
+ @rlm.lock 'pe-aix-72-builder', 'stahnma'
18
+ assert_equal(true, @rlm.locked?('pe-aix-72-builder'))
19
+ @rlm.unlock 'pe-aix-72-builder', 'stahnma'
20
+ end
21
+
22
+ def test_unlock_an_unlocked_item
23
+ @rlm.unlock 'pe-aix-72-builder', 'stahnma'
24
+ refute @rlm.unlock 'pe-aix-72-builder', 'stahnma'
25
+ end
26
+
27
+ def test_dont_unlock_a_lock_not_with_owner_mismatch
28
+ refute @rlm.unlock 'pe-aix-72-builder', 'fooman'
29
+ assert @rlm.unlock 'pe-aix-72-builder', 'stahnma'
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lock_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Puppet Labs
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ description: A simple interface for locking hardware resources available via ruby
56
+ and backed by redis.
57
+ email: info@puppetlabs.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - test/test_helper.rb
65
+ - test/test_lock.rb
66
+ - test/test_poll.rb
67
+ - test/test_redis_connect.rb
68
+ - test/test_show.rb
69
+ - test/test_unlock.rb
70
+ homepage: http://github.com/puppetlabs/lock_manager
71
+ licenses:
72
+ - Apache-2.0
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.1'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.4.8
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Simply system for locking and unlocking hardware resources.
94
+ test_files:
95
+ - test/test_redis_connect.rb
96
+ - test/test_unlock.rb
97
+ - test/test_show.rb
98
+ - test/test_lock.rb
99
+ - test/test_helper.rb
100
+ - test/test_poll.rb