Github user gauravaradhye commented on a diff in the pull request:

    https://github.com/apache/cloudstack/pull/153#discussion_r31501168
  
    --- Diff: test/integration/component/test_escalations_templates.py ---
    @@ -915,3 +968,223 @@ def test_04_copy_template(self):
                 )
             del self.services["privatetemplate"]["ostype"]
             return
    +
    +    @attr(tags=["advanced", "basic"], required_hardware="true")
    +    def test_05_template_permissions(self):
    +        """
    +        @Desc: Test to create Public Template by registering or by 
snapshot and volume when
    +        Global parameter 'allow.public.user.template' is set to  False
    +        @steps:
    +        1.Set Global parameter 'allow.public.user.template' as False. 
Restart Management server
    +        2. Create a domain
    +        3. Create a domain admin and a domain user
    +        4. Create a vm as domain user
    +        5. take snapshot of root disk as user vm
    +        6. try to create public template from snapshot . It should fail
    +        7. stop the VM
    +        8. take the public template from volume. it should fail
    +        9. register a public template as a domain user . it should fail
    +        10. create a VM  as domain admin
    +        11. create a snapshot of root disk as domain admin
    +        12 create a public template of the snapshot .it should fail
    +        13. Register a public template as domain admin. it should fail
    +        14 Stop the vm as domain admin
    +        15. Create a template from volume as domain admin . it should fail
    +
    +        """
    +        self.updateConfigurAndRestart("allow.public.user.templates", 
"false")
    +
    +        subdomain = Domain.create(
    +            self.api_client,
    +            self.services["domain"],
    +        )
    +
    +        admin_account = Account.create(
    +            self.api_client,
    +            self.services["account"],
    +            admin=True,
    +            domainid=subdomain.id
    +        )
    +        user_account = Account.create(
    +            self.api_client,
    +            self.services["account2"],
    +            admin=False,
    +            domainid=subdomain.id
    +        )
    +        admin_user = admin_account.user[0]
    +        self.admin_api_client = self.testClient.getUserApiClient(
    +            admin_user.username,
    +            subdomain.name)
    +        user = user_account.user[0]
    +        self.user_api_client = self.testClient.getUserApiClient(
    +            user.username,
    +            subdomain.name)
    +
    +        self.services["templates"]["ispublic"] = True
    +        # Register new public template as domain user
    +        # Exception should be raised for registering public template
    +        try:
    +            template = Template.register(
    +                self.user_api_client,
    +                self.services["templates"],
    +                zoneid=self.zone.id,
    +                account=user_account.name,
    +                domainid=user_account.domainid,
    +                hypervisor=self.hypervisor
    +            )
    +            self.updateConfigurAndRestart("allow.public.user.templates", 
"true")
    +            self.fail("Template creation passed for user")
    +        except CloudstackAPIException  as e:
    +            self.assertRaises("Exception Raised : %s" % e)
    +        # Register new public template as domain admin
    +        # Exception should be raised for registering public template
    +        try:
    +            template = Template.register(
    +                self.admin_api_client,
    +                self.services["templates"],
    +                zoneid=self.zone.id,
    +                account=admin_account.name,
    +                domainid=admin_account.domainid,
    +                hypervisor=self.hypervisor
    +            )
    +            self.updateConfigurAndRestart("allow.public.user.templates", 
"true")
    +            self.fail("Template creation passed for domain admin")
    +        except CloudstackAPIException  as e:
    +            self.assertRaises("Exception Raised : %s" % e)
    +
    +        if self.hypervisor.lower() in ['hyperv', 'lxc']:
    +            self.updateConfigurAndRestart("allow.public.user.templates", 
"true")
    +            return
    +        else:
    +            user_vm_created = VirtualMachine.create(
    +                self.user_api_client,
    +                self.services["virtual_machine"],
    +                accountid=user_account.name,
    +                domainid=user_account.domainid,
    +                serviceofferingid=self.service_offering.id,
    +            )
    +            self.assertIsNotNone(user_vm_created,
    +                                 "VM creation failed"
    +            )
    +            # Get the Root disk of VM
    +            volume = list_volumes(
    +                self.user_api_client,
    +                virtualmachineid=user_vm_created.id,
    +                type='ROOT',
    +                listall=True
    +            )
    +            snapshot_created = Snapshot.create(
    +                self.user_api_client,
    +                volume[0].id,
    +                account=user_account.name,
    +                domainid=user_account.domainid
    +            )
    +            self.assertIsNotNone(
    +                snapshot_created,
    +                "Snapshot creation failed"
    +            )
    +            self.debug("Creating a template from snapshot: %s" % 
snapshot_created.id)
    +            #
    +            # Generate public template from the snapshot
    +            self.services["template"]["ispublic"] = True
    +            try:
    +                user_template = Template.create_from_snapshot(
    +                    self.user_api_client,
    +                    snapshot_created,
    +                    self.services["template"]
    +                )
    +                
