1# Copyright 2015 Docker, Inc. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may 4# not use this file except in compliance with the License. You may obtain 5# a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations 13# under the License. 14 15import pytest 16 17from molecule import interpolation 18 19 20@pytest.fixture 21def _mock_env(): 22 return { 23 'FOO': 'foo', 24 'BAR': '', 25 'DEPENDENCY_NAME': 'galaxy', 26 'VERIFIER_NAME': 'testinfra', 27 'MOLECULE_SCENARIO_NAME': 'default', 28 } 29 30 31@pytest.fixture 32def _instance(_mock_env): 33 return interpolation.Interpolator(interpolation.TemplateWithDefaults, _mock_env) 34 35 36def test_escaped_interpolation(_instance): 37 assert '${foo}' == _instance.interpolate('$${foo}') 38 39 40def test_invalid_interpolation(_instance): 41 with pytest.raises(interpolation.InvalidInterpolation): 42 _instance.interpolate('${') 43 with pytest.raises(interpolation.InvalidInterpolation): 44 _instance.interpolate('$}') 45 with pytest.raises(interpolation.InvalidInterpolation): 46 _instance.interpolate('${}') 47 with pytest.raises(interpolation.InvalidInterpolation): 48 _instance.interpolate('${ }') 49 with pytest.raises(interpolation.InvalidInterpolation): 50 _instance.interpolate('${ foo}') 51 with pytest.raises(interpolation.InvalidInterpolation): 52 _instance.interpolate('${foo }') 53 with pytest.raises(interpolation.InvalidInterpolation): 54 _instance.interpolate('${foo!}') 55 56 57def test_interpolate_missing_no_default(_instance): 58 assert 'This var' == _instance.interpolate('This ${missing} var') 59 assert 'This var' == _instance.interpolate('This ${BAR} var') 60 61 62def test_interpolate_with_value(_instance): 63 assert 'This foo var' == _instance.interpolate('This $FOO var') 64 assert 'This foo var' == _instance.interpolate('This ${FOO} var') 65 66 67def test_interpolate_missing_with_default(_instance): 68 assert 'ok def' == _instance.interpolate('ok ${missing:-def}') 69 assert 'ok def' == _instance.interpolate('ok ${missing-def}') 70 assert 'ok /non:-alphanumeric' == _instance.interpolate( 71 'ok ${BAR:-/non:-alphanumeric}' 72 ) 73 74 75def test_interpolate_with_empty_and_default_value(_instance): 76 assert 'ok def' == _instance.interpolate('ok ${BAR:-def}') 77 assert 'ok ' == _instance.interpolate('ok ${BAR-def}') 78 79 80def test_interpolate_interpolates_MOLECULE_strings(_instance): 81 assert 'default' == _instance.interpolate('$MOLECULE_SCENARIO_NAME') 82 83 84def test_interpolate_does_not_interpolate_MOLECULE_strings(_instance): 85 assert 'foo $MOLECULE_SCENARIO_NAME' == _instance.interpolate( 86 'foo $MOLECULE_SCENARIO_NAME', keep_string='MOLECULE_' 87 ) 88 89 90def test_interpolate_with_molecule_yaml(_instance): 91 data = """ 92--- 93dependency: 94 name: $DEPENDENCY_NAME 95driver: 96 name: docker 97lint: 98 name: yamllint 99platforms: 100 - name: instance-1 101provisioner: 102 name: ansible 103scenario: 104 name: $MOLECULE_SCENARIO_NAME 105verifier: 106 name: ${VERIFIER_NAME} 107 options: 108 $FOO: bar 109""".strip() 110 111 x = """ 112--- 113dependency: 114 name: galaxy 115driver: 116 name: docker 117lint: 118 name: yamllint 119platforms: 120 - name: instance-1 121provisioner: 122 name: ansible 123scenario: 124 name: default 125verifier: 126 name: testinfra 127 options: 128 foo: bar 129""".strip() 130 131 assert x == _instance.interpolate(data) 132