github.com/1and1/oneandone-cloudserver-sdk-go@v1.4.1/publicips_test.go (about)

     1  package oneandone
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  var (
    11  	set_ip  sync.Once
    12  	test_ip *PublicIp
    13  )
    14  
    15  const (
    16  	ip_dns = "test.oneandone.com"
    17  )
    18  
    19  // Helper functions
    20  
    21  func wait_for_ip_ready(ip_address *PublicIp, sec time.Duration, count int, state string) *PublicIp {
    22  	if ip_address.State == state {
    23  		return ip_address
    24  	}
    25  	for i := 0; i < count; i++ {
    26  		ip, err := api.GetPublicIp(ip_address.Id)
    27  		if err == nil {
    28  			if ip.State == state {
    29  				return ip
    30  			}
    31  		}
    32  		time.Sleep(sec * time.Second)
    33  	}
    34  	return ip_address
    35  }
    36  
    37  func create_public_ip() *PublicIp {
    38  	fmt.Printf("Creating an IPV4 public ip...\n")
    39  	pip_id, pip, err := api.CreatePublicIp(IpTypeV4, ip_dns, "")
    40  	if err != nil {
    41  		fmt.Printf("Unable to create a public ip address. Error: %s", err.Error())
    42  		return nil
    43  	}
    44  	if pip_id == "" || pip.Id == "" {
    45  		fmt.Printf("Unable to create a public ip address.")
    46  		return nil
    47  	}
    48  	return wait_for_ip_ready(pip, 5, 30, "ACTIVE")
    49  }
    50  
    51  func set_public_ip() {
    52  	test_ip = create_public_ip()
    53  }
    54  
    55  // /public_ips tests
    56  
    57  func TestCreatePublicIp(t *testing.T) {
    58  	set_ip.Do(set_public_ip)
    59  
    60  	if test_ip == nil {
    61  		t.Errorf("CreatePublicIp failed.")
    62  	}
    63  	if test_ip.IpAddress == "" {
    64  		t.Errorf("Missing IP address.")
    65  	}
    66  	if test_ip.Type != IpTypeV4 {
    67  		t.Errorf("Wrong IP type.")
    68  	}
    69  	if test_ip.ReverseDns != ip_dns {
    70  		t.Errorf("Wrong reverse dns of ip address '%s'.", test_ip.IpAddress)
    71  	}
    72  }
    73  
    74  func TestGetPublicIp(t *testing.T) {
    75  	set_ip.Do(set_public_ip)
    76  
    77  	fmt.Printf("Getting public ip '%s'...\n", test_ip.IpAddress)
    78  	ip, err := api.GetPublicIp(test_ip.Id)
    79  
    80  	if err != nil {
    81  		t.Errorf("GetPublicIp failed. Error: " + err.Error())
    82  	}
    83  	if ip.IpAddress != test_ip.IpAddress {
    84  		t.Errorf("Wrong IP address.")
    85  	}
    86  	if ip.Type != test_ip.Type {
    87  		t.Errorf("Wrong IP type.")
    88  	}
    89  	if ip.ReverseDns != test_ip.ReverseDns {
    90  		t.Errorf("Wrong reverse dns of ip address '%s'.", ip.IpAddress)
    91  	}
    92  }
    93  
    94  func TestListPublicIps(t *testing.T) {
    95  	set_ip.Do(set_public_ip)
    96  	fmt.Println("Listing all public ip addresses...")
    97  
    98  	ips, err := api.ListPublicIps()
    99  	if err != nil {
   100  		t.Errorf("ListPublicIps failed. Error: " + err.Error())
   101  	}
   102  	if len(ips) == 0 {
   103  		t.Errorf("No public ip found.")
   104  	}
   105  
   106  	ips, err = api.ListPublicIps(1, 3, "id", "", "id,ip")
   107  	if err != nil {
   108  		t.Errorf("ListPublicIps with parameter options failed. Error: " + err.Error())
   109  	}
   110  	if len(ips) == 0 {
   111  		t.Errorf("No public ip found.")
   112  	}
   113  	if len(ips) > 3 {
   114  		t.Errorf("Wrong number of objects per page.")
   115  	}
   116  	if ips[0].Id == "" {
   117  		t.Errorf("Filtering parameters failed.")
   118  	}
   119  	if ips[0].IpAddress == "" {
   120  		t.Errorf("Filtering parameters failed.")
   121  	}
   122  	if ips[0].State != "" {
   123  		t.Errorf("Filtering parameters failed.")
   124  	}
   125  	if len(ips) >= 2 && ips[0].Id >= ips[1].Id {
   126  		t.Errorf("Sorting parameters failed.")
   127  	}
   128  	// Test for error response
   129  	ips, err = api.ListPublicIps(0, 0, "", "", false)
   130  	if ips != nil || err == nil {
   131  		t.Errorf("ListPublicIps failed to handle incorrect argument type.")
   132  	}
   133  
   134  	ips, err = api.ListPublicIps(0, 0, "", test_ip.IpAddress, "")
   135  	if err != nil {
   136  		t.Errorf("ListPublicIps with parameter options failed. Error: " + err.Error())
   137  	}
   138  	if len(ips) != 1 {
   139  		t.Errorf("Search parameter failed.")
   140  	}
   141  	if ips[0].IpAddress != test_ip.IpAddress {
   142  		t.Errorf("Search parameter failed.")
   143  	}
   144  }
   145  
   146  func TestUpdatePublicIp(t *testing.T) {
   147  	set_ip.Do(set_public_ip)
   148  
   149  	fmt.Printf("Updating public ip '%s'...\n", test_ip.IpAddress)
   150  	new_dns := "test.oneandone.de"
   151  
   152  	ip, err := api.UpdatePublicIp(test_ip.Id, new_dns)
   153  
   154  	if err != nil {
   155  		t.Errorf("UpdatePublicIp failed. Error: " + err.Error())
   156  	}
   157  	ip = wait_for_ip_ready(ip, 5, 10, "ACTIVE")
   158  	if ip.Id != test_ip.Id {
   159  		t.Errorf("Wrong IP address ID.")
   160  	}
   161  	if ip.ReverseDns != new_dns {
   162  		t.Errorf("Wrong reverse dns of ip address '%s'.", ip.IpAddress)
   163  	}
   164  }
   165  
   166  func TestDeletePublicIp(t *testing.T) {
   167  	set_ip.Do(set_public_ip)
   168  
   169  	fmt.Printf("Deleting public ip '%s'...\n", test_ip.IpAddress)
   170  	ip, err := api.DeletePublicIp(test_ip.Id)
   171  
   172  	if err != nil {
   173  		t.Errorf("DeletePublicIp failed. Error: " + err.Error())
   174  	}
   175  
   176  	ip = wait_for_ip_ready(ip, 5, 30, "REMOVING")
   177  	time.Sleep(30 * time.Second)
   178  	ip, _ = api.GetPublicIp(test_ip.Id)
   179  
   180  	if ip != nil {
   181  		t.Errorf("Unable to delete the public ip.")
   182  	} else {
   183  		test_ip = nil
   184  	}
   185  }