Merge pull request #37030 from adshmh/refactor-service-create-integration-tests-use-swarm-create-service

Refactor service create integration tests to use swarm CreateService
This commit is contained in:
Yong Tang 2018-05-14 14:07:22 -07:00 committed by GitHub
commit 6de3be1366
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -34,15 +34,14 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
overlayID := netResp.ID overlayID := netResp.ID
var instances uint64 = 4 var instances uint64 = 4
serviceSpec := swarmServiceSpec("TestService", instances)
serviceSpec.TaskTemplate.Networks = append(serviceSpec.TaskTemplate.Networks, swarmtypes.NetworkAttachmentConfig{Target: overlayName})
serviceResp, err := client.ServiceCreate(context.Background(), serviceSpec, types.ServiceCreateOptions{ serviceSpec := []swarm.ServiceSpecOpt{
QueryRegistry: false, swarm.ServiceWithReplicas(instances),
}) swarm.ServiceWithName("TestService"),
assert.NilError(t, err) swarm.ServiceWithNetwork(overlayName),
}
serviceID := serviceResp.ID serviceID := swarm.CreateService(t, d, serviceSpec...)
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll) poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll)
_, _, err = client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{}) _, _, err = client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
@ -54,12 +53,7 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll) poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll)
poll.WaitOn(t, noTasks(client), swarm.ServicePoll) poll.WaitOn(t, noTasks(client), swarm.ServicePoll)
serviceResp, err = client.ServiceCreate(context.Background(), serviceSpec, types.ServiceCreateOptions{ serviceID2 := swarm.CreateService(t, d, serviceSpec...)
QueryRegistry: false,
})
assert.NilError(t, err)
serviceID2 := serviceResp.ID
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID2, instances), swarm.ServicePoll) poll.WaitOn(t, serviceRunningTasksCount(client, serviceID2, instances), swarm.ServicePoll)
err = client.ServiceRemove(context.Background(), serviceID2) err = client.ServiceRemove(context.Background(), serviceID2)
@ -100,25 +94,25 @@ func TestCreateWithDuplicateNetworkNames(t *testing.T) {
// Create Service with the same name // Create Service with the same name
var instances uint64 = 1 var instances uint64 = 1
serviceSpec := swarmServiceSpec("top", instances)
serviceSpec.TaskTemplate.Networks = append(serviceSpec.TaskTemplate.Networks, swarmtypes.NetworkAttachmentConfig{Target: name}) serviceID := swarm.CreateService(t, d,
swarm.ServiceWithReplicas(instances),
swarm.ServiceWithName("top"),
swarm.ServiceWithNetwork(name),
)
service, err := client.ServiceCreate(context.Background(), serviceSpec, types.ServiceCreateOptions{}) poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll)
assert.NilError(t, err)
poll.WaitOn(t, serviceRunningTasksCount(client, service.ID, instances), swarm.ServicePoll) resp, _, err := client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
resp, _, err := client.ServiceInspectWithRaw(context.Background(), service.ID, types.ServiceInspectOptions{})
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, is.Equal(n3.ID, resp.Spec.TaskTemplate.Networks[0].Target)) assert.Check(t, is.Equal(n3.ID, resp.Spec.TaskTemplate.Networks[0].Target))
// Remove Service // Remove Service
err = client.ServiceRemove(context.Background(), service.ID) err = client.ServiceRemove(context.Background(), serviceID)
assert.NilError(t, err) assert.NilError(t, err)
// Make sure task has been destroyed. // Make sure task has been destroyed.
poll.WaitOn(t, serviceIsRemoved(client, service.ID), swarm.ServicePoll) poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll)
// Remove networks // Remove networks
err = client.NetworkRemove(context.Background(), n3.ID) err = client.NetworkRemove(context.Background(), n3.ID)
@ -153,44 +147,26 @@ func TestCreateServiceSecretFileMode(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
var instances uint64 = 1 var instances uint64 = 1
serviceSpec := swarmtypes.ServiceSpec{ serviceID := swarm.CreateService(t, d,
Annotations: swarmtypes.Annotations{ swarm.ServiceWithReplicas(instances),
Name: "TestService", swarm.ServiceWithName("TestService"),
}, swarm.ServiceWithCommand([]string{"/bin/sh", "-c", "ls -l /etc/secret || /bin/top"}),
TaskTemplate: swarmtypes.TaskSpec{ swarm.ServiceWithSecret(&swarmtypes.SecretReference{
ContainerSpec: &swarmtypes.ContainerSpec{ File: &swarmtypes.SecretReferenceFileTarget{
Image: "busybox:latest", Name: "/etc/secret",
Command: []string{"/bin/sh", "-c", "ls -l /etc/secret || /bin/top"}, UID: "0",
Secrets: []*swarmtypes.SecretReference{ GID: "0",
{ Mode: 0777,
File: &swarmtypes.SecretReferenceFileTarget{
Name: "/etc/secret",
UID: "0",
GID: "0",
Mode: 0777,
},
SecretID: secretResp.ID,
SecretName: "TestSecret",
},
},
}, },
}, SecretID: secretResp.ID,
Mode: swarmtypes.ServiceMode{ SecretName: "TestSecret",
Replicated: &swarmtypes.ReplicatedService{ }),
Replicas: &instances, )
},
},
}
serviceResp, err := client.ServiceCreate(ctx, serviceSpec, types.ServiceCreateOptions{ poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll)
QueryRegistry: false,
})
assert.NilError(t, err)
poll.WaitOn(t, serviceRunningTasksCount(client, serviceResp.ID, instances), swarm.ServicePoll)
filter := filters.NewArgs() filter := filters.NewArgs()
filter.Add("service", serviceResp.ID) filter.Add("service", serviceID)
tasks, err := client.TaskList(ctx, types.TaskListOptions{ tasks, err := client.TaskList(ctx, types.TaskListOptions{
Filters: filter, Filters: filter,
}) })
@ -207,10 +183,10 @@ func TestCreateServiceSecretFileMode(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, is.Contains(string(content), "-rwxrwxrwx")) assert.Check(t, is.Contains(string(content), "-rwxrwxrwx"))
err = client.ServiceRemove(ctx, serviceResp.ID) err = client.ServiceRemove(ctx, serviceID)
assert.NilError(t, err) assert.NilError(t, err)
poll.WaitOn(t, serviceIsRemoved(client, serviceResp.ID), swarm.ServicePoll) poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll)
poll.WaitOn(t, noTasks(client), swarm.ServicePoll) poll.WaitOn(t, noTasks(client), swarm.ServicePoll)
err = client.SecretRemove(ctx, "TestSecret") err = client.SecretRemove(ctx, "TestSecret")
@ -234,44 +210,26 @@ func TestCreateServiceConfigFileMode(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
var instances uint64 = 1 var instances uint64 = 1
serviceSpec := swarmtypes.ServiceSpec{ serviceID := swarm.CreateService(t, d,
Annotations: swarmtypes.Annotations{ swarm.ServiceWithName("TestService"),
Name: "TestService", swarm.ServiceWithCommand([]string{"/bin/sh", "-c", "ls -l /etc/config || /bin/top"}),
}, swarm.ServiceWithReplicas(instances),
TaskTemplate: swarmtypes.TaskSpec{ swarm.ServiceWithConfig(&swarmtypes.ConfigReference{
ContainerSpec: &swarmtypes.ContainerSpec{ File: &swarmtypes.ConfigReferenceFileTarget{
Image: "busybox:latest", Name: "/etc/config",
Command: []string{"/bin/sh", "-c", "ls -l /etc/config || /bin/top"}, UID: "0",
Configs: []*swarmtypes.ConfigReference{ GID: "0",
{ Mode: 0777,
File: &swarmtypes.ConfigReferenceFileTarget{
Name: "/etc/config",
UID: "0",
GID: "0",
Mode: 0777,
},
ConfigID: configResp.ID,
ConfigName: "TestConfig",
},
},
}, },
}, ConfigID: configResp.ID,
Mode: swarmtypes.ServiceMode{ ConfigName: "TestConfig",
Replicated: &swarmtypes.ReplicatedService{ }),
Replicas: &instances, )
},
},
}
serviceResp, err := client.ServiceCreate(ctx, serviceSpec, types.ServiceCreateOptions{ poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances))
QueryRegistry: false,
})
assert.NilError(t, err)
poll.WaitOn(t, serviceRunningTasksCount(client, serviceResp.ID, instances))
filter := filters.NewArgs() filter := filters.NewArgs()
filter.Add("service", serviceResp.ID) filter.Add("service", serviceID)
tasks, err := client.TaskList(ctx, types.TaskListOptions{ tasks, err := client.TaskList(ctx, types.TaskListOptions{
Filters: filter, Filters: filter,
}) })
@ -288,35 +246,16 @@ func TestCreateServiceConfigFileMode(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, is.Contains(string(content), "-rwxrwxrwx")) assert.Check(t, is.Contains(string(content), "-rwxrwxrwx"))
err = client.ServiceRemove(ctx, serviceResp.ID) err = client.ServiceRemove(ctx, serviceID)
assert.NilError(t, err) assert.NilError(t, err)
poll.WaitOn(t, serviceIsRemoved(client, serviceResp.ID)) poll.WaitOn(t, serviceIsRemoved(client, serviceID))
poll.WaitOn(t, noTasks(client)) poll.WaitOn(t, noTasks(client))
err = client.ConfigRemove(ctx, "TestConfig") err = client.ConfigRemove(ctx, "TestConfig")
assert.NilError(t, err) assert.NilError(t, err)
} }
func swarmServiceSpec(name string, replicas uint64) swarmtypes.ServiceSpec {
return swarmtypes.ServiceSpec{
Annotations: swarmtypes.Annotations{
Name: name,
},
TaskTemplate: swarmtypes.TaskSpec{
ContainerSpec: &swarmtypes.ContainerSpec{
Image: "busybox:latest",
Command: []string{"/bin/top"},
},
},
Mode: swarmtypes.ServiceMode{
Replicated: &swarmtypes.ReplicatedService{
Replicas: &replicas,
},
},
}
}
func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string, instances uint64) func(log poll.LogT) poll.Result { func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string, instances uint64) func(log poll.LogT) poll.Result {
return func(log poll.LogT) poll.Result { return func(log poll.LogT) poll.Result {
filter := filters.NewArgs() filter := filters.NewArgs()