【发布时间】:2016-06-17 17:10:32
【问题描述】:
我在单元测试一些有风的 json.erb 逻辑时遇到了一些麻烦。以下 ERB 模板构建所需的 JSON 文件:
图1:
"api": {
<% unless @GLANCE_IMAGE_API_WORKERS_NUM.nil? %>
"workers": <%= @GLANCE_IMAGE_API_WORKERS_NUM %>
<% if @GLANCE_BACKEND == 'ceph' %>
<% unless @GLANCE_IMAGE_API_WORKERS_NUM.to_s.empty? %>,<% end %>
"default_store": "rbd"
<% end %>
<% if !@IMAGE_CACHE_MAX_SIZE.to_s.empty? %>
<% unless @GLANCE_IMAGE_API_WORKERS_NUM.to_s.empty? && @GLANCE_BACKEND != 'ceph' %>,<% end %>
"cache" : {
"image_cache_max_size": <%= @IMAGE_CACHE_MAX_SIZE %>
}
<% end %>
为了构建 JSON,它会在需要时添加尾随逗号。例如,如果glance_backend 设置为'ceph',则glance.image.api_workers 之后需要一个逗号以允许以下条目并使JSON 有效- 示例:
图 2.
"api": {
"workers": 8,
"cache": {
"image_cache_max_size": 10000000000
}
},
我正在尝试测试图 1 中逻辑的所有潜在场景。我相信它们是这样的(伪代码):
如果
image_cache_max_size != empty && glance_backend == 'ceph' && glance_image_api_workers == "not_empty",在 default_store 条目后添加逗号以允许下一项;如果
image_cache_max_size != empty && glance_backend == 'ceph' && glance_image_api_workers == "",则没有尾随逗号;如果
image_cache_max_size != empty && glance_backend == 'not_ceph' && glance_image_api_workers == "not_empty",则没有尾随逗号;如果
image_cache_max_size != empty && glance_backend == 'ceph' && glance_image_api_workers == "",则没有尾随逗号。
到目前为止,我的 rspec 逻辑如下所示 - Fig3:
context 'comma appended to "glance.api.workers" in correct circumstances' do
before do
data_bag['IMAGE_CACHE_MAX_SIZE'] = 10000
data_bag['GLUSTER_BACKEND'] = 'ceph'
data_bag['GLUSTER_IMAGE_API_WORKERS'] = 'not_empty'
end
it 'sets default_store attributes' do
expect(override_attr['openstack']['image']['api']['workers'])
.to include(",")
end
before do
data_bag['IMAGE_CACHE_MAX_SIZE'] = 10000
data_bag['GLUSTER_BACKEND'] = 'local'
data_bag['GLUSTER_IMAGE_API_WORKERS'] = ''
end
it 'sets default_store attributes' do
expect(override_attr['openstack']['image']['api']['workers'])
.to_not include(",")
end
before do
data_bag['IMAGE_CACHE_MAX_SIZE'] = 10000
data_bag['GLUSTER_BACKEND'] = 'local'
data_bag['GLUSTER_IMAGE_API_WORKERS'] = 'not_empty'
end
it 'sets default_store attributes' do
expect(override_attr['openstack']['image']['api']['workers'])
.to_not include(",")
end
end
这可能是完全错误的方法。期待听到一些建议。谢谢。
【问题讨论】: