github.com/google/cadvisor@v0.49.1/container/containerd/identifiers/validate_test.go (about)

     1  // Copyright 2017 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  /*
    15     Copyright The containerd Authors.
    16  
    17     Licensed under the Apache License, Version 2.0 (the "License");
    18     you may not use this file except in compliance with the License.
    19     You may obtain a copy of the License at
    20  
    21         http://www.apache.org/licenses/LICENSE-2.0
    22  
    23     Unless required by applicable law or agreed to in writing, software
    24     distributed under the License is distributed on an "AS IS" BASIS,
    25     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    26     See the License for the specific language governing permissions and
    27     limitations under the License.
    28  */
    29  
    30  package identifiers
    31  
    32  import (
    33  	"strings"
    34  	"testing"
    35  
    36  	"github.com/google/cadvisor/container/containerd/errdefs"
    37  )
    38  
    39  func TestValidIdentifiers(t *testing.T) {
    40  	for _, input := range []string{
    41  		"default",
    42  		"Default",
    43  		t.Name(),
    44  		"default-default",
    45  		"containerd.io",
    46  		"foo.boo",
    47  		"swarmkit.docker.io",
    48  		"0912341234",
    49  		"task.0.0123456789",
    50  		"container.system-75-f19a.00",
    51  		"underscores_are_allowed",
    52  		strings.Repeat("a", maxLength),
    53  	} {
    54  		t.Run(input, func(t *testing.T) {
    55  			if err := Validate(input); err != nil {
    56  				t.Fatalf("unexpected error: %v != nil", err)
    57  			}
    58  		})
    59  	}
    60  }
    61  
    62  func TestInvalidIdentifiers(t *testing.T) {
    63  	for _, input := range []string{
    64  		"",
    65  		".foo..foo",
    66  		"foo/foo",
    67  		"foo/..",
    68  		"foo..foo",
    69  		"foo.-boo",
    70  		"-foo.boo",
    71  		"foo.boo-",
    72  		"but__only_tasteful_underscores",
    73  		"zn--e9.org", // or something like it!
    74  		"default--default",
    75  		strings.Repeat("a", maxLength+1),
    76  	} {
    77  
    78  		t.Run(input, func(t *testing.T) {
    79  			if err := Validate(input); err == nil {
    80  				t.Fatal("expected invalid error")
    81  			} else if !errdefs.IsInvalidArgument(err) {
    82  				t.Fatal("error should be an invalid identifier error")
    83  			}
    84  		})
    85  	}
    86  }