44import datetime
55import subprocess
66from azure .identity import DefaultAzureCredential
7- from azure .mgmt .resource .resources .models import Deployment ,DeploymentMode
7+ from azure .mgmt .resource .resources .models import Deployment , DeploymentMode
8+
89
910class BaseTest (unittest .TestCase ):
1011
@@ -58,22 +59,81 @@ def deploy_template(self):
5859
5960 def get_git_info (self ):
6061 repo_slug = "SumoLogic/sumologic-azure-function"
61- if os .environ .get ("TRAVIS_EVENT_TYPE" ) == "pull_request" :
62- branch_name = os .environ ["TRAVIS_PULL_REQUEST_BRANCH" ]
63- repo_slug = os .environ ["TRAVIS_PULL_REQUEST_SLUG" ]
64- elif os .environ .get ("TRAVIS_EVENT_TYPE" ) == "push" :
65- branch_name = os .environ ["TRAVIS_BRANCH" ]
66- repo_slug = os .environ ["TRAVIS_REPO_SLUG" ]
67- else :
68- git_cmd = "git rev-parse --abbrev-ref HEAD" # will not work in detached state
69- branch_name = subprocess .Popen (git_cmd , shell = True , stdout = subprocess .PIPE ).stdout .read ().strip ()
62+ try :
63+ branch_name = subprocess .check_output ("git branch --show-current" , stderr = subprocess .STDOUT , shell = True )
64+ if not branch_name :
65+ # in detached head state
66+ branch_name = os .environ ["SOURCE_BRANCH" ]
67+ else :
68+ branch_name = self .branch_name .decode ("utf-8" ).strip ()
69+
70+ except Exception as e :
71+ raise Exception (f"Error getting branch name: { e } " )
7072
71- repo_name = "https://github.com/%s" % (repo_slug )
72- if not branch_name or branch_name == "undefined" or not repo_name :
73- raise Exception ("No branch Found" )
74- print ("Testing for repo %s in branch %s" % (repo_name , branch_name ))
73+ if not branch_name or branch_name == "undefined" or not repo_slug :
74+ raise Exception ("No branch found" )
7575
76- if isinstance (branch_name , bytes ):
77- branch_name = branch_name .decode ()
76+ repo_name = f"https://github.com/{ repo_slug } "
77+
78+ print (f"Testing for repo { repo_name } in branch { branch_name } " )
7879
7980 return repo_name , branch_name
81+
82+ def api_endpoint (self , sumo_deployment ):
83+ if sumo_deployment == "us1" :
84+ return "https://api.sumologic.com/api"
85+ elif sumo_deployment in ["ca" , "au" , "de" , "eu" , "jp" , "us2" , "fed" , "in" ]:
86+ return "https://api.%s.sumologic.com/api" % sumo_deployment
87+ else :
88+ return 'https://%s-api.sumologic.net/api' % sumo_deployment
89+
90+ def create_collector (self , collector_name ):
91+ print ("create_collector start" )
92+ collector_id = None
93+ collector = {
94+ 'collector' : {
95+ 'collectorType' : 'Hosted' ,
96+ 'name' : collector_name ,
97+ 'description' : "" ,
98+ 'category' : None
99+ }
100+ }
101+ try :
102+ resp = self .sumologic_cli .create_collector (collector , headers = None )
103+ collector_id = json .loads (resp .text )['collector' ]['id' ]
104+ print (f"created collector { collector_id } " )
105+ except Exception as e :
106+ raise Exception (e )
107+
108+ return collector_id
109+
110+ def delete_collector (self , collector_id ):
111+ sources = self .sumologic_cli .sources (collector_id , limit = 10 )
112+ if len (sources ) == 0 :
113+ self .sumologic_cli .delete_collector ({"collector" : {"id" : collector_id }})
114+ print (f"deleted collector { collector_id } " )
115+
116+ def create_source (self , collector_id , source_name ):
117+ print ("create_source start" )
118+ endpoint = source_id = None
119+ params = {
120+ "sourceType" : "HTTP" ,
121+ "name" : source_name ,
122+ "messagePerRequest" : False ,
123+ "multilineProcessingEnabled" : True ,
124+ "category" : "AZURE/UnitTest/logs"
125+ }
126+
127+ try :
128+ resp = self .sumologic_cli .create_source (collector_id , {"source" : params })
129+ data = resp .json ()['source' ]
130+ source_id = data ["id" ]
131+ endpoint = data ["url" ]
132+ print (f"created source '{ source_id } ' endpoint '{ endpoint } '" )
133+ except Exception as e :
134+ raise Exception (e )
135+ return source_id , endpoint
136+
137+ def delete_source (self , collector_id , source_id ):
138+ self .sumologic_cli .delete_source (collector_id , {"source" : {"id" : source_id }})
139+ print (f"deleted source { source_id } " )
0 commit comments