a
    !fY                     @   s   d dl Z dZdZddiZddiZdZddd	d
Zdddd
ZdZdZ	dZ
dZdZG dd de jZG dd de jZG dd de jZG dd de jZG dd de jZG dd deZG dd deZdS )     Nz
my-projectz&compute.googleapis.com/instance/uptimeinstance_namez
instance-1z
instance-2Zgce_instancez
us-east1-aZ1234567890123456789)Z
project_idzoneZinstance_idz
us-east1-bZ9876543210987654321ZDELTADOUBLEz2016-04-06T22:05:00.042Zz2016-04-06T22:05:01.042Zz2016-04-06T22:05:02.042Zc                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestAlignerc                 C   s   ddl m} |S )Nr   )Aligner)gcloud.monitoring.queryr   )selfr    r	   ]/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/monitoring/test_query.py_getTargetClass-   s    zTestAligner._getTargetClassc                 C   s   |  t|  d d S )NZ
ALIGN_RATE
assertTruehasattrr   r   r	   r	   r
   test_one1   s    zTestAligner.test_onec                 C   s4   |   jD ]$}|ds
| t|   || q
d S N_r   __dict__
startswithassertEqualgetattrr   namer	   r	   r
   
test_names4   s    
zTestAligner.test_namesN__name__
__module____qualname__r   r   r   r	   r	   r	   r
   r   +   s   r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestReducerc                 C   s   ddl m} |S )Nr   )Reducer)r   r    )r   r    r	   r	   r
   r   <   s    zTestReducer._getTargetClassc                 C   s   |  t|  d d S )NZREDUCE_PERCENTILE_99r   r   r	   r	   r
   r   @   s    zTestReducer.test_onec                 C   s4   |   jD ]$}|ds
| t|   || q
d S r   r   r   r	   r	   r
   r   D   s    
zTestReducer.test_namesNr   r	   r	   r	   r
   r   :   s   r   c                   @   s   e Zd Zdd Zdd Zedd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)S )*	TestQueryc                 C   s   ddl m} |S )Nr   )Query)r   r"   )r   r"   r	   r	   r
   r   L   s    zTestQuery._getTargetClassc                 O   s   |   |i |S Nr   )r   argskwargsr	   r	   r
   _makeOneP   s    zTestQuery._makeOnec                 C   s   ddl m} || S )Nr   )_datetime_to_rfc3339)Zgcloud._helpersr(   )valuer(   r	   r	   r
   _make_timestampS   s    zTestQuery._make_timestampc                 C   s   t tt d}| |}| |j| | |jj|  j	 | 
|j | 
|j | 
|j | 
|j | 
|j | |jd d S )Nproject
connectionr	   )_ClientPROJECT_Connectionr'   r   _client_filtermetric_typer   ZDEFAULT_METRIC_TYPEassertIsNone_start_time	_end_time_per_series_aligner_alignment_period_seconds_cross_series_reducer_group_by_fieldsr   clientqueryr	   r	   r
   test_constructor_minimalX   s    

z"TestQuery.test_constructor_minimalc           	      C   s   dd l }| dddddd}d\}}}||j|||d }ttt d	}| j|t||||d
}| |j| | |j	j
t | |j| | |j| | |j | |j | |j | |jd d S )Nr                 )   rB      )dayshoursminutesr+   )end_timerF   rG   rH   r	   )datetime	timedeltar.   r/   r0   r'   METRIC_TYPEr   r1   r2   r3   r5   r6   r4   r7   r8   r9   r:   )	r   rJ   T1DAYSZHOURSMINUTEST0r<   r=   r	   r	   r
   test_constructor_maximalh   s"    
z"TestQuery.test_constructor_maximalc           	   
      s   dd l }ddlm} ddlm} d}| ddddd	d	| ddddd
