filter: add new time convert filter (#347)

* filter: add new time convert filters

The plugin include:
- to_seconds
- to_minutes
- to_hours

* refactor and extend

* more UX improvements

* Apply suggestions from code review

Co-authored-by: Felix Fontein <felix@fontein.de>

* even more UX improvments

* fix indentation

* fix for py3

* enable aix in ci

* simplify

* add to_months, use 360d as year.

* rearrange tests

* year back to 365 days

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
René Moser 2020-05-18 09:54:23 +02:00 committed by GitHub
parent cd5e3e2d85
commit 945296f314
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 233 additions and 0 deletions

View file

@ -0,0 +1,2 @@
shippable/posix/group2
skip/python2.6 # filters are controller only, and we no longer support Python 2.6 on the controller

View file

@ -0,0 +1,82 @@
---
- name: test to_milliseconds filter
assert:
that:
- "('1000ms' | community.general.to_milliseconds) == 1000"
- "('1s' | community.general.to_milliseconds) == 1000"
- "('1m' | community.general.to_milliseconds) == 60000"
- name: test to_seconds filter
assert:
that:
- "('1000msecs' | community.general.to_seconds) == 1"
- "('1ms' | community.general.to_seconds) == 0.001"
- "('12m' | community.general.to_seconds) == 720"
- "('300minutes' | community.general.to_seconds) == 18000"
- "('3h 12m' | community.general.to_seconds) == 11520"
- "('2days 3hours 12mins 15secs' | community.general.to_seconds) == 184335"
- "('2d -2d -12s' | community.general.to_seconds) == -12"
- name: test to_minutes filter
assert:
that:
- "('30s' | community.general.to_minutes) == 0.5"
- "('12m' | community.general.to_minutes) == 12"
- "('3h 72m' | community.general.to_minutes) == 252"
- "('300s' | community.general.to_minutes) == 5"
- name: test to_hours filter
assert:
that:
- "('30m' | community.general.to_hours) == 0.5"
- "('3h 119m 61s' | community.general.to_hours) > 5"
- name: test to_days filter
assert:
that:
- "('1year' | community.general.to_days) == 365"
- "('1week' | community.general.to_days) == 7"
- "('2weeks' | community.general.to_days) == 14"
- "('1mo' | community.general.to_days) == 30"
- name: test to_weeks filter
assert:
that:
- "('1y' | community.general.to_weeks | int) == 52"
- "('7d' | community.general.to_weeks) == 1"
- name: test to_months filter
assert:
that:
- "('30d' | community.general.to_months) == 1"
- "('1year' | community.general.to_months | int) == 12"
- name: test to_years filter
assert:
that:
- "('365d' | community.general.to_years | int) == 1"
- "('12mo' | community.general.to_years | round(0, 'ceil')) == 1"
- name: test fail unknown unit
debug:
msg: "{{ '1s' | community.general.to_time_unit('lightyears') }}"
ignore_errors: yes
register: res
- name: verify test fail unknown unit
assert:
that:
- res is failed
- "'to_time_unit() can not convert to the following unit: lightyears' in res.msg"
- name: test fail unknown string
debug:
msg: "{{ '1 s' | community.general.to_time_unit('s') }}"
ignore_errors: yes
register: res
- name: test fail unknown string
assert:
that:
- res is failed
- "'to_time_unit() can not interpret following string' in res.msg"