Skip to content

Commit

Permalink
Add support for yast2-configuration-management
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Dec 17, 2018
1 parent dd48881 commit 274904b
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Makefile.am
Expand Up @@ -24,7 +24,8 @@ client_DATA = \
clients/firstboot_auto.rb \
clients/firstboot_hostname.rb \
clients/firstboot_root.rb \
clients/firstboot_user.rb
clients/firstboot_user.rb \
clients/firstboot_configuration_management.rb

yncludedir = @yncludedir@/firstboot
ynclude_DATA = \
Expand All @@ -42,6 +43,7 @@ fillup_DATA = \

ylibclientdir = "${yast2dir}/lib/y2firstboot/clients"
ylibclient_DATA = \
lib/y2firstboot/clients/configuration_management.rb \
lib/y2firstboot/clients/root.rb \
lib/y2firstboot/clients/user.rb

Expand Down
24 changes: 24 additions & 0 deletions src/clients/firstboot_configuration_management.rb
@@ -0,0 +1,24 @@
# encoding: utf-8

# Copyright (c) [2018] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "y2firstboot/clients/configuration_management"

Y2Firstboot::Clients::ConfigurationManagement.new.run
64 changes: 64 additions & 0 deletions src/lib/y2firstboot/clients/configuration_management.rb
@@ -0,0 +1,64 @@
# encoding: utf-8

# Copyright (c) [2018] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "yast"
require "configuration_management/clients/provision"
require "configuration_management/configurators/base"

Yast.import "ProductFeatures"

module Y2Firstboot
module Clients
# This client is meant to be used in firstboot
class ConfigurationManagement
# Runs the client
def run
configurator = Yast::ConfigurationManagement::Configurators::Base.for(config)
configurator.prepare
Yast::ConfigurationManagement::Clients::Provision.new.run
end

private

# @return [Hash] Default settings
FIXED_SETTINGS = { "type" => "salt", "mode" => "masterless" }.freeze

# Returns the configuration management configuration
#
# It relies in the configuration found in the control file.
#
# @return [Yast::ConfigurationManagement::Configurations::Base]
def config
settings = settings_from_control_file.merge(FIXED_SETTINGS)
Yast::ConfigurationManagement::Configurations::Base.import(settings)
end

# Retrieves the configuration management settings found in the control file
#
# It returns a minimal configuration if none is found.
#
# @return [Hash]
def settings_from_control_file
Yast::ProductFeatures.GetFeature("globals", "configuration_management") || {}
end
end
end
end
49 changes: 49 additions & 0 deletions test/test_helper.rb
@@ -0,0 +1,49 @@
srcdir = File.expand_path("../../src", __FILE__)
y2dirs = ENV.fetch("Y2DIR", "").split(":")
ENV["Y2DIR"] = y2dirs.unshift(srcdir).join(":")

require "yast"
require "pathname"

TESTS_PATH = Pathname.new(File.dirname(__FILE__))
FIXTURES_PATH = TESTS_PATH.join("fixtures")

if ENV["COVERAGE"]
require "simplecov"
SimpleCov.start do
add_filter "/test/"
end

# for coverage we need to load all ruby files
src_location = File.expand_path("../../src", __FILE__)
Dir["#{src_location}/{module,lib}/**/*.rb"].each { |f| require_relative f }

# use coveralls for on-line code coverage reporting at Travis CI
if ENV["TRAVIS"]
require "coveralls"
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
SimpleCov::Formatter::HTMLFormatter,
Coveralls::SimpleCov::Formatter
]
end
end

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
end
83 changes: 83 additions & 0 deletions test/y2firstboot/clients/configuration_management_test.rb
@@ -0,0 +1,83 @@
#!/usr/bin/env rspec
# encoding: utf-8

# Copyright (c) [2018] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../../test_helper"
require "y2firstboot/clients/configuration_management"
require "configuration_management/configurators/salt"

describe Y2Firstboot::Clients::ConfigurationManagement do
subject(:client) { described_class.new }

describe "#run" do
let(:provisioner) do
instance_double(Yast::ConfigurationManagement::Clients::Provision, run: nil)
end
let(:configurator) do
instance_double(Yast::ConfigurationManagement::Configurators::Salt, prepare: nil)
end
let(:settings) { { "states_roots" => ["/srv/salt"] } }

before do
allow(Yast::ProductFeatures).to receive(:GetFeature)
.with("globals", "configuration_management")
.and_return(settings)
allow(Yast::ConfigurationManagement::Clients::Provision).to receive(:new)
.and_return(provisioner)
allow(Yast::ConfigurationManagement::Configurators::Base).to receive(:for)
.and_return(configurator)
end

it "runs the provisioner" do
expect(provisioner).to receive(:run)
client.run
end

it "uses the configuration from the control file" do
expect(Yast::ConfigurationManagement::Configurators::Base).to receive(:for) do |config|
expect(config.states_roots).to include(Pathname.new("/srv/salt"))
configurator
end
client.run
end

context "when type or mode are specified in the configuration" do
let(:settings) { { "type" => "puppet", "mode" => "client" } }

it "forces type and mode" do
expect(Yast::ConfigurationManagement::Configurators::Base).to receive(:for)
.with(an_object_having_attributes(type: "salt", mode: :masterless))
.and_return(configurator)
client.run
end
end

context "when no settings are specified" do
let(:settings) { nil }

it "uses the default configuration" do
expect(Yast::ConfigurationManagement::Configurators::Base).to receive(:for)
.with(an_object_having_attributes(type: "salt")).and_return(configurator)
client.run
end
end
end
end

0 comments on commit 274904b

Please sign in to comment.