github.com/google/cadvisor@v0.49.1/validate/validate_test.go (about)

     1  // Copyright 2015 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 validate
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  var (
    24  	standardDesc       = "Versions >= %s are supported. %s+ are recommended"
    25  	dockerStandardDesc = fmt.Sprintf(standardDesc, "1.0", "1.2")
    26  	kernelStandardDesc = fmt.Sprintf(standardDesc, "2.6", "3.0")
    27  	dockerErrorDesc    = "Could not parse docker version"
    28  	kernelErrorDesc    = "Could not parse kernel version"
    29  )
    30  
    31  func TestGetMajorMinor(t *testing.T) {
    32  	cases := []struct {
    33  		version string
    34  		major   int
    35  		minor   int
    36  		err     error
    37  	}{
    38  		{"0.1beta", 0, 1, nil},
    39  		{"0.1.2", 0, 1, nil},
    40  		{"-1.-1beta", -1, -1, nil},
    41  		{"0.1", -1, -1, fmt.Errorf("have error")},
    42  		{"0", -1, -1, fmt.Errorf("have error")},
    43  		{"beta", -1, -1, fmt.Errorf("have error")},
    44  	}
    45  
    46  	for i, c := range cases {
    47  		ma, mi, e := getMajorMinor(c.version)
    48  		if (e != nil) != (c.err != nil) {
    49  			t.Errorf("[%d] Unexpected err, should %v, but got %v", i, c.err, e)
    50  		}
    51  		if ma != c.major {
    52  			t.Errorf("[%d] Unexpected major, should %v, but got %v", i, c.major, ma)
    53  		}
    54  		if mi != c.minor {
    55  			t.Errorf("[%d] Unexpected minor, should %v, but got %v", i, c.minor, mi)
    56  		}
    57  	}
    58  }
    59  
    60  func TestValidateKernelVersion(t *testing.T) {
    61  	cases := []struct {
    62  		version string
    63  		result  string
    64  		desc    string
    65  	}{
    66  		{"2.6.3", Supported, kernelStandardDesc},
    67  		{"3.6.3", Recommended, kernelStandardDesc},
    68  		{"1.0beta", Unsupported, kernelStandardDesc},
    69  		{"0.1beta", Unsupported, kernelStandardDesc},
    70  		{"0.1", Unknown, kernelErrorDesc},
    71  		{"3.1", Unknown, kernelErrorDesc},
    72  	}
    73  
    74  	for i, c := range cases {
    75  		res, desc := validateKernelVersion(c.version)
    76  		if res != c.result {
    77  			t.Errorf("[%d] Unexpected result, should %v, but got %v", i, c.result, res)
    78  		}
    79  		if !strings.Contains(desc, c.desc) {
    80  			t.Errorf("[%d] Unexpected description, should %v, but got %v", i, c.desc, desc)
    81  		}
    82  	}
    83  }
    84  
    85  func TestValidateDockerVersion(t *testing.T) {
    86  	cases := []struct {
    87  		version string
    88  		result  string
    89  		desc    string
    90  	}{
    91  		{"1.1.3", Supported, dockerStandardDesc},
    92  		{"1.6.3", Recommended, dockerStandardDesc},
    93  		{"1.0beta", Supported, dockerStandardDesc},
    94  		{"0.1beta", Unsupported, dockerStandardDesc},
    95  		{"0.1", Unknown, dockerErrorDesc},
    96  		{"1.6", Unknown, dockerErrorDesc},
    97  	}
    98  
    99  	for i, c := range cases {
   100  		res, desc := validateDockerVersion(c.version)
   101  		if res != c.result {
   102  			t.Errorf("[%d] Unexpected result, should %v, but got %v", i, c.result, res)
   103  		}
   104  		if !strings.Contains(desc, c.desc) {
   105  			t.Errorf("[%d] Unexpected description, should %v, but got %v", i, c.desc, desc)
   106  		}
   107  	}
   108  }
   109  
   110  func TestAreCgroupsPresent(t *testing.T) {
   111  	cases := []struct {
   112  		available map[string]int
   113  		desired   []string
   114  		result    bool
   115  		reason    string
   116  	}{
   117  		{map[string]int{"memory": 1}, []string{"memory"}, true, ""},
   118  		{map[string]int{"memory": 2}, []string{"memory"}, false, "memory not enabled. Available cgroups"},
   119  		{map[string]int{"memory": 0}, []string{"memory"}, false, "memory not enabled. Available cgroups"},
   120  		{map[string]int{"memory": 1}, []string{"blkio"}, false, "Missing cgroup blkio. Available cgroups"},
   121  	}
   122  	for i, c := range cases {
   123  		result, reason := areCgroupsPresent(c.available, c.desired)
   124  		if result != c.result {
   125  			t.Errorf("[%d] Unexpected result, should %v, but got %v", i, c.result, result)
   126  		}
   127  		if (c.reason == "" && reason != "") || (c.reason != "" && !strings.Contains(reason, c.reason)) {
   128  			t.Errorf("[%d] Unexpected result, should %v, but got %v", i, c.reason, reason)
   129  		}
   130  	}
   131  }