@Test
  public void testUnregisterHostFail() throws Exception {
    UnregisterHostRequest request = new UnregisterHostRequest();
    request.setId("host1");

    doThrow(new Exception()).when(configDict).write("host1", null);
    UnregisterHostResponse response = service.unregister_host(request);
    assertThat(response.getResult(), is(UnregisterHostResultCode.NOT_IN_MAJORITY));
  }
  @Test
  public void testUnregisterHost() throws Exception {
    UnregisterHostRequest request = new UnregisterHostRequest();
    request.setId("host1");
    UnregisterHostResponse response = service.unregister_host(request);

    assertThat(response.getResult(), Matchers.is(UnregisterHostResultCode.OK));
    verify(configDict).write("host1", null);
  }
 /**
  * Verify that reportedDatastores/reportedImageDatastores/reportedNetworks don't get reset when
  * datastores/imageDatastores/networks are null.
  */
 @Test
 public void testSetHostStateEmpty() throws Throwable {
   service.setHostState("host-id", AgentState.ACTIVE, null, null, null);
   ArgumentCaptor<String> arg1 = ArgumentCaptor.forClass(String.class);
   ArgumentCaptor<ServiceDocument> arg2 = ArgumentCaptor.forClass(ServiceDocument.class);
   verify(dcpRestClient).patch(arg1.capture(), arg2.capture());
   HostService.State patch = (HostService.State) arg2.getValue();
   assertThat(patch.agentState, is(AgentState.ACTIVE));
   assertThat(patch.reportedDatastores, is(nullValue()));
   assertThat(patch.reportedImageDatastores, is(nullValue()));
   assertThat(patch.reportedNetworks, is(nullValue()));
   verifyNoMoreInteractions(dcpRestClient);
 }
  @Test
  public void testSimpleRegistrationFail() throws Exception {
    Datastore datastore1 = new Datastore("ds1");
    datastore1.setType(DatastoreType.SHARED_VMFS);
    datastores.add(datastore1);
    RegisterHostRequest request =
        createRegReq(datastores, networks, new LinkedHashSet<>(Arrays.asList("ds1")));
    request.setId("host1");
    request.getConfig().setAgent_id("host1");

    doThrow(new Exception()).when(configDict).write("host1", serialize(request.getConfig()));
    RegisterHostResponse response = service.register_host(request);
    assertThat(response.getResult(), is(RegisterHostResultCode.NOT_IN_MAJORITY));
  }
  @Test
  public void testSimpleRegistration() throws Throwable {
    // Set up initial host state in cloudstore.
    String hostId = "host1";
    String link = service.getHostDocumentLink(hostId);
    DatastoreService.State ds1 = new DatastoreService.State();
    ds1.id = "ds1";
    ds1.name = "ds1";
    ds1.type = "SHARED_VMFS";
    ds1.tags = new LinkedHashSet<>();
    Datastore datastore1 = new Datastore(ds1.id);
    datastore1.setName(ds1.name);
    datastore1.setType(DatastoreType.SHARED_VMFS);
    datastore1.setTags(ds1.tags);
    datastores.add(datastore1);

    DatastoreService.State ds2 = new DatastoreService.State();
    ds2.id = "ds2";
    ds2.name = "ds2";
    ds2.type = "SHARED_VMFS";
    ds2.tags = new LinkedHashSet<>();
    Datastore datastore2 = new Datastore(ds2.id);
    datastore2.setName(ds2.name);
    datastore2.setType(DatastoreType.SHARED_VMFS);
    datastore2.setTags(ds2.tags);
    datastores.add(datastore2);

    String dsLink1 = DatastoreServiceFactory.getDocumentLink(ds1.id);
    String dsLink2 = DatastoreServiceFactory.getDocumentLink(ds2.id);
    HostService.State hostState = new HostService.State();
    hostState.agentState = AgentState.MISSING;
    Operation result = mock(Operation.class);
    when(result.getBody(HostService.State.class)).thenReturn(hostState);
    when(dcpRestClient.get(link)).thenReturn(result);

    // Non-VM networks should get filtered out.
    Network nw1 = new Network("nw1");
    nw1.setTypes(Arrays.asList(NetworkType.VM));
    networks.add(nw1);
    Network nw2 = new Network("nw2");
    nw2.setTypes(Arrays.asList(NetworkType.VM, NetworkType.VMOTION));
    networks.add(nw2);
    Network nw3 = new Network("nw3");
    nw3.setTypes(Arrays.asList(NetworkType.MANAGEMENT, NetworkType.VMOTION));
    networks.add(nw3);

    RegisterHostRequest request =
        createRegReq(datastores, networks, new LinkedHashSet<>(Arrays.asList("ds1", "ds2")));
    request.setId(hostId);
    request.getConfig().setAgent_id(hostId);
    RegisterHostResponse response = service.register_host(request);
    assertThat(response.getResult(), Matchers.is(RegisterHostResultCode.OK));

    verify(configDict).write(hostId, serialize(request.getConfig()));

    // Verify that patch gets called with "READY" state.
    ArgumentCaptor<String> arg1 = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<ServiceDocument> arg2 = ArgumentCaptor.forClass(ServiceDocument.class);
    verify(dcpRestClient, times(3)).patch(arg1.capture(), arg2.capture());
    assertThat(arg1.getAllValues().get(0), is(link));
    HostService.State newState = (HostService.State) (arg2.getAllValues().get(0));
    assertThat(newState.agentState, is(AgentState.ACTIVE));
    assertThat(newState.reportedDatastores, containsInAnyOrder("ds1", "ds2"));
    assertThat(newState.reportedNetworks, containsInAnyOrder("nw1", "nw2"));
    assertThat(newState.reportedImageDatastores, containsInAnyOrder("ds1", "ds2"));

    // Verify that the isImageDatastore flag gets set on ds1 and ds2.
    assertThat(arg1.getAllValues().get(1), is(dsLink1));
    DatastoreService.State newDsState = (DatastoreService.State) (arg2.getAllValues().get(1));
    assertThat(newDsState.isImageDatastore, is(true));
    assertThat(arg1.getAllValues().get(2), is(dsLink2));
    newDsState = (DatastoreService.State) (arg2.getAllValues().get(2));
    assertThat(newDsState.isImageDatastore, is(true));

    // Verify that chairman attempted to create datastore documents.
    arg1 = ArgumentCaptor.forClass(String.class);
    arg2 = ArgumentCaptor.forClass(ServiceDocument.class);
    verify(dcpRestClient, times(2)).post(arg1.capture(), arg2.capture());
    DatastoreService.State actualDs1 = (DatastoreService.State) (arg2.getAllValues().get(0));
    DatastoreService.State actualDs2 = (DatastoreService.State) (arg2.getAllValues().get(1));
    assertThat(
        arg1.getAllValues(),
        contains(DatastoreServiceFactory.SELF_LINK, DatastoreServiceFactory.SELF_LINK));
    verifyDatastore(ds1, actualDs1);
    verifyDatastore(ds2, actualDs2);
  }