Fixing logic for cpu_model parameter
Backporting from pike to ocata and newton
Adding docs and tests

Prod-Related: PROD-19272

Change-Id: I8948f48e4ef57ece3ccdee76a5152536cced5a3c
diff --git a/README.rst b/README.rst
index 3abd1fb..e0ba486 100644
--- a/README.rst
+++ b/README.rst
@@ -633,6 +633,18 @@
     compute:
       cpu_mode: host-model
 
+Nova compute cpu model
+----------------------
+
+.. code-block:: yaml
+
+  nova:
+    compute:
+      cpu_mode: custom
+      libvirt:
+        cpu_model: IvyBridge
+
+
 Nova compute workarounds
 ------------------------
 
diff --git a/nova/files/newton/nova-compute.conf.Debian b/nova/files/newton/nova-compute.conf.Debian
index 0d63bae..999da48 100644
--- a/nova/files/newton/nova-compute.conf.Debian
+++ b/nova/files/newton/nova-compute.conf.Debian
@@ -183,7 +183,7 @@
 {%- endif %}
 
 [libvirt]
-cpu_mode = {{ compute.get('cpu_mode', 'host-passthrough') }}
+cpu_mode = {{ compute.cpu_mode }}
 {%- if compute.libvirt.virt_type is defined %}
 virt_type = {{ compute.libvirt.virt_type }}
 {%-  else %}
@@ -201,6 +201,9 @@
 libvirt_inject_key=True
 inject_key=False
 vif_driver=nova.virt.libvirt.vif.LibvirtGenericVIFDriver
+{%- if compute.get('libvirt', {}).cpu_model is defined and compute.cpu_mode == 'custom' %}
+cpu_model = {{ compute.libvirt.cpu_model }}
+{%- endif %}
 
 {%- if compute.get('ceph', {}).ephemeral is defined %}
 images_type=rbd
diff --git a/nova/files/ocata/nova-compute.conf.Debian b/nova/files/ocata/nova-compute.conf.Debian
index d7854af..0e922d5 100644
--- a/nova/files/ocata/nova-compute.conf.Debian
+++ b/nova/files/ocata/nova-compute.conf.Debian
@@ -5939,7 +5939,7 @@
 #
 # From nova.conf
 #
-cpu_mode = {{ compute.get('cpu_mode', 'host-passthrough') }}
+cpu_mode = {{ compute.cpu_mode }}
 {%- if compute.libvirt.virt_type is defined %}
 virt_type = {{ compute.libvirt.virt_type }}
 {%-  else %}
@@ -6456,7 +6456,11 @@
 #   This would result in an error and the instance won't be launched.
 # * ``virt_type``: Only the virtualization types ``kvm`` and ``qemu`` use this.
 #  (string value)
+{%- if compute.get('libvirt', {}).cpu_model is defined and compute.cpu_mode == 'custom' %}
+cpu_model = {{ compute.libvirt.cpu_model }}
+{%- else %}
 #cpu_model=<None>
+{%- endif %}
 
 # Location where libvirt driver will store snapshots before uploading them to
 # image service (string value)
diff --git a/nova/files/pike/nova-compute.conf.Debian b/nova/files/pike/nova-compute.conf.Debian
index 7a1e90a..402ae02 100644
--- a/nova/files/pike/nova-compute.conf.Debian
+++ b/nova/files/pike/nova-compute.conf.Debian
@@ -5939,7 +5939,7 @@
 #
 # From nova.conf
 #
-cpu_mode = {{ compute.get('cpu_mode', 'host-passthrough') }}
+cpu_mode = {{ compute.cpu_mode }}
 {%- if compute.libvirt.virt_type is defined %}
 virt_type = {{ compute.libvirt.virt_type }}
 {%-  else %}
@@ -6456,7 +6456,7 @@
 #   This would result in an error and the instance won't be launched.
 # * ``virt_type``: Only the virtualization types ``kvm`` and ``qemu`` use this.
 #  (string value)
-{%- if compute.get('libvirt', {}).cpu_model is defined %}
+{%- if compute.get('libvirt', {}).cpu_model is defined and compute.cpu_mode == 'custom' %}
 cpu_model = {{ compute.libvirt.cpu_model }}
 {%- else %}
 #cpu_model=<None>
diff --git a/nova/map.jinja b/nova/map.jinja
index 03b509f..ee4b679 100644
--- a/nova/map.jinja
+++ b/nova/map.jinja
@@ -129,6 +129,7 @@
   notification: false
   availability_zone:
   aggregates: []
+  cpu_mode: host-passthrough
   identity:
     region: RegionOne
   network: {{ compute_network }}
diff --git a/tests/pillar/compute_single.sls b/tests/pillar/compute_single.sls
index abaa7ad..08250ba 100644
--- a/tests/pillar/compute_single.sls
+++ b/tests/pillar/compute_single.sls
@@ -1,12 +1,15 @@
 nova:
   compute:
-    version: liberty
+    version: newton
     enabled: true
     virtualization: kvm
     heal_instance_info_cache_interval: 60
     vncproxy_url: openstack:6080
     vnc_keymap: en-gb
     resume_guests_state_on_host_boot: False
+    cpu_mode: custom
+    libvirt:
+      cpu_model: IvyBridge
     bind:
       vnc_address: 127.0.0.1
       vnc_port: 6080