d| ddddd	dg\ }}ttt d}|| fddd  | j|t	|d}W d    n1 s0    Y  | 
|j| | 
|j| d S )Nr   )_Monkey)r=      r?   r@   rA   rB   rC      r+   c                      s    S r#   r	   r	   ZNOWr	   r
   <lambda>       z=TestQuery.test_constructor_default_end_time.<locals>.<lambda>)Z_UTCNOW)rH   )rJ   Zgcloud._testingrR   Zgcloud.monitoringr=   r.   r/   r0   r'   rL   r   r5   r6   )	r   rJ   rR   ZMUTrO   rP   rM   r<   r=   r	   rU   r
   !test_constructor_default_end_time   s    
.z+TestQuery.test_constructor_default_end_timec                 C   sh   dd l }| dddddd}ttt d}| t  | j|t|d W d    n1 sZ0    Y  d S )	Nr   r?   r@   rA   rB   rC   r+   rI   )rJ   r.   r/   r0   assertRaises
ValueErrorr'   rL   )r   rJ   rM   r<   r	   r	   r
   )test_constructor_nonzero_duration_illegal   s
    z3TestQuery.test_constructor_nonzero_duration_illegalc                 C   sP   t tt d}| |t}| t t| W d    n1 sB0    Y  d S Nr+   )r.   r/   r0   r'   rL   rZ   r[   listr;   r	   r	   r
   'test_execution_without_interval_illegal   s    z1TestQuery.test_execution_without_interval_illegalc                 C   s,   t tt d}| |t}| |jt d S r]   )r.   r/   r0   r'   rL   r   r3   r;   r	   r	   r
   test_metric_type   s    zTestQuery.test_metric_typec                 C   s8   t tt d}| |t}djtd}| |j| d S )Nr+   metric.type = "{type}"type)r.   r/   r0   r'   rL   formatr   filter)r   r<   r=   expectedr	   r	   r
   test_filter   s    zTestQuery.test_filterc                 C   sH   d}t tt d}| |t}||}djt|d}| |j| d S )N1234567r+   z/metric.type = "{type}" AND group.id = "{group}")rc   group)	r.   r/   r0   r'   rL   Zselect_grouprd   r   re   )r   ZGROUPr<   r=   rf   r	   r	   r
   test_filter_by_group   s    
zTestQuery.test_filter_by_groupc                 C   sP   d\}}t tt d}| |t}|||}djt||d}| |j| d S )Nz	project-1z	project-2r+   zKmetric.type = "{type}" AND project = "{project1}" OR project = "{project2}")rc   Zproject1Zproject2)	r.   r/   r0   r'   rL   Zselect_projectsrd   r   re   )r   ZPROJECT1ZPROJECT2r<   r=   rf   r	   r	   r
   test_filter_by_projects   s    z!TestQuery.test_filter_by_projectsc                 C   sJ   d}t tt d}| |t}|j|d}djt|d}| |j| d S )Nzeurope-r+   )Zzone_prefixzHmetric.type = "{type}" AND resource.label.zone = starts_with("{prefix}"))rc   prefix)	r.   r/   r0   r'   rL   select_resourcesrd   r   re   )r   ZZONE_PREFIXr<   r=   rf   r	   r	   r
   test_filter_by_resources   s    z"TestQuery.test_filter_by_resourcesc                 C   sJ   d}t tt d}| |t}|j|d}djt|d}| |j| d S )Nzmy-instancer+   )r   zDmetric.type = "{type}" AND metric.label.instance_name = "{instance}")rc   instance)	r.   r/   r0   r'   rL   select_metricsrd   r   re   )r   ZINSTANCEr<   r=   rf   r	   r	   r
   test_filter_by_metrics   s    z TestQuery.test_filter_by_metricsc                 C   s|   dd l }| dddddd}ttt d}| |t}|j|d}t| }d	d
