本文整理汇总了Golang中github.com/coreos/flannel/pkg/ip.ParseIP4函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseIP4函数的具体用法?Golang ParseIP4怎么用?Golang ParseIP4使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseIP4函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: mustParseIP4
func mustParseIP4(s string) ip.IP4 {
a, err := ip.ParseIP4(s)
if err != nil {
panic(err)
}
return a
}
示例2: TestAcquireLease
func TestAcquireLease(t *testing.T) {
msr := newDummyRegistry()
sm := NewMockManager(msr)
extIaddr, _ := ip.ParseIP4("1.2.3.4")
attrs := LeaseAttrs{
PublicIP: extIaddr,
}
l, err := sm.AcquireLease(context.Background(), "_", &attrs)
if err != nil {
t.Fatal("AcquireLease failed: ", err)
}
if !inAllocatableRange(context.Background(), sm, l.Subnet) {
t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", l.Subnet)
}
// Acquire again, should reuse
l2, err := sm.AcquireLease(context.Background(), "_", &attrs)
if err != nil {
t.Fatal("AcquireLease failed: ", err)
}
if !l.Subnet.Equal(l2.Subnet) {
t.Fatalf("AcquireLease did not reuse subnet; expected %v, got %v", l.Subnet, l2.Subnet)
}
}
示例3: TestConfigChanged
func TestConfigChanged(t *testing.T) {
msr := newDummyRegistry()
sm := NewMockManager(msr)
extIaddr, _ := ip.ParseIP4("1.2.3.4")
attrs := LeaseAttrs{
PublicIP: extIaddr,
}
l, err := sm.AcquireLease(context.Background(), "_", &attrs)
if err != nil {
t.Fatal("AcquireLease failed: ", err)
}
if !inAllocatableRange(context.Background(), sm, l.Subnet) {
t.Fatal("Acquired subnet outside of valid range: ", l.Subnet)
}
// Change config
config := `{ "Network": "10.4.0.0/16" }`
msr.setConfig("_", config)
// Acquire again, should not reuse
if l, err = sm.AcquireLease(context.Background(), "_", &attrs); err != nil {
t.Fatal("AcquireLease failed: ", err)
}
if !inAllocatableRange(context.Background(), sm, l.Subnet) {
t.Fatal("Acquired subnet outside of valid range: ", l.Subnet)
}
}
示例4: TestConfigChanged
func TestConfigChanged(t *testing.T) {
msr := newDummyRegistry(1000)
sm := newEtcdManager(msr)
extIaddr, _ := ip.ParseIP4("1.2.3.4")
attrs := LeaseAttrs{
PublicIP: extIaddr,
}
l, err := sm.AcquireLease(context.Background(), "", &attrs)
if err != nil {
t.Fatal("AcquireLease failed: ", err)
}
if l.Subnet.String() != "10.3.3.0/24" {
t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", l.Subnet)
}
// Change config
config := `{ "Network": "10.4.0.0/16" }`
msr.setConfig(config)
// Acquire again, should not reuse
if l, err = sm.AcquireLease(context.Background(), "", &attrs); err != nil {
t.Fatal("AcquireLease failed: ", err)
}
newNet := newIP4Net("10.4.0.0", 16)
if !newNet.Contains(l.Subnet.IP) {
t.Fatalf("Subnet mismatch: expected within %v, got: %v", newNet, l.Subnet)
}
}
示例5: TestAcquireLease
func TestAcquireLease(t *testing.T) {
msr := newDummyRegistry(1000)
sm := newEtcdManager(msr)
extIaddr, _ := ip.ParseIP4("1.2.3.4")
attrs := LeaseAttrs{
PublicIP: extIaddr,
}
l, err := sm.AcquireLease(context.Background(), "", &attrs)
if err != nil {
t.Fatal("AcquireLease failed: ", err)
}
if l.Subnet.String() != "10.3.3.0/24" {
t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", l.Subnet)
}
// Acquire again, should reuse
if l, err = sm.AcquireLease(context.Background(), "", &attrs); err != nil {
t.Fatal("AcquireLease failed: ", err)
}
if l.Subnet.String() != "10.3.3.0/24" {
t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", l.Subnet)
}
}
示例6: TestRenewLease
func TestRenewLease(t *testing.T) {
msr := newMockSubnetRegistry(1)
sm, err := newSubnetManager(msr)
if err != nil {
t.Fatalf("Failed to create subnet manager: %s", err)
}
ip, _ := ip.ParseIP4("1.2.3.4")
data := `{ "PublicIP": "1.2.3.4" }`
cancel := make(chan bool)
defer close(cancel)
sn, err := sm.AcquireLease(ip, data, cancel)
if err != nil {
t.Fatal("AcquireLease failed: ", err)
}
go sm.LeaseRenewer(cancel)
fmt.Println("Waiting for lease to pass original expiration")
time.Sleep(2 * time.Second)
// check that it's still good
for _, n := range msr.subnets.Nodes {
if n.Key == sn.StringSep(".", "-") {
if n.Expiration.Before(time.Now()) {
t.Fatalf("Failed to renew lease")
}
return
}
}
t.Fatalf("Failed to find acquired lease")
}
示例7: TestAcquireLease
func TestAcquireLease(t *testing.T) {
msr := newMockSubnetRegistry(0)
sm, err := newSubnetManager(msr)
if err != nil {
t.Fatalf("Failed to create subnet manager: %s", err)
}
ip, _ := ip.ParseIP4("1.2.3.4")
data := `{ "PublicIP": "1.2.3.4" }`
cancel := make(chan bool)
sn, err := sm.AcquireLease(ip, data, cancel)
if err != nil {
t.Fatal("AcquireLease failed: ", err)
}
if sn.String() != "10.3.3.0/24" {
t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", sn)
}
// Acquire again, should reuse
if sn, err = sm.AcquireLease(ip, data, cancel); err != nil {
t.Fatal("AcquireLease failed: ", err)
}
if sn.String() != "10.3.3.0/24" {
t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", sn)
}
}
示例8: TestRenewLease
func TestRenewLease(t *testing.T) {
msr := newDummyRegistry()
sm := NewMockManager(msr)
now := time.Now()
fakeClock := clockwork.NewFakeClockAt(now)
clock = fakeClock
// Create LeaseAttrs
extIaddr, _ := ip.ParseIP4("1.2.3.4")
attrs := LeaseAttrs{
PublicIP: extIaddr,
BackendType: "vxlan",
}
ld, err := json.Marshal(&leaseData{Dummy: "test string"})
if err != nil {
t.Fatalf("Failed to marshal leaseData: %v", err)
}
attrs.BackendData = json.RawMessage(ld)
// Acquire lease
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
l, err := sm.AcquireLease(ctx, "_", &attrs)
if err != nil {
t.Fatal("AcquireLease failed: ", err)
}
now = now.Add(subnetTTL)
fakeClock.Advance(24 * time.Hour)
if err := sm.RenewLease(ctx, "_", l); err != nil {
t.Fatal("RenewLease failed: ", err)
}
// check that it's still good
n, err := msr.getNetwork(ctx, "_")
if err != nil {
t.Error("Failed to renew lease: could not get networks: %v", err)
}
for _, sn := range n.subnets {
if sn.Subnet.Equal(l.Subnet) {
expected := now.Add(subnetTTL)
if !sn.Expiration.Equal(expected) {
t.Errorf("Failed to renew lease: bad expiration; expected %v, got %v", expected, sn.Expiration)
}
if !reflect.DeepEqual(sn.Attrs, attrs) {
t.Errorf("LeaseAttrs changed: was %#v, now %#v", attrs, sn.Attrs)
}
return
}
}
t.Fatalf("Failed to find acquired lease")
}
示例9: newIP4Net
func newIP4Net(ipaddr string, prefix uint) ip.IP4Net {
a, err := ip.ParseIP4(ipaddr)
if err != nil {
panic("failed to parse ipaddr")
}
return ip.IP4Net{
IP: a,
PrefixLen: prefix,
}
}
示例10: TestRenewLease
func TestRenewLease(t *testing.T) {
msr := newDummyRegistry(1)
sm := newEtcdManager(msr)
// Create LeaseAttrs
extIaddr, _ := ip.ParseIP4("1.2.3.4")
attrs := LeaseAttrs{
PublicIP: extIaddr,
BackendType: "vxlan",
}
ld, err := json.Marshal(&leaseData{Dummy: "test string"})
if err != nil {
t.Fatalf("Failed to marshal leaseData: %v", err)
}
attrs.BackendData = json.RawMessage(ld)
// Acquire lease
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
l, err := sm.AcquireLease(ctx, "_", &attrs)
if err != nil {
t.Fatal("AcquireLease failed: ", err)
}
go LeaseRenewer(ctx, sm, "_", l)
fmt.Println("Waiting for lease to pass original expiration")
time.Sleep(2 * time.Second)
// check that it's still good
net, err := msr.getNetwork(ctx, "_")
if err != nil {
t.Error("Failed to renew lease: could not get networks: %v", err)
}
for _, n := range net.Node.Nodes {
if n.Key == l.Subnet.StringSep(".", "-") {
if n.Expiration.Before(time.Now()) {
t.Error("Failed to renew lease: expiration did not advance")
}
a := LeaseAttrs{}
if err := json.Unmarshal([]byte(n.Value), &a); err != nil {
t.Errorf("Failed to JSON-decode LeaseAttrs: %v", err)
return
}
if !reflect.DeepEqual(a, attrs) {
t.Errorf("LeaseAttrs changed: was %#v, now %#v", attrs, a)
}
return
}
}
t.Fatalf("Failed to find acquired lease")
}
示例11: acquireLease
func acquireLease(ctx context.Context, t *testing.T, sm Manager) *Lease {
extIaddr, _ := ip.ParseIP4("1.2.3.4")
attrs := LeaseAttrs{
PublicIP: extIaddr,
}
l, err := sm.AcquireLease(ctx, "_", &attrs)
if err != nil {
t.Fatal("AcquireLease failed: ", err)
}
return l
}
示例12: nodeToLease
func nodeToLease(n api.Node) (l subnet.Lease, err error) {
l.Attrs.PublicIP, err = ip.ParseIP4(n.Annotations[backendPublicIPAnnotation])
if err != nil {
return l, err
}
l.Attrs.BackendType = n.Annotations[backendTypeAnnotation]
l.Attrs.BackendData = json.RawMessage(n.Annotations[backendDataAnnotation])
_, cidr, err := net.ParseCIDR(n.Spec.PodCIDR)
if err != nil {
return l, err
}
l.Subnet = ip.FromIPNet(cidr)
l.Expiration = time.Now().Add(24 * time.Hour)
return l, nil
}