github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/testing/ipchecker.go (about)

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	"fmt"
     8  	"net"
     9  
    10  	gc "gopkg.in/check.v1"
    11  )
    12  
    13  type ipsEqualChecker struct {
    14  	*gc.CheckerInfo
    15  }
    16  
    17  var IPsEqual gc.Checker = &ipsEqualChecker{
    18  	&gc.CheckerInfo{Name: "IPsEqual", Params: []string{"obtained", "expected"}},
    19  }
    20  
    21  func (c *ipsEqualChecker) Check(params []interface{}, name []string) (bool, string) {
    22  	ips1, ok := params[0].([]net.IP)
    23  	if !ok {
    24  		return false, "param 0 is not of type []net.IP"
    25  	}
    26  	ips2, ok := params[1].([]net.IP)
    27  	if !ok {
    28  		return false, "param 0 is not of type []net.IP"
    29  	}
    30  
    31  	if len(ips1) != len(ips2) {
    32  		return false, fmt.Sprintf("legnth of ip slices not equal %d != %d",
    33  			len(ips1), len(ips2))
    34  	}
    35  
    36  	for i := range ips1 {
    37  		if !ips1[i].Equal(ips2[i]) {
    38  			return false, "ip slices are not equal"
    39  		}
    40  	}
    41  	return true, ""
    42  }