pi_customizer 0.4.0.pre.alpha → 0.5.0.pre.alpha

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.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/CODE_OF_CONDUCT.md +1 -1
  4. data/README.md +55 -20
  5. data/Rakefile +6 -5
  6. data/docs/concept.md +0 -18
  7. data/docs/incubation.md +3 -14
  8. data/pi_build_modifier/Gemfile.lock +50 -0
  9. data/pi_build_modifier/lib/pi_build_modifier.rb +4 -7
  10. data/pi_build_modifier/lib/pi_build_modifier/modify/configs.rb +335 -0
  11. data/pi_build_modifier/lib/pi_build_modifier/modify/modifiers.rb +281 -0
  12. data/pi_build_modifier/lib/pi_build_modifier/{locale → modify}/templates/00-debconf.erb +1 -1
  13. data/pi_build_modifier/lib/pi_build_modifier/{boot-files → modify}/templates/07-resize-init.diff.erb +1 -1
  14. data/pi_build_modifier/lib/pi_build_modifier/{net-tweaks → modify}/templates/wpa_supplicant.conf.erb +1 -1
  15. data/pi_build_modifier/lib/pi_build_modifier/{config → utils}/logex.rb +5 -1
  16. data/pi_build_modifier/lib/pi_build_modifier/version.rb +1 -1
  17. data/pi_build_modifier/spec/pi_build_modifier/modify/modify_spec.rb +306 -0
  18. data/pi_build_modifier/spec/pi_build_modifier_spec.rb +3 -3
  19. data/pi_build_modifier/spec/spec_helper.rb +1 -0
  20. data/pi_customizer/Gemfile.lock +47 -0
  21. data/pi_customizer/lib/pi_customizer.rb +15 -9
  22. data/pi_customizer/lib/pi_customizer/build/builder/build_executor.rb +12 -24
  23. data/pi_customizer/lib/pi_customizer/build/builder/builder.rb +17 -13
  24. data/pi_customizer/{spec/pi_customizer/environment/environment_builder_factory_spec.rb → lib/pi_customizer/build/config/build_config.rb} +27 -18
  25. data/pi_customizer/lib/pi_customizer/build/{workspace → config}/local_workspace.rb +8 -8
  26. data/pi_customizer/lib/pi_customizer/build/{workspace → config}/remote_workspace.rb +1 -1
  27. data/pi_customizer/lib/pi_customizer/build/environment/docker/docker.rb +1 -1
  28. data/pi_customizer/lib/pi_customizer/build/environment/environment.rb +1 -1
  29. data/pi_customizer/lib/pi_customizer/build/{builder/start_prepare_execute_builder.rb → environment/environment_factory.rb} +27 -16
  30. data/pi_customizer/lib/pi_customizer/build/environment/vagrant/templates/Vagrantfile.erb +2 -2
  31. data/pi_customizer/lib/pi_customizer/build/environment/vagrant/vagrant.rb +3 -8
  32. data/pi_customizer/lib/pi_customizer/build/environment/vagrant/vagrant_file.rb +3 -3
  33. data/pi_customizer/lib/pi_customizer/version.rb +1 -1
  34. data/pi_customizer/lib/pi_customizer/write/image_writer.rb +3 -2
  35. data/pi_customizer/spec/pi_customizer/build/builder/builder_spec.rb +122 -0
  36. data/pi_customizer/spec/pi_customizer/{environment/aws_spec.rb → build/config/build_config_spec.rb} +20 -25
  37. data/pi_customizer/spec/pi_customizer/{workspace/local_workspace_spec.rb → build/config/local_workspace_config_spec.rb} +10 -10
  38. data/pi_customizer/spec/pi_customizer/{workspace → build/config}/remote_workspace_spec.rb +2 -2
  39. data/pi_customizer/spec/pi_customizer/{environment/vagrant/vagrant_spec.rb → build/environment/environment_builder_factory_spec.rb} +17 -13
  40. data/pi_customizer/spec/pi_customizer/{environment → build/environment}/environment_spec.rb +1 -1
  41. data/pi_customizer/spec/pi_customizer/{environment → build/environment}/vagrant/vagrant_file_spec.rb +7 -7
  42. data/pi_customizer/spec/pi_customizer/build/environment/vagrant/vagrant_spec.rb +80 -0
  43. data/pi_customizer/spec/pi_customizer_spec.rb +19 -14
  44. data/pi_customizer/spec/spec_helper.rb +21 -0
  45. data/version.rb +1 -1
  46. metadata +23 -42
  47. data/Vagrantfile +0 -27
  48. data/pi_build_modifier/.gitignore +0 -12
  49. data/pi_build_modifier/lib/pi_build_modifier/boot-files/boot.rb +0 -64
  50. data/pi_build_modifier/lib/pi_build_modifier/locale/locale_debconf.rb +0 -66
  51. data/pi_build_modifier/lib/pi_build_modifier/modifier/erb_mapper.rb +0 -72
  52. data/pi_build_modifier/lib/pi_build_modifier/modifier/mapper.rb +0 -49
  53. data/pi_build_modifier/lib/pi_build_modifier/modifier/pi_modifier.rb +0 -81
  54. data/pi_build_modifier/lib/pi_build_modifier/modifier_task.rb +0 -79
  55. data/pi_build_modifier/lib/pi_build_modifier/net-tweaks/wifi_network.rb +0 -124
  56. data/pi_build_modifier/lib/pi_build_modifier/sys_tweaks/run_modifier.rb +0 -63
  57. data/pi_build_modifier/lib/pi_build_modifier/sys_tweaks/ssh.rb +0 -61
  58. data/pi_build_modifier/lib/pi_build_modifier/sys_tweaks/templates/ssh.sh.erb +0 -8
  59. data/pi_build_modifier/lib/pi_build_modifier/system/system_type.rb +0 -84
  60. data/pi_build_modifier/spec/pi_build_modifier/boot/boot_spec.rb +0 -103
  61. data/pi_build_modifier/spec/pi_build_modifier/locale/locale_spec.rb +0 -87
  62. data/pi_build_modifier/spec/pi_build_modifier/modifier/mapper_spec.rb +0 -105
  63. data/pi_build_modifier/spec/pi_build_modifier/modifier/modifier_spec.rb +0 -83
  64. data/pi_build_modifier/spec/pi_build_modifier/modifier_task_spec.rb +0 -63
  65. data/pi_build_modifier/spec/pi_build_modifier/net-tweaks/wifi_network_spec.rb +0 -91
  66. data/pi_build_modifier/spec/pi_build_modifier/sys_tweaks/run_modifier_spec.rb +0 -74
  67. data/pi_build_modifier/spec/pi_build_modifier/sys_tweaks/ssh_spec.rb +0 -73
  68. data/pi_build_modifier/spec/pi_build_modifier/system/system_spec.rb +0 -66
  69. data/pi_customizer/lib/pi_customizer/build/builder/prepare_start_execute_builder.rb +0 -43
  70. data/pi_customizer/lib/pi_customizer/build/environment/aws/aws.rb +0 -76
  71. data/pi_customizer/lib/pi_customizer/build/environment/environment_builder_factory.rb +0 -81
  72. data/pi_customizer/spec/pi_customizer/builder/build_executor_spec.rb +0 -64
