File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\test\java\hudson\slaves\DummyCloudImpl.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.slaves;
25
26 import hudson.model.Computer;
27 import hudson.model.Descriptor;
28 import hudson.model.Node;
29 import hudson.model.Label;
30 import hudson.slaves.NodeProvisioner.PlannedNode;
31 import org.jvnet.hudson.test.HudsonTestCase;
32
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.List;
36 import java.util.concurrent.Callable;
37 import java.util.concurrent.Future;
38
39 /**
40 * {@link Cloud} implementation useful for testing.
41 *
42 * <p>
43 * This implementation launches "java -jar slave.jar" on the localhost when provisioning a new slave.
44 *
45 * @author Kohsuke Kawaguchi
46 */
47 public class DummyCloudImpl extends Cloud {
48 private final transient HudsonTestCase caller;
49
50 /**
51 * Configurable delay between the {@link Cloud#provision(Label,int)} and the actual launch of a slave,
52 * to emulate a real cloud that takes some time for provisioning a new system.
53 *
54 * <p>
55 * Number of milliseconds.
56 */
57 private final int delay;
58
59 // stats counter to perform assertions later
60 public int numProvisioned;
61
62 /**
63 * Only reacts to provisioning for this label.
64 */
65 public Label label;
66
67 public DummyCloudImpl(HudsonTestCase caller, int delay) {
68 super("test");
69 this.caller = caller;
70 this.delay = delay;
71 }
72
73 public Collection<PlannedNode> provision(Label label, int excessWorkload) {
74 List<PlannedNode> r = new ArrayList<PlannedNode>();
75 if(label!=this.label) return r; // provisioning impossible
76
77 while(excessWorkload>0) {
78 System.out.println("Provisioning");
79 numProvisioned++;
80 Future<Node> f = Computer.threadPoolForRemoting.submit(new Launcher(delay));
81 r.add(new PlannedNode(name+" #"+numProvisioned,f,1));
82 excessWorkload-=1;
83 }
84 return r;
85 }
86
87 public boolean canProvision(Label label) {
88 return label==this.label;
89 }
90
91 private final class Launcher implements Callable<Node> {
92 private final long time;
93 /**
94 * This is so that we can find out the status of Callable from the debugger.
95 */
96 private volatile Computer computer;
97
98 private Launcher(long time) {
99 this.time = time;
100 }
101
102 public Node call() throws Exception {
103 // simulate the delay in provisioning a new slave,
104 // since it's normally some async operation.
105 Thread.sleep(time);
106
107 System.out.println("launching slave");
108 DumbSlave slave = caller.createSlave(label);
109 computer = slave.toComputer();
110 computer.connect(false).get();
111 synchronized (DummyCloudImpl.this) {
112 System.out.println(computer.getName()+" launch"+(computer.isOnline()?"ed successfully":" failed"));
113 System.out.println(computer.getLog());
114 }
115 return slave;
116 }
117 }
118
119 public Descriptor<Cloud> getDescriptor() {
120 throw new UnsupportedOperationException();
121 }
122 }
123