View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.chukwa.datacollection.agent;
20  
21  
22  import org.apache.hadoop.chukwa.datacollection.adaptor.Adaptor;
23  import org.apache.hadoop.chukwa.datacollection.adaptor.ChukwaTestAdaptor;
24  import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent.AlreadyRunningException;
25  import org.apache.hadoop.chukwa.datacollection.connector.ChunkCatcherConnector;
26  import org.apache.hadoop.chukwa.datacollection.test.ConsoleOutConnector;
27  import org.apache.hadoop.conf.Configuration;
28  import junit.framework.TestCase;
29  import java.net.*;
30  import java.io.*;
31  
32  public class TestCmd extends TestCase {
33  
34    public void testAddCmdWithParam() {
35      ChukwaAgent agent;
36      try {
37        Configuration conf = new Configuration();
38        conf.set("chukwaAgent.control.port", "0");
39        agent = new ChukwaAgent(conf);
40        ConsoleOutConnector conn = new ConsoleOutConnector(agent, true);
41        conn.start();
42        String l = agent
43            .processAddCommand("ADD org.apache.hadoop.chukwa.datacollection.adaptor.ChukwaTestAdaptor  chukwaTestAdaptorType 0 my param1 param2 /var/log/messages 114027");
44        assertTrue(l != null);
45        Adaptor adaptor = agent.getAdaptor(l);
46        ChukwaTestAdaptor chukwaTestAdaptor = (ChukwaTestAdaptor) adaptor;
47        assertTrue("error in type",
48            "chukwaTestAdaptorType".intern() == chukwaTestAdaptor.getType()
49                .intern());
50        assertTrue("error in param", "0 my param1 param2 /var/log/messages"
51            .intern() == chukwaTestAdaptor.getParams().intern());
52        assertTrue("error in startOffset", 114027l == chukwaTestAdaptor
53            .getStartOffset());
54        agent.stopAdaptor(l, false);
55        agent.shutdown();
56  
57        Thread.sleep(2000);
58      } catch (InterruptedException e) {
59  
60      } catch (AlreadyRunningException e) {
61        e.printStackTrace();
62        fail(e.toString());
63      }
64    }
65  
66    public void testAddCmdWithoutParam1() {
67      ChukwaAgent agent;
68      try {
69        Configuration conf = new Configuration();
70        conf.set("chukwaAgent.control.port", "0");
71        agent = new ChukwaAgent(conf);
72        ConsoleOutConnector conn = new ConsoleOutConnector(agent, true);
73        conn.start();
74        String name = agent
75            .processAddCommand("ADD org.apache.hadoop.chukwa.datacollection.adaptor.ChukwaTestAdaptor  chukwaTestAdaptorType 114027");
76        assertTrue(name != null);
77        Adaptor adaptor = agent.getAdaptor(name);
78        ChukwaTestAdaptor chukwaTestAdaptor = (ChukwaTestAdaptor) adaptor;
79        assertTrue("error in type",
80            "chukwaTestAdaptorType".intern() == chukwaTestAdaptor.getType()
81                .intern());
82        assertTrue("error in param", "".intern() == chukwaTestAdaptor.getParams()
83            .intern());
84        assertTrue("error in startOffset", 114027l == chukwaTestAdaptor
85            .getStartOffset());
86        agent.stopAdaptor(name, false);
87        agent.shutdown();
88        Thread.sleep(2000);
89      } catch (InterruptedException e) {
90  
91      } catch (AlreadyRunningException e) {
92        e.printStackTrace();
93        fail(e.toString());
94      }
95    }
96  
97    public void testAddCmdWithoutParam2() {
98      ChukwaAgent agent;
99      try {
100       Configuration conf = new Configuration();
101       conf.set("chukwaAgent.control.port", "0");
102       agent = new ChukwaAgent(conf);
103       ConsoleOutConnector conn = new ConsoleOutConnector(agent, true);
104       conn.start();
105       String n = agent
106           .processAddCommand("ADD org.apache.hadoop.chukwa.datacollection.adaptor.ChukwaTestAdaptor"
107               + "  chukwaTestAdaptorType 0  114027");
108       assertTrue(n != null);
109       Adaptor adaptor = agent.getAdaptor(n);
110       ChukwaTestAdaptor chukwaTestAdaptor = (ChukwaTestAdaptor) adaptor;
111       assertTrue("error in type",
112           "chukwaTestAdaptorType".intern() == chukwaTestAdaptor.getType()
113               .intern());
114       assertTrue("error in param", "0".intern() == chukwaTestAdaptor
115           .getParams().intern());
116       assertTrue("error in startOffset", 114027l == chukwaTestAdaptor
117           .getStartOffset());
118       agent.stopAdaptor(n, false);
119       agent.shutdown();
120       Thread.sleep(2000);
121     } catch (InterruptedException e) {
122 
123     } catch (AlreadyRunningException e) {
124       e.printStackTrace();
125       fail(e.toString());
126     }
127   }
128   
129   public void testStopAll() throws Exception{
130     Configuration conf = new Configuration();
131     conf.set("chukwaAgent.control.port", "0");
132     ChukwaAgent agent = new ChukwaAgent(conf);
133     ChunkCatcherConnector chunks = new ChunkCatcherConnector();
134     chunks.start();
135     agent.processAddCommand(
136         "ADD adaptor1 = org.apache.hadoop.chukwa.datacollection.adaptor.ChukwaTestAdaptor"
137         + "  chukwaTestAdaptorType 0");
138 
139     agent.processAddCommand(
140     "ADD adaptor2 = org.apache.hadoop.chukwa.datacollection.adaptor.ChukwaTestAdaptor"
141     + "  chukwaTestAdaptorType 0");
142     assertEquals(2, agent.adaptorCount());
143 
144     Socket s = new Socket("localhost", agent.getControllerPort());
145     PrintWriter bw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
146     bw.println("stopAll");
147     bw.flush();
148     InputStreamReader in = new InputStreamReader(s.getInputStream());
149     in.read();
150     assertEquals(0, agent.adaptorCount());
151     agent.shutdown();
152   }
153 }