Best Python code snippet using autotest_python
kubectl.py
Source:kubectl.py  
...51    def create_pvc(self,pvc,namespace):52        k8s_apps_v1 = client.CoreV1Api(self.aApiClient)53        return k8s_apps_v1.create_namespaced_persistent_volume_claim(54            body=pvc, namespace=namespace)55    def create_kind(self, filename,kind_name,namespace="default"):56        results = []57        try:58            with open(filename) as f:59                kinds = yaml.safe_load_all(f)60                try:61                    for kind in kinds:62                        if kind_name == 'Service': resp = self.create_service(kind,namespace)63                        if kind_name == 'Deployment': resp = self.create_deployment(kind, namespace)64                        if kind_name == 'PersistentVolumes': resp = self.create_pvc(kind, namespace)65                        log.info("%s %s created" % (kind_name,resp.metadata.name))66                        results.append(KubectlResult.generate(filename, kind_name, namespace, self.kubectl_server, resp=resp))67                except MaxRetryError as e:68                    log.info(f"Connection to server {self.kubectl_server.host}:{self.kubectl_server.port} is bad.")69                    results.append(KubectlResult.generate(filename, kind_name, namespace, self.kubectl_server, max_retries=True))70        except client.exceptions.ApiException as e:71            resp = json.loads(e.body)72            results.append(KubectlResult.generate(filename, kind_name, namespace, self.kubectl_server, api_exception=True,resp=resp))73            log.info(f"Error connection with kubernetes API. Error: {resp['message']}")74        return results75class KubectlLauncher:76    def __init__(self, config: Config, data: InitData):77        self.config = config78        self.data = data79        self.__results = []80    def run(self):81        log.info("Starting the creation of kinds in kubectl")82        for server in self.config.kubectl_servers:83            kubectl_server = KubectlServer.parse(server)84            try:85                kw = KubectlWorker(kubectl_server)86                for deployment in self.config.deployments:87                    if {'name' : server['name']} in deployment['servers']:88                        result = kw.create_kind(deployment['filename'], 'Deployment' ,deployment['namespace'])89                        self.__result_append(result)90                for service in self.config.services:91                    if {'name': server['name']} in service['servers']:92                        result = kw.create_kind(service['filename'], 'Service' ,service['namespace'])93                        self.__result_append(result)94                for volume in self.config.persistentVolumes:95                    if {'name': server['name']} in volume['servers']:96                        result = kw.create_kind(volume['filename'], 'PersistentVolumes' ,volume['namespace'])97                        self.__result_append(result)98            except KeyError as e:99                raise KubectlLauncherYamlException(f"A required key in kind was not found in the config. {e}")100        log.info("Finish the creation of kinds in kubectl")101    def __result_append(self, results):102        for result in results:103            self.__results.append(result.__dict__['result'])104    @property105    def all_results(self):106        return self.__results107    @all_results.getter108    def all_results(self):109        return self.__results110class KubectlResult:...constants.py
Source:constants.py  
...5from enum import IntEnum6# GREENDAY7from .utils import choices8CODES_PER_MODEL = 1009def create_kind(model, code):10    """11        Creates a compound event kind from an12        :class:`greenday_core.constants.EventModel <greenday_core.constants.EventModel>`13        and a code unique within that model's kinds14    """15    return model*CODES_PER_MODEL+code16class EventModel(IntEnum):17    """18        Defines models for which events may be recorded19    """20    PROJECT = 021    VIDEO = 122    USER = 223    VIDEO_COLLECTION = 324    PENDINGUSER = 425    PROJECT_COMMENT = 526    TIMED_VIDEO_COMMENT = 627    TIMED_VIDEO_COMMENT_REPLY = 728    PROJECT_COMMENT_REPLY = 829class EventCommonCodes(IntEnum):30    """31        These codes are common to many objects and the values32        are consistent between EventKind values33    """34    CREATED = 035    UPDATED = 136    DELETED = 237@choices38class EventKind(IntEnum):39    """40        A way to uniquely identify a type of event which can happen within the41        system. These should be very specific.42        This *may* look crazy but it does make sense. It allows us to use a43        single value to represent an event on a given model and keep consistent44        values for similar events (create, update, delete).45        Essentially these values are constructed so that the result of dividing46        by CODES_PER_MODEL will give you the EventModel and getting the47        remainder will give you the event code.48        I.e.49            EventKind.VIDEODELETED == 10250            102 / CODES_PER_MODEL == 1 (EventModel.VIDEO)51            102 % CODES_PER_MODEL == 2 (EventCommonCodes.DELETED)52        Better: model, code = divmod(102, CODES_PER_MODEL)53            == (1, 2)54    """55    # PROJECTS 0-9956    PROJECTCREATED = create_kind(EventModel.PROJECT, EventCommonCodes.CREATED)57    PROJECTUPDATED = create_kind(EventModel.PROJECT, EventCommonCodes.UPDATED)58    PROJECTDELETED = create_kind(EventModel.PROJECT, EventCommonCodes.DELETED)59    PROJECTRESTORED = create_kind(EventModel.PROJECT, 50)60    # VIDEOS 100-19961    VIDEOCREATED = create_kind(EventModel.VIDEO, EventCommonCodes.CREATED)62    VIDEOUPDATED = create_kind(EventModel.VIDEO, EventCommonCodes.UPDATED)63    VIDEODELETED = create_kind(EventModel.VIDEO, EventCommonCodes.DELETED)64    VIDEOHIGHLIGHTED = create_kind(EventModel.VIDEO, 50)65    VIDEOUNHIGHLIGHTED = create_kind(EventModel.VIDEO, 51)66    VIDEOARCHIVED = create_kind(EventModel.VIDEO, 52)67    VIDEOUNARCHIVED = create_kind(EventModel.VIDEO, 53)68    # USER 200-29969    USERCREATED = create_kind(EventModel.USER, EventCommonCodes.CREATED)70    USERUPDATED = create_kind(EventModel.USER, EventCommonCodes.UPDATED)71    USERDELETED = create_kind(EventModel.USER, EventCommonCodes.DELETED)72    USERACCEPTEDNDA = create_kind(EventModel.USER, 50)73    USERINVITEDASPROJECTUSER = create_kind(EventModel.USER, 51)74    USERINVITEDASPROJECTADMIN = create_kind(EventModel.USER, 52)75    USERACCEPTEDPROJECTINVITE = create_kind(EventModel.USER, 53)76    USERREJECTEDPROJECTINVITE = create_kind(EventModel.USER, 54)77    USERREMOVED = create_kind(EventModel.USER, 55)78    PROJECTCOLLABORATORONLINE = create_kind(EventModel.USER, 56)79    PROJECTCOLLABORATOROFFLINE = create_kind(EventModel.USER, 57)80    USEREXPORTEDVIDEOS = create_kind(EventModel.USER, 58)81    # COLLECTIONS 300-39982    VIDEOCOLLECTIONCREATED = create_kind(83        EventModel.VIDEO_COLLECTION, EventCommonCodes.CREATED)84    VIDEOCOLLECTIONUPDATED = create_kind(85        EventModel.VIDEO_COLLECTION, EventCommonCodes.UPDATED)86    VIDEOCOLLECTIONDELETED = create_kind(87        EventModel.VIDEO_COLLECTION, EventCommonCodes.DELETED)88    VIDEOADDEDTOCOLLECTION = create_kind(EventModel.VIDEO_COLLECTION, 50)89    VIDEOREMOVEDFROMCOLLECTION = create_kind(EventModel.VIDEO_COLLECTION, 51)90    # PENDING USER 400-49991    PENDINGUSERINVITEDASPROJECTADMIN = create_kind(EventModel.PENDINGUSER, 50)92    PENDINGUSERINVITEDASPROJECTUSER = create_kind(EventModel.PENDINGUSER, 51)93    PENDINGUSERREMOVED = create_kind(EventModel.USER, 52)94    # PROJECT COMMENT 500-59995    PROJECTROOTCOMMENTCREATED = create_kind(96        EventModel.PROJECT_COMMENT, EventCommonCodes.CREATED)97    PROJECTCOMMENTUPDATED = create_kind(98        EventModel.PROJECT_COMMENT, EventCommonCodes.UPDATED)99    PROJECTCOMMENTDELETED = create_kind(100        EventModel.PROJECT_COMMENT, EventCommonCodes.DELETED)101    PROJECTREPLYCOMMENTCREATED = create_kind(102        EventModel.PROJECT_COMMENT_REPLY, EventCommonCodes.CREATED)103    PROJECTREPLYCOMMENTUPDATED = create_kind(104        EventModel.PROJECT_COMMENT_REPLY, EventCommonCodes.UPDATED)105    PROJECTREPLYCOMMENTDELETED = create_kind(106        EventModel.PROJECT_COMMENT_REPLY, EventCommonCodes.DELETED)107    # TIMED VIDEO COMMENT 600-699108    TIMEDVIDEOROOTCOMMENTCREATED = create_kind(109        EventModel.TIMED_VIDEO_COMMENT, EventCommonCodes.CREATED)110    TIMEDVIDEOCOMMENTUPDATED = create_kind(111        EventModel.TIMED_VIDEO_COMMENT, EventCommonCodes.UPDATED)112    TIMEDVIDEOCOMMENTDELETED = create_kind(113        EventModel.TIMED_VIDEO_COMMENT, EventCommonCodes.DELETED)114    TIMEDVIDEOREPLYCOMMENTCREATED = create_kind(115        EventModel.TIMED_VIDEO_COMMENT_REPLY, EventCommonCodes.CREATED)116    TIMEDVIDEOREPLYCOMMENTUPDATED = create_kind(117        EventModel.TIMED_VIDEO_COMMENT_REPLY, EventCommonCodes.UPDATED)118    TIMEDVIDEOREPLYCOMMENTDELETED = create_kind(119        EventModel.TIMED_VIDEO_COMMENT_REPLY, EventCommonCodes.DELETED)120# Don't use HTTP status codes121class ErrorCodes(IntEnum):122    """123        Codes understand by the client application. These are returned in124        the case of exceptions and the client application can react125        appropriately126    """127    TAG_NAME_ALREADY_EXISTS = 1000128    BAD_SEARCH_DATE_FORMAT = 1001129    BAD_SEARCH_GEO_FORMAT = 1002...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!!