j	tdfd| 
|fg}| || d S )Nr   r?   r@   rA   rB   rC   r+   rY   re   ra   rb   interval.endTime)rJ   r.   r/   r0   r'   rL   select_intervalr^   _build_query_paramsrd   r*   r   )r   rJ   rM   r<   r=   actualrf   r	   r	   r
   test_request_parameters_minimal   s    z)TestQuery.test_request_parameters_minimalc                 C   s  dd l }| dddddd}| dddddd}d}d\}}}d	}d
\}	}
d}d}ttt d}| |t}|j||d}|j|||d}|||	|
}t	|j
d||d}ddjtdfd| |fd| |fd|fd|fd|fd|	fd|
fdd|fd|fg}| || d S )Nr   r?   r@   rA   rB   rC   ZALIGN_DELTA)rD   rC   Z90sZREDUCE_MEAN)zresource.zonezmetric.instance_named   second-page-pleaser+   
start_timerI   )rH   secondsT)headers_onlyZ	page_sizeZ
page_tokenre   ra   rb   rs   interval.startTimezaggregation.perSeriesAlignerzaggregation.alignmentPeriodzaggregation.crossSeriesReducerzaggregation.groupByFieldsviewHEADERSZpageSize	pageToken)rJ   r.   r/   r0   r'   rL   rt   alignreducer^   ru   rd   r*   r   )r   rJ   rP   rM   ZALIGNERrO   ZSECONDSZPERIODZREDUCERZFIELD1ZFIELD2Z	PAGE_SIZEZ
PAGE_TOKENr<   r=   rv   rf   r	   r	   r
   test_request_parameters_maximal   s>    
z)TestQuery.test_request_parameters_maximalc                 C   s  dd l }| dddddd}| dddddd}ttd}ttd}d	}d
}ttdttdtt	|d|id|d|idgd}tt
dttdtt	|d|id|d|idgd}	d||	gi}
t|
}tt|d}| |t}|j||d}t|}| t|d |\}}| |jjt | |jjt
 | |jjt | |jjt | dd |jD ||g | dd |jD ||g | dd |jD ttg | dd |jD ttg ddjtdddjtdfd| |fd| |fgd }|j\}| || d S )!Nr   r?   r@         rS   
   Z	startTimeZendTime<   㥛  N@rc   labelsdoubleValueintervalr)   metricresource
metricKind	valueTypepoints
timeSeriesr+   rz   rB   c                 S   s   g | ]
}|j qS r	   r)   .0pr	   r	   r
   
<listcomp><  rW   z,TestQuery.test_iteration.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r	   r   r   r	   r	   r
   r   =  rW   c                 S   s   g | ]
}|j qS r	   rY   r   r	   r	   r
   r   >  rW   c                 S   s   g | ]
}|j qS r	   rY   r   r	   r	   r
   r   ?  rW   GET/projects/{project}/timeSeries/r,   re   ra   rb   rs   r~   methodpathquery_params)rJ   TS0TS1TS2rL   METRIC_LABELSRESOURCE_TYPERESOURCE_LABELSMETRIC_KIND
