diff options
-rw-r--r-- | hscript/network.cc | 33 | ||||
-rw-r--r-- | hscript/network.hh | 22 | ||||
-rw-r--r-- | hscript/script.cc | 3 | ||||
-rw-r--r-- | hscript/script_i.hh | 13 | ||||
-rw-r--r-- | tests/fixtures/0226-netconfigtype-netifrc.installfile | 20 | ||||
-rw-r--r-- | tests/fixtures/0227-netconfigtype-eni.installfile | 20 | ||||
-rw-r--r-- | tests/fixtures/0228-netconfigtype-invalid.installfile | 20 | ||||
-rw-r--r-- | tests/spec/validator_spec.rb | 19 |
8 files changed, 150 insertions, 0 deletions
diff --git a/hscript/network.cc b/hscript/network.cc index 1750a60..a7b2bee 100644 --- a/hscript/network.cc +++ b/hscript/network.cc @@ -47,6 +47,39 @@ bool Network::execute() const { } +Key *NetConfigType::parseFromData(const std::string &data, int lineno, + int *errors, int *, const Script *script) { + std::string type = data; + ConfigSystem system; + + std::transform(type.cbegin(), type.cend(), type.begin(), ::tolower); + + if(type == "netifrc") { + system = Netifrc; + } else if(type == "eni") { + system = ENI; + } else { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netconfigtype: invalid or missing config type", + "one of 'netifrc', 'eni' required"); + return nullptr; + } + + return new NetConfigType(script, lineno, system); +} + +bool NetConfigType::validate() const { + /* Validation takes place during parsing. */ + return true; +} + +bool NetConfigType::execute() const { + /* This key doesn't perform any action by itself. */ + return true; +} + + Key *NetAddress::parseFromData(const std::string &data, int lineno, int *errors, int *, const Script *script) { long elements = std::count(data.cbegin(), data.cend(), ' ') + 1; diff --git a/hscript/network.hh b/hscript/network.hh index ec8d2cf..415a337 100644 --- a/hscript/network.hh +++ b/hscript/network.hh @@ -29,6 +29,28 @@ public: bool execute() const override; }; +class NetConfigType : public Key { +public: + enum ConfigSystem { + Netifrc, + ENI + }; +private: + ConfigSystem _sys; + + NetConfigType(const Script *_sc, int _line, const ConfigSystem _s) : + Key(_sc, _line), _sys(_s) {} +public: + static Key *parseFromData(const std::string &, int, int*, int*, + const Script *); + + /*! Retrieve the desired network configuration system. */ + ConfigSystem type() const { return this->_sys; } + + bool validate() const override; + bool execute() const override; +}; + class NetAddress : public Key { public: /*! Determines the type of address an interface will obtain. */ diff --git a/hscript/script.cc b/hscript/script.cc index 7db3ed0..fd9fc5a 100644 --- a/hscript/script.cc +++ b/hscript/script.cc @@ -49,6 +49,7 @@ const std::map<std::string, key_parse_fn> valid_keys = { {"repository", &Repository::parseFromData}, {"signingkey", &SigningKey::parseFromData}, + {"netconfigtype", &NetConfigType::parseFromData}, {"netaddress", &NetAddress::parseFromData}, {"nameserver", &Nameserver::parseFromData}, {"netssid", &NetSSID::parseFromData}, @@ -78,6 +79,8 @@ bool Script::ScriptPrivate::store_key(const std::string &key_name, Key *obj, const ScriptOptions &opts) { if(key_name == "network") { return store_network(obj, lineno, errors, warnings, opts); + } else if(key_name == "netconfigtype") { + return store_netconfig(obj, lineno, errors, warnings, opts); } else if(key_name == "netaddress") { std::unique_ptr<NetAddress> addr(dynamic_cast<NetAddress *>(obj)); this->addresses.push_back(std::move(addr)); diff --git a/hscript/script_i.hh b/hscript/script_i.hh index 0ed362e..a94a0d7 100644 --- a/hscript/script_i.hh +++ b/hscript/script_i.hh @@ -43,6 +43,8 @@ struct Script::ScriptPrivate { /*! Determines whether or not to enable networking. */ std::unique_ptr<Network> network; + /*! Determines the network configuration system to use. */ + std::unique_ptr<NetConfigType> netconfig; /*! The target system's hostname. */ std::unique_ptr<Hostname> hostname; /*! The packages to install to the target system. */ @@ -126,6 +128,17 @@ struct Script::ScriptPrivate { return true; } + bool store_netconfig(Key *obj, int line, int *errors, int *, ScriptOptions) { + if(netconfig) { + DUPLICATE_ERROR(netconfig, "netconfigtype", + netconfig->type()); + return false; + } + std::unique_ptr<NetConfigType> nc(dynamic_cast<NetConfigType *>(obj)); + netconfig = std::move(nc); + return true; + } + bool store_hostname(Key* obj, int line, int *errors, int *, ScriptOptions) { if(hostname) { DUPLICATE_ERROR(hostname, "hostname", hostname->value()) diff --git a/tests/fixtures/0226-netconfigtype-netifrc.installfile b/tests/fixtures/0226-netconfigtype-netifrc.installfile new file mode 100644 index 0000000..3b7cace --- /dev/null +++ b/tests/fixtures/0226-netconfigtype-netifrc.installfile @@ -0,0 +1,20 @@ +hostname horizon-netifrc-test +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +network true +netconfigtype netifrc +netaddress eth0 dhcp +netaddress eth0 slaac +nameserver 9.9.9.10 +diskid /dev/sda QEMU_HARDDISK +disklabel /dev/sda gpt +partition /dev/sda 1 fill +fs /dev/sda1 ext4 +mount /dev/sda1 / +repository https://distfiles.adelielinux.org/adelie/current/system +repository https://distfiles.adelielinux.org/adelie/current/user +signingkey /etc/apk/keys/packages@adelielinux.org.pub +pkginstall adelie-base-posix dash-binsh easy-kernel-power8 easy-kernel-power8-modules grub-ieee1275 netifrc openrc s6-linux-init +keymap us +language en_GB.UTF-8 +timezone America/Chicago +firmware false diff --git a/tests/fixtures/0227-netconfigtype-eni.installfile b/tests/fixtures/0227-netconfigtype-eni.installfile new file mode 100644 index 0000000..2eb6712 --- /dev/null +++ b/tests/fixtures/0227-netconfigtype-eni.installfile @@ -0,0 +1,20 @@ +hostname horizon-netifrc-test +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +network true +netconfigtype eni +netaddress eth0 dhcp +netaddress eth0 slaac +nameserver 9.9.9.10 +diskid /dev/sda QEMU_HARDDISK +disklabel /dev/sda gpt +partition /dev/sda 1 fill +fs /dev/sda1 ext4 +mount /dev/sda1 / +repository https://distfiles.adelielinux.org/adelie/current/system +repository https://distfiles.adelielinux.org/adelie/current/user +signingkey /etc/apk/keys/packages@adelielinux.org.pub +pkginstall adelie-base-posix dash-binsh easy-kernel easy-kernel-modules grub-ieee1275 openrc s6-linux-init +keymap us +language en_GB.UTF-8 +timezone America/Chicago +firmware false diff --git a/tests/fixtures/0228-netconfigtype-invalid.installfile b/tests/fixtures/0228-netconfigtype-invalid.installfile new file mode 100644 index 0000000..fcb29df --- /dev/null +++ b/tests/fixtures/0228-netconfigtype-invalid.installfile @@ -0,0 +1,20 @@ +hostname horizon-netifrc-test +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +network true +netconfigtype netbsd-rcconf +netaddress eth0 dhcp +netaddress eth0 slaac +nameserver 9.9.9.10 +diskid /dev/sda QEMU_HARDDISK +disklabel /dev/sda gpt +partition /dev/sda 1 fill +fs /dev/sda1 ext4 +mount /dev/sda1 / +repository https://distfiles.adelielinux.org/adelie/current/system +repository https://distfiles.adelielinux.org/adelie/current/user +signingkey /etc/apk/keys/packages@adelielinux.org.pub +pkginstall adelie-base-posix dash-binsh easy-kernel-power8 easy-kernel-power8-modules grub-ieee1275 netifrc openrc s6-linux-init +keymap us +language en_GB.UTF-8 +timezone America/Chicago +firmware false diff --git a/tests/spec/validator_spec.rb b/tests/spec/validator_spec.rb index 971b91d..963eecf 100644 --- a/tests/spec/validator_spec.rb +++ b/tests/spec/validator_spec.rb @@ -221,6 +221,25 @@ RSpec.describe 'HorizonScript validation', :type => :aruba do expect(last_command_started).to have_output(/error: .*arch.*expected/) end end + context "for 'netconfigtype' key" do + it "succeeds with netifrc specified" do + use_fixture '0226-netconfigtype-netifrc.installfile' + run_validate + expect(last_command_started).to have_output(PARSER_SUCCESS) + expect(last_command_started).to have_output(VALIDATOR_SUCCESS) + end + it "succeeds with /etc/network/interfaces specified" do + use_fixture '0227-netconfigtype-eni.installfile' + run_validate + expect(last_command_started).to have_output(PARSER_SUCCESS) + expect(last_command_started).to have_output(VALIDATOR_SUCCESS) + end + it "fails with an invalid value" do + use_fixture '0228-netconfigtype-invalid.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*netconfigtype.*valid/) + end + end context "for 'nameserver' key" do it "succeeds with IPv4 and IPv6 addresses" do use_fixture '0183-nameserver-basic.installfile' |