1414import org .slf4j .Logger ;
1515import org .slf4j .LoggerFactory ;
1616
17+ import java .util .Collections ;
1718import java .util .List ;
19+ import java .util .Map ;
1820
1921import static com .github .dockerjava .junit .DockerRule .DEFAULT_IMAGE ;
2022import static org .hamcrest .MatcherAssert .assertThat ;
2426public class ListTasksCmdExecIT extends SwarmCmdIT {
2527 public static final Logger LOG = LoggerFactory .getLogger (CreateServiceCmdExecIT .class );
2628 private static final String SERVICE_NAME = "inspect_task" ;
29+ private static final String TASK_LABEL_KEY = "com.github.dockerjava.usage" ;
30+ private static final String TASK_LABEL_VALUE = "test" ;
2731
2832 @ Test
2933 public void testListTasks () throws DockerException {
3034 dockerRule .getClient ().initializeSwarmCmd (new SwarmSpec ())
3135 .withListenAddr ("127.0.0.1" )
3236 .withAdvertiseAddr ("127.0.0.1" )
3337 .exec ();
34-
38+ Map < String , String > taskLabels = Collections . singletonMap ( TASK_LABEL_KEY , TASK_LABEL_VALUE );
3539 CreateServiceResponse response = dockerRule .getClient ().createServiceCmd (new ServiceSpec ()
3640 .withName (SERVICE_NAME )
3741 .withMode (new ServiceModeConfig ().withReplicated (
@@ -40,7 +44,7 @@ public void testListTasks() throws DockerException {
4044 ))
4145 .withTaskTemplate (new TaskSpec ()
4246 .withContainerSpec (new ContainerSpec ()
43- .withImage (DEFAULT_IMAGE ))))
47+ .withImage (DEFAULT_IMAGE ))). withLabels ( taskLabels ) )
4448 .exec ();
4549 String serviceId = response .getId ();
4650 //filtering with service id
@@ -60,6 +64,11 @@ public void testListTasks() throws DockerException {
6064 //filtering with state
6165 tasks = dockerRule .getClient ().listTasksCmd ().withStateFilter (TaskState .RUNNING ).exec ();
6266 assertThat (tasks , hasSize (2 ));
67+ //filter labels
68+ tasks = dockerRule .getClient ().listTasksCmd ().withLabelFilter (taskLabels ).exec ();
69+ assertThat (tasks , hasSize (2 ));
70+ tasks = dockerRule .getClient ().listTasksCmd ().withLabelFilter (TASK_LABEL_KEY + "=" + TASK_LABEL_VALUE ).exec ();
71+ assertThat (tasks , hasSize (2 ));
6372 dockerRule .getClient ().removeServiceCmd (SERVICE_NAME ).exec ();
6473 }
6574}
0 commit comments