@@ -0,0 +1,281 @@
1
+ # Copyright (c) 2017-2019 Beate Ottenwälder
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ require 'json'
22
+ require 'erb'
23
+ require 'fileutils'
24
+ require 'pi_build_modifier/modify/configs'
25
+ require 'pi_build_modifier/utils/logex'
26
+
27
+ module PiBuildModifier
28
+
29
+ module Modifiers
30
+
31
+ ##
32
+ # ModifiersBuilder creates a Modifiers object and adds build configuration modifiers which map the (json) config to
33
+ # the the individual pi-gen build sources
34
+
35
+ class ModifiersBuilder
36
+ class << self
37
+
38
+ ##
39
+ # Initialization and configuration of the modifiers that are used to change and configure the build sources
40
+
41
+ def build_defaults(workspace, config)
42
+ modifiers = Modifiers.new
43
+ modifiers.with_json_configuration(config)
44
+ create_modifiers(workspace).each do |*, modifier|
45
+ modifiers.with_config_modifier(modifier)
46
+ end
47
+ modifiers
48
+ end
49
+
50
+ private def create_modifiers(workspace)
51
+ {
52
+ :erb_config => create_erb_modifier(workspace),
53
+ :config_file => create_config_file_modifier(workspace),
54
+ :default => create_config_modifier(workspace)
55
+ }
56
+ end
57
+
58
+ private def create_config_modifier(workspace)
59
+ config_modifier = ConfigModifier.new(workspace)
60
+ Configs::create_config_modifiers.each do |modifier|
61
+ config_modifier.add(modifier)
62
+ end
63
+ config_modifier
64
+ end
65
+
66
+ private def create_erb_modifier(workspace)
67
+ erb_modifier = ERBConfigModifier.new(workspace)
68
+ Configs::create_erb_modifiers.each do |modifier|
69
+ erb_modifier.add(modifier)
70
+ end
71
+ erb_modifier
72
+ end
73
+
74
+ private def create_config_file_modifier(workspace)
75
+ config_file_modifier = ConfigFileModifier.new(workspace)
76
+ Configs::create_config_file_modifiers.each do |modifier|
77
+ config_file_modifier.add(modifier)
78
+ end
79
+ config_file_modifier
80
+ end
81
+ end
82
+ end
83
+
84
+ ##
85
+ # Modifiers orchestrates all changes specified in the (json) config file.
86
+
87
+ class Modifiers
88
+
89
+ attr_reader :config_modifiers, :json_data
90
+
91
+ def initialize
92
+ @config_modifiers = Array.new
93
+ @json_path = nil
94
+ @json_data = nil
95
+ end
96
+
97
+ def with_json_configuration(json_file)
98
+ @json_path = json_file
99
+ self
100
+ end
101
+
102
+ def with_config_modifier(config_modifier)
103
+ if config_modifier.is_a? ConfigModifier
104
+ @config_modifiers << config_modifier
105
+ else
106
+ raise 'Parameter must be of type "ConfigModifier"'
107
+ end
108
+ self
109
+ end
110
+
111
+ def modify_configs
112
+ load_config
113
+ check_all
114
+ map_all
115
+ verify_all
116
+ modify_all
117
+ finish_all
118
+ end
119
+
120
+ private def load_config
121
+ if File.file?(@json_path)
122
+ File.open(@json_path, 'r+') do |f|
123
+ @json_data = JSON.parse(f.read)
124
+ end
125
+ else
126
+ raise "Json configuration file does not exist at '#{@json_path}!'"
127
+ end
128
+ end
129
+
130
+ private def verify_all
131
+ @config_modifiers.each(&:verify)
132
+ end
133
+
134
+ def check_all
135
+ @config_modifiers.each(&:check)
136
+ end
137
+
138
+ private def map_all
139
+ @config_modifiers.each do |mapper|
140
+ mapper.map(@json_data)
141
+ end
142
+ end
143
+
144
+ private def modify_all
145
+ @config_modifiers.each(&:modify)
146
+ end
147
+
148
+ private def finish_all
149
+ @config_modifiers.each(&:finish)
150
+ end
151
+
152
+ end
153
+
154
+ ##
155
+ # ConfigModifier (and derived classes) calls the necessary steps to modify pi-gen build sources.
156
+ # It triggers check, mapping, and modification steps for one build configuration change.
157
+
158
+ class ConfigModifier
159
+
160
+ def initialize(workspace)
161
+ @workspace = workspace
162
+ @configs = Array.new
163
+ end
164
+
165
+ def add(config)
166
+ @configs << config
167
+ end
168
+
169
+ def check
170
+ $logger.debug("No pre check specified")
171
+ end
172
+
173
+ def verify
174
+ @configs.each {|config| config.verify unless config.nil?}
175
+ end
176
+
177
+ def map(json_data)
178
+ @configs.each {|config| config.map(json_data) unless config.nil?}
179
+ end
180
+
181
+ def modify
182
+ @configs.each {|config| config.modify(@workspace) unless config.nil?}
183
+ end
184
+
185
+ def finish
186
+ $logger.debug("Skipped finish")
187
+ end
188
+
189
+ end
190
+
191
+ ##
192
+ # ConfigFileModifier creates a pi-gen config file
193
+
194
+ class ConfigFileModifier < ConfigModifier
195
+
196
+ def initialize(workspace)
197
+ super(workspace)
198
+ @cfg_lines = Array.new
199
+ @config_file = 'config'
200
+ end
201
+
202
+ def modify
203
+ @configs.each {|config| @cfg_lines << config.config_line unless config.nil?}
204
+ end
205
+
206
+ def finish
207
+ FileUtils.mkdir_p "#{@workspace}"
208
+ File.open(File.join("#{@workspace}", "#{@config_file}"), 'w+') do |file|
209
+ @cfg_lines.each {|cfg_line| file.puts(cfg_line)}
210
+ end
211
+ end
212
+
213
+ end
214
+
215
+ ##
216
+ # ERBConfigModifier creates configurations from erb templates
217
+
218
+ class ERBConfigModifier < ConfigModifier
219
+
220
+ def initialize(workspace)
221
+ super(workspace)
222
+ end
223
+
224
+ def check
225
+ @configs.each do |config|
226
+ template_path = config.template_path
227
+ output_path = config.relative_output_path
228
+ template = read_template(template_path)
229
+
230
+ rendered = render(template, config.get_binding)
231
+ original = ""
232
+ File.open(output_path, 'r+') do |f|
233
+ original = f.read
234
+ end
235
+
236
+ unless rendered.equal? original
237
+ $logger.warn "Check of sources with default configuration failed:\nrendered = \n\n #{rendered} \n\n != original = \n\n #{original}"
238
+ end
239
+
240
+ end
241
+ end
242
+
243
+ def modify
244
+ create_conf
245
+ end
246
+
247
+ private def create_conf
248
+
249
+ @configs.each do |config|
250
+ template_path = config.template_path
251
+ output_path = File.join(@workspace, config.relative_output_path)
252
+
253
+ template = read_template(template_path)
254
+
255
+ # ensure that the output_path is available
256
+ FileUtils.mkdir_p File.dirname output_path
257
+
258
+ File.open(output_path, 'w+') do |f|
259
+ f.write(render(template, config.get_binding))
260
+ end
261
+
262
+ $logger.info "Created config at #{output_path} from #{template_path}"
263
+ end
264
+ end
265
+
266
+ private def read_template(template_path)
267
+ template = ""
268
+ File.open(template_path.to_s, 'r+') do |f|
269
+ template = f.read
270
+ end
271
+ template
272
+ end
273
+
274
+ private def render(template, binding)
275
+ ERB.new(template).result(binding)
276
+ end
277
+
278
+ end
279
+
280
+ end
281
+ end
@@ -3,4 +3,4 @@
3
3
  locales locales/locales_to_be_generated multiselect <%= @gen_locales.join(', ') %>
