github.com/google/cadvisor@v0.49.1/container/docker/docker_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  package docker
    16  
    17  import (
    18  	"reflect"
    19  	"regexp"
    20  	"testing"
    21  )
    22  
    23  func TestParseDockerAPIVersion(t *testing.T) {
    24  	tests := []struct {
    25  		version       string
    26  		regex         *regexp.Regexp
    27  		length        int
    28  		expected      []int
    29  		expectedError string
    30  	}{
    31  		{"17.03.0", VersionRe, 3, []int{17, 03, 0}, ""},
    32  		{"17.a3.0", VersionRe, 3, []int{}, `version string "17.a3.0" doesn't match expected regular expression: "(\d+)\.(\d+)\.(\d+)"`},
    33  		{"1.20", apiVersionRe, 2, []int{1, 20}, ""},
    34  		{"1.a", apiVersionRe, 2, []int{}, `version string "1.a" doesn't match expected regular expression: "(\d+)\.(\d+)"`},
    35  	}
    36  
    37  	for _, test := range tests {
    38  		actual, err := ParseVersion(test.version, test.regex, test.length)
    39  		if err != nil {
    40  			if len(test.expectedError) == 0 {
    41  				t.Errorf("%s: expected no error, got %v", test.version, err)
    42  			} else if err.Error() != test.expectedError {
    43  				t.Errorf("%s: expected error %v, got %v", test.version, test.expectedError, err)
    44  			}
    45  		} else {
    46  			if !reflect.DeepEqual(actual, test.expected) {
    47  				t.Errorf("%s: expected array %v, got %v", test.version, test.expected, actual)
    48  			}
    49  		}
    50  	}
    51  }