github.com/elfadel/cilium@v1.6.12/pkg/health/client/client_test.go (about)

     1  // Copyright 2018 Authors of Cilium
     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  // +build !privileged_tests
    16  
    17  package client
    18  
    19  import (
    20  	"io/ioutil"
    21  	"testing"
    22  
    23  	"github.com/cilium/cilium/api/v1/health/models"
    24  
    25  	. "gopkg.in/check.v1"
    26  )
    27  
    28  // Hook up gocheck into the "go test" runner.
    29  func Test(t *testing.T) {
    30  	TestingT(t)
    31  }
    32  
    33  type ClientTestSuite struct{}
    34  
    35  var _ = Suite(&ClientTestSuite{})
    36  
    37  func (s *ClientTestSuite) TestFormatNodeStatus(c *C) {
    38  	// This test generates permutations of models.NodeStatus and sees whether
    39  	// the calls to formatNodeStatus panic; the result of this test being
    40  	// successful is whether the test does not panic.
    41  
    42  	// not testing output, just that permutations of NodeStatus don't cause
    43  	// panics.
    44  	w := ioutil.Discard
    45  
    46  	connectivityStatusGood := &models.ConnectivityStatus{
    47  		Latency: 1,
    48  		Status:  "",
    49  	}
    50  	connectivityStatusBad := &models.ConnectivityStatus{
    51  		Latency: 1,
    52  		Status:  "bad status",
    53  	}
    54  	possibleConnectivityStatus := []*models.ConnectivityStatus{
    55  		connectivityStatusBad,
    56  		connectivityStatusGood,
    57  	}
    58  	possibleIPs := []string{"192.168.1.1", ""}
    59  
    60  	possibleNames := []string{"node1", ""}
    61  
    62  	possiblePathStatuses := []*models.PathStatus{}
    63  
    64  	for _, connectivityStatusHTTP := range possibleConnectivityStatus {
    65  		for _, connectivityStatusICMP := range possibleConnectivityStatus {
    66  			for _, possibleIP := range possibleIPs {
    67  				pathStatus := &models.PathStatus{
    68  					HTTP: connectivityStatusHTTP,
    69  					Icmp: connectivityStatusICMP,
    70  					IP:   possibleIP,
    71  				}
    72  				possiblePathStatuses = append(possiblePathStatuses, pathStatus)
    73  			}
    74  		}
    75  	}
    76  
    77  	possibleSecondaryAddresses := make([]*models.PathStatus, 0, len(possiblePathStatuses)+1)
    78  	possibleSecondaryAddresses = append(possibleSecondaryAddresses, nil)
    79  	possibleSecondaryAddresses = append(possibleSecondaryAddresses, possiblePathStatuses...)
    80  
    81  	// Assemble possible host statuses.
    82  	possibleHostStatuses := []*models.HostStatus{
    83  		nil,
    84  	}
    85  
    86  	for _, possiblePrimaryAddress := range possiblePathStatuses {
    87  		hostStatus := &models.HostStatus{
    88  			PrimaryAddress:     possiblePrimaryAddress,
    89  			SecondaryAddresses: possibleSecondaryAddresses,
    90  		}
    91  		possibleHostStatuses = append(possibleHostStatuses, hostStatus)
    92  	}
    93  
    94  	printAllOptions := []bool{true, false}
    95  	succinctOptions := []bool{true, false}
    96  	verboseOptions := []bool{true, false}
    97  	localhostOptions := []bool{true, false}
    98  
    99  	for _, possibleEndpointStatus := range possiblePathStatuses {
   100  		for _, hostStatus := range possibleHostStatuses {
   101  			for _, name := range possibleNames {
   102  				ns := &models.NodeStatus{
   103  					Endpoint: possibleEndpointStatus,
   104  					Host:     hostStatus,
   105  					Name:     name,
   106  				}
   107  				for _, printAllOpt := range printAllOptions {
   108  					for _, succintOpt := range succinctOptions {
   109  						for _, verboseOpt := range verboseOptions {
   110  							for _, localhostOpt := range localhostOptions {
   111  								formatNodeStatus(w, ns, printAllOpt, succintOpt, verboseOpt, localhostOpt)
   112  							}
   113  						}
   114  					}
   115  				}
   116  			}
   117  		}
   118  	}
   119  }
   120  
   121  func (s *ClientTestSuite) TestGetHostPrimaryAddress(c *C) {
   122  	nilHostNS := &models.NodeStatus{
   123  		Host: nil,
   124  	}
   125  
   126  	pathStatus := GetHostPrimaryAddress(nilHostNS)
   127  	c.Assert(pathStatus, IsNil)
   128  
   129  	nilPrimaryAddressNS := &models.NodeStatus{
   130  		Host: &models.HostStatus{
   131  			PrimaryAddress: nil,
   132  		},
   133  	}
   134  
   135  	pathStatus = GetHostPrimaryAddress(nilPrimaryAddressNS)
   136  	c.Assert(pathStatus, IsNil)
   137  
   138  	primaryAddressNS := &models.NodeStatus{
   139  		Host: &models.HostStatus{
   140  			PrimaryAddress: &models.PathStatus{},
   141  		},
   142  	}
   143  
   144  	pathStatus = GetHostPrimaryAddress(primaryAddressNS)
   145  	c.Assert(pathStatus, Not(IsNil))
   146  }
   147  
   148  func (s *ClientTestSuite) TestGetPrimaryAddressIP(c *C) {
   149  	nilHostNS := &models.NodeStatus{
   150  		Host: nil,
   151  	}
   152  
   153  	pathStatus := getPrimaryAddressIP(nilHostNS)
   154  	c.Assert(pathStatus, Equals, ipUnavailable)
   155  
   156  	nilPrimaryAddressNS := &models.NodeStatus{
   157  		Host: &models.HostStatus{
   158  			PrimaryAddress: nil,
   159  		},
   160  	}
   161  
   162  	pathStatus = getPrimaryAddressIP(nilPrimaryAddressNS)
   163  	c.Assert(pathStatus, Equals, ipUnavailable)
   164  
   165  	primaryAddressNS := &models.NodeStatus{
   166  		Host: &models.HostStatus{
   167  			PrimaryAddress: &models.PathStatus{},
   168  		},
   169  	}
   170  
   171  	pathStatus = getPrimaryAddressIP(primaryAddressNS)
   172  	c.Assert(pathStatus, Equals, "")
   173  }