4
4
  # Default locale for the system environment:
5
5
  # Choices: None, C.UTF-8, en_GB.UTF-8
6
- locales locales/default_environmentg_locale select <%= @sys_locale %>
6
+ locales locales/default_environment_locale select <%= @sys_locale %>
@@ -2,4 +2,4 @@
2
2
  +++ b/rootfs/boot/cmdline.txt
3
3
  @@ -1 +1 @@
4
4
  -dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=ROOTDEV rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
5
- +dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=ROOTDEV rootfstype=ext4 <%= @c_groups.join(' ') %> elevator=deadline fsck.repair=yes rootwait quiet init=/usr/lib/raspi-config/init_resize.sh
5
+ +dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=ROOTDEV rootfstype=ext4 <%= @cgroups.join(' ') %> elevator=deadline fsck.repair=yes rootwait quiet init=/usr/lib/raspi-config/init_resize.sh
@@ -2,4 +2,4 @@ country=<%= @wpa_country %>
2
2
  ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
3
3
  update_config=1
4
4
 
5
- <% @networks.each do |network| %>network=<%= network %><% end %>
5
+ <% @networks.each do |network| %>network=<%= network %><% end %>
@@ -21,5 +21,9 @@
21
21
  require 'logger'
22
22
 
23
23
  module PiBuildModifier