VALUE_TYPEMETRIC_LABELS2RESOURCE_LABELS2r0   r.   r/   r'   rt   r^   r   lenr   r   r   r   rd   r*   
_requested)r   rJ   rP   rM   	INTERVAL1	INTERVAL2VALUE1VALUE2SERIES1SERIES2RESPONSEr-   r<   r=   responseseries1series2expected_requestrequestr	   r	   r
   test_iteration  s`    



zTestQuery.test_iterationc                 C   s  dd l }dd l}ddlm} |dddddd}|dddddd}ttd	}ttd	}d
}d}	ttdt	t
dtt|d|id|d|idgd}
ttdt	tdtt|d|	idgd}ttdt	tdtt|d|	idgd}d}|
|g|d}d|gi}t||}tt|d}| |t}|j||d}t|}| t|d |\}}| |jjt | |jjt | |jjt
 | |jjt | dd |jD ||g | dd |jD |	|	g | dd |jD ttg | dd |jD ttg ddjtdddjtd fd!| |fd"| |fgd#}||}|d$ d%|f |j\}}| || | || |  | t| W d    n1 s0    Y  d S )&Nr   NotFoundr?   r@   r   r   rS   r   r   r   r   r   r   r   r   ry   )r   ZnextPageTokenr   r+   rz   rB   c                 S   s   g | ]
}|j qS r	   r   r   r	   r	   r
   r     rW   z2TestQuery.test_iteration_paged.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r	   r   r   r	   r	   r
   r     rW   c                 S   s   g | ]
}|j qS r	   rY   r   r	   r	   r
   r     rW   c                 S   s   g | ]
}|j qS r	   rY   r   r	   r	   r
   r     rW   r   r   r   re   ra   rb   rs   r~   r   r   r   )!copyrJ   gcloud.exceptionsr   r   r   r   rL   r   r   r   r   r   r   r   r0   r.   r/   r'   rt   r^   r   r   r   r   r   r   rd   r*   deepcopyappendr   rZ   )r   r   rJ   r   rP   rM   r   r   r   r   r   ZSERIES2_PART1ZSERIES2_PART2ZTOKENZ	RESPONSE1Z	RESPONSE2r-   r<   r=   r   r   r   Zexpected_request1Zexpected_request2Zrequest1Zrequest2r	   r	   r
   test_iteration_pagedN  s    









zTestQuery.test_iteration_pagedc           
      C   s   dd l }| dddddd}| dddddd}ti }tt|d}| |t}|j||d	}t|}| t	|d d
dj
tdddj
tdfd| |fd| |fgd}|j\}	| |	| d S )Nr   r?   r@   r   r   rS   r   r+   rz   r   r   r   re   ra   rb   rs   r~   r   )rJ   r0   r.   r/   r'   rL   rt   r^   r   r   rd   r*   r   )
r   rJ   rP   rM   r-   r<   r=   r   r   r   r	   r	   r
   test_iteration_empty  s$    
	zTestQuery.test_iteration_emptyc                 C   sv  dd l }| dddddd}| dddddd}ttdttdttd	}ttdttdttd	}d
||gi}t	|}t
t|d}| |t}	|	j||d}	t|	jdd}
| t|
d |
\}}| |jjt | |jjt | |jjt | |jjt | |jg  | |jg  ddjtdddjtdfd| |fd| |fdgd}|j\}| || d S )Nr   r?   r@   r   r   rS   r   r   )r   r   r   r   r   r+   rz   T)r}   rB   r   r   r   re   ra   rb   rs   r~   r   r   )rJ   rL   r   r   r   r   r   r   r   r0   r.   r/   r'   rt   r^   iterr   r   r   r   r   r   rd   r*   r   )r   rJ   rP   rM   r   r   r   r-   r<   r=   r   r   r   r   r   r	   r	   r
   test_iteration_headers_only  sJ    
z%TestQuery.test_iteration_headers_onlyN)r   r   r   r   r'   staticmethodr*   r>   rQ   rX   r\   r_   r`   rg   rj   rl   ro   rr   rw   r   r   r   r   r   r	   r	   r	   r
   r!   J   s*   
&CWr!   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
Test_Filterc                 C   s   ddl m} |S )Nr   )_Filter)r   r   )r   r   r	   r	   r
   r     s    zTest_Filter._getTargetClassc                 C   s   |   |S r#   r$   )r   r3   r	   r	   r
   r'     s    zTest_Filter._makeOnec                 C   s*   |  t}djtd}| t|| d S )Nra   rb   )r'   rL   rd   r   strr   objrf   r	   r	   r
   test_minimal  s    
