Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.VisibleForTesting;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.inject.Inject;
Expand Down Expand Up @@ -320,6 +321,12 @@ public HostImpl(@Assisted HostEntity hostEntity, Gson gson, HostDAO hostDAO, Hos
maintMap = ensureMaintMap(hostEntity.getHostStateEntity());
}

@VisibleForTesting
HostImpl(HostEntity hostEntity, Gson gson, HostDAO hostDAO, HostStateDAO hostStateDAO, AmbariEventPublisher publisher) {
this(hostEntity, gson, hostDAO, hostStateDAO);
this.ambariEventPublisher = publisher;
}

@Override
public int compareTo(Object o) {
if ((o != null ) && (o instanceof Host)) {
Expand Down Expand Up @@ -957,18 +964,27 @@ public void setTimeInState(long timeInState) {
}
}


@Override
public String getStatus() {
return status;
}

/**
* Sets the Host's status.
*
* @param status Host Status string representation of the Host's status
* @throws IllegalArgumentException if <code>status</code> is <code>null</code> or {@link HealthStatus} enum has no such constant name.
*/
@Override
public void setStatus(String status) {
if (this.status != status) {
ambariEventPublisher.publish(new HostStatusUpdateEvent(getHostName(), status));
if (status == null) {
throw new IllegalArgumentException("Host status cannot be null");
}
String newStatus = HealthStatus.valueOf(status).name();
if (!newStatus.equals(this.status)) {
ambariEventPublisher.publish(new HostStatusUpdateEvent(getHostName(), newStatus));
this.status = newStatus;
}
this.status = status;
}

@Override
Expand Down Expand Up @@ -1323,8 +1339,8 @@ public boolean isRepositoryVersionCorrect(RepositoryVersionEntity repositoryVers
// for every host component, if it matches the desired repo and has reported
// the correct version then we're good
for (HostComponentStateEntity hostComponentState : hostComponentStates) {
ServiceComponentDesiredStateEntity desiredComponmentState = hostComponentState.getServiceComponentDesiredStateEntity();
RepositoryVersionEntity desiredRepositoryVersion = desiredComponmentState.getDesiredRepositoryVersion();
ServiceComponentDesiredStateEntity desiredComponentState = hostComponentState.getServiceComponentDesiredStateEntity();
RepositoryVersionEntity desiredRepositoryVersion = desiredComponentState.getDesiredRepositoryVersion();

ComponentInfo componentInfo = ambariMetaInfo.getComponent(
desiredRepositoryVersion.getStackName(), desiredRepositoryVersion.getStackVersion(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,24 @@
*/
package org.apache.ambari.server.state.host;

import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.junit.Assert.assertEquals;

import java.util.Map;


import org.apache.ambari.server.events.HostStatusUpdateEvent;
import org.apache.ambari.server.events.publishers.AmbariEventPublisher;
import org.apache.ambari.server.orm.dao.HostDAO;
import org.apache.ambari.server.orm.dao.HostStateDAO;
import org.apache.ambari.server.orm.entities.HostEntity;
import org.apache.ambari.server.orm.entities.HostStateEntity;

import org.apache.ambari.server.state.Host;
import org.apache.ambari.server.state.HostHealthStatus;

import org.easymock.EasyMockSupport;
import org.junit.Test;

Expand Down Expand Up @@ -66,7 +75,7 @@ public void testGetHostAttributes() throws Exception {
}

@Test
public void testGetHealthStatus() throws Exception {
public void testGetHealthStatus() {

HostEntity hostEntity = createNiceMock(HostEntity.class);
HostStateEntity hostStateEntity = createNiceMock(HostStateEntity.class);
Expand All @@ -93,4 +102,62 @@ public void testGetHealthStatus() throws Exception {

verifyAll();
}

@Test(expected = IllegalArgumentException.class)
public void testSetStatusWithNull() {
HostEntity hostEntity = createNiceMock(HostEntity.class);
HostDAO hostDAO = createNiceMock(HostDAO.class);
HostStateDAO hostStateDAO = createNiceMock(HostStateDAO.class);

expect(hostEntity.getHostId()).andReturn(1L).anyTimes();
replayAll();

Host host = new HostImpl(hostEntity, new Gson(), hostDAO, hostStateDAO);
host.setStatus(null);
}

@Test(expected = IllegalArgumentException.class)
public void testSetStatusWithIllegalState() {
HostEntity hostEntity = createNiceMock(HostEntity.class);
HostDAO hostDAO = createNiceMock(HostDAO.class);
HostStateDAO hostStateDAO = createNiceMock(HostStateDAO.class);

expect(hostEntity.getHostId()).andReturn(1L).anyTimes();
replayAll();

Host host = new HostImpl(hostEntity, new Gson(), hostDAO, hostStateDAO);
host.setStatus("illegal status");
}

@Test
public void testSetStatusWithValidArgs() {
HostEntity hostEntity = createNiceMock(HostEntity.class);
HostDAO hostDAO = createNiceMock(HostDAO.class);
HostStateDAO hostStateDAO = createNiceMock(HostStateDAO.class);
AmbariEventPublisher eventPublisher = createNiceMock(AmbariEventPublisher.class);

expect(hostEntity.getHostName()).andReturn("host1").anyTimes();
expect(hostEntity.getHostId()).andReturn(1L).anyTimes();

eventPublisher.publish(anyObject(HostStatusUpdateEvent.class));
expectLastCall().times(4);

replayAll();

Host host = new HostImpl(hostEntity, new Gson(), hostDAO, hostStateDAO, eventPublisher);

host.setStatus(HostHealthStatus.HealthStatus.UNHEALTHY.name());
assertEquals(HostHealthStatus.HealthStatus.UNHEALTHY.name(), host.getStatus());

host.setStatus(HostHealthStatus.HealthStatus.HEALTHY.name());
assertEquals(HostHealthStatus.HealthStatus.HEALTHY.name(), host.getStatus());

host.setStatus(HostHealthStatus.HealthStatus.ALERT.name());
assertEquals(HostHealthStatus.HealthStatus.ALERT.name(), host.getStatus());

host.setStatus(HostHealthStatus.HealthStatus.UNKNOWN.name());
assertEquals(HostHealthStatus.HealthStatus.UNKNOWN.name(), host.getStatus());

verifyAll();
}
}