github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/orchestration/v1/stacks/environment_test.go (about)

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