@BeforeMethod public void setup() throws Exception { when(configurationHolder.getAgentConfiguration()).thenReturn(agentConfiguration); when(configurationHolder.getEnvironment()).thenReturn(environment); when(configurationHolder.getInstrumentationAppliers()).thenReturn(Collections.singletonList(holdedInstrumentationApplier)); when(agentCacheEntry.getConfigurationHolder()).thenReturn(configurationHolder); when(agentCacheEntry.getClassCache()).thenReturn(classCache); when(agentCacheEntry.getId()).thenReturn(PLATFORM_ID); when(classCache.getInstrumentationService()).thenReturn(instrumentationService); when(classType.isClass()).thenReturn(true); when(classType.castToClass()).thenReturn(immutableClassType); when(classType.getFQN()).thenReturn("fqn"); when(event.getAfter()).thenReturn(updateEnvironment); }
@BeforeMethod public void init() throws Exception { MockitoAnnotations.initMocks(this); job.log = LoggerFactory.getLogger(EnvironmentMappingUpdateJob.class); job.setAgentCacheEntry(agentCacheEntry); when(configurationHolder.getEnvironment()).thenReturn(environment); when(configurationHolder.getInstrumentationAppliers()).thenReturn(Collections.singletonList(holdedInstrumentationApplier)); when(agentCacheEntry.getConfigurationHolder()).thenReturn(configurationHolder); when(agentCacheEntry.getClassCache()).thenReturn(classCache); when(agentCacheEntry.getId()).thenReturn(PLATFORM_ID); when(classCache.getInstrumentationService()).thenReturn(instrumentationService); when(classCache.getLookupService()).thenReturn(lookupService); when(classType.isClass()).thenReturn(true); when(classType.castToClass()).thenReturn(immutableClassType); }
@BeforeMethod public void setup() throws Exception { when(configurationHolder.getAgentConfiguration()).thenReturn(agentConfiguration); when(configurationHolder.getEnvironment()).thenReturn(environment); when(configurationHolder.getInstrumentationAppliers()).thenReturn(Collections.singletonList(holdedInstrumentationApplier)); when(agentCacheEntry.getConfigurationHolder()).thenReturn(configurationHolder); when(agentCacheEntry.getClassCache()).thenReturn(classCache); when(agentCacheEntry.getId()).thenReturn(10L); when(classCache.getInstrumentationService()).thenReturn(instrumentationService); when(classTypeOne.isClass()).thenReturn(true); when(classTypeTwo.isClass()).thenReturn(true); when(classTypeOne.castToClass()).thenReturn(immutableClassTypeOne); when(classTypeTwo.castToClass()).thenReturn(immutableClassTypeTwo); when(immutableClassTypeOne.hasInstrumentationPoints()).thenReturn(true); when(immutableClassTypeOne.hasInstrumentationPoints()).thenReturn(false); when(classTypeOne.getFQN()).thenReturn("fqnOne"); when(classTypeTwo.getFQN()).thenReturn("fqnTwo"); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void yesEnvironment() { when(configurationHolder.isInitialized()).thenReturn(true); when(configurationHolder.getAgentConfiguration()).thenReturn(updateConfiguration); doReturn(Collections.singletonList(classType)).when(instrumentationService).addInstrumentationPoints(any(AgentConfig.class), any(Collection.class)); job.setEnvironment(updateEnvironment); job.run(); verify(instrumentationService, times(1)).removeInstrumentationPoints(); verify(configurationHolder, times(1)).update(updateEnvironment, PLATFORM_ID); Collection<IInstrumentationApplier> appliers = configurationHolder.getInstrumentationAppliers(); verify(instrumentationService, times(1)).addInstrumentationPoints(updateConfiguration, appliers); ArgumentCaptor<Collection> typeCaptor = ArgumentCaptor.forClass(Collection.class); verify(instrumentationService).getInstrumentationResults(typeCaptor.capture()); assertThat((Collection<Type>) typeCaptor.getValue(), hasItems((Type) classType)); verify(eventPublisher).publishEvent(any(ClassInstrumentationChangedEvent.class)); verifyNoMoreInteractions(instrumentationService, updateConfiguration, updateEnvironment, eventPublisher); } }
@Test public void removedAssignment() throws RemoteException, BusinessException { Collection<ClassType> types = Collections.singleton(classType); doReturn(instrumentationApplier).when(configurationResolver).getInstrumentationApplier(sensorAssignment, environment); doReturn(types).when(classCacheSearchNarrower).narrowByClassSensorAssignment(classCache, sensorAssignment); doReturn(types).when(instrumentationService).removeInstrumentationPoints(eq(types), Matchers.<Collection<IInstrumentationApplier>> any()); doReturn(Collections.singletonList(sensorAssignment)).when(event).getRemovedSensorAssignments(functionalAssignmentFactory); job.setEnvironmentUpdateEvent(event); job.run(); verify(configurationHolder, times(1)).update(updateEnvironment, PLATFORM_ID); verify(instrumentationService, times(1)).removeInstrumentationPoints(types, Collections.singleton(instrumentationApplier)); ArgumentCaptor<Collection> captor = ArgumentCaptor.forClass(Collection.class); Collection<IInstrumentationApplier> appliers = configurationHolder.getInstrumentationAppliers(); verify(instrumentationService, times(1)).addInstrumentationPoints(captor.capture(), eq(agentConfiguration), eq(appliers)); assertThat((Collection<ClassType>) captor.getValue(), hasSize(1)); assertThat(((Collection<ClassType>) captor.getValue()).iterator().next(), is(classType)); ArgumentCaptor<ClassInstrumentationChangedEvent> eventCaptor = ArgumentCaptor.forClass(ClassInstrumentationChangedEvent.class); verify(eventPublisher).publishEvent(eventCaptor.capture()); assertThat(eventCaptor.getValue().getAgentId(), is(equalTo(PLATFORM_ID))); assertThat(eventCaptor.getValue().getInstrumentationDefinitions(), contains(org.hamcrest.Matchers.<InstrumentationDefinition> hasProperty("className", equalTo("fqn")))); verifyNoMoreInteractions(eventPublisher); verifyZeroInteractions(environment, agentConfiguration); }
@Test public void existingClassTypeInstrumented() throws BusinessException, ClassCacheModificationException { List<String> definedIPs = mock(List.class); String agentName = "agentName"; String version = "v1"; when(registrationService.registerPlatformIdent(definedIPs, agentName, version)).thenReturn(ID); manager.register(definedIPs, agentName, version); ClassType classType = mock(ClassType.class); when(classType.isClass()).thenReturn(true); when(classType.castToClass()).thenReturn(classType); when(lookupService.findByHash(HASH)).thenReturn(classType); when(configurationHolder.isInitialized()).thenReturn(true); AgentConfig configuration = mock(AgentConfig.class); Collection<IInstrumentationApplier> appliers = mock(Collection.class); InstrumentationDefinition instrumentationResult = mock(InstrumentationDefinition.class); when(configurationHolder.getAgentConfiguration()).thenReturn(configuration); when(configurationHolder.getInstrumentationAppliers()).thenReturn(appliers); when(instrumentationService.addAndGetInstrumentationResult(classType, configuration, appliers)).thenReturn(instrumentationResult); InstrumentationDefinition result = manager.analyze(ID, HASH, type); assertThat(result, is(instrumentationResult)); verify(instrumentationService).addAndGetInstrumentationResult(classType, configuration, appliers); verifyNoMoreInteractions(instrumentationService); verifyZeroInteractions(modificationService); }
@Test public void update() { long platformId = 11; Environment environment = mock(Environment.class); AgentConfig configuration = mock(AgentConfig.class); IInstrumentationApplier applier = mock(IInstrumentationApplier.class); JmxMonitoringApplier jmxApplier = mock(JmxMonitoringApplier.class); when(configurationCreator.environmentToConfiguration(environment, platformId)).thenReturn(configuration); when(configurationResolver.getInstrumentationAppliers(environment)).thenReturn(Collections.singleton(applier)); when(configurationResolver.getJmxMonitoringAppliers(environment)).thenReturn(Collections.singleton(jmxApplier)); holder.update(environment, platformId); assertThat(holder.isInitialized(), is(true)); assertThat(holder.getEnvironment(), is(environment)); assertThat(holder.getAgentConfiguration(), is(configuration)); assertThat(holder.getInstrumentationAppliers(), hasSize(1)); assertThat(holder.getInstrumentationAppliers(), hasItem(applier)); assertThat(holder.getJmxMonitoringAppliers(), hasSize(1)); assertThat(holder.getJmxMonitoringAppliers(), hasItem(jmxApplier)); verify(configurationCreator).environmentToConfiguration(environment, platformId); verify(configurationResolver).getInstrumentationAppliers(environment); verify(configurationResolver).getJmxMonitoringAppliers(environment); verifyNoMoreInteractions(configurationCreator, configurationResolver); }
@Test public void removedAssignment() throws RemoteException { Collection<ClassType> types = ImmutableList.of(classTypeOne, classTypeTwo); doReturn(instrumentationApplier).when(configurationResolver).getInstrumentationApplier(sensorAssignment, environment); doReturn(types).when(classCacheSearchNarrower).narrowByClassSensorAssignment(classCache, sensorAssignment); doReturn(types).when(instrumentationService).removeInstrumentationPoints(eq(types), Matchers.<Collection<IInstrumentationApplier>> any()); doReturn(Collections.singleton(sensorAssignment)).when(event).getRemovedSensorAssignments(); job.setProfileUpdateEvent(event); job.run(); ArgumentCaptor<Collection> captor = ArgumentCaptor.forClass(Collection.class); verify(instrumentationService, times(1)).removeInstrumentationPoints(eq(types), captor.capture()); assertThat((Collection<IInstrumentationApplier>) captor.getValue(), hasSize(1)); assertThat(((Collection<IInstrumentationApplier>) captor.getValue()).iterator().next(), is(instrumentationApplier)); ArgumentCaptor<Collection> typeCaptor = ArgumentCaptor.forClass(Collection.class); verify(instrumentationService).getInstrumentationResults(typeCaptor.capture()); assertThat((Collection<ClassType>) typeCaptor.getValue(), hasItems(classTypeOne, classTypeTwo)); Collection<IInstrumentationApplier> appliers = configurationHolder.getInstrumentationAppliers(); verify(instrumentationService, times(1)).addInstrumentationPoints(captor.capture(), eq(agentConfiguration), eq(appliers)); assertThat((Collection<ClassType>) captor.getValue(), hasSize(2)); assertThat(((Collection<ClassType>) captor.getValue()).iterator().next(), is(classTypeOne)); ArgumentCaptor<ClassInstrumentationChangedEvent> eventCaptor = ArgumentCaptor.forClass(ClassInstrumentationChangedEvent.class); verify(eventPublisher).publishEvent(eventCaptor.capture()); assertThat(eventCaptor.getValue().getAgentId(), is(equalTo(10L))); Matcher<InstrumentationDefinition> matcherOne = org.hamcrest.Matchers.<InstrumentationDefinition> hasProperty("className", equalTo("fqnOne")); Matcher<InstrumentationDefinition> matcherTwo = org.hamcrest.Matchers.<InstrumentationDefinition> hasProperty("className", equalTo("fqnTwo")); assertThat(eventCaptor.getValue().getInstrumentationDefinitions(), hasItems(matcherOne, matcherTwo)); verifyNoMoreInteractions(instrumentationService, eventPublisher); verifyZeroInteractions(environment); }
/** * Process the removed assignments. All instrumentation points affected by the any of these * assignments are first completely removed. All classes that have any point removed will be * re-analyzed against complete configuration in order to reset the possible points coming not * from removed assignments. * * @param classSensorAssignments * Collection of removed {@link AbstractClassSensorAssignment}s. * @return Returns a {@link Collection} of {@link ImmutableClassType} which have been removed. */ protected Collection<ImmutableClassType> processRemovedAssignments(Collection<? extends AbstractClassSensorAssignment<?>> classSensorAssignments) { Collection<ImmutableClassType> changedClassTypes = new ArrayList<>(); // process all class sensor assignments for removal for (AbstractClassSensorAssignment<?> assignment : classSensorAssignments) { // narrow the search Collection<? extends ImmutableClassType> classTypes = classCacheSearchNarrower.narrowByClassSensorAssignment(getClassCache(), assignment); // get the applier IInstrumentationApplier instrumentationApplier = configurationResolver.getInstrumentationApplier(assignment, getEnvironment()); changedClassTypes.addAll(getClassCache().getInstrumentationService().removeInstrumentationPoints(classTypes, Collections.singleton(instrumentationApplier))); } // if no class was affected just return if (CollectionUtils.isNotEmpty(changedClassTypes)) { // if any class was affected re-check those classes against complete configuration // because we removed all instrumentation points Collection<IInstrumentationApplier> instrumentationAppliers = getConfigurationHolder().getInstrumentationAppliers(); getClassCache().getInstrumentationService().addInstrumentationPoints(changedClassTypes, getAgentConfiguration(), instrumentationAppliers); } return changedClassTypes; }
/** * {@inheritDoc} */ @Override protected Collection<ImmutableType> execute() { Collection<ImmutableType> changedClassTypes = new HashSet<>(); // add current instrumentation changedClassTypes.addAll(getClassCache().getLookupService().findInstrumentedTypes()); // first remove all existing instrumentation points getClassCache().getInstrumentationService().removeInstrumentationPoints(); // then update configuration holder ConfigurationHolder configurationHolder = getConfigurationHolder(); configurationHolder.update(environment, getAgentId()); // if we are initialized analyze the complete class cache if (configurationHolder.isInitialized()) { // then add instrumentation points Collection<IInstrumentationApplier> instrumentationAppliers = configurationHolder.getInstrumentationAppliers(); Collection<? extends ImmutableClassType> instrumentedTypes = getClassCache().getInstrumentationService().addInstrumentationPoints(getAgentConfiguration(), instrumentationAppliers); changedClassTypes.addAll(instrumentedTypes); } return changedClassTypes; }
return classCache.getInstrumentationService().addAndGetInstrumentationResult(classType, configurationHolder.getAgentConfiguration(), configurationHolder.getInstrumentationAppliers());