def test_no_compose(self, m): m.get('http://test.com', status_code=500) self.populate_file( 'main.yaml', '''--- steps: - http: {get: {url: 'http://test.com', response_code: 200}} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertFalse(runner.run_tests())
def test_inventory_vars_available(self): self.populate_file( 'inventory.yml', '''--- prod_url: http://prod.url foo: bar ''') self.populate_file( 'main.yaml', '''--- steps: - echo: {from: '{{ prod_url }}', to: main1.output} - echo: {from: '{{ foo }}', to: main2.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), join(self.test_dir, 'inventory.yml')) runner.run_tests() self.assertTrue( check_file(join(self.test_dir, 'main1.output'), 'http://prod.url')) self.assertTrue(check_file(join(self.test_dir, 'main2.output'), 'bar'))
def test_run_ignore_test(self): self.populate_file( 'main.yaml', '''--- ignore: true # for some reason this test is not working steps: - check: {equals: {the: true, is: false}} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests())
def test_stop_from_include(self): self.populate_file( 'test_inventory.yml', ''' postgres: dbname: test user: test password: test host: localhost port: 5433 ''') self.populate_file( 'main.yaml', '''--- include: file: migration.yaml as: migrate steps: - run: migrate.check - postgres: request: conf: '{{ postgres }}' query: "insert into test(id, num) values(3, 3);" - run: migrate.commit ''') self.populate_file( 'migration.yaml', '''--- steps: - postgres: request: conf: '{{ postgres }}' query: "select count(*) from migration where hash = '{{ TEST_NAME }}';" register: {result: '{{ OUTPUT }}'} tag: check name: 'check_migration_{{ TEST_NAME }}' - stop: if: equals: {the: '{{ result }}', is: 1} tag: check name: 'stop_if_already_run_{{ TEST_NAME }}' - postgres: request: conf: '{{ postgres }}' query: "insert into migration(id, hash) values(1, '{{ TEST_NAME }}');" tag: commit name: 'commit_migration_{{ TEST_NAME }}' ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), join(self.test_dir, 'test_inventory.yml')) self.assertTrue(runner.run_tests()) self.assertTrue(runner.run_tests()) conn = psycopg2.connect(self.conf) cur = conn.cursor() cur.execute("select count(*) from test") response = cur.fetchall() conn.commit() cur.close() conn.close() self.assertEqual([(1, )], response)
def test_run_skip_if_short(self): self.populate_file( 'main.yaml', '''--- variables: no_output: true steps: - echo: from: 'hello world' to: main1.output skip_if: '{{ no_output }}' - echo: {from: 'test2', to: main2.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) runner.run_tests() self.assertTrue( not os.path.exists(join(self.test_dir, 'main1.output'))) self.assertTrue( check_file(join(self.test_dir, 'main2.output'), 'test2'))
def test_computed_equals_constant(self): self.populate_file( 'main.yaml', '''--- steps: - echo: {from: 'hello', register: {'foo': 'value'}} - check: {equals: {the: '{{ foo }}', is: 'value'}} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests())
def test_check_short_form(self): self.populate_file('main.yaml', '''--- variables: foo: true steps: - check: '{{ foo }}' ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests())
def test_register_override_cmd(self): self.populate_file( 'main.yaml', '''--- variables: foo: baz steps: - echo: actions: - {from: 'bar', register: {foo: '{{ OUTPUT }}'}} - {from: '{{ foo }}', to: one.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None, cmd_env={'foo': 'bad'}) runner.run_tests() self.assertTrue(check_file(join(self.test_dir, 'one.output'), 'bar'))
def test_populate_from_scratch(self): self.populate_file('resources/pg_schema.sql', ''' CREATE TABLE foo( user_id varchar(36) primary key, email varchar(36) NOT NULL ); CREATE TABLE bar( key varchar(36) primary key, value varchar(36) NOT NULL ); ''') self.populate_file('resources/foo.csv', "user_id,email\n" "1,[email protected]\n" "2,[email protected]\n" ) self.populate_file('resources/bar.csv', "key,value\n" "k1,v1\n" "k2,v2\n" ) self.populate_file('test_inventory.yml', ''' postgres: dbname: test user: test password: test host: localhost port: 5433 ''') self.populate_file('main.yaml', '''--- steps: - prepare: populate: postgres: conf: '{{ postgres }}' schema: pg_schema.sql data: foo: foo.csv bar: bar.csv ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), join(self.test_dir, 'test_inventory.yml')) self.assertTrue(runner.run_tests()) response = self.get_values('foo') self.assertEqual(2, len(response)) self.assertEqual('1', response[0][0]) self.assertEqual('*****@*****.**', response[0][1]) self.assertEqual('2', response[1][0]) self.assertEqual('*****@*****.**', response[1][1]) response = self.get_values('bar') self.assertEqual(2, len(response)) self.assertEqual('k1', response[0][0]) self.assertEqual('v1', response[0][1]) self.assertEqual('k2', response[1][0]) self.assertEqual('v2', response[1][1])
def test_vars_available_computed(self): self.populate_file('main.yaml', '''--- include: - one.yaml - two.yaml ''') self.populate_file('one.yaml', '''--- steps: - echo: {from: 'hello', register: {foo: 'bar'}} ''') self.populate_file('two.yaml', '''--- steps: - echo: {from: '{{ foo }}', to: one.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) runner.run_tests() self.assertTrue(check_file(join(self.test_dir, 'one.output'), 'bar'))
def test_random_functions(self): Faker.seed(4321) self.populate_file('main.yaml', '''--- steps: - echo: {from: '{{ random("ipv4_private") }}', to: one.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests()) self.assertTrue(check_file(join(self.test_dir, 'one.output'), '10.32.135.245'))
def test_compate_dates(self): self.populate_file( 'main.yaml', '''--- steps: - check: equals: {the: '2020-03-11', is_not: '2020-11-03' } ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests())
def test_var_override_env(self): os.environ['FOO'] = '1' self.populate_file('main.yaml', '''--- variables: FOO: 2 steps: - check: {equals: {the: '{{ FOO }}', is: '2'}} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests())
def test_run_negative(self): self.populate_file( 'main.yaml', '''--- steps: - sh: command: 'rm foobarbaz' - echo: {from: '{{ content }}', to: 'debug.output'} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertFalse(runner.run_tests())
def test_env_vars_included(self): os.environ['FOO'] = '1' self.populate_file( 'main.yaml', '''--- steps: - check: {equals: {the: '{{ FOO }}', is: '1'}} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests())
def test_random_choice(self): self.populate_file('main.yaml', '''--- variables: my_list: ['one', 'two', 'three'] steps: - echo: {from: '{{ random_choice(my_list) }}', to: one.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) random.seed(123) self.assertTrue(runner.run_tests()) self.assertTrue(check_file(join(self.test_dir, 'one.output'), 'three'))
def test_run_named(self): self.populate_file( 'main.yaml', '''--- steps: - echo: from: 'user-{{ RANDOM_STR }}' register: {uuid: '{{ OUTPUT }}'} name: 'first' ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests())
def test_wait_fixed(self): self.populate_file( 'main.yaml', '''--- steps: - echo: {from: '{{ NOW_TS }}', register: {before: '{{ OUTPUT }}'}} - wait: {seconds: 1} - echo: {from: '{{ NOW_TS }}', register: {after: '{{ OUTPUT }}'}} - check: {equals: {the: '{{ after - before >= 1 }}', is: true}} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests())
def test_env_var_in_vars(self): os.environ['FOO'] = '1' self.populate_file('main.yaml', '''--- variables: foo: '{{ FOO }}' steps: - check: {equals: {the: '{{ foo }}', is: '1'}} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None, system_environment=dict(os.environ)) self.assertTrue(runner.run_tests())
def test_simple_include(self): self.populate_file( 'main.yaml', '''--- include: file: simple_file.yaml as: simple run_on_include: false steps: - run: 'simple' ''') self.populate_file( 'simple_file.yaml', '''--- variables: foo: bar steps: - echo: {from: '{{ foo }}', to: foo.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) runner.run_tests() self.assertTrue(check_file(join(self.test_dir, 'foo.output'), 'bar'))
def test_run_skip_if_multiple(self): self.populate_file( 'main.yaml', '''--- variables: list: ['a', 'b', 'c'] steps: - echo: from: 'hello world' to: main1.output skip_if: or: - contains: {the: '1', in: '{{ list }}'} - equals: {the: '{{ list[0] }}', is: 'a'} - contains: {the: 'b', in: '{{ list }}'} - echo: {from: 'test2', to: main2.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) runner.run_tests() self.assertTrue( not os.path.exists(join(self.test_dir, 'main1.output')))
def test_dt_fitlers_revertable(self): self.populate_file('main.yaml', '''--- variables: current_ts: '{{ now_ts() }}' steps: - echo: {from: '{{ current_ts | asdate }}', register: {current_dt: '{{ OUTPUT }}'}} - check: {equals: {the: '{{ current_ts }}', is: '{{ current_dt | astimestamp }}'}} - check: {equals: {the: '{{ current_dt }}', is: '{{ current_ts | asdate }}'}} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests())
def test_inventory_vars_available_in_include(self): self.populate_file('inventory.yml', '''--- foo: bar ''') self.populate_file( 'main.yaml', '''--- include: one.yaml steps: - echo: {from: '{{ foo }}', to: main.output} ''') self.populate_file( 'one.yaml', '''--- steps: - echo: {from: '{{ foo }}', to: include.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), join(self.test_dir, 'inventory.yml')) runner.run_tests() self.assertTrue( check_file(join(self.test_dir, 'include.output'), 'bar'))
def test_should_fail(self): self.populate_file( 'main.yaml', '''--- steps: - http: get: url: 'http://undefined' should_fail: true ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests())
def test_write_variable(self): self.populate_file( 'main.yaml', '''--- variables: user_email: 123 steps: - echo: {from: '{{ user_email }}', register: {'foo': '{{ OUTPUT }}'} } - check: {equals: {the: '{{ foo }}', is: '123'}} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests())
def test_cmd_variable_override_inventory(self): self.populate_file('inventory.yml', '''--- one: bar ''') self.populate_file( 'main.yaml', '''--- steps: - echo: {from: '{{ one }}', to: 1.output} - echo: {from: '{{ two }}', to: 2.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), join(self.test_dir, 'inventory.yml'), environment={ 'one': 'baz', 'two': 'bax' }) runner.run_tests() self.assertTrue(check_file(join(self.test_dir, '1.output'), 'baz')) self.assertTrue(check_file(join(self.test_dir, '2.output'), 'bax'))
def test_write_file(self): self.populate_file( 'main.yaml', '''--- variables: user_email: 123 steps: - echo: {from: '{{ user_email }}', to: 'debug.output'} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests()) self.assertTrue(check_file(join(self.test_dir, 'debug.output'), '123'))
def test_vars_override_file(self): self.populate_file('main.yaml', '''--- include: - one.yaml - two.yaml ''') self.populate_file('one.yaml', '''--- variables: foo: bar steps: - echo: {from: 'hello'} ''') self.populate_file('two.yaml', '''--- variables: foo: baz steps: - echo: {from: '{{ foo }}', to: one.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) runner.run_tests() self.assertTrue(check_file(join(self.test_dir, 'one.output'), 'baz'))
def test_read_file(self): self.populate_resource('debug.input', '123') self.populate_file( 'main.yaml', '''--- variables: foo: true steps: - echo: {from_file: debug.input, register: {user_email: '{{ OUTPUT }}'}} - check: {equals: {the: '{{ user_email }}', is: '123'}} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests())
def test_run_named_variables(self): self.populate_file( 'main.yaml', '''--- steps: - echo: {from: 'user-{{ RANDOM_STR }}', register: {uuid: '{{ OUTPUT }}'}} - echo: from: '{{ uuid }}' to: main.output name: 'Save user {{ uuid }} to main.output' ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests())