github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/testing/util_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"os"
     7  	"path/filepath"
     8  	"reflect"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/vnpaycloud-console/gophercloud/v2"
    14  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    15  )
    16  
    17  func TestWaitFor(t *testing.T) {
    18  	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
    19  	defer cancel()
    20  
    21  	err := gophercloud.WaitFor(ctx, func(context.Context) (bool, error) {
    22  		return true, nil
    23  	})
    24  	th.CheckNoErr(t, err)
    25  }
    26  
    27  func TestWaitForTimeout(t *testing.T) {
    28  	if testing.Short() {
    29  		t.Skip("skipping test in short mode.")
    30  	}
    31  
    32  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
    33  	defer cancel()
    34  
    35  	err := gophercloud.WaitFor(ctx, func(context.Context) (bool, error) {
    36  		return false, nil
    37  	})
    38  	th.AssertErrIs(t, err, context.DeadlineExceeded)
    39  }
    40  
    41  func TestWaitForError(t *testing.T) {
    42  	if testing.Short() {
    43  		t.Skip("skipping test in short mode.")
    44  	}
    45  
    46  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
    47  	defer cancel()
    48  
    49  	err := gophercloud.WaitFor(ctx, func(context.Context) (bool, error) {
    50  		return false, errors.New("Error has occurred")
    51  	})
    52  	th.AssertEquals(t, "Error has occurred", err.Error())
    53  }
    54  
    55  func TestWaitForPredicateExceed(t *testing.T) {
    56  	if testing.Short() {
    57  		t.Skip("skipping test in short mode.")
    58  	}
    59  
    60  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
    61  	defer cancel()
    62  
    63  	err := gophercloud.WaitFor(ctx, func(ctx context.Context) (bool, error) {
    64  		// NOTE: predicate should obey context cancellation
    65  		select {
    66  		case <-ctx.Done():
    67  			return true, ctx.Err()
    68  
    69  		case <-time.After(4 * time.Second):
    70  			return false, errors.New("Just wasting time")
    71  		}
    72  	})
    73  	th.AssertErrIs(t, err, context.DeadlineExceeded)
    74  }
    75  
    76  func TestNormalizeURL(t *testing.T) {
    77  	urls := []string{
    78  		"NoSlashAtEnd",
    79  		"SlashAtEnd/",
    80  	}
    81  	expected := []string{
    82  		"NoSlashAtEnd/",
    83  		"SlashAtEnd/",
    84  	}
    85  	for i := 0; i < len(expected); i++ {
    86  		th.CheckEquals(t, expected[i], gophercloud.NormalizeURL(urls[i]))
    87  	}
    88  }
    89  
    90  func TestNormalizePathURL(t *testing.T) {
    91  	baseDir, _ := os.Getwd()
    92  
    93  	rawPath := "template.yaml"
    94  	basePath, _ := filepath.Abs(".")
    95  	result, _ := gophercloud.NormalizePathURL(basePath, rawPath)
    96  	expected := strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "template.yaml"}, "/")
    97  	th.CheckEquals(t, expected, result)
    98  
    99  	rawPath = "http://www.google.com"
   100  	basePath, _ = filepath.Abs(".")
   101  	result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
   102  	expected = "http://www.google.com"
   103  	th.CheckEquals(t, expected, result)
   104  
   105  	rawPath = "very/nested/file.yaml"
   106  	basePath, _ = filepath.Abs(".")
   107  	result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
   108  	expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "very/nested/file.yaml"}, "/")
   109  	th.CheckEquals(t, expected, result)
   110  
   111  	rawPath = "very/nested/file.yaml"
   112  	basePath = "http://www.google.com"
   113  	result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
   114  	expected = "http://www.google.com/very/nested/file.yaml"
   115  	th.CheckEquals(t, expected, result)
   116  
   117  	rawPath = "very/nested/file.yaml/"
   118  	basePath = "http://www.google.com/"
   119  	result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
   120  	expected = "http://www.google.com/very/nested/file.yaml"
   121  	th.CheckEquals(t, expected, result)
   122  
   123  	rawPath = "very/nested/file.yaml"
   124  	basePath = "http://www.google.com/even/more"
   125  	result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
   126  	expected = "http://www.google.com/even/more/very/nested/file.yaml"
   127  	th.CheckEquals(t, expected, result)
   128  
   129  	rawPath = "very/nested/file.yaml"
   130  	basePath = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more"}, "/")
   131  	result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
   132  	expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more/very/nested/file.yaml"}, "/")
   133  	th.CheckEquals(t, expected, result)
   134  
   135  	rawPath = "very/nested/file.yaml/"
   136  	basePath = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more"}, "/")
   137  	result, _ = gophercloud.NormalizePathURL(basePath, rawPath)
   138  	expected = strings.Join([]string{"file:/", filepath.ToSlash(baseDir), "only/file/even/more/very/nested/file.yaml"}, "/")
   139  	th.CheckEquals(t, expected, result)
   140  
   141  }
   142  
   143  func TestRemainingKeys(t *testing.T) {
   144  	type User struct {
   145  		UserID    string `json:"user_id"`
   146  		Username  string `json:"username"`
   147  		Location  string `json:"-"`
   148  		CreatedAt string `json:"-"`
   149  		Status    string
   150  		IsAdmin   bool
   151  	}
   152  
   153  	userResponse := map[string]any{
   154  		"user_id":      "abcd1234",
   155  		"username":     "jdoe",
   156  		"location":     "Hawaii",
   157  		"created_at":   "2017-06-08T02:49:03.000000",
   158  		"status":       "active",
   159  		"is_admin":     "true",
   160  		"custom_field": "foo",
   161  	}
   162  
   163  	expected := map[string]any{
   164  		"created_at":   "2017-06-08T02:49:03.000000",
   165  		"is_admin":     "true",
   166  		"custom_field": "foo",
   167  	}
   168  
   169  	actual := gophercloud.RemainingKeys(User{}, userResponse)
   170  
   171  	isEqual := reflect.DeepEqual(expected, actual)
   172  	if !isEqual {
   173  		t.Fatalf("expected %s but got %s", expected, actual)
   174  	}
   175  }