github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/rts/v1/stacks/environment_test.go (about)

     1  package stacks
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/url"
     7  	"strings"
     8  	"testing"
     9  
    10  	th "github.com/huaweicloud/golangsdk/testhelper"
    11  )
    12  
    13  func TestEnvironmentValidation(t *testing.T) {
    14  
    15  	environmentJSON := new(Environment)
    16  	environmentJSON.Bin = []byte(ValidJSONEnvironment)
    17  	err := environmentJSON.Validate()
    18  	th.AssertNoErr(t, err)
    19  
    20  	environmentYAML := new(Environment)
    21  	environmentYAML.Bin = []byte(ValidYAMLEnvironment)
    22  	err = environmentYAML.Validate()
    23  	th.AssertNoErr(t, err)
    24  
    25  	environmentInvalid := new(Environment)
    26  	environmentInvalid.Bin = []byte(InvalidEnvironment)
    27  	if err = environmentInvalid.Validate(); err == nil {
    28  		t.Error("environment validation did not catch invalid environment")
    29  	}
    30  }
    31  
    32  func TestEnvironmentParsing(t *testing.T) {
    33  	environmentJSON := new(Environment)
    34  	environmentJSON.Bin = []byte(ValidJSONEnvironment)
    35  	err := environmentJSON.Parse()
    36  	th.AssertNoErr(t, err)
    37  	th.AssertDeepEquals(t, ValidJSONEnvironmentParsed, environmentJSON.Parsed)
    38  
    39  	environmentYAML := new(Environment)
    40  	environmentYAML.Bin = []byte(ValidJSONEnvironment)
    41  	err = environmentYAML.Parse()
    42  	th.AssertNoErr(t, err)
    43  	th.AssertDeepEquals(t, ValidJSONEnvironmentParsed, environmentYAML.Parsed)
    44  
    45  	environmentInvalid := new(Environment)
    46  	environmentInvalid.Bin = []byte("Keep Austin Weird")
    47  	err = environmentInvalid.Parse()
    48  	if err == nil {
    49  		t.Error("environment parsing did not catch invalid environment")
    50  	}
    51  }
    52  
    53  func TestIgnoreIfEnvironment(t *testing.T) {
    54  	var keyValueTests = []struct {
    55  		key   string
    56  		value interface{}
    57  		out   bool
    58  	}{
    59  		{"base_url", "afksdf", true},
    60  		{"not_type", "hooks", false},
    61  		{"get_file", "::", true},
    62  		{"hooks", "dfsdfsd", true},
    63  		{"type", "sdfubsduf.yaml", false},
    64  		{"type", "sdfsdufs.environment", false},
    65  		{"type", "sdfsdf.file", false},
    66  		{"type", map[string]string{"key": "value"}, true},
    67  	}
    68  	var result bool
    69  	for _, kv := range keyValueTests {
    70  		result = ignoreIfEnvironment(kv.key, kv.value)
    71  		if result != kv.out {
    72  			t.Errorf("key: %v, value: %v expected: %v, actual: %v", kv.key, kv.value, kv.out, result)
    73  		}
    74  	}
    75  }
    76  
    77  func TestGetRRFileContents(t *testing.T) {
    78  	th.SetupHTTP()
    79  	defer th.TeardownHTTP()
    80  	environmentContent := `
    81  heat_template_version: 2013-05-23
    82  
    83  description:
    84    Heat WordPress template to support F18, using only Heat OpenStack-native
    85    resource types, and without the requirement for heat-cfntools in the image.
    86    WordPress is web software you can use to create a beautiful website or blog.
    87    This template installs a single-instance WordPress deployment using a local
    88    MySQL database to store the data.
    89  
    90  parameters:
    91  
    92    key_name:
    93      type: string
    94      description : Name of a KeyPair to enable SSH access to the instance
    95  
    96  resources:
    97    wordpress_instance:
    98      type: OS::Nova::Server
    99      properties:
   100        image: { get_param: image_id }
   101        flavor: { get_param: instance_type }
   102        key_name: { get_param: key_name }`
   103  
   104  	dbContent := `
   105  heat_template_version: 2014-10-16
   106  
   107  description:
   108    Test template for Trove resource capabilities
   109  
   110  parameters:
   111    db_pass:
   112      type: string
   113      hidden: true
   114      description: Database access password
   115      default: secrete
   116  
   117  resources:
   118  
   119  service_db:
   120    type: OS::Trove::Instance
   121    properties:
   122      name: trove_test_db
   123      datastore_type: mariadb
   124      flavor: 1GB Instance
   125      size: 10
   126      databases:
   127      - name: test_data
   128      users:
   129      - name: kitchen_sink
   130        password: { get_param: db_pass }
   131        databases: [ test_data ]`
   132  	baseurl, err := getBasePath()
   133  	th.AssertNoErr(t, err)
   134  
   135  	fakeEnvURL := strings.Join([]string{baseurl, "my_env.yaml"}, "/")
   136  	urlparsed, err := url.Parse(fakeEnvURL)
   137  	th.AssertNoErr(t, err)
   138  	// handler for my_env.yaml
   139  	th.Mux.HandleFunc(urlparsed.Path, func(w http.ResponseWriter, r *http.Request) {
   140  		th.TestMethod(t, r, "GET")
   141  		w.Header().Set("Content-Type", "application/json")
   142  		w.WriteHeader(http.StatusOK)
   143  		fmt.Fprintf(w, environmentContent)
   144  	})
   145  
   146  	fakeDBURL := strings.Join([]string{baseurl, "my_db.yaml"}, "/")
   147  	urlparsed, err = url.Parse(fakeDBURL)
   148  	th.AssertNoErr(t, err)
   149  
   150  	// handler for my_db.yaml
   151  	th.Mux.HandleFunc(urlparsed.Path, func(w http.ResponseWriter, r *http.Request) {
   152  		th.TestMethod(t, r, "GET")
   153  		w.Header().Set("Content-Type", "application/json")
   154  		w.WriteHeader(http.StatusOK)
   155  		fmt.Fprintf(w, dbContent)
   156  	})
   157  
   158  	client := fakeClient{BaseClient: getHTTPClient()}
   159  	env := new(Environment)
   160  	env.Bin = []byte(`{"resource_registry": {"My::WP::Server": "my_env.yaml", "resources": {"my_db_server": {"OS::DBInstance": "my_db.yaml"}}}}`)
   161  	env.client = client
   162  
   163  	err = env.Parse()
   164  	th.AssertNoErr(t, err)
   165  	err = env.getRRFileContents(ignoreIfEnvironment)
   166  	th.AssertNoErr(t, err)
   167  	expectedEnvFilesContent := "\nheat_template_version: 2013-05-23\n\ndescription:\n  Heat WordPress template to support F18, using only Heat OpenStack-native\n  resource types, and without the requirement for heat-cfntools in the image.\n  WordPress is web software you can use to create a beautiful website or blog.\n  This template installs a single-instance WordPress deployment using a local\n  MySQL database to store the data.\n\nparameters:\n\n  key_name:\n    type: string\n    description : Name of a KeyPair to enable SSH access to the instance\n\nresources:\n  wordpress_instance:\n    type: OS::Nova::Server\n    properties:\n      image: { get_param: image_id }\n      flavor: { get_param: instance_type }\n      key_name: { get_param: key_name }"
   168  	expectedDBFilesContent := "\nheat_template_version: 2014-10-16\n\ndescription:\n  Test template for Trove resource capabilities\n\nparameters:\n  db_pass:\n    type: string\n    hidden: true\n    description: Database access password\n    default: secrete\n\nresources:\n\nservice_db:\n  type: OS::Trove::Instance\n  properties:\n    name: trove_test_db\n    datastore_type: mariadb\n    flavor: 1GB Instance\n    size: 10\n    databases:\n    - name: test_data\n    users:\n    - name: kitchen_sink\n      password: { get_param: db_pass }\n      databases: [ test_data ]"
   169  
   170  	th.AssertEquals(t, expectedEnvFilesContent, env.Files[fakeEnvURL])
   171  	th.AssertEquals(t, expectedDBFilesContent, env.Files[fakeDBURL])
   172  
   173  	// Update env's fileMaps to replace relative filenames by absolute URLs.
   174  	env.fileMaps = map[string]string{
   175  		"my_env.yaml": fakeEnvURL,
   176  		"my_db.yaml":  fakeDBURL,
   177  	}
   178  	env.fixFileRefs()
   179  
   180  	expectedParsed := map[string]interface{}{
   181  		"resource_registry": map[string]interface{}{
   182  			"My::WP::Server": fakeEnvURL,
   183  			"resources": map[string]interface{}{
   184  				"my_db_server": map[string]interface{}{
   185  					"OS::DBInstance": fakeDBURL,
   186  				},
   187  			},
   188  		},
   189  	}
   190  	env.Parse()
   191  	th.AssertDeepEquals(t, expectedParsed, env.Parsed)
   192  }