lundi 2 novembre 2020

Python is wrongly evaluating a string from subprocess output?

I have a function which checks just returns true or false based on a variable ctx.

def isContextFCP():
    ctx = getKubernetesContext()
    print(ctx)
    print(type(ctx))
    if(ctx=="fcp"):
        return True
    else:
        return False

ctx is defined here:

def getKubernetesContext():
    """
    Returns: Kubernetes context
    """
    result = False
    try:
        result = subprocess.check_output('kubectl config current-context', shell=True, universal_newlines=True )
        logging.debug("context is: " + result)
        return result

When I run this code isContextFCP() evaluates to False despite ctx being a string "fcp" The output looks like this:

DEBUG: context is: fcp 
fcp 
<class 'str'>

I have no idea why this could be happening, I've never run into this problem before. The visual debugger hasn't been of any help. Any help or guidance would be very appreciated. The only time it evaluates to true is when I just hardcode ctx. With ctx="fcp". I've tried using single quotes as well.

Aucun commentaire:

Enregistrer un commentaire