github.com/gophercloud/gophercloud@v1.11.0/testing/util_test.go (about)

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