Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand LXD driver functionality #1436

Merged
merged 14 commits into from
Sep 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions molecule/driver/lxd.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,23 @@ class LXD(base.Base):
platforms:
- name: instance
url: https://127.0.0.1:8443
cert_file: "/root/.config/lxc/client.crt"
key_file: "/root/.config/lxc/client.key"
cert_file: /root/.config/lxc/client.crt
key_file: /root/.config/lxc/client.key
trust_password: password
mode: pull|local
server: https://images.linuxcontainers.org
alias: ubuntu/xenial/amd64
source:
type: image
mode: pull|local
server: https://images.linuxcontainers.org
protocol: lxd|simplestreams
alias: ubuntu/xenial/amd64
architecture: x86_64|i686
devices:
kvm:
path: /dev/kvm
type: unix-char
profiles:
- default
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you have implemented this API for molecule.yml we need to validate the schema. Can you update the v2 schema similar to what we did with dockder and vagrant.

https://github.com/metacloud/molecule/blob/master/molecule/model/schema_v2.py#L596
https://github.com/metacloud/molecule/blob/master/molecule/model/schema_v2.py#L862

And we will need to update tests with working and failing tests:

https://github.com/metacloud/molecule/blob/master/test/unit/model/v2/test_platforms_section.py

force_stop: True|False

Provide the files Molecule will preserve upon each subcommand execution.

Expand Down
81 changes: 81 additions & 0 deletions molecule/model/schema_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,85 @@ def pre_validate_base_schema(env, keep_string):
},
}

platforms_lxd_schema = {
'platforms': {
'type': 'list',
'schema': {
'type': 'dict',
'schema': {
'name': {
'type': 'string',
},
'url': {
'type': 'string',
},
'cert_file': {
'type': 'string',
},
'key_file': {
'type': 'string',
},
'trust_password': {
'type': 'string',
},
'source': {
'type': 'dict',
'schema': {
'type': {
'type': 'string',
},
'mode': {
'type': 'string',
'allowed': [
'pull',
'local',
],
},
'server': {
'type': 'string',
},
'protocol': {
'type': 'string',
'allowed': [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the schema as specific as possible, these are the only values valid for the LXD api. There are now allowed values (source).

'lxd',
'simplestreams',
],
},
'alias': {
'type': 'string',
},
},
},
'architecture': {
'type': 'string',
'allowed': [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here (source)

'x86_64',
'i686',
],
},
'devices': {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a good look at the devices schema, that is the one I struggled with since I am not familiar with the markup. Might be not 100% correct

'type': 'dict',
'schema': {
'type': 'dict',
'keyschema': {
'type': 'string',
},
},
},
'profiles': {
'type': 'list',
'schema': {
'type': 'string'
}
},
'force_stop': {
'type': 'boolean',
},
}
}
},
}

dependency_command_nullable_schema = {
'dependency': {
'type': 'dict',
Expand Down Expand Up @@ -863,6 +942,8 @@ def validate(c):
elif c['driver']['name'] == 'vagrant':
util.merge_dicts(schema, driver_vagrant_provider_section_schema)
util.merge_dicts(schema, platforms_vagrant_schema)
elif c['driver']['name'] == 'lxd':
util.merge_dicts(schema, platforms_lxd_schema)
else:
util.merge_dicts(schema, platforms_base_schema)

Expand Down
16 changes: 10 additions & 6 deletions molecule/provisioner/ansible/playbooks/lxd/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
gather_facts: false
no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}"
tasks:
- name: Create default source variable
set_fact:
default_source:
type: image
server: https://images.linuxcontainers.org
alias: ubuntu/xenial/amd64

- name: Create molecule instance(s)
lxd_container:
url: "{{ item.url | default(omit) }}"
Expand All @@ -13,12 +20,9 @@
trust_password: "{{ item.trust_password | default(omit) }}"
name: "{{ item.name }}"
state: started
source:
type: image
mode: "{{ item.mode | default('pull') }}"
server: "{{ item.server | default('https://images.linuxcontainers.org') }}"
protocol: lxd
alias: "{{ item.alias | default('ubuntu/xenial/amd64') }}"
source: "{{ default_source | combine(item.source | default({})) }}"
architecture: "{{ item.architecture | default(omit) }}"
devices: "{{ item.devices | default(omit) }}"
profiles: "{{ item.profiles | default(omit) }}"
wait_for_ipv4_addresses: true
timeout: 600
Expand Down
16 changes: 10 additions & 6 deletions test/resources/playbooks/lxd/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
gather_facts: false
no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}"
tasks:
- name: Create default source variable
set_fact:
default_source:
type: image
server: https://images.linuxcontainers.org
alias: ubuntu/xenial/amd64

