Best Python code snippet using tempest_python
test_simple_tenant_usage.py
Source:test_simple_tenant_usage.py  
1# Copyright 2013 NEC Corporation2# All Rights Reserved.3#4#    Licensed under the Apache License, Version 2.0 (the "License"); you may5#    not use this file except in compliance with the License. You may obtain6#    a copy of the License at7#8#         http://www.apache.org/licenses/LICENSE-2.09#10#    Unless required by applicable law or agreed to in writing, software11#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT12#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the13#    License for the specific language governing permissions and limitations14#    under the License.15import datetime16from tempest.api.compute import base17from tempest import test18from tempest_lib import exceptions as e19# Time that waits for until returning valid response20# TODO(takmatsu): Ideally this value would come from configuration.21VALID_WAIT = 3022class TenantUsagesTestJSON(base.BaseV2ComputeAdminTest):23    @classmethod24    def setup_clients(cls):25        super(TenantUsagesTestJSON, cls).setup_clients()26        cls.adm_client = cls.os_adm.tenant_usages_client27        cls.client = cls.os.tenant_usages_client28    @classmethod29    def resource_setup(cls):30        super(TenantUsagesTestJSON, cls).resource_setup()31        cls.tenant_id = cls.client.tenant_id32        # Create a server in the demo tenant33        cls.create_test_server(wait_until='ACTIVE')34        now = datetime.datetime.now()35        cls.start = cls._parse_strtime(now - datetime.timedelta(days=1))36        cls.end = cls._parse_strtime(now + datetime.timedelta(days=1))37    @classmethod38    def _parse_strtime(cls, at):39        # Returns formatted datetime40        return at.strftime('%Y-%m-%dT%H:%M:%S.%f')41    def call_until_valid(self, func, duration, *args, **kwargs):42        # Call until get valid response for "duration"43        # because tenant usage doesn't become available immediately44        # after create VM.45        def is_valid():46            try:47                self.resp = func(*args, **kwargs)48                return True49            except e.InvalidHTTPResponseBody:50                return False51        test.call_until_true(is_valid, duration, 1)52        return self.resp53    @test.idempotent_id('062c8ae9-9912-4249-8b51-e38d664e926e')54    def test_list_usage_all_tenants(self):55        # Get usage for all tenants56        tenant_usage = self.call_until_valid(57            self.adm_client.list_tenant_usages, VALID_WAIT,58            start=self.start, end=self.end, detailed="1")['tenant_usages'][0]59        self.assertEqual(len(tenant_usage), 8)60    @test.idempotent_id('94135049-a4c5-4934-ad39-08fa7da4f22e')61    def test_get_usage_tenant(self):62        # Get usage for a specific tenant63        tenant_usage = self.call_until_valid(64            self.adm_client.show_tenant_usage, VALID_WAIT,65            self.tenant_id, start=self.start, end=self.end)['tenant_usage']66        self.assertEqual(len(tenant_usage), 8)67    @test.idempotent_id('9d00a412-b40e-4fd9-8eba-97b496316116')68    def test_get_usage_tenant_with_non_admin_user(self):69        # Get usage for a specific tenant with non admin user70        tenant_usage = self.call_until_valid(71            self.client.show_tenant_usage, VALID_WAIT,72            self.tenant_id, start=self.start, end=self.end)['tenant_usage']...test_simple_tenant_usage_negative.py
Source:test_simple_tenant_usage_negative.py  
1# Copyright 2013 NEC Corporation2# All Rights Reserved.3#4#    Licensed under the Apache License, Version 2.0 (the "License"); you may5#    not use this file except in compliance with the License. You may obtain6#    a copy of the License at7#8#         http://www.apache.org/licenses/LICENSE-2.09#10#    Unless required by applicable law or agreed to in writing, software11#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT12#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the13#    License for the specific language governing permissions and limitations14#    under the License.15import datetime16from tempest_lib import exceptions as lib_exc17from tempest.api.compute import base18from tempest import test19class TenantUsagesNegativeTestJSON(base.BaseV2ComputeAdminTest):20    @classmethod21    def setup_clients(cls):22        super(TenantUsagesNegativeTestJSON, cls).setup_clients()23        cls.adm_client = cls.os_adm.tenant_usages_client24        cls.client = cls.os.tenant_usages_client25    @classmethod26    def resource_setup(cls):27        super(TenantUsagesNegativeTestJSON, cls).resource_setup()28        now = datetime.datetime.now()29        cls.start = cls._parse_strtime(now - datetime.timedelta(days=1))30        cls.end = cls._parse_strtime(now + datetime.timedelta(days=1))31    @classmethod32    def _parse_strtime(cls, at):33        # Returns formatted datetime34        return at.strftime('%Y-%m-%dT%H:%M:%S.%f')35    @test.attr(type=['negative'])36    @test.idempotent_id('8b21e135-d94b-4991-b6e9-87059609c8ed')37    def test_get_usage_tenant_with_empty_tenant_id(self):38        # Get usage for a specific tenant empty39        params = {'start': self.start,40                  'end': self.end}41        self.assertRaises(lib_exc.NotFound,42                          self.adm_client.show_tenant_usage,43                          '', **params)44    @test.attr(type=['negative'])45    @test.idempotent_id('4079dd2a-9e8d-479f-869d-6fa985ce45b6')46    def test_get_usage_tenant_with_invalid_date(self):47        # Get usage for tenant with invalid date48        params = {'start': self.end,49                  'end': self.start}50        self.assertRaises(lib_exc.BadRequest,51                          self.adm_client.show_tenant_usage,52                          self.client.tenant_id, **params)53    @test.attr(type=['negative'])54    @test.idempotent_id('bbe6fe2c-15d8-404c-a0a2-44fad0ad5cc7')55    def test_list_usage_all_tenants_with_non_admin_user(self):56        # Get usage for all tenants with non admin user57        params = {'start': self.start,58                  'end': self.end,59                  'detailed': "1"}60        self.assertRaises(lib_exc.Forbidden,...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
