github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/internal/acceptance/clients/conditions.go (about)

     1  package clients
     2  
     3  import (
     4  	"os"
     5  	"strconv"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  // RequiredSystemScope will restrict a test to only be run by system scope.
    11  func RequiredSystemScope(t *testing.T) {
    12  	if os.Getenv("OS_SYSTEM_SCOPE") != "all" {
    13  		t.Skip("must use system scope to run this test")
    14  	}
    15  }
    16  
    17  // RequireManilaReplicas will restrict a test to only be run with enabled
    18  // manila replicas.
    19  func RequireManilaReplicas(t *testing.T) {
    20  	if os.Getenv("OS_MANILA_REPLICAS") != "true" {
    21  		t.Skip("manila replicas must be enabled to run this test")
    22  	}
    23  }
    24  
    25  // RequireAdmin will restrict a test to only be run by admin users.
    26  func RequireAdmin(t *testing.T) {
    27  	if os.Getenv("OS_USERNAME") != "admin" {
    28  		t.Skip("must be admin to run this test")
    29  	}
    30  }
    31  
    32  // RequireNonAdmin will restrict a test to only be run by non-admin users.
    33  func RequireNonAdmin(t *testing.T) {
    34  	if os.Getenv("OS_USERNAME") == "admin" {
    35  		t.Skip("must be a non-admin to run this test")
    36  	}
    37  }
    38  
    39  // RequirePortForwarding will restrict a test to only be run in environments
    40  // that support port forwarding
    41  func RequirePortForwarding(t *testing.T) {
    42  	if os.Getenv("OS_PORTFORWARDING_ENVIRONMENT") == "" {
    43  		t.Skip("this test requires support for port forwarding")
    44  	}
    45  }
    46  
    47  // RequireGuestAgent will restrict a test to only be run in
    48  // environments that support the QEMU guest agent.
    49  func RequireGuestAgent(t *testing.T) {
    50  	if os.Getenv("OS_GUEST_AGENT") == "" {
    51  		t.Skip("this test requires support for qemu guest agent and to set OS_GUEST_AGENT to 1")
    52  	}
    53  }
    54  
    55  // RequireIdentityV2 will restrict a test to only be run in
    56  // environments that support the Identity V2 API.
    57  func RequireIdentityV2(t *testing.T) {
    58  	if os.Getenv("OS_IDENTITY_API_VERSION") != "2.0" {
    59  		t.Skip("this test requires support for the identity v2 API")
    60  	}
    61  }
    62  
    63  // RequireLiveMigration will restrict a test to only be run in
    64  // environments that support live migration.
    65  func RequireLiveMigration(t *testing.T) {
    66  	if os.Getenv("OS_LIVE_MIGRATE") == "" {
    67  		t.Skip("this test requires support for live migration and to set OS_LIVE_MIGRATE to 1")
    68  	}
    69  }
    70  
    71  // RequireLong will ensure long-running tests can run.
    72  func RequireLong(t *testing.T) {
    73  	if testing.Short() {
    74  		t.Skip("skipping test in short mode")
    75  	}
    76  }
    77  
    78  // RequireNovaNetwork will restrict a test to only be run in
    79  // environments that support nova-network.
    80  func RequireNovaNetwork(t *testing.T) {
    81  	if os.Getenv("OS_NOVANET") == "" {
    82  		t.Skip("this test requires nova-network and to set OS_NOVANET to 1")
    83  	}
    84  }
    85  
    86  // RequireCinderNoAuth will restrict a test to be only run in environments that
    87  // have Cinder using noauth.
    88  func RequireCinderNoAuth(t *testing.T) {
    89  	if os.Getenv("CINDER_ENDPOINT") == "" || os.Getenv("OS_USERNAME") == "" {
    90  		t.Skip("this test requires Cinder using noauth, set OS_USERNAME and CINDER_ENDPOINT")
    91  	}
    92  }
    93  
    94  // RequireIronicNoAuth will restrict a test to be only run in environments that
    95  // have Ironic using noauth.
    96  func RequireIronicNoAuth(t *testing.T) {
    97  	if os.Getenv("IRONIC_ENDPOINT") == "" || os.Getenv("OS_USERNAME") == "" {
    98  		t.Skip("this test requires IRONIC using noauth, set OS_USERNAME and IRONIC_ENDPOINT")
    99  	}
   100  }
   101  
   102  // RequireIronicHTTPBasic will restrict a test to be only run in environments
   103  // that have Ironic using http_basic.
   104  func RequireIronicHTTPBasic(t *testing.T) {
   105  	if os.Getenv("IRONIC_ENDPOINT") == "" || os.Getenv("OS_USERNAME") == "" || os.Getenv("OS_PASSWORD") == "" {
   106  		t.Skip("this test requires Ironic using http_basic, set OS_USERNAME, OS_PASSWORD and IRONIC_ENDPOINT")
   107  	}
   108  }
   109  
   110  func getReleaseFromEnv(t *testing.T) string {
   111  	current := strings.TrimPrefix(os.Getenv("OS_BRANCH"), "stable/")
   112  	if current == "" {
   113  		t.Fatal("this test requires OS_BRANCH to be set but it wasn't")
   114  	}
   115  	return current
   116  }
   117  
   118  // SkipRelease will have the test be skipped on a certain
   119  // release. Releases are named such as 'stable/mitaka', master, etc.
   120  func SkipRelease(t *testing.T, release string) {
   121  	current := getReleaseFromEnv(t)
   122  	if current == release {
   123  		t.Skipf("this is not supported in %s", release)
   124  	}
   125  }
   126  
   127  // SkipReleasesBelow will have the test be skipped on releases below a certain
   128  // one. Releases are named such as 'stable/mitaka', master, etc.
   129  func SkipReleasesBelow(t *testing.T, release string) {
   130  	current := getReleaseFromEnv(t)
   131  
   132  	if IsCurrentBelow(t, release) {
   133  		t.Skipf("this is not supported below %s, testing in %s", release, current)
   134  	}
   135  }
   136  
   137  // SkipReleasesAbove will have the test be skipped on releases above a certain
   138  // one. The test is always skipped on master release. Releases are named such
   139  // as 'stable/mitaka', master, etc.
   140  func SkipReleasesAbove(t *testing.T, release string) {
   141  	current := getReleaseFromEnv(t)
   142  
   143  	if IsCurrentAbove(t, release) {
   144  		t.Skipf("this is not supported above %s, testing in %s", release, current)
   145  	}
   146  }
   147  
   148  func isReleaseNumeral(release string) bool {
   149  	_, err := strconv.Atoi(release[0:1])
   150  	return err == nil
   151  }
   152  
   153  // IsCurrentAbove will return true on releases above a certain
   154  // one. The result is always true on master release. Releases are named such
   155  // as 'stable/mitaka', master, etc.
   156  func IsCurrentAbove(t *testing.T, release string) bool {
   157  	current := getReleaseFromEnv(t)
   158  	release = strings.TrimPrefix(release, "stable/")
   159  
   160  	if release != "master" {
   161  		// Assume master is always too new
   162  		if current == "master" {
   163  			return true
   164  		}
   165  		// Numeral releases are always newer than non-numeral ones
   166  		if isReleaseNumeral(current) && !isReleaseNumeral(release) {
   167  			return true
   168  		}
   169  		if current > release && !(!isReleaseNumeral(current) && isReleaseNumeral(release)) {
   170  			return true
   171  		}
   172  	}
   173  	t.Logf("Target release %s is below the current branch %s", release, current)
   174  	return false
   175  }
   176  
   177  // IsCurrentBelow will return true on releases below a certain
   178  // one. Releases are named such as 'stable/mitaka', master, etc.
   179  func IsCurrentBelow(t *testing.T, release string) bool {
   180  	current := getReleaseFromEnv(t)
   181  	release = strings.TrimPrefix(release, "stable/")
   182  
   183  	if current != "master" {
   184  		// Assume master is always too new
   185  		if release == "master" {
   186  			return true
   187  		}
   188  		// Numeral releases are always newer than non-numeral ones
   189  		if isReleaseNumeral(release) && !isReleaseNumeral(current) {
   190  			return true
   191  		}
   192  		if release > current && !(!isReleaseNumeral(release) && isReleaseNumeral(current)) {
   193  			return true
   194  		}
   195  	}
   196  	t.Logf("Target release %s is above the current branch %s", release, current)
   197  	return false
   198  }