跳转至

09 占位符格式化

特性描述

使用运行时数据替换占位符内容

特性约束

子场景

规则占位符测试。占位符在规则执行过程中会替换为设备的特定属性,本用例中设计了22种占位符使用场景,测试占位符展开内容的有效性和长、短占位符展开后的一致性。

备注

准备工作

创建配置文件:/etc/devmaster/config.toml

rules_d = ["/tmp/1/rules.d"]

创建规则文件:/tmp/1/rules.d/00-test.rules

KERNEL=="sda1", GOTO="block"
KERNEL=="lo", GOTO="netif"
GOTO="end"

LABEL="block"

PROGRAM=="/bin/bash -c 'echo block devnode:     $devnode     %N >> /tmp/test_formatter_block.txt'"
PROGRAM=="/bin/bash -c 'echo block tempnode:    $tempnode    %N >> /tmp/test_formatter_block.txt'"
PROGRAM=="/bin/bash -c 'echo block attr{size}:  $attr{size}  %s{size} >> /tmp/test_formatter_block.txt'"
PROGRAM=="/bin/bash -c 'echo block sysfs{size}: $sysfs{size} %s{size} >> /tmp/test_formatter_block.txt'"
PROGRAM=="/bin/bash -c 'echo block major:      $major      %M >> /tmp/test_formatter_block.txt'"
PROGRAM=="/bin/bash -c 'echo block minor:      $minor      %m >> /tmp/test_formatter_block.txt'"
PROGRAM=="/bin/bash -c 'echo block name:   $name   %D >> /tmp/test_formatter_block.txt'"

ENV{hello}="world", \
PROGRAM=="/bin/bash -c 'echo env{hello}: $env{hello} %E{hello} >> /tmp/test_formatter_block.txt'"
PROGRAM=="/bin/bash -c 'echo kernel:     $kernel     %k >> /tmp/test_formatter_block.txt'"
PROGRAM=="/bin/bash -c 'echo number:     $number     %n >> /tmp/test_formatter_block.txt'"
PROGRAM=="/bin/bash -c 'echo devpath:    $devpath    %p >> /tmp/test_formatter_block.txt'"
PROGRAM=="/bin/bash -c 'echo id:         $id         %b >> /tmp/test_formatter_block.txt'"

PROGRAM=="/bin/echo one two three"
PROGRAM=="/bin/bash -c 'echo result: $result{1}  %c{1} >> /tmp/test_formatter_block.txt'"
PROGRAM=="/bin/echo one two three"
PROGRAM=="/bin/bash -c 'echo result: $result{1+} %c{1+} >> /tmp/test_formatter_block.txt'"

PROGRAM=="/bin/bash -c 'echo parent: $parent %P >> /tmp/test_formatter_block.txt'"

SYMLINK+="formatter_link", \
PROGRAM=="/bin/bash -c 'echo links:  $links  %L >> /tmp/test_formatter_block.txt'"

PROGRAM=="/bin/bash -c 'echo root: $root %r >> /tmp/test_formatter_block.txt'"
PROGRAM=="/bin/bash -c 'echo sys:  $sys  %S >> /tmp/test_formatter_block.txt'"

PROGRAM=="/bin/bash -c 'echo $$ %% >> /tmp/test_formatter_block.txt'"

GOTO="end"

LABEL="netif"

NAME="jonny", PROGRAM=="/bin/bash -c 'echo netif name:  $name  %D >> /tmp/test_formatter_net.txt'"
PROGRAM=="/bin/bash -c 'echo netif attr{[net/lo]ifindex}:  $attr{[net/lo]ifindex}  %s{[net/lo]ifindex} >> /tmp/test_formatter_net.txt'"
PROGRAM=="/bin/bash -c 'echo netif attr{ifindex}:  $attr{ifindex}  %s{ifindex} >> /tmp/test_formatter_net.txt'"

GOTO="end"

LABEL="end"

启动devmaster:

# devmaster &> log.txt &

测试步骤

触发/dev/sda1的uevent事件,有结果1:

# devctl trigger /dev/sda1

触发虚拟网卡lo的uevent事件,有结果2:

# devctl trigger /sys/class/net/lo

结果验证

结果1,生成/tmp/test_formatter_block.txt临时文件,除了最后一行外,各行后两列内容一致:

# cat /tmp/test_formatter_block.txt
block devnode: /dev/sda1 /dev/sda1
block tempnode: /dev/sda1 /dev/sda1
block attr{size}: 2097152 2097152
block sysfs{size}: 2097152 2097152
block major: 8 8
block minor: 1 1
block name: sda1 sda1
env{hello}: world world
kernel: sda1 sda1
number: 1 1
devpath: /devices/pci0000:00/0000:00:10.0/host2/target2:0:0/2:0:0:0/block/sda/sda1 /devices/pci0000:00/0000:00:10.0/host2/target2:0:0/2:0:0:0/block/sda/sda1
id:
result: two two
result: two three two three
parent: sda sda
links: formatter_link formatter_link
root: /dev /dev
sys: /sys /sys
$ %

结果2,生成/tmp/test_formatter_net.txt临时文件,各行后两列内容一致::

# cat /tmp/test_formatter_net.txt
netif name: jonny jonny
netif attr{[net/lo]ifindex}: 1 1
netif attr{ifindex}: 1 1

测试结束

删除devmaster残留数据

# rm -rf /run/devmaster/*

清理临时文件

# rm -rf /tmp/test_formatter_*

测试场景约束

系统中存在sda1分区块设备


最后更新: November 4, 2023
创建日期: August 23, 2023