24
- $logger = Logger.new(STDOUT)
24
+ begin
25
+ $logger = Logger.new('/etc/pi_build_modifier/modifier.log')
26
+ rescue
27
+ $logger = Logger.new(STDOUT)
28
+ end
25
29
  end
@@ -19,5 +19,5 @@
19
19
  # SOFTWARE.
20
20
 
21
21
  module PiBuildModifier
22
- VERSION = '0.4.0.pre.alpha'
22
+ VERSION = '0.5.0.pre.alpha'
23
23
  end
@@ -0,0 +1,306 @@
1
+ # Copyright (c) 2017-2019 Beate Ottenwälder
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ require_relative '../../spec_helper'
22
+ require 'pi_build_modifier/modify/modifiers'
23
+ require 'pi_build_modifier/modify/configs'
24
+
25
+
26
+ RSpec.describe PiBuildModifier::Modifiers do
27
+
28
+ context 'ModifiersBuilder' do
29
+
30
+ it 'creates Modifiers' do
31
+ modifier = PiBuildModifier::Modifiers::ModifiersBuilder::build_defaults(".", "no.cfg")
32
+ expect(modifier.config_modifiers).not_to be_nil
33
+ expect(modifier.config_modifiers).not_to be_a PiBuildModifier::Modifiers::Modifiers
34
+ end
35
+
36
+ it 'creates and adds erb and config file modifiers to the Modifiers' do
37
+ modifier = PiBuildModifier::Modifiers::ModifiersBuilder::build_defaults("no.cfg", ".")
38
+ expect(modifier.config_modifiers).to include(be_a PiBuildModifier::Modifiers::ERBConfigModifier)
39
+ expect(modifier.config_modifiers).to include(be_a PiBuildModifier::Modifiers::ConfigFileModifier)
40
+ end
41
+
42
+ end
43
+
44
+ context 'Modifiers' do
45
+
46
+ let(:tmp_cfg_file) {'tmp.json'}
47
+
48
+ before(:each) do
49
+ File.open(tmp_cfg_file, 'w') {|file| file.write('{"cgroups": {"memory" : true}}')}
50
+ end
51
+
52
+ after(:each) do
53
+ FileUtils.rm_rf tmp_cfg_file
54
+ end
55
+
56
+ it 'holds configuration modifiers' do
57
+ modifiers = PiBuildModifier::Modifiers::Modifiers.new
58
+ modifiers.with_config_modifier(PiBuildModifier::Modifiers::ConfigModifier.new("."))
59
+ expect(modifiers.config_modifiers).to include(be_a PiBuildModifier::Modifiers::ConfigModifier)
60
+ end
61
+
62
+ it 'reads configurations from files' do
63
+ modifiers = PiBuildModifier::Modifiers::Modifiers.new
64
+ modifiers.with_json_configuration(tmp_cfg_file).modify_configs
65
+ expect(modifiers.json_data.has_key?('cgroups')).to be true
66
+ end
67
+
68
+ it 'triggers checks of the configuration modifiers' do
69
+ test_modifier = PiBuildModifier::Modifiers::ConfigModifier.new(".")
70
+ expect(test_modifier).to receive(:check)
71
+ modifiers = PiBuildModifier::Modifiers::Modifiers.new
72
+ modifiers.with_config_modifier(test_modifier).with_json_configuration(tmp_cfg_file).modify_configs
73
+ end
74
+
75
+ it 'triggers verifications of the configuration modifiers' do
76
+ test_modifier = PiBuildModifier::Modifiers::ConfigModifier.new(".")
77
+ expect(test_modifier).to receive(:verify)
78
+ modifiers = PiBuildModifier::Modifiers::Modifiers.new
79
+ modifiers.with_config_modifier(test_modifier).with_json_configuration(tmp_cfg_file).modify_configs
80
+ end
81
+
82
+ it 'triggers mappings of the configuration modifiers' do
83
+ test_modifier = PiBuildModifier::Modifiers::ConfigModifier.new(".")
84
+ expect(test_modifier).to receive(:map)
85
+ modifiers = PiBuildModifier::Modifiers::Modifiers.new
86
+ modifiers.with_config_modifier(test_modifier).with_json_configuration(tmp_cfg_file).modify_configs
87
+ end
88
+
89
+ it 'triggers modifications of the configuration modifiers' do
90
+ test_modifier = PiBuildModifier::Modifiers::ConfigModifier.new(".")
91
+ expect(test_modifier).to receive(:modify)
92
+ modifiers = PiBuildModifier::Modifiers::Modifiers.new
93
+ modifiers.with_config_modifier(test_modifier).with_json_configuration(tmp_cfg_file).modify_configs
94
+ end
95
+
96
+ it 'triggers finishing steps of the configuration modifiers' do
97
+ test_modifier = PiBuildModifier::Modifiers::ConfigModifier.new(".")
98
+ expect(test_modifier).to receive(:finish)
99
+ modifiers = PiBuildModifier::Modifiers::Modifiers.new
100
+ modifiers.with_json_configuration(tmp_cfg_file).with_config_modifier(test_modifier).modify_configs
101
+ end
102
+
103
+ end
104
+
105
+ context 'SystemConfig' do
106
+ it 'handle the name config' do
107
+ configuration = PiBuildModifier::Configs::SystemConfig.new
108
+ configuration.map(JSON.parse '{"system": {"name" : "test"}}')
109
+ expect(configuration.name).to eq('test')
110
+ end
111
+ it 'handle the name config' do
112
+ configuration = PiBuildModifier::Configs::SystemConfig.new
113
+ configuration.map(JSON.parse '{"system": {"username" : "user", "password" : "pwd"}}')
114
+ expect(configuration.username).to eq('user')
115
+ expect(configuration.password).to eq('pwd')
116
+ end
117
+ it 'returns the config change w/o username and password' do
118
+ configuration = PiBuildModifier::Configs::SystemConfig.new
119
+ configuration.map(JSON.parse '{"system": {"name" : "test"}}')
120
+ expect(configuration.config_line).to eq("IMG_NAME='test'")
121
+ end
122
+ it 'returns the config change w/ username and password' do
123
+ configuration = PiBuildModifier::Configs::SystemConfig.new
124
+ configuration.map(JSON.parse '{"system": {"username" : "user", "password" : "pwd"}}')
125
+ expect(configuration.config_line).to eq("IMG_NAME='custompi'\nFIRST_USER_NAME=user\nFIRST_USER_PASS=pwd")
126
+ end
127
+ it 'ensures that a name for the image is provided' do
128
+ configuration = PiBuildModifier::Configs::SystemConfig.new
129
+ configuration.map(JSON.parse '{"system": {"name" : ""}}')
130
+ expect {configuration.verify}.to raise_error("No valid image name string provided in the config file! E.g., it might be an empty string or not a string at all. Configured image name: ''.")
131
+ end
132
+ it 'ensures that the name for the image ca' do
133
+ configuration = PiBuildModifier::Configs::SystemConfig.new
134
+ configuration.map(JSON.parse '{"system": {"name" : 1234}}')
135
+ expect {configuration.verify}.to raise_error("No valid image name string provided in the config file! E.g., it might be an empty string or not a string at all. Configured image name: '1234'.")
136
+ end
137
+ end
138
+
139
+ context 'SshConfig' do
140
+ it 'handle the ssh config' do
141
+ configuration = PiBuildModifier::Configs::SshConfig.new
142
+ configuration.map(JSON.parse '{"ssh": {"enable" : true}}')
143
+ expect(configuration.enable).to be_truthy
144
+ end
145
+ it 'returns the config change' do
146
+ configuration = PiBuildModifier::Configs::SshConfig.new
147
+ configuration.map(JSON.parse '{"ssh": {"enable" : true}}')
148
+ expect(configuration.config_line).to eq("ENABLE_SSH=1")
149
+ end
150
+ end
151
+
152
+ context 'Boot' do
153
+
154
+ let(:empty_json_config) {JSON.parse '{}'}
155
+ let(:json_config) {JSON.parse '{ "cgroups": { "memory" : true }}'}
156
+ let(:disabled_json_config) {JSON.parse '{ "cgroups": { "memory" : false }}'}
157
+ let(:workspace) {File.join(File.dirname(__FILE__), '/workspace_boot')}
158
+
159
+ before(:each) do
160
+ FileUtils.mkdir_p workspace
161
+ end
162
+
163
+ after(:each) do
164
+ FileUtils.rm_rf workspace
165
+ end
166
+
167
+ it 'should create a cmdline.txt file with all cgroups enabled when they are specified' do
168
+ modifier = PiBuildModifier::Modifiers::ERBConfigModifier.new(workspace)
169
+ config = PiBuildModifier::Configs::Boot.new
170
+ modifier.add(config)
171
+ modifier.map(json_config)
172
+ modifier.modify
173
+ expect(Pathname.new(File.join(workspace, config.relative_output_path))).to be_file
174
+ expect(File.read(File.join(workspace, config.relative_output_path))).to match(/ cgroup_memory=1 /)
175
+ end
176
+
177
+ it 'should create a cmdline.txt file without cgroups enabled when they are disabled' do
178
+ modifier = PiBuildModifier::Modifiers::ERBConfigModifier.new(workspace)
179
+ config = PiBuildModifier::Configs::Boot.new
180
+ modifier.add(config)
181
+ modifier.map(disabled_json_config)
182
+ modifier.modify
183
+ expect(Pathname.new(File.join(workspace, config.relative_output_path))).to be_file
184
+ expect(File.read(File.join(workspace, config.relative_output_path))).to_not match(/ cgroup_memory=1 /)
185
+ end
186
+
187
+ it 'should create a cmdline.txt file without cgroups enabled when they are not specified' do
188
+ modifier = PiBuildModifier::Modifiers::ERBConfigModifier.new(workspace)
189
+ config = PiBuildModifier::Configs::Boot.new
190
+ modifier.add(config)
191
+ modifier.map(empty_json_config)
192
+ modifier.modify
193
+ expect(Pathname.new(File.join(workspace, config.relative_output_path))).to be_file
194
+ expect(File.read(File.join(workspace, config.relative_output_path))).to_not match(/ cgroup_memory=1 /)
195
+ end
196
+ end
197
+
198
+ context 'Type' do
199
+
200
+ let(:json_config) {JSON.parse '{ "system": { "type" : "lite" }}'}
201
+ let(:invalid_json_config) {JSON.parse '{ "system": { "type" : "image" }}'}
202
+ let(:workspace) {File.join(File.dirname(__FILE__), '/workspace_boot')}
203
+
204
+ before(:each) do
205
+ FileUtils.mkdir_p workspace
206
+ end
207
+
208
+ after(:each) do
209
+ FileUtils.rm_rf workspace
210
+ end
211
+
212
+ it 'ensures that a valid type is specified' do
213
+ modifier = PiBuildModifier::Modifiers::ConfigModifier.new(workspace)
214
+ config = PiBuildModifier::Configs::TypeConfig.new
215
+ modifier.add(config)
216
+ modifier.map(invalid_json_config)
217
+ modifier.modify
218
+
219
+ expect {modifier.verify}.to raise_error
220
+ end
221
+
222
+ it 'should create skip files' do
223
+ modifier = PiBuildModifier::Modifiers::ConfigModifier.new(workspace)
224
+ config = PiBuildModifier::Configs::TypeConfig.new
225
+ modifier.add(config)
226
+ modifier.map(json_config)
227
+ modifier.modify
228
+
229
+ expect(Pathname.new(File.join(workspace, '/stage3/SKIP'))).to be_file
230
+ expect(Pathname.new(File.join(workspace, '/stage4/SKIP'))).to be_file
231
+ expect(Pathname.new(File.join(workspace, '/stage5/SKIP'))).to be_file
232
+ end
233
+
234
+ end
235
+
236
+ context 'WiFi' do
237
+ let(:empty_json_config) {JSON.parse '{}'}
238
+ let(:json_config) {JSON.parse '{"wifi" : { "wpa_country" : "DE_test", "networks": [{ "ssid": "test_wpsk", "wpsk": "test_wpsk" }]}}'}
239
+ let(:workspace) {File.dirname(__FILE__) + '/workspace'}
240
+
241
+ before do
242
+ FileUtils.mkdir_p workspace
243
+ end
244
+
245
+ after do
246
+ FileUtils.rm_rf workspace
247
+ end
248
+
249
+ it 'should create a default wpa_supplicant.config when no change is specified in the json configuration file' do
250
+ modifier = PiBuildModifier::Modifiers::ERBConfigModifier.new(workspace)
251
+ config = PiBuildModifier::Configs::Wifi.new
252
+ modifier.add(config)
253
+ modifier.map(json_config)
254
+ modifier.modify
255
+ expect(Pathname.new(File.join(workspace, config.relative_output_path))).to be_file
256
+ end
257
+
258
+ it 'should create a wpa_supplicant.config with all changes that are specified in the json configuration file' do
259
+ modifier = PiBuildModifier::Modifiers::ERBConfigModifier.new(workspace)
260
+ config = PiBuildModifier::Configs::Wifi.new
261
+ modifier.add(config)
262
+ modifier.map(json_config)
263
+ modifier.modify
264
+
265
+ wpa_supplicant_file = File.read(File.join(workspace, config.relative_output_path))
266
+ expect(wpa_supplicant_file).to match(/network/)
267
+ end
268
+ end
269
+
270
+ context 'Locales' do
271
+ let(:empty_json_config) {JSON.parse '{}'}
272
+ let(:json_config) {JSON.parse '{ "locale": { "gen" : ["aaa", "bbb"], "sys" : "ccc" }}'}
273
+ let(:workspace) {File.join(File.dirname(__FILE__), '/workspace')}
274
+
275
+ before(:each) do
276
+ FileUtils.mkdir_p workspace
277
+ end
278
+
279
+ after(:each) do
280
+ FileUtils.rm_rf workspace
281
+ end
282
+
283
+ it 'should create a debconf file' do
284
+ modifier = PiBuildModifier::Modifiers::ERBConfigModifier.new(workspace)
285
+ config = PiBuildModifier::Configs::Locale.new
286
+ modifier.add(config)
287
+ modifier.map(json_config)
288
+ modifier.modify
289
+
290
+ expect(Pathname.new(File.join(workspace, config.relative_output_path))).to be_file
291
+ expect(File.read(File.join(workspace, config.relative_output_path))).to match(/aaa, bbb/)
292
+ expect(File.read(File.join(workspace, config.relative_output_path))).to match(/ccc/)
293
+ end
294
+
295
+ it 'should create a default debconf file' do
296
+ modifier = PiBuildModifier::Modifiers::ERBConfigModifier.new(workspace)
297
+ config = PiBuildModifier::Configs::Locale.new
298
+ modifier.add(config)
299
+ modifier.map(empty_json_config)
300
+ modifier.modify
301
+
302
+ expect(Pathname.new(File.join(workspace, config.relative_output_path))).to be_file
303
+ end
304
+ end
305
+
306
+ end