github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/system_unit_test.go (about)

     1  //go:build unit || ALL
     2  
     3  /*
     4  * Copyright 2019 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	"testing"
    11  )
    12  
    13  // Tests reliability of getBareEntityUuid, which returns a bare UUID from an
    14  // entity ID field
    15  func Test_BareEntityID(t *testing.T) {
    16  
    17  	type idTest struct {
    18  		rawId    string
    19  		expected string
    20  	}
    21  
    22  	idTestList := []idTest{
    23  		{
    24  			"urn:vcloud:catalog:97384890-180c-4563-b9b7-0dc50a2430b0",
    25  			"97384890-180c-4563-b9b7-0dc50a2430b0",
    26  		},
    27  		{
    28  			"urn:vcloud:org:deadbeef-0000-0000-0000-000000000000",
    29  			"deadbeef-0000-0000-0000-000000000000",
    30  		},
    31  		{
    32  			"urn:vcloud:task:11111111-0000-0000-0000-000000000000",
    33  			"11111111-0000-0000-0000-000000000000",
    34  		},
    35  		{
    36  			"urn:vcloud:task:aaaaaaaa-bbbb-ccc0-dddd-eeeeeeeeeeee",
    37  			"aaaaaaaa-bbbb-ccc0-dddd-eeeeeeeeeeee",
    38  		},
    39  		{
    40  			"urn:vcloud:vdc:72fefde7-4fed-45b8-a774-79b72c870325",
    41  			"72fefde7-4fed-45b8-a774-79b72c870325",
    42  		},
    43  		{
    44  			"urn:composite-name:vdc:72fefde7-4fed-45b8-a774-79b72c870325",
    45  			"72fefde7-4fed-45b8-a774-79b72c870325",
    46  		},
    47  		{
    48  			"urn:underscored_name:vdc:72fefde7-4fed-45b8-a774-79b72c870325",
    49  			"72fefde7-4fed-45b8-a774-79b72c870325",
    50  		},
    51  		{
    52  			"urn:mixed_name-with-dashes:double-string:72fefde7-4fed-45b8-a774-79b72c870325",
    53  			"72fefde7-4fed-45b8-a774-79b72c870325",
    54  		},
    55  	}
    56  	idTestExpectedToFailList := []idTest{
    57  		{
    58  			"missing:one:digit:12345678-1234-1234-1234-12345678901",
    59  			"",
    60  		},
    61  		{
    62  			"missing:one:digit:1234567-1234-1234-1234-123456789012",
    63  			"",
    64  		},
    65  		{
    66  			"missing:one:digit:12345678-123-1234-1234-123456789012",
    67  			"",
    68  		},
    69  		{
    70  			"too:many:digits:123456789-1234-1234-1234-123456789012",
    71  			"",
    72  		},
    73  		{
    74  			"too:many:digits:12345678-12345-1234-1234-123456789012",
    75  			"",
    76  		},
    77  		{
    78  			"too:many:digits:12345678-1234-1234-1234-1234567890123",
    79  			"",
    80  		},
    81  		{
    82  			"unexpected:letters:in_ID:abcdefgh-1234-1234-1234-123456789012",
    83  			"",
    84  		},
    85  		{
    86  			"unexpected:letters:in_ID:abcdef00-x234-w234-1234-123456789012",
    87  			"",
    88  		},
    89  		{
    90  			"unexpected:letters:in,prefix:12345678-1234-1234-1234-123456789012",
    91  			"",
    92  		},
    93  		{
    94  			"unexpected:letters:in/prefix:12345678-1234-1234-1234-123456789012",
    95  			"",
    96  		},
    97  	}
    98  
    99  	for _, it := range idTestList {
   100  		bareId, err := getBareEntityUuid(it.rawId)
   101  		if err != nil {
   102  			t.Logf("error extracting bare ID: %s", err)
   103  			t.Fail()
   104  		}
   105  		if bareId == it.expected {
   106  			if testVerbose {
   107  				t.Logf("ID '%s': found '%s' as expected", it.rawId, it.expected)
   108  			}
   109  		} else {
   110  			t.Logf("error getting bare ID: expected '%s' but found '%s'", it.expected, bareId)
   111  			t.Fail()
   112  		}
   113  	}
   114  	for _, it := range idTestExpectedToFailList {
   115  		bareId, err := getBareEntityUuid(it.rawId)
   116  		if err == nil {
   117  			t.Logf("unexpected success with raw ID %s", it.rawId)
   118  			t.Fail()
   119  		}
   120  		if bareId == it.expected {
   121  			if testVerbose {
   122  				t.Logf("ID '%s': found '%s' as expected", it.rawId, it.expected)
   123  			}
   124  		} else {
   125  			t.Logf("error getting bare ID: expected '%s' but found '%s'", it.expected, bareId)
   126  			t.Fail()
   127  		}
   128  	}
   129  }
   130  
   131  // Tests reliability of GetUuidFromHref, which returns a bare UUID from an
   132  // entity HREF field when id is at the end of URL
   133  func Test_GetUuidFromHrefIdAtEnd(t *testing.T) {
   134  
   135  	type idTest struct {
   136  		rawHref  string
   137  		expected string
   138  	}
   139  
   140  	idTestList := []idTest{
   141  		{
   142  			"https://vcd.somecompany.org/api/entity/97384890-180c-4563-b9b7-0dc50a2430b0",
   143  			"97384890-180c-4563-b9b7-0dc50a2430b0",
   144  		},
   145  		{
   146  			"https://vcd.somecompany.org/api/entity/deadbeef-0000-0000-0000-000000000000",
   147  			"deadbeef-0000-0000-0000-000000000000",
   148  		},
   149  		{
   150  			"https://vcd.somecompany.org/api/entity/11111111-0000-0000-0000-000000000000",
   151  			"11111111-0000-0000-0000-000000000000",
   152  		},
   153  		{
   154  			"https://vcd.somecompany.org/api/entity/aaaaaaaa-bbbb-ccc0-dddd-eeeeeeeeeeee",
   155  			"aaaaaaaa-bbbb-ccc0-dddd-eeeeeeeeeeee",
   156  		},
   157  		{
   158  			"https://vcd.somecompany.org/api/entity/72fefde7-4fed-45b8-a774-79b72c870325",
   159  			"72fefde7-4fed-45b8-a774-79b72c870325",
   160  		},
   161  		{
   162  			"https://vcd.somecompany.org/api/entity/72fefde7-4fed-45b8-a774-79b72c870325",
   163  			"72fefde7-4fed-45b8-a774-79b72c870325",
   164  		},
   165  		{
   166  			"https://vcd.somecompany.org/api/entity/72fefde7-4fed-45b8-a774-79b72c870325",
   167  			"72fefde7-4fed-45b8-a774-79b72c870325",
   168  		},
   169  		{
   170  			"https://vcd.somecompany.org/api/entity/72fefde7-4fed-45b8-a774-79b72c870325",
   171  			"72fefde7-4fed-45b8-a774-79b72c870325",
   172  		},
   173  	}
   174  	idTestExpectedToFailList := []idTest{
   175  		{
   176  			"https://vcd.somecompany.org/api/entity/12345678-1234-1234-1234-12345678901",
   177  			"",
   178  		},
   179  		{
   180  			"https://vcd.somecompany.org/api/entity/1234567-1234-1234-1234-123456789012",
   181  			"",
   182  		},
   183  		{
   184  			"https://vcd.somecompany.org/api/entity/12345678-123-1234-1234-123456789012",
   185  			"",
   186  		},
   187  		{
   188  			"https://vcd.somecompany.org/api/entity/123456789-1234-1234-1234-123456789012",
   189  			"",
   190  		},
   191  		{
   192  			"https://vcd.somecompany.org/api/entity/12345678-12345-1234-1234-123456789012",
   193  			"",
   194  		},
   195  		{
   196  			"https://vcd.somecompany.org/api/entity/12345678-1234-1234-1234-1234567890123",
   197  			"",
   198  		},
   199  		{
   200  			"https://vcd.somecompany.org/api/entity/abcdefgh-1234-1234-1234-123456789012",
   201  			"",
   202  		},
   203  		{
   204  			"https://vcd.somecompany.org/api/entity/abcdef00-x234-w234-1234-123456789012",
   205  			"",
   206  		},
   207  	}
   208  
   209  	for _, it := range idTestList {
   210  		bareId, err := GetUuidFromHref(it.rawHref, true)
   211  		if err != nil {
   212  			t.Logf("error extracting UUID from HREF: %s", err)
   213  			t.Fail()
   214  		}
   215  		if bareId == it.expected {
   216  			if testVerbose {
   217  				t.Logf("ID '%s': found '%s' as expected", it.rawHref, it.expected)
   218  			}
   219  		} else {
   220  			t.Logf("error getting UUID from HREF: expected '%s' but found '%s'", it.expected, bareId)
   221  			t.Fail()
   222  		}
   223  	}
   224  	for _, it := range idTestExpectedToFailList {
   225  		bareId, err := GetUuidFromHref(it.rawHref, true)
   226  		if err == nil {
   227  			t.Logf("unexpected success with raw HREF %s", it.rawHref)
   228  			t.Fail()
   229  		}
   230  		if bareId == it.expected {
   231  			if testVerbose {
   232  				t.Logf("ID '%s': found '%s' as expected", it.rawHref, it.expected)
   233  			}
   234  		} else {
   235  			t.Logf("error getting UUID from HREF: expected '%s' but found '%s'", it.expected, bareId)
   236  			t.Fail()
   237  		}
   238  	}
   239  }
   240  
   241  // Tests reliability of GetUuidFromHref, which returns a bare UUID from an
   242  // entity HREF field when Id is not the end of URL
   243  func Test_GetUuidFromHref(t *testing.T) {
   244  
   245  	type idTest struct {
   246  		rawHref  string
   247  		expected string
   248  	}
   249  
   250  	idTestList := []idTest{
   251  		{
   252  			"https://vcd.somecompany.org/api/entity/97384890-180c-4563-b9b7-0dc50a2430b0",
   253  			"97384890-180c-4563-b9b7-0dc50a2430b0",
   254  		},
   255  		{
   256  			"https://vcd.somecompany.org/api/entity/deadbeef-0000-0000-0000-000000000000/action/reset",
   257  			"deadbeef-0000-0000-0000-000000000000",
   258  		},
   259  		{
   260  			"https://vcd.somecompany.org/api/entity/11111111-0000-0000-0000-000000000000",
   261  			"11111111-0000-0000-0000-000000000000",
   262  		},
   263  		{
   264  			"https://vcd.somecompany.org/api/entity/aaaaaaaa-bbbb-ccc0-dddd-eeeeeeeeeeee/network",
   265  			"aaaaaaaa-bbbb-ccc0-dddd-eeeeeeeeeeee",
   266  		},
   267  		{
   268  			"https://vcd.somecompany.org/api/entity/72fefde7-4fed-45b8-a774-79b72c870325",
   269  			"72fefde7-4fed-45b8-a774-79b72c870325",
   270  		},
   271  		{
   272  			"https://vcd.somecompany.org/api/entity/72fefde7-4fed-45b8-a774-79b72c870325/test1/test2/test3",
   273  			"72fefde7-4fed-45b8-a774-79b72c870325",
   274  		},
   275  		{
   276  			"https://vcd.somecompany.org/api/entity/72fefde7-4fed-45b8-a774-79b72c870325/",
   277  			"72fefde7-4fed-45b8-a774-79b72c870325",
   278  		},
   279  		{
   280  			"https://vcd.somecompany.org/api/entity/72fefde7-4fed-45b8-a774-79b72c870325/test1/",
   281  			"72fefde7-4fed-45b8-a774-79b72c870325",
   282  		},
   283  	}
   284  	idTestExpectedToFailList := []idTest{
   285  		{
   286  			"https://vcd.somecompany.org/api/entity/12345678-1234-1234-1234-12345678901",
   287  			"",
   288  		},
   289  		{
   290  			"https://vcd.somecompany.org/api/entity/1234567-1234-1234-1234-123456789012",
   291  			"",
   292  		},
   293  		{
   294  			"https://vcd.somecompany.org/api/entity/12345678-123-1234-1234-123456789012",
   295  			"",
   296  		},
   297  		{
   298  			"https://vcd.somecompany.org/api/entity/123456789-1234-1234-1234-123456789012",
   299  			"",
   300  		},
   301  		{
   302  			"https://vcd.somecompany.org/api/entity/12345678-12345-1234-1234-123456789012",
   303  			"",
   304  		},
   305  		{
   306  			"https://vcd.somecompany.org/api/entity/abcdefgh-1234-1234-1234-123456789012",
   307  			"",
   308  		},
   309  		{
   310  			"https://vcd.somecompany.org/api/entity/abcdef00-x234-w234-1234-123456789012",
   311  			"",
   312  		},
   313  	}
   314  
   315  	for _, it := range idTestList {
   316  		bareId, err := GetUuidFromHref(it.rawHref, false)
   317  		if err != nil {
   318  			t.Logf("error extracting UUID from HREF: %s", err)
   319  			t.Fail()
   320  		}
   321  		if bareId == it.expected {
   322  			if testVerbose {
   323  				t.Logf("ID '%s': found '%s' as expected", it.rawHref, it.expected)
   324  			}
   325  		} else {
   326  			t.Logf("error getting UUID from HREF: expected '%s' but found '%s'", it.expected, bareId)
   327  			t.Fail()
   328  		}
   329  	}
   330  	for _, it := range idTestExpectedToFailList {
   331  		bareId, err := GetUuidFromHref(it.rawHref, false)
   332  		if err == nil {
   333  			t.Logf("unexpected success with raw HREF %s", it.rawHref)
   334  			t.Fail()
   335  		}
   336  		if bareId == it.expected {
   337  			if testVerbose {
   338  				t.Logf("ID '%s': found '%s' as expected", it.rawHref, it.expected)
   339  			}
   340  		} else {
   341  			t.Logf("error getting UUID from HREF: expected '%s' but found '%s'", it.expected, bareId)
   342  			t.Fail()
   343  		}
   344  	}
   345  }