zTest_Filter.test_minimalc                 C   sP   |  t}d|_d|_|jddd |jdd djtd	}| t|| d S )
Nrh   rk   zsome-resourcefoo)resource_typeZresource_labelzbar-)Zmetric_label_prefixzmetric.type = "{type}" AND group.id = "1234567" AND project = "project-1" OR project = "project-2" AND resource.label.resource_label = "foo" AND resource.type = "some-resource" AND metric.label.metric_label = starts_with("bar-")rb   )	r'   rL   Zgroup_idZprojectsrn   rq   rd   r   r   r   r	   r	   r
   test_maximal  s    
	zTest_Filter.test_maximalN)r   r   r   r   r'   r   r   r	   r	   r	   r
   r     s   r   c                   @   sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )Test__build_label_filterc                 O   s   ddl m} ||i |S )Nr   )_build_label_filter)r   r   )r   r%   r&   r   r	   r	   r
   _callFUT  s    z!Test__build_label_filter._callFUTc                 C   s   |  | dd d S )Nr    r   r   r   r	   r	   r
   test_no_labels  s    z'Test__build_label_filter.test_no_labelsc                 C   s   |  | jdd dd d S )Nr   )r   r   r   r   r	   r	   r
   test_label_is_none  s    z+Test__build_label_filter.test_label_is_nonec                 C   s&   | j ddddd}d}| || d S )Nr   a--bxyzZalpha_prefixZbeta_gamma_suffixZdelta_epsilonz{metric.label.alpha = starts_with("a-") AND metric.label.beta_gamma = ends_with("-b") AND metric.label.delta_epsilon = "xyz"r   r   r   rv   rf   r	   r	   r
   test_metric_labels"  s    z+Test__build_label_filter.test_metric_labelsc                 C   s&   | j ddddd}d}| || d S )Nr   r   r   r   r   resource.label.alpha = starts_with("a-") AND resource.label.beta_gamma = ends_with("-b") AND resource.label.delta_epsilon = "xyz"r   r   r	   r	   r
   test_resource_labels0  s    z-Test__build_label_filter.test_resource_labelsc                 C   s$   |  dddd}d}| || d S )Nr   z(resource.label.alpha = starts_with("a-")z+resource.label.beta_gamma = ends_with("-b")z$resource.label.delta_epsilon = "xyz"r   r   r   r	   r	   r
   test_raw_label_filters>  s    z/Test__build_label_filter.test_raw_label_filtersc                 C   s"   | j ddd}d}| || d S )Nr   r   )r   zresource.type = "foo"r   r   r	   r	   r
   test_resource_typeL  s    z+Test__build_label_filter.test_resource_typec                 C   s"   | j ddd}d}| || d S )Nr   zfoo-)Zresource_type_prefixz#resource.type = starts_with("foo-")r   r   r	   r	   r
   test_resource_type_prefixQ  s    z2Test__build_label_filter.test_resource_type_prefixc                 C   s"   | j ddd}d}| || d S )Nr   z-foo)Zresource_type_suffixz!resource.type = ends_with("-foo")r   r   r	   r	   r
   test_resource_type_suffixV  s    z2Test__build_label_filter.test_resource_type_suffixN)r   r   r   r   r   r   r   r   r   r   r   r   r	   r	   r	   r
   r     s   r   c                   @   s   e Zd Zdd Zdd ZdS )r0   c                 G   s   t || _g | _d S r#   )r^   
_responsesr   )r   	responsesr	   r	   r
   __init__^  s    
z_Connection.__init__c                 K   sF   ddl m} | j| z| jdW S  ty@   |dY n0 d S )Nr   r   Zmiss)r   r   r   r   r   pop
IndexError)r   r&   r   r	   r	   r
   api_requestb  s    z_Connection.api_requestN)r   r   r   r   r   r	   r	   r	   r
   r0   \  s   r0   c                   @   s   e Zd Zdd ZdS )r.   c                 C   s   || _ || _d S r#   r+   )r   r,   r-   r	   r	   r
   r   m  s    z_Client.__init__N)r   r   r   r   r	   r	   r	   r
   r.   k  s   r.   )Z	unittest2r/   rL   r   r   r   r   r   r   r   r   r   r   ZTestCaser   r   r!   r   r   objectr0   r.   r	   r	   r	   r
   <module>   s8      -"F