@Test
public void testNodeRegistrationWithInvalidAttributes() throws Exception {

  writeToHostsFile("host2");
  Configuration conf = new Configuration();
  conf.set(YarnConfiguration.RM_NODES_INCLUDE_FILE_PATH,
      hostFile.getAbsolutePath());
  conf.setClass(YarnConfiguration.FS_NODE_ATTRIBUTE_STORE_IMPL_CLASS,
      FileSystemNodeAttributeStore.class, NodeAttributeStore.class);
  conf.set(YarnConfiguration.FS_NODE_ATTRIBUTE_STORE_ROOT_DIR,
      TEMP_DIR.getAbsolutePath());
  rm = new MockRM(conf);
  rm.start();
  ResourceTrackerService resourceTrackerService =
      rm.getResourceTrackerService();
  RegisterNodeManagerRequest req =
      Records.newRecord(RegisterNodeManagerRequest.class);
  NodeId nodeId = NodeId.newInstance("host2", 1234);
  Resource capability = BuilderUtils.newResource(1024, 1);
  NodeAttribute validNodeAttribute = NodeAttribute
      .newInstance(NodeAttribute.PREFIX_DISTRIBUTED, "Attr1",
          NodeAttributeType.STRING, "V1");
  NodeAttribute invalidPrefixNodeAttribute = NodeAttribute
      .newInstance("_P", "Attr1",
          NodeAttributeType.STRING, "V2");
  NodeAttribute invalidNameNodeAttribute = NodeAttribute
      .newInstance(NodeAttribute.PREFIX_DISTRIBUTED, "_N",
          NodeAttributeType.STRING, "V2");
  NodeAttribute invalidValueNodeAttribute = NodeAttribute
      .newInstance(NodeAttribute.PREFIX_DISTRIBUTED, "Attr2",
          NodeAttributeType.STRING, "...");
  req.setResource(capability);
  req.setNodeId(nodeId);
  req.setHttpPort(1234);
  req.setNMVersion(YarnVersionInfo.getVersion());
  // check invalid prefix
  req.setNodeAttributes(
      toSet(validNodeAttribute, invalidPrefixNodeAttribute));
  RegisterNodeManagerResponse response =
      resourceTrackerService.registerNodeManager(req);
  Assert.assertEquals(0, rm.getRMContext().getNodeAttributesManager()
      .getAttributesForNode(nodeId.getHost()).size());
  assertRegisterResponseForInvalidAttributes(response);
  Assert.assertTrue(response.getDiagnosticsMessage()
      .endsWith("attributes in HB must have prefix nm.yarn.io"));
  // check invalid name
  req.setNodeAttributes(toSet(validNodeAttribute, invalidNameNodeAttribute));
  response = resourceTrackerService.registerNodeManager(req);
  Assert.assertEquals(0, rm.getRMContext().getNodeAttributesManager()
      .getAttributesForNode(nodeId.getHost()).size());
  assertRegisterResponseForInvalidAttributes(response);
  Assert.assertTrue(response.getDiagnosticsMessage()
      .startsWith("attribute name should only contains"));
  // check invalid value
  req.setNodeAttributes(toSet(validNodeAttribute, invalidValueNodeAttribute));
  response = resourceTrackerService.registerNodeManager(req);
  Assert.assertEquals(0, rm.getRMContext().getNodeAttributesManager()
      .getAttributesForNode(nodeId.getHost()).size());
  assertRegisterResponseForInvalidAttributes(response);
  Assert.assertTrue(response.getDiagnosticsMessage()
      .startsWith("attribute value should only contains"));
  if (rm != null) {
    rm.stop();
  }
}