self.updateConfigurAndRestart("allow.public.user.templates", "true")
    +                self.fail("Template creation passed from snapshot for 
domain user")
    +            except CloudstackAPIException  as e:
    +                self.assertRaises("Exception Raised : %s" % e)
    +
    +            VirtualMachine.stop(user_vm_created, self.user_api_client)
    +            list_stopped_vms_after = VirtualMachine.list(
    +                self.user_api_client,
    +                listall=self.services["listall"],
    +                domainid=user_account.domainid,
    +                state="Stopped")
    +            status = validateList(list_stopped_vms_after)
    +            self.assertEquals(
    +                PASS,
    +                status[0],
    +                "Stopped VM is not in Stopped state"
    +            )
    +            try:
    +                user_template = Template.create(
    +                    self.user_api_client, self.services["template"],
    +                    volume[0].id
    +                )
    +                
self.updateConfigurAndRestart("allow.public.user.templates", "true")
    +                self.fail("Template creation passed from volume for domain 
user")
    +            except CloudstackAPIException  as e:
    +                self.assertRaises("Exception Raised : %s" % e)
    +
    +            admin_vm_created = VirtualMachine.create(
    +                self.admin_api_client,
    +                self.services["virtual_machine"],
    +                accountid=admin_account.name,
    +                domainid=admin_account.domainid,
    +                serviceofferingid=self.service_offering.id,
    +            )
    +            self.assertIsNotNone(
    +                admin_vm_created,
    +                "VM creation failed"
    +            )
    +            # Get the Root disk of VM
    +            volume = list_volumes(
    +                self.admin_api_client,
    +                virtualmachineid=admin_vm_created.id,
    +                type='ROOT',
    +                listall=True
    +            )
    +            snapshot_created = Snapshot.create(
    +                self.admin_api_client,
    +                volume[0].id,
    +                account=admin_account.name,
    +                domainid=admin_account.domainid
    +            )
    +            self.assertIsNotNone(
    +                snapshot_created,
    +                "Snapshot creation failed"
    +            )
    +            self.debug("Creating a template from snapshot: %s" % 
snapshot_created.id)
    +            #
    +            #    Generate public template from the snapshot
    +            try:
    +                admin_template = Template.create_from_snapshot(
    +                    self.admin_api_client,
    +                    snapshot_created,
    +                    self.services["template"]
    +                )
    +                
self.updateConfigurAndRestart("allow.public.user.templates", "true")
    +                self.fail("Template creation passed from snapshot for 
domain admin")
    +            except CloudstackAPIException  as e:
    +                self.assertRaises("Exception Raised : %s" % e)
    +
    +            VirtualMachine.stop(admin_vm_created, self.admin_api_client)
    +            list_stopped_vms_after = VirtualMachine.list(
    +                self.admin_api_client,
    +                listall=self.services["listall"],
    +                domainid=admin_account.domainid,
    +                state="Stopped")
    +            status = validateList(list_stopped_vms_after)
    +            self.assertEquals(
    +                PASS,
    +                status[0],
    +                "Stopped VM is not in Stopped state"
    +            )
    +            try:
    +                admin_template = Template.create(
    +                    self.admin_api_client, self.services["template"],
    +                    volume[0].id
    +                )
    +                
self.updateConfigurAndRestart("allow.public.user.templates", "true")
    +                self.fail("Template creation passed from volume for domain 
admin")
    +            except CloudstackAPIException  as e:
    +                self.assertRaises("Exception Raised : %s" % e)
    +
    +            self.updateConfigurAndRestart("allow.public.user.templates", 
"true")
    --- End diff --
    
    Have this step in tearDownClass() also. In case test case fails before 
executing completely, the config value will remain to False. We need to 
explicitly ensure it is set to true even if test case fails at any stage.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to