...

Text file src/edge-infra.dev/config/pallets/o11y/fluentbit-alias-verifiy_test.py

Documentation: edge-infra.dev/config/pallets/o11y

     1import os 
     2
     3# Define global variables
     4totalAliasCount = 0
     5uniqueAlias = {} 
     6 
     7def fileOpener(path):
     8    dirList = os.listdir(path)
     9    global totalAliasCount
    10    global uniqueAlias
    11
    12    for f in dirList:
    13        # ignore test, input, output, and Kustomization files
    14        if "test" in f or "kustomization" in f or "output" in f or "input" in f:
    15            continue 
    16
    17        file = open(path + "/" + f, 'r')
    18        lines = file.readlines()
    19        totalAliasCount, uniqueAlias = globalAliasFinder(totalAliasCount, uniqueAlias, lines , f)
    20
    21def globalAliasFinder(totalAliasCount, uniqueAlias, lines, filename):
    22
    23    for line in lines:
    24        if "alias" in line or "Alias" in line:
    25            totalAliasCount += 1
    26            linesplit = line.split()
    27            # Example of linesplit results
    28            # ['alias:', 'kured']
    29            key = linesplit[1]
    30
    31            if key in uniqueAlias.keys():
    32                if filename not in uniqueAlias[key][0]:
    33                    uniqueAlias[key][0].append(filename) 
    34                uniqueAlias[key][1] += 1
    35            else:
    36                uniqueAlias[key] = [[filename], 1]
    37    return totalAliasCount, uniqueAlias
    38
    39# # logic for individual file finder
    40# def aliasFinderWithinFile(file):
    41#     global totalAliasCount
    42#     global uniqueAlias
    43#     openedFile = open(file, "r")
    44#     lines = openedFile.readlines()
    45
    46#     # split the file name on "/" so we can extract the file name which will be the last value in the split
    47#     filename = file.split("/")
    48#     totalAliasCount, uniqueAlias = globalAliasFinder(totalAliasCount, uniqueAlias, lines , filename[-1])
    49
    50
    51
    52# Get the list of all files and directories 
    53def main():
    54    path = os.getcwd()
    55
    56    # Directories
    57    containerPath = "/config/pallets/o11y/fluentbit/base/k8s-container"
    58    nodePath = "/config/pallets/o11y/fluentbit/base/k8s-node"
    59    luaPath = "/config/pallets/o11y/fluentbit/base/lua-preprocess"
    60    restrictionPath = "/config/pallets/o11y/fluentbit-restrictions/"
    61
    62
    63    fileOpener(path + containerPath)
    64    fileOpener(path + nodePath)
    65    fileOpener(path + luaPath)
    66    fileOpener(path + restrictionPath)
    67
    68    print(totalAliasCount, " alias found")
    69    print( "---------------")
    70
    71    for key, value in uniqueAlias.items():
    72        if value[1] > 1:
    73            raise Exception(key, " has duplicate alias names (", value[1], " times) . List of files with same name alias: ", value[0]) 
    74        else:
    75            print(key, "is a clean alias")
    76
    77if __name__=="__main__": 
    78    main() 

View as plain text