| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- /*
- Copyright 2020 Docker, Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package run
- import (
- "errors"
- "regexp"
- "testing"
- "github.com/google/go-cmp/cmp/cmpopts"
- "gotest.tools/v3/assert"
- "gotest.tools/v3/assert/cmp"
- "github.com/docker/api/containers"
- )
- var (
- // AzureNameRegex is used to validate container names
- // Regex was taken from server side error:
- // The container name must contain no more than 63 characters and must match the regex '[a-z0-9]([-a-z0-9]*[a-z0-9])?' (e.g. 'my-name').
- AzureNameRegex = regexp.MustCompile("[a-z0-9]([-a-z0-9]*[a-z0-9])")
- )
- // TestAzureRandomName ensures compliance with Azure naming requirements
- func TestAzureRandomName(t *testing.T) {
- n := getRandomName()
- assert.Assert(t, len(n) < 64)
- assert.Assert(t, len(n) > 1)
- assert.Assert(t, cmp.Regexp(AzureNameRegex, n))
- }
- func TestPortParse(t *testing.T) {
- testCases := []struct {
- in string
- expected []containers.Port
- }{
- {
- in: "80",
- expected: []containers.Port{
- {
- HostPort: 80,
- ContainerPort: 80,
- Protocol: "tcp",
- },
- },
- },
- {
- in: "80:80",
- expected: []containers.Port{
- {
- HostPort: 80,
- ContainerPort: 80,
- Protocol: "tcp",
- },
- },
- },
- {
- in: "80:80/udp",
- expected: []containers.Port{
- {
- ContainerPort: 80,
- HostPort: 80,
- Protocol: "udp",
- },
- },
- },
- {
- in: "8080:80",
- expected: []containers.Port{
- {
- HostPort: 8080,
- ContainerPort: 80,
- Protocol: "tcp",
- },
- },
- },
- {
- in: "192.168.0.2:8080:80",
- expected: []containers.Port{
- {
- HostPort: 8080,
- ContainerPort: 80,
- Protocol: "tcp",
- HostIP: "192.168.0.2",
- },
- },
- },
- {
- in: "80-81:80-81",
- expected: []containers.Port{
- {
- HostPort: 80,
- ContainerPort: 80,
- Protocol: "tcp",
- },
- {
- HostPort: 81,
- ContainerPort: 81,
- Protocol: "tcp",
- },
- },
- },
- }
- for _, testCase := range testCases {
- opts := Opts{
- Publish: []string{testCase.in},
- }
- result, err := opts.toPorts()
- assert.NilError(t, err)
- assert.DeepEqual(t, result, testCase.expected, cmpopts.SortSlices(func(x, y containers.Port) bool {
- return x.ContainerPort < y.ContainerPort
- }))
- }
- }
- func TestLabels(t *testing.T) {
- testCases := []struct {
- in []string
- expected map[string]string
- expectedError error
- }{
- {
- in: []string{"label=value"},
- expected: map[string]string{
- "label": "value",
- },
- expectedError: nil,
- },
- {
- in: []string{"label=value", "label=value2"},
- expected: map[string]string{
- "label": "value2",
- },
- expectedError: nil,
- },
- {
- in: []string{"label=value", "label2=value2"},
- expected: map[string]string{
- "label": "value",
- "label2": "value2",
- },
- expectedError: nil,
- },
- {
- in: []string{"label"},
- expected: nil,
- expectedError: errors.New(`wrong label format "label"`),
- },
- }
- for _, testCase := range testCases {
- result, err := toLabels(testCase.in)
- if testCase.expectedError == nil {
- assert.NilError(t, err)
- } else {
- assert.Error(t, err, testCase.expectedError.Error())
- }
- assert.DeepEqual(t, result, testCase.expected)
- }
- }
|