|
19 | 19 | #Import Local Modules |
20 | 20 | from marvin.codes import FAILED |
21 | 21 | from marvin.cloudstackTestCase import cloudstackTestCase, unittest |
| 22 | +from marvin.cloudstackAPI import listZones |
22 | 23 | from marvin.lib.utils import random_gen, cleanup_resources |
23 | 24 | from marvin.lib.base import (Account, |
24 | 25 | ServiceOffering, |
@@ -794,3 +795,200 @@ def test_08_list_system_templates(self): |
794 | 795 | "ListTemplates should not list any system templates" |
795 | 796 | ) |
796 | 797 | return |
| 798 | + |
| 799 | +class TestCopyDeleteTemplate(cloudstackTestCase): |
| 800 | + |
| 801 | + def setUp(self): |
| 802 | + |
| 803 | + self.apiclient = self.testClient.getApiClient() |
| 804 | + self.dbclient = self.testClient.getDbConnection() |
| 805 | + self.cleanup = [] |
| 806 | + |
| 807 | + if self.unsupportedHypervisor: |
| 808 | + self.skipTest("Skipping test because unsupported hypervisor\ |
| 809 | + %s" % self.hypervisor) |
| 810 | + return |
| 811 | + |
| 812 | + def tearDown(self): |
| 813 | + try: |
| 814 | + #Clean up, terminate the created templates |
| 815 | + cleanup_resources(self.apiclient, self.cleanup) |
| 816 | + |
| 817 | + except Exception as e: |
| 818 | + raise Exception("Warning: Exception during cleanup : %s" % e) |
| 819 | + return |
| 820 | + |
| 821 | + @classmethod |
| 822 | + def setUpClass(cls): |
| 823 | + testClient = super(TestCopyDeleteTemplate, cls).getClsTestClient() |
| 824 | + cls.apiclient = testClient.getApiClient() |
| 825 | + cls._cleanup = [] |
| 826 | + cls.services = testClient.getParsedTestDataConfig() |
| 827 | + cls.unsupportedHypervisor = False |
| 828 | + cls.hypervisor = testClient.getHypervisorInfo() |
| 829 | + if cls.hypervisor.lower() in ['lxc']: |
| 830 | + # Template creation from root volume is not supported in LXC |
| 831 | + cls.unsupportedHypervisor = True |
| 832 | + return |
| 833 | + |
| 834 | + # Get Zone, Domain and templates |
| 835 | + cls.domain = get_domain(cls.apiclient) |
| 836 | + cls.zone = get_zone(cls.apiclient, testClient.getZoneForTests()) |
| 837 | + cls.services['mode'] = cls.zone.networktype |
| 838 | + try: |
| 839 | + cls.disk_offering = DiskOffering.create( |
| 840 | + cls.apiclient, |
| 841 | + cls.services["disk_offering"] |
| 842 | + ) |
| 843 | + cls._cleanup.append(cls.disk_offering) |
| 844 | + template = get_template( |
| 845 | + cls.apiclient, |
| 846 | + cls.zone.id, |
| 847 | + cls.services["ostype"] |
| 848 | + ) |
| 849 | + if template == FAILED: |
| 850 | + assert False, "get_template() failed to return template with description %s" % cls.services["ostype"] |
| 851 | + |
| 852 | + cls.services["template"]["ostypeid"] = template.ostypeid |
| 853 | + cls.services["template_2"]["ostypeid"] = template.ostypeid |
| 854 | + cls.services["ostypeid"] = template.ostypeid |
| 855 | + |
| 856 | + cls.services["virtual_machine"]["zoneid"] = cls.zone.id |
| 857 | + cls.services["volume"]["diskoffering"] = cls.disk_offering.id |
| 858 | + cls.services["volume"]["zoneid"] = cls.zone.id |
| 859 | + cls.services["sourcezoneid"] = cls.zone.id |
| 860 | + cls.account = Account.create( |
| 861 | + cls.apiclient, |
| 862 | + cls.services["account"], |
| 863 | + domainid=cls.domain.id |
| 864 | + ) |
| 865 | + cls._cleanup.append(cls.account) |
| 866 | + cls.service_offering = ServiceOffering.create( |
| 867 | + cls.apiclient, |
| 868 | + cls.services["service_offerings"]["tiny"] |
| 869 | + ) |
| 870 | + cls._cleanup.append(cls.service_offering) |
| 871 | + #create virtual machine |
| 872 | + cls.virtual_machine = VirtualMachine.create( |
| 873 | + cls.apiclient, |
| 874 | + cls.services["virtual_machine"], |
| 875 | + templateid=template.id, |
| 876 | + accountid=cls.account.name, |
| 877 | + domainid=cls.account.domainid, |
| 878 | + serviceofferingid=cls.service_offering.id, |
| 879 | + mode=cls.services["mode"] |
| 880 | + ) |
| 881 | + #Stop virtual machine |
| 882 | + cls.virtual_machine.stop(cls.apiclient) |
| 883 | + |
| 884 | + list_volume = Volume.list( |
| 885 | + cls.apiclient, |
| 886 | + virtualmachineid=cls.virtual_machine.id, |
| 887 | + type='ROOT', |
| 888 | + listall=True |
| 889 | + ) |
| 890 | + |
| 891 | + cls.volume = list_volume[0] |
| 892 | + except Exception as e: |
| 893 | + cls.tearDownClass() |
| 894 | + raise unittest.SkipTest("Exception in setUpClass: %s" % e) |
| 895 | + return |
| 896 | + |
| 897 | + @classmethod |
| 898 | + def tearDownClass(cls): |
| 899 | + try: |
| 900 | + cls.apiclient = super(TestCopyDeleteTemplate, cls).getClsTestClient().getApiClient() |
| 901 | + #Cleanup resources used |
| 902 | + cleanup_resources(cls.apiclient, cls._cleanup) |
| 903 | + |
| 904 | + except Exception as e: |
| 905 | + raise Exception("Warning: Exception during cleanup : %s" % e) |
| 906 | + |
| 907 | + return |
| 908 | + |
| 909 | + |
| 910 | + |
| 911 | + @attr(tags=["advanced", "advancedns"], required_hardware="false") |
| 912 | + def test_09_copy_delete_template(self): |
| 913 | + cmd = listZones.listZonesCmd() |
| 914 | + zones = self.apiclient.listZones(cmd) |
| 915 | + if not isinstance(zones, list): |
| 916 | + raise Exception("Failed to find zones.") |
| 917 | + if len(zones) < 2: |
| 918 | + self.skipTest( |
| 919 | + "Skipping test due to there are less than two zones.") |
| 920 | + return |
| 921 | + |
| 922 | + self.sourceZone = zones[0] |
| 923 | + self.destZone = zones[1] |
| 924 | + |
| 925 | + template = Template.create( |
| 926 | + self.apiclient, |
| 927 | + self.services["template"], |
| 928 | + self.volume.id, |
| 929 | + account=self.account.name, |
| 930 | + domainid=self.account.domainid |
| 931 | + ) |
| 932 | + self.cleanup.append(template) |
| 933 | + |
| 934 | + self.debug("Created template with ID: %s" % template.id) |
| 935 | + |
| 936 | + list_template_response = Template.list( |
| 937 | + self.apiclient, |
| 938 | + templatefilter=\ |
| 939 | + self.services["templatefilter"], |
| 940 | + id=template.id |
| 941 | + ) |
| 942 | + |
| 943 | + self.assertEqual( |
| 944 | + isinstance(list_template_response, list), |
| 945 | + True, |
| 946 | + "Check list response returns a valid list" |
| 947 | + ) |
| 948 | + #Verify template response to check whether template added successfully |
| 949 | + self.assertNotEqual( |
| 950 | + len(list_template_response), |
| 951 | + 0, |
| 952 | + "Check template available in List Templates" |
| 953 | + ) |
| 954 | + #Copy template from zone1 to zone2 |
| 955 | + copytemplate = Template.copy( |
| 956 | + cls.apiclient, |
| 957 | + zoneid=cls.sourceZone.id, |
| 958 | + destzoneid = cls.destZone.id |
| 959 | + ) |
| 960 | + cls._cleanup.append(cls.copytemplate) |
| 961 | + |
| 962 | + list_template_response = Template.list( |
| 963 | + self.apiclient, |
| 964 | + templatefilter=self.services["template"]["templatefilter"], |
| 965 | + id=self.template.id, |
| 966 | + zoneid=self.destZone.id |
| 967 | + ) |
| 968 | + self.assertEqual( |
| 969 | + list_template_response, |
| 970 | + None, |
| 971 | + "Check template available in List Templates" |
| 972 | + ) |
| 973 | + |
| 974 | + self.deltemplate = list_template_response[0] |
| 975 | + |
| 976 | + self.debug("Deleting template: %s" % self.deltemplate) |
| 977 | + # Delete the template |
| 978 | + self.deltemplate.delete(self.apiclient) |
| 979 | + self.debug("Delete template: %s successful" % self.deltemplate) |
| 980 | + |
| 981 | + copytemplate = Template.copy( |
| 982 | + self.apiclient, |
| 983 | + zoneid=self.sourceZone.id, |
| 984 | + destzoneid = self.destZone.id |
| 985 | + ) |
| 986 | + |
| 987 | + removed = cls.dbclient.execute("select removed from template_zone_ref where zone_id='%s' and template_id='%s';" % self.destZone.id, self.template.id) |
| 988 | + |
| 989 | + self.assertEqual( |
| 990 | + removed, |
| 991 | + NULL, |
| 992 | + "Removed state is not correct." |
| 993 | + ) |
| 994 | + return |
0 commit comments