2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2021-09-16 06:13:09 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
docker "github.com/fsouza/go-dockerclient"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2021-09-16 06:13:09 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDriver_createSandboxContainerConfig(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2021-09-16 06:13:09 +00:00
|
|
|
testCases := []struct {
|
|
|
|
inputAllocID string
|
|
|
|
inputNetworkCreateRequest *drivers.NetworkCreateRequest
|
|
|
|
expectedOutputOpts *docker.CreateContainerOptions
|
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
inputAllocID: "768b5e8c-a52e-825c-d564-51100230eb62",
|
|
|
|
inputNetworkCreateRequest: &drivers.NetworkCreateRequest{
|
|
|
|
Hostname: "",
|
|
|
|
},
|
|
|
|
expectedOutputOpts: &docker.CreateContainerOptions{
|
|
|
|
Name: "nomad_init_768b5e8c-a52e-825c-d564-51100230eb62",
|
|
|
|
Config: &docker.Config{
|
|
|
|
Image: "gcr.io/google_containers/pause-amd64:3.1",
|
2023-01-30 15:46:45 +00:00
|
|
|
Labels: map[string]string{
|
|
|
|
dockerLabelAllocID: "768b5e8c-a52e-825c-d564-51100230eb62",
|
|
|
|
},
|
2021-09-16 06:13:09 +00:00
|
|
|
},
|
|
|
|
HostConfig: &docker.HostConfig{
|
2023-01-10 13:50:09 +00:00
|
|
|
NetworkMode: "none",
|
|
|
|
RestartPolicy: docker.RestartUnlessStopped(),
|
2021-09-16 06:13:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
name: "no input hostname",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inputAllocID: "768b5e8c-a52e-825c-d564-51100230eb62",
|
|
|
|
inputNetworkCreateRequest: &drivers.NetworkCreateRequest{
|
|
|
|
Hostname: "linux",
|
|
|
|
},
|
|
|
|
expectedOutputOpts: &docker.CreateContainerOptions{
|
|
|
|
Name: "nomad_init_768b5e8c-a52e-825c-d564-51100230eb62",
|
|
|
|
Config: &docker.Config{
|
|
|
|
Image: "gcr.io/google_containers/pause-amd64:3.1",
|
|
|
|
Hostname: "linux",
|
2023-01-30 15:46:45 +00:00
|
|
|
Labels: map[string]string{
|
|
|
|
dockerLabelAllocID: "768b5e8c-a52e-825c-d564-51100230eb62",
|
|
|
|
},
|
2021-09-16 06:13:09 +00:00
|
|
|
},
|
|
|
|
HostConfig: &docker.HostConfig{
|
2023-01-10 13:50:09 +00:00
|
|
|
NetworkMode: "none",
|
|
|
|
RestartPolicy: docker.RestartUnlessStopped(),
|
2021-09-16 06:13:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
name: "supplied input hostname",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
d := &Driver{
|
|
|
|
config: &DriverConfig{
|
|
|
|
InfraImage: "gcr.io/google_containers/pause-amd64:3.1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
actualOutput, err := d.createSandboxContainerConfig(tc.inputAllocID, tc.inputNetworkCreateRequest)
|
|
|
|
assert.Nil(t, err, tc.name)
|
|
|
|
assert.Equal(t, tc.expectedOutputOpts, actualOutput, tc.name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|