github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/config/clouds/clouds_test.go (about)

     1  package clouds_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/config/clouds"
    11  )
    12  
    13  func ExampleWithCloudName() {
    14  	const exampleClouds = `clouds:
    15    openstack:
    16      auth:
    17        auth_url: https://example.com:13000`
    18  
    19  	ao, _, _, err := clouds.Parse(
    20  		clouds.WithCloudsYAML(strings.NewReader(exampleClouds)),
    21  		clouds.WithCloudName("openstack"),
    22  	)
    23  	if err != nil {
    24  		panic(err)
    25  	}
    26  
    27  	fmt.Println(ao.IdentityEndpoint)
    28  	// Output: https://example.com:13000
    29  }
    30  
    31  func ExampleWithUserID() {
    32  	const exampleClouds = `clouds:
    33    openstack:
    34      auth:
    35        auth_url: https://example.com:13000`
    36  
    37  	ao, _, _, err := clouds.Parse(
    38  		clouds.WithCloudsYAML(strings.NewReader(exampleClouds)),
    39  		clouds.WithCloudName("openstack"),
    40  		clouds.WithUsername("Kris"),
    41  	)
    42  	if err != nil {
    43  		panic(err)
    44  	}
    45  
    46  	fmt.Println(ao.Username)
    47  	// Output: Kris
    48  }
    49  
    50  func ExampleWithRegion() {
    51  	const exampleClouds = `clouds:
    52    openstack:
    53      auth:
    54        auth_url: https://example.com:13000`
    55  
    56  	_, eo, _, err := clouds.Parse(
    57  		clouds.WithCloudsYAML(strings.NewReader(exampleClouds)),
    58  		clouds.WithCloudName("openstack"),
    59  		clouds.WithRegion("mars"),
    60  	)
    61  	if err != nil {
    62  		panic(err)
    63  	}
    64  
    65  	fmt.Println(eo.Region)
    66  	// Output: mars
    67  }
    68  
    69  func TestParse(t *testing.T) {
    70  	const tempDirPrefix = "gophercloud-test-"
    71  
    72  	rmTmpDirOrPanic := func(tmpDir string) {
    73  		if err := os.RemoveAll(tmpDir); err != nil {
    74  			panic("unable to remove the temporary files: " + err.Error())
    75  		}
    76  	}
    77  
    78  	t.Run("parses the local clouds.yaml and secure.yaml if present", func(t *testing.T) {
    79  		const cloudsYAML = `clouds:
    80    gophercloud-test:
    81      auth:
    82        auth_url: https://example.com/gophercloud-test-12345:13000`
    83  		const secureYAML = `clouds:
    84    gophercloud-test:
    85      auth:
    86        password: secret
    87        username: gophercloud-test-username`
    88  
    89  		tmpDir, err := os.MkdirTemp(os.TempDir(), tempDirPrefix)
    90  		if err != nil {
    91  			t.Fatalf("unable to create a temporary directory: %v", err)
    92  		}
    93  		defer rmTmpDirOrPanic(tmpDir)
    94  
    95  		cwd, err := os.Getwd()
    96  		if err != nil {
    97  			t.Fatalf("unable to determine the current working directory: %v", err)
    98  		}
    99  		if err := os.Chdir(tmpDir); err != nil {
   100  			t.Fatalf("unable to move to a temporary directory: %v", err)
   101  		}
   102  		defer func() {
   103  			if err := os.Chdir(cwd); err != nil {
   104  				panic("unable to reset the current working directory: " + err.Error())
   105  			}
   106  		}()
   107  
   108  		if err := os.WriteFile("clouds.yaml", []byte(cloudsYAML), 0644); err != nil {
   109  			t.Fatalf("unable to create a mock clouds.yaml file: %v", err)
   110  		}
   111  
   112  		if err := os.WriteFile("secure.yaml", []byte(secureYAML), 0644); err != nil {
   113  			t.Fatalf("unable to create a mock secure.yaml file: %v", err)
   114  		}
   115  
   116  		ao, _, _, err := clouds.Parse(
   117  			clouds.WithCloudName("gophercloud-test"),
   118  		)
   119  		if err != nil {
   120  			t.Fatalf("unexpected error: %v", err)
   121  		}
   122  
   123  		if got := ao.IdentityEndpoint; got != "https://example.com/gophercloud-test-12345:13000" {
   124  			t.Errorf("unexpected identity endpoint: %q", got)
   125  		}
   126  
   127  		if got := ao.Username; got != "gophercloud-test-username" {
   128  			t.Errorf("unexpected username: %q", got)
   129  		}
   130  	})
   131  
   132  	t.Run("parses the locations in order", func(t *testing.T) {
   133  		const cloudsYAML1 = `clouds:
   134    gophercloud-test:
   135      auth:
   136        auth_url: https://example.com/gophercloud-test-1:13000`
   137  		const cloudsYAML2 = `clouds:
   138    gophercloud-test:
   139      auth:
   140        auth_url: https://example.com/gophercloud-test-2:13000`
   141  
   142  		tmpDir1, err := os.MkdirTemp(os.TempDir(), tempDirPrefix)
   143  		if err != nil {
   144  			t.Fatalf("unable to create a temporary directory: %v", err)
   145  		}
   146  		defer rmTmpDirOrPanic(tmpDir1)
   147  
   148  		tmpDir2, err := os.MkdirTemp(os.TempDir(), tempDirPrefix)
   149  		if err != nil {
   150  			t.Fatalf("unable to create a temporary directory: %v", err)
   151  		}
   152  		defer rmTmpDirOrPanic(tmpDir2)
   153  
   154  		cloudsPath1, cloudsPath2 := path.Join(tmpDir1, "clouds.yaml"), path.Join(tmpDir2, "clouds.yaml")
   155  
   156  		if err := os.WriteFile(cloudsPath1, []byte(cloudsYAML1), 0644); err != nil {
   157  			t.Fatalf("unable to create a mock clouds.yaml file in path %q: %v", cloudsPath1, err)
   158  		}
   159  		if err := os.WriteFile(cloudsPath2, []byte(cloudsYAML2), 0644); err != nil {
   160  			t.Fatalf("unable to create a mock clouds.yaml file in path %q: %v", cloudsPath2, err)
   161  		}
   162  
   163  		ao, _, _, err := clouds.Parse(
   164  			clouds.WithCloudName("gophercloud-test"),
   165  			clouds.WithLocations(cloudsPath1, cloudsPath2),
   166  		)
   167  		if err != nil {
   168  			t.Fatalf("unexpected error: %v", err)
   169  		}
   170  
   171  		if got := ao.IdentityEndpoint; got != "https://example.com/gophercloud-test-1:13000" {
   172  			t.Errorf("unexpected identity endpoint: %q", got)
   173  		}
   174  	})
   175  
   176  	t.Run("falls back to the next location if clouds.yaml is not found", func(t *testing.T) {
   177  		const cloudsYAML1 = `clouds:
   178    gophercloud-test:
   179      auth:
   180        auth_url: https://example.com/gophercloud-test-1:13000`
   181  		const cloudsYAML2 = `clouds:
   182    gophercloud-test:
   183      auth:
   184        auth_url: https://example.com/gophercloud-test-2:13000`
   185  
   186  		tmpDir0, err := os.MkdirTemp(os.TempDir(), tempDirPrefix)
   187  		if err != nil {
   188  			t.Fatalf("unable to create a temporary directory: %v", err)
   189  		}
   190  		defer rmTmpDirOrPanic(tmpDir0)
   191  
   192  		tmpDir1, err := os.MkdirTemp(os.TempDir(), tempDirPrefix)
   193  		if err != nil {
   194  			t.Fatalf("unable to create a temporary directory: %v", err)
   195  		}
   196  		defer rmTmpDirOrPanic(tmpDir1)
   197  
   198  		tmpDir2, err := os.MkdirTemp(os.TempDir(), tempDirPrefix)
   199  		if err != nil {
   200  			t.Fatalf("unable to create a temporary directory: %v", err)
   201  		}
   202  		defer rmTmpDirOrPanic(tmpDir2)
   203  
   204  		cloudsPath0, cloudsPath1, cloudsPath2 := path.Join(tmpDir0, "clouds.yaml"), path.Join(tmpDir1, "clouds.yaml"), path.Join(tmpDir2, "clouds.yaml")
   205  
   206  		if err := os.WriteFile(cloudsPath1, []byte(cloudsYAML1), 0644); err != nil {
   207  			t.Fatalf("unable to create a mock clouds.yaml file in path %q: %v", cloudsPath1, err)
   208  		}
   209  		if err := os.WriteFile(cloudsPath2, []byte(cloudsYAML2), 0644); err != nil {
   210  			t.Fatalf("unable to create a mock clouds.yaml file in path %q: %v", cloudsPath2, err)
   211  		}
   212  
   213  		ao, _, _, err := clouds.Parse(
   214  			clouds.WithCloudName("gophercloud-test"),
   215  			clouds.WithLocations(cloudsPath0, cloudsPath1, cloudsPath2),
   216  		)
   217  		if err != nil {
   218  			t.Fatalf("unexpected error: %v", err)
   219  		}
   220  
   221  		if got := ao.IdentityEndpoint; got != "https://example.com/gophercloud-test-1:13000" {
   222  			t.Errorf("unexpected identity endpoint: %q", got)
   223  		}
   224  	})
   225  }