- name: Create molecule instance(s)
lxd_container:
url: "{{ item.url | default(omit) }}"
Expand All @@ -13,12 +20,9 @@
trust_password: "{{ item.trust_password | default(omit) }}"
name: "{{ item.name }}"
state: started
source:
type: image
mode: "{{ item.mode | default('pull') }}"
server: "{{ item.server | default('https://images.linuxcontainers.org') }}"
protocol: lxd
alias: "{{ item.alias | default('ubuntu/xenial/amd64') }}"
source: "{{ default_source | combine(item.source | default({})) }}"
architecture: "{{ item.architecture | default(omit) }}"
devices: "{{ item.devices | default(omit) }}"
profiles: "{{ item.profiles | default(omit) }}"
wait_for_ipv4_addresses: true
timeout: 600
Expand Down
111 changes: 111 additions & 0 deletions test/unit/model/v2/test_platforms_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,117 @@ def test_platforms_vagrant_has_errors(_config):
assert x == schema_v2.validate(_config)


@pytest.fixture
def _model_platforms_lxd_section_data():
return {
'driver': {
'name': 'lxd',
},
'platforms': [{
'name':
'instance',
'url':
'https://127.0.0.1:8443',
'cert_file':
'/root/.config/lxc/client.crt',
'key_file':
'/root/.config/lxc/client.key',
'trust_password':
'password',
'source': {
'type':
'image',
'mode':
'pull',
'protocol':
'lxd',
'server':
'https://images.linuxcontainers.org',
'alias':
'ubuntu/xenial/amd64',
},
'architecture':
'x86_64',
'devices': {
'kvm': {
'path': '/dev/kvm',
'type': 'unix-char',
}
},
'profiles': [
'default',
],
'force_stop':
True,
}]
}


@pytest.mark.parametrize(
'_config', ['_model_platforms_lxd_section_data'], indirect=True)
def test_platforms_lxd(_config):
assert {} == schema_v2.validate(_config)


@pytest.fixture
def _model_platforms_lxd_errors_section_data():
return {
'driver': {
'name': 'lxd',
},
'platforms': [{
'name': int(),
'url': int(),
'cert_file': int(),
'key_file': int(),
'trust_password': int(),
'source': {
'type': int(),
'mode': int(),
'server': int(),
'protocol': 'not-lxd-or-simplestreams',
'alias': int(),
},
'architecture': 'not-x86_64-or-i686',
'devices': int(),
'profiles': [
int(),
],
'force_stop': int()
}]
}


@pytest.mark.parametrize(
'_config', ['_model_platforms_docker_errors_section_data'], indirect=True)
def test_platforms_lxd_has_errors(_config):
x = {
'platforms': [{
0: [{
'name': ['must be of string type'],
'url': ['must be of string type'],
'cert_file': ['must be of string type'],
'key_file': ['must be of string type'],
'trust_password': ['must be of string type'],
'source': {
'mode': ['must be of string type'],
'server': ['must be of string type'],
'protocol': ['must be lxd or simplestreams'],
'alias': ['must be of string type'],
},
'architecture': ['must be x86_64 or i686'],
'devices': ['must be of dict type'],
'profiles': [{
0: ['must be of string type']
}],
'force_stop': ['must be of boolean type'],
}]
}]
}

assert x == schema_v2.validate(_config)


def test_platforms_driver_name_required(_config):
del _config['platforms'][0]['name']
x = {'platforms': [{0: [{'name': ['required field']}]}]}
Expand Down