gapinc-cobbler 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/COPYING +510 -0
- data/ChangeLog +40 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +41 -0
- data/NEWS +5 -0
- data/README.rdoc +87 -0
- data/Rakefile +75 -0
- data/TODO +6 -0
- data/config/cobbler.yml +5 -0
- data/examples/example_distros.rb +119 -0
- data/examples/example_version.rb +31 -0
- data/examples/utils.rb +44 -0
- data/lib/cobbler.rb +35 -0
- data/lib/cobbler/base.rb +128 -0
- data/lib/cobbler/common/debug.rb +59 -0
- data/lib/cobbler/common/finders.rb +55 -0
- data/lib/cobbler/common/lifecycle.rb +163 -0
- data/lib/cobbler/connection/common.rb +79 -0
- data/lib/cobbler/connection/handling.rb +108 -0
- data/lib/cobbler/distro.rb +32 -0
- data/lib/cobbler/image.rb +31 -0
- data/lib/cobbler/profile.rb +34 -0
- data/lib/cobbler/repo.rb +33 -0
- data/lib/cobbler/system.rb +45 -0
- data/rubygem-cobbler.spec +100 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +21 -0
- data/spec/unit/cobbler/base_spec.rb +194 -0
- data/spec/unit/cobbler/common/debug_spec.rb +44 -0
- data/spec/unit/cobbler/common/finders_spec.rb +101 -0
- data/spec/unit/cobbler/common/lifecycle_spec.rb +192 -0
- data/spec/unit/cobbler/connection/common_spec.rb +59 -0
- data/spec/unit/cobbler/connection/handling_spec.rb +119 -0
- data/spec/unit/cobbler/system_spec.rb +27 -0
- metadata +181 -0
@@ -0,0 +1,108 @@
|
|
1
|
+
#
|
2
|
+
# handling.rb
|
3
|
+
#
|
4
|
+
# Copyright (C) 2008,2009 Red Hat, Inc.
|
5
|
+
# Written by Darryl L. Pierce <dpierce@redhat.com>
|
6
|
+
# Extended 2012 by duritong <peter.meier@immerda.ch>
|
7
|
+
#
|
8
|
+
# This file is part of rubygem-cobbler.
|
9
|
+
#
|
10
|
+
# rubygem-cobbler is free software: you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU Lesser General Public License as published
|
12
|
+
# by the Free Software Foundation, either version 2.1 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# rubygem-cobbler is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU General Public License
|
21
|
+
# along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
|
23
|
+
require 'xmlrpc/client'
|
24
|
+
|
25
|
+
# +Handling+ provides common methods to handle the xmlrpc connection to the
|
26
|
+
# cobbler server
|
27
|
+
module Cobbler
|
28
|
+
module Connection
|
29
|
+
module Handling
|
30
|
+
include Cobbler::Common::Debug
|
31
|
+
def self.included(base)
|
32
|
+
base.extend ClassMethods
|
33
|
+
end
|
34
|
+
|
35
|
+
module ClassMethods
|
36
|
+
|
37
|
+
# Set hostname, username, password for the Cobbler server, overriding any settings
|
38
|
+
# from cobbler.yml.
|
39
|
+
attr_accessor :hostname, :username, :password
|
40
|
+
|
41
|
+
# Returns the version for the remote cobbler instance.
|
42
|
+
def remote_version
|
43
|
+
connect unless connection
|
44
|
+
@version ||= make_call("version")
|
45
|
+
end
|
46
|
+
|
47
|
+
# Logs into the Cobbler server.
|
48
|
+
def login
|
49
|
+
@auth_token ||= make_call('login', username, password)
|
50
|
+
end
|
51
|
+
|
52
|
+
def logout
|
53
|
+
make_call('logout',@auth_token)
|
54
|
+
@auth_token = nil
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
# Makes a remote call.
|
59
|
+
def make_call(*args)
|
60
|
+
raise Exception.new("No connection established on #{self.name}.") unless connection
|
61
|
+
|
62
|
+
debug("Remote call: #{args.first} (#{args[1..-1].inspect})")
|
63
|
+
result = connection.call(*args)
|
64
|
+
debug("Result: #{result}\n")
|
65
|
+
result
|
66
|
+
end
|
67
|
+
|
68
|
+
def in_transaction(do_login=false,&blk)
|
69
|
+
begin
|
70
|
+
begin_transaction
|
71
|
+
token = do_login ? login : nil
|
72
|
+
result = yield(token)
|
73
|
+
logout if do_login
|
74
|
+
ensure
|
75
|
+
end_transaction
|
76
|
+
end
|
77
|
+
result
|
78
|
+
end
|
79
|
+
|
80
|
+
protected
|
81
|
+
# Returns a connection to the Cobbler server.
|
82
|
+
def connect
|
83
|
+
debug("Connecting to http://#{hostname}/cobbler_api")
|
84
|
+
@connection = XMLRPC::Client.new2("http://#{hostname}/cobbler_api")
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
# Establishes a connection with the Cobbler system.
|
89
|
+
def begin_transaction
|
90
|
+
@connection = connect
|
91
|
+
end
|
92
|
+
# Ends a transaction and disconnects.
|
93
|
+
def end_transaction
|
94
|
+
@connection = @auth_token = @version = nil
|
95
|
+
end
|
96
|
+
|
97
|
+
def connection
|
98
|
+
@connection
|
99
|
+
end
|
100
|
+
|
101
|
+
def valid_properties?(properties)
|
102
|
+
properties && !properties.empty? && properties != '~'
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
#
|
2
|
+
# distro.rb
|
3
|
+
#
|
4
|
+
# Copyright (C) 2008,2009 Red Hat, Inc.
|
5
|
+
# Written by Darryl L. Pierce <dpierce@redhat.com>
|
6
|
+
# Extended 2012 by duritong <peter.meier@immerda.ch>
|
7
|
+
#
|
8
|
+
# This file is part of rubygem-cobbler.
|
9
|
+
#
|
10
|
+
# rubygem-cobbler is free software: you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU Lesser General Public License as published
|
12
|
+
# by the Free Software Foundation, either version 2.1 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# rubygem-cobbler is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU General Public License
|
21
|
+
# along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
|
23
|
+
# +Distro+ represents a single distrobution within Cobbler.
|
24
|
+
#
|
25
|
+
module Cobbler
|
26
|
+
class Distro < Base
|
27
|
+
cobbler_fields :name, :kernel, :breed, :depth, :arch, :initrd,
|
28
|
+
:source_repos, :kernel_options, :parent, :ks_meta
|
29
|
+
|
30
|
+
cobbler_collection :owners
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#
|
2
|
+
# image.rb
|
3
|
+
#
|
4
|
+
# Copyright (C) 2008,2009 Red Hat, Inc.
|
5
|
+
# Written by Darryl L. Pierce <dpierce@redhat.com>
|
6
|
+
# Extended 2012 by duritong <peter.meier@immerda.ch>
|
7
|
+
#
|
8
|
+
# This file is part of rubygem-cobbler.
|
9
|
+
#
|
10
|
+
# rubygem-cobbler is free software: you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU Lesser General Public License as published
|
12
|
+
# by the Free Software Foundation, either version 2.1 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# rubygem-cobbler is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU General Public License
|
21
|
+
# along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
|
23
|
+
# +Image+ represents an image within Cobbler.
|
24
|
+
#
|
25
|
+
module Cobbler
|
26
|
+
class Image < Base
|
27
|
+
cobbler_fields :name, :owners, :depth, :virt_file_size,
|
28
|
+
:virt_path, :xml_file, :virt_bridge, :file, :parent,
|
29
|
+
:image_type, :virt_ram, :virt_auto_boot, :virt_cpus, :virt_type, :network_count
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
#
|
2
|
+
# profile.rb
|
3
|
+
#
|
4
|
+
# Copyright (C) 2008,2009 Red Hat, Inc.
|
5
|
+
# Written by Darryl L. Pierce <dpierce@redhat.com>
|
6
|
+
# Extended 2012 by duritong <peter.meier@immerda.ch>
|
7
|
+
#
|
8
|
+
# This file is part of rubygem-cobbler.
|
9
|
+
#
|
10
|
+
# rubygem-cobbler is free software: you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU Lesser General Public License as published
|
12
|
+
# by the Free Software Foundation, either version 2.1 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# rubygem-cobbler is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU General Public License
|
21
|
+
# along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
|
23
|
+
# +Profile+ represents a single profile within Cobbler.
|
24
|
+
#
|
25
|
+
module Cobbler
|
26
|
+
class Profile < Base
|
27
|
+
cobbler_fields :name, :parent, :dhcp_tag, :depth, :virt_file_size,
|
28
|
+
:virt_path, :virt_type, :repos, :distro, :server, :virt_bridge,
|
29
|
+
:virt_ram, :virt_auto_boot, :kernel_options, :virt_cpus, :ks_meta,
|
30
|
+
:kickstart
|
31
|
+
|
32
|
+
cobbler_collection :owners
|
33
|
+
end
|
34
|
+
end
|
data/lib/cobbler/repo.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
#
|
2
|
+
# repo.rb
|
3
|
+
#
|
4
|
+
# Copyright (C) 2008,2009 Red Hat, Inc.
|
5
|
+
# Written by Darryl L. Pierce <dpierce@redhat.com>
|
6
|
+
# Extended 2012 by duritong <peter.meier@immerda.ch>
|
7
|
+
#
|
8
|
+
# This file is part of rubygem-cobbler.
|
9
|
+
#
|
10
|
+
# rubygem-cobbler is free software: you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU Lesser General Public License as published
|
12
|
+
# by the Free Software Foundation, either version 2.1 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# rubygem-cobbler is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU General Public License
|
21
|
+
# along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
|
23
|
+
# +Repo+ represents a single repository within Cobbler.
|
24
|
+
#
|
25
|
+
module Cobbler
|
26
|
+
class Repo < Base
|
27
|
+
cobbler_fields :name, :arch, :breed, :comment, :keep_updated, :mirror,
|
28
|
+
:parent, :rpm_list, :createrepo_flags, :environment, :mirror_locally, :priority,
|
29
|
+
:yumopts
|
30
|
+
|
31
|
+
cobbler_collection :owners
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#
|
2
|
+
# system.rb
|
3
|
+
#
|
4
|
+
# Copyright (C) 2008,2009 Red Hat, Inc.
|
5
|
+
# Written by Darryl L. Pierce <dpierce@redhat.com>
|
6
|
+
# Extended 2012 by duritong <peter.meier@immerda.ch>
|
7
|
+
#
|
8
|
+
# This file is part of rubygem-cobbler.
|
9
|
+
#
|
10
|
+
# rubygem-cobbler is free software: you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU Lesser General Public License as published
|
12
|
+
# by the Free Software Foundation, either version 2.1 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# rubygem-cobbler is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU General Public License
|
21
|
+
# along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
|
23
|
+
# +System+ represents a system within Cobbler.
|
24
|
+
#
|
25
|
+
module Cobbler
|
26
|
+
class System < Base
|
27
|
+
cobbler_fields :name, :profile, :image, :kickstart, :netboot_enabled, :server, :virt_cpus,
|
28
|
+
:virt_file_size, :virt_path, :virt_ram, :virt_auto_boot, :virt_type, :gateway, :hostname
|
29
|
+
|
30
|
+
cobbler_collection :kernel_options, :packing => :hash
|
31
|
+
cobbler_collection :ks_meta, :packing => :hash
|
32
|
+
cobbler_collection :owners
|
33
|
+
cobbler_collection :interfaces, :packing => :hash, :store => :store_interfaces
|
34
|
+
|
35
|
+
def store_interfaces(sysid,token)
|
36
|
+
interfaces.each do |interface,values|
|
37
|
+
values2store = values.keys.inject({}) do |result,member|
|
38
|
+
result["#{member.to_s.gsub(/_/,'')}-#{interface}"] = values[member] if values[member]
|
39
|
+
result
|
40
|
+
end
|
41
|
+
self.class.make_call('modify_system',sysid,'modify_interface',values2store,token) unless values2store.empty?
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# Generated from cobbler-0.0.1.gem by gem2rpm -*- rpm-spec -*-
|
2
|
+
%define ruby_sitelib %(ruby -rrbconfig -e "puts Config::CONFIG['sitelibdir']")
|
3
|
+
%define gemdir %(ruby -rubygems -e 'puts Gem::dir' 2>/dev/null)
|
4
|
+
%define gemname cobbler
|
5
|
+
%define geminstdir %{gemdir}/gems/%{gemname}-%{version}
|
6
|
+
%define installroot %{buildroot}%{geminstdir}
|
7
|
+
|
8
|
+
Summary: An interface for interacting with a Cobbler server
|
9
|
+
Name: rubygem-%{gemname}
|
10
|
+
Version: 2.0.0
|
11
|
+
Release: 1%{?dist}
|
12
|
+
Group: Development/Languages
|
13
|
+
License: LGPLv2+
|
14
|
+
URL: https://github.com/duritong/ruby-cobbler
|
15
|
+
Source0: https://rubygems.org/downloads/%{gemname}-%{version}.gem
|
16
|
+
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
|
17
|
+
Requires: rubygems
|
18
|
+
BuildRequires: ruby-flexmock
|
19
|
+
BuildRequires: rubygems
|
20
|
+
BuildRequires: rubygem-rake
|
21
|
+
BuildArch: noarch
|
22
|
+
Provides: rubygem(%{gemname}) = %{version}
|
23
|
+
|
24
|
+
%description
|
25
|
+
Provides Ruby bindings to interact with a Cobbler server.
|
26
|
+
|
27
|
+
%prep
|
28
|
+
|
29
|
+
%build
|
30
|
+
|
31
|
+
%check
|
32
|
+
|
33
|
+
cd %{installroot}
|
34
|
+
|
35
|
+
rake test
|
36
|
+
|
37
|
+
%install
|
38
|
+
rm -rf %{buildroot}
|
39
|
+
mkdir -p %{buildroot}%{gemdir}
|
40
|
+
gem install --local --install-dir %{buildroot}%{gemdir} --force %{SOURCE0}
|
41
|
+
|
42
|
+
chmod +x %{installroot}/examples/*.rb
|
43
|
+
|
44
|
+
%clean
|
45
|
+
rm -rf %{buildroot}
|
46
|
+
|
47
|
+
%files
|
48
|
+
%defattr(-, root, root, -)
|
49
|
+
%{gemdir}/gems/%{gemname}-%{version}/
|
50
|
+
%{gemdir}/cache/%{gemname}-%{version}.gem
|
51
|
+
%{gemdir}/specifications/%{gemname}-%{version}.gemspec
|
52
|
+
|
53
|
+
%doc %{geminstdir}/COPYING
|
54
|
+
%doc %{geminstdir}/NEWS
|
55
|
+
%doc %{geminstdir}/README
|
56
|
+
|
57
|
+
%{geminstdir}/config/cobbler.yml
|
58
|
+
|
59
|
+
|
60
|
+
%changelog
|
61
|
+
* Thu Jan 05 2012 duritong <peter.meier@immerda.ch> - 2.0.0-1
|
62
|
+
- Fix all issues when API returns ~ on a nonexisting system
|
63
|
+
- Add some missing fields to the System model
|
64
|
+
- Remove unnecessary object abstraction for interfaces
|
65
|
+
- Fix storing of interfaces
|
66
|
+
- Rework connection handling
|
67
|
+
- Rewrite to enable capabilities for all items and to work against 2.0
|
68
|
+
|
69
|
+
* Mon Sep 08 2008 Darryl Pierce <dpierce@redhat.com> - 0.1.0-1
|
70
|
+
- First official build for Fedora.
|
71
|
+
|
72
|
+
* Fri Sep 05 2008 Darryl Pierce <dpierce@redhat.com> - 0.0.2-4
|
73
|
+
- Bad BuildRequires slipped into the last version.
|
74
|
+
|
75
|
+
* Wed Sep 03 2008 Darryl Pierce <dpierce@redhat.com> - 0.0.2-3
|
76
|
+
- Added a build requirement for rubygem-rake.
|
77
|
+
|
78
|
+
* Tue Aug 26 2008 Darryl Pierce <dpierce@redhat.com> - 0.0.2-2
|
79
|
+
- Fixed the licensing in each source module to show the code is released under
|
80
|
+
LGPLv2.1.
|
81
|
+
- Added %check to the spec file to run tests prior to creating the RPM.
|
82
|
+
|
83
|
+
* Thu Aug 21 2008 Darryl Pierce <dpierce@redhat.com> - 0.0.2-1
|
84
|
+
- Added a call to update prior to saving or updating a system. If the update
|
85
|
+
fails, then an Exception is raised.
|
86
|
+
|
87
|
+
* Wed Aug 13 2008 Darryl Pierce <dpierce@redhat.com> - 0.0.1-3
|
88
|
+
- Added caching for the auth_token to prevent extraneous calls to login.
|
89
|
+
- Reworked and refined how cobbler_collection fields are processed, adding
|
90
|
+
support for both array and has properties.
|
91
|
+
- Rewrote the documentation for Cobbler::Base to make it easier to understand
|
92
|
+
how to extend it to support other Cobbler types.
|
93
|
+
- Refactored the examples to clean up the code.
|
94
|
+
|
95
|
+
* Wed Aug 13 2008 Darryl Pierce <dpierce@redhat.com> - 0.0.1-2
|
96
|
+
- Removed markup of cobbler.yml and a config file. Fixed a few small bugs
|
97
|
+
in the code for using it as a gem.
|
98
|
+
|
99
|
+
* Mon Aug 04 2008 Darryl Pierce <dpierce@redhat.com> - 0.0.1-1
|
100
|
+
- Initial package
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
dir = Pathname.new(__FILE__).parent
|
3
|
+
$LOAD_PATH.unshift(File.join(dir,'lib'))
|
4
|
+
require 'cobbler'
|
5
|
+
gem 'rspec', '~> 2.5.0'
|
6
|
+
require 'mocha'
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.mock_with :mocha
|
10
|
+
end
|
11
|
+
|
12
|
+
def with_real_cobbler(calzz,&blk)
|
13
|
+
unless ENV['NO_REAL_COBBLER'] == '1'
|
14
|
+
config = (ENV['COBBLER_YML'] || File.expand_path(File.join(File.dirname(__FILE__),'..','config','cobbler.yml')))
|
15
|
+
if File.exist?(config) && (yml = YAML::load(File.open(config))) && (yml['hostname'] && yml['username'] && yml['password'])
|
16
|
+
yield(yml)
|
17
|
+
else
|
18
|
+
puts "No cobbler data found."
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,194 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
3
|
+
|
4
|
+
class BaseTest < Cobbler::Base
|
5
|
+
cobbler_field :name
|
6
|
+
cobbler_field :locked_field, :locked => true
|
7
|
+
cobbler_field :saveable_field
|
8
|
+
end
|
9
|
+
|
10
|
+
class Unremovable < Cobbler::Base
|
11
|
+
cobbler_lifecycle :remove => false
|
12
|
+
end
|
13
|
+
|
14
|
+
class Unsaveable1 < Cobbler::Base
|
15
|
+
cobbler_lifecycle :handle => false
|
16
|
+
end
|
17
|
+
|
18
|
+
class Unsaveable2 < Cobbler::Base
|
19
|
+
cobbler_lifecycle :modify => false
|
20
|
+
end
|
21
|
+
|
22
|
+
class Unsaveable3 < Cobbler::Base
|
23
|
+
cobbler_lifecycle :new => false, :save => false
|
24
|
+
end
|
25
|
+
|
26
|
+
class CollTest1 < Cobbler::Base
|
27
|
+
cobbler_field :name
|
28
|
+
cobbler_collection :coll1
|
29
|
+
end
|
30
|
+
class CollTest2 < Cobbler::Base
|
31
|
+
cobbler_field :name
|
32
|
+
cobbler_collection :coll2, :store => :store_coll2
|
33
|
+
|
34
|
+
def store_coll2
|
35
|
+
coll2.to_a.each do |item|
|
36
|
+
#do something
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe Cobbler::Base do
|
42
|
+
|
43
|
+
[:remove, :save ].each do |method|
|
44
|
+
it "should provide a method to #{method} the item" do
|
45
|
+
test = BaseTest.new
|
46
|
+
test.should respond_to(method)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "when initializing" do
|
51
|
+
it "should add the fields to the user fields" do
|
52
|
+
test = BaseTest.new({'name' => 1, 'b' => 2})
|
53
|
+
test.definitions.keys.should be_empty
|
54
|
+
test.user_definitions.keys.should_not be_empty
|
55
|
+
test.user_definitions.keys.should include('name')
|
56
|
+
test.user_definitions.keys.should include('b')
|
57
|
+
test.name.should == 1
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should add the fields to the old definitions if it's an old record" do
|
61
|
+
test = BaseTest.new({:a => 1, :b => 2},false)
|
62
|
+
test.definitions.keys.should_not be_empty
|
63
|
+
test.definitions.keys.should include(:a)
|
64
|
+
test.definitions.keys.should include(:b)
|
65
|
+
test.user_definitions.keys.should be_empty
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "when removing an item" do
|
70
|
+
it "should raise an exception if we have no api_method to remove the item" do
|
71
|
+
lambda { Unremovable.new.remove }.should raise_error(Exception)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should call the remove api_method for the item" do
|
75
|
+
test = BaseTest.new({'name' => 'foo'})
|
76
|
+
test.name.should == 'foo'
|
77
|
+
connection = Object.new
|
78
|
+
BaseTest.expects(:login).returns('muh')
|
79
|
+
BaseTest.expects(:logout)
|
80
|
+
BaseTest.expects(:connect).returns(connection)
|
81
|
+
BaseTest.expects(:make_call).with(test.api_methods[:remove],test.name,'muh')
|
82
|
+
test.remove
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "when trying to save an item" do
|
87
|
+
it "should raise an exception if we have no api_method to save and/or update the item" do
|
88
|
+
[Unsaveable1,Unsaveable2,Unsaveable3].each do |klass|
|
89
|
+
lambda { klass.new.save }.should raise_error(Exception)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
describe "when saving an existing item" do
|
94
|
+
before(:each) do
|
95
|
+
@test1 = BaseTest.new({'name' => 'foo','locked_field' => '1', 'saveable_field' => 2},false)
|
96
|
+
BaseTest.expects(:connect).returns(Object.new)
|
97
|
+
BaseTest.expects(:login).returns('muh')
|
98
|
+
BaseTest.expects(:logout)
|
99
|
+
BaseTest.expects(:find_one).with('foo').returns('entry')
|
100
|
+
BaseTest.expects(:make_call).with('get_base_test_handle','foo','muh').returns('id')
|
101
|
+
BaseTest.expects(:make_call).with('save_base_test','id','muh')
|
102
|
+
end
|
103
|
+
it "should store no fields on a unchanged record" do
|
104
|
+
[:name,:locked_field,:saveable_field].each do |field|
|
105
|
+
BaseTest.expects(:make_call).with('modify_base_test','id',"#{field}",@test1.send(field),'muh').never
|
106
|
+
end
|
107
|
+
@test1.save
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should only store changed fields" do
|
111
|
+
@test1.saveable_field = 3
|
112
|
+
[:name,:locked_field].each do |field|
|
113
|
+
BaseTest.expects(:make_call).with('modify_base_test','id',"#{field}",@test1.send(field),'muh').never
|
114
|
+
end
|
115
|
+
BaseTest.expects(:make_call).with('modify_base_test','id',"saveable_field",3,'muh')
|
116
|
+
@test1.save
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should store no locked fields" do
|
120
|
+
@test1.locked_field = 'foobar'
|
121
|
+
[:name,:locked_field,:saveable_field].each do |field|
|
122
|
+
BaseTest.expects(:make_call).with('modify_base_test','id',"#{field}",@test1.send(field),'muh').never
|
123
|
+
end
|
124
|
+
@test1.save
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "when saving a not yet existing item" do
|
129
|
+
before(:each) do
|
130
|
+
@test1 = BaseTest.new({'name' => 'foo','locked_field' => '1', 'saveable_field' => 2},false)
|
131
|
+
BaseTest.expects(:connect).returns(Object.new)
|
132
|
+
BaseTest.expects(:find_one).with('foo').returns(nil)
|
133
|
+
BaseTest.expects(:login).returns('muh')
|
134
|
+
BaseTest.expects(:logout)
|
135
|
+
BaseTest.expects(:make_call).with('new_base_test','muh').returns('id')
|
136
|
+
BaseTest.expects(:make_call).with('modify_base_test','id',"name",@test1.name,'muh')
|
137
|
+
BaseTest.expects(:make_call).with('save_base_test','id','muh')
|
138
|
+
end
|
139
|
+
it "should store no fields on a unchanged record" do
|
140
|
+
[:name,:locked_field,:saveable_field].each do |field|
|
141
|
+
BaseTest.expects(:make_call).with('modify_base_test','id',"#{field}",@test1.send(field),'muh').never
|
142
|
+
end
|
143
|
+
@test1.save
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should only store changed fields" do
|
147
|
+
@test1.saveable_field = 3
|
148
|
+
[:name,:locked_field].each do |field|
|
149
|
+
BaseTest.expects(:make_call).with('modify_base_test','id',"#{field}",@test1.send(field),'muh').never
|
150
|
+
end
|
151
|
+
BaseTest.expects(:make_call).with('modify_base_test','id',"saveable_field",3,'muh')
|
152
|
+
@test1.save
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should store no locked fields" do
|
156
|
+
@test1.locked_field = 'foobar'
|
157
|
+
[:name,:locked_field,:saveable_field].each do |field|
|
158
|
+
BaseTest.expects(:make_call).with('modify_base_test','id',"#{field}",@test1.send(field),'muh').never
|
159
|
+
end
|
160
|
+
@test1.save
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
describe "when saving an item with collections" do
|
165
|
+
it "should store a normal collection as a normal field" do
|
166
|
+
@test1 = CollTest1.new({'name' => 'foo'},false)
|
167
|
+
@test1.coll1 = [1,2]
|
168
|
+
CollTest1.expects(:connect).returns(Object.new)
|
169
|
+
CollTest1.expects(:find_one).with('foo').returns(nil)
|
170
|
+
CollTest1.expects(:login).returns('muh')
|
171
|
+
CollTest1.expects(:logout)
|
172
|
+
CollTest1.expects(:make_call).with('new_coll_test1','muh').returns('id')
|
173
|
+
CollTest1.expects(:make_call).with('modify_coll_test1','id',"name",@test1.name,'muh')
|
174
|
+
CollTest1.expects(:make_call).with('modify_coll_test1','id',"coll1",[1,2],'muh')
|
175
|
+
CollTest1.expects(:make_call).with('save_coll_test1','id','muh')
|
176
|
+
@test1.save
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should call the store methods for a special storeable field" do
|
180
|
+
@test1 = CollTest2.new({'name' => 'foo'},false)
|
181
|
+
@test1.coll2 = [1,2]
|
182
|
+
CollTest2.expects(:connect).returns(Object.new)
|
183
|
+
CollTest2.expects(:find_one).with('foo').returns(nil)
|
184
|
+
CollTest2.expects(:login).returns('muh')
|
185
|
+
CollTest2.expects(:logout)
|
186
|
+
CollTest2.expects(:make_call).with('new_coll_test2','muh').returns('id')
|
187
|
+
CollTest2.expects(:make_call).with('modify_coll_test2','id',"name",@test1.name,'muh')
|
188
|
+
@test1.expects(:store_coll2)
|
189
|
+
CollTest2.expects(:make_call).with('save_coll_test2','id','muh')
|
190
|
+
@test1.save
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|