github.com/openshift/installer@v1.4.17/pkg/asset/manifests/openstack/cloudproviderconfig_test.go (about) 1 package openstack 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/gophercloud/utils/v2/openstack/clientconfig" 8 "github.com/stretchr/testify/assert" 9 10 "github.com/openshift/installer/pkg/types" 11 "github.com/openshift/installer/pkg/types/openstack" 12 ) 13 14 func TestCloudProviderConfigSecret(t *testing.T) { 15 cloud := clientconfig.Cloud{ 16 AuthInfo: &clientconfig.AuthInfo{ 17 Username: "my_user", 18 Password: "my_secret_password", 19 AuthURL: "https://my_auth_url.com/v3/", 20 ProjectID: "f12f928576ae4d21bdb984da5dd1d3bf", 21 DomainID: "default", 22 DomainName: "Default", 23 }, 24 RegionName: "my_region", 25 } 26 27 expectedConfig := `[Global] 28 auth-url = "https://my_auth_url.com/v3/" 29 username = "my_user" 30 password = "my_secret_password" 31 tenant-id = "f12f928576ae4d21bdb984da5dd1d3bf" 32 domain-id = "default" 33 domain-name = "Default" 34 region = "my_region" 35 ` 36 actualConfig, err := CloudProviderConfigSecret(&cloud) 37 assert.NoError(t, err, "failed to create cloud provider config") 38 assert.Equal(t, expectedConfig, string(actualConfig), "unexpected cloud provider config") 39 } 40 41 func TestCloudProviderConfigSecretUserDomain(t *testing.T) { 42 cloud := clientconfig.Cloud{ 43 AuthInfo: &clientconfig.AuthInfo{ 44 Username: "my_user", 45 Password: "my_secret_password", 46 AuthURL: "https://my_auth_url.com/v3/", 47 ProjectID: "f12f928576ae4d21bdb984da5dd1d3bf", 48 UserDomainID: "default", 49 UserDomainName: "Default", 50 }, 51 RegionName: "my_region", 52 } 53 54 expectedConfig := `[Global] 55 auth-url = "https://my_auth_url.com/v3/" 56 username = "my_user" 57 password = "my_secret_password" 58 tenant-id = "f12f928576ae4d21bdb984da5dd1d3bf" 59 domain-id = "default" 60 domain-name = "Default" 61 region = "my_region" 62 ` 63 actualConfig, err := CloudProviderConfigSecret(&cloud) 64 assert.NoError(t, err, "failed to create cloud provider config") 65 assert.Equal(t, expectedConfig, string(actualConfig), "unexpected cloud provider config") 66 } 67 68 func TestCloudProviderConfigSecretQuoting(t *testing.T) { 69 passwords := map[string]string{ 70 "regular": "regular", 71 "with\\n": "with\\\\n", 72 "with#": "with#", 73 "with$": "with$", 74 "with;": "with;", 75 "with \n \" \\ ": "with \\n \\\" \\\\ ", 76 "with!": "with!", 77 "with?": "with?", 78 "with`": "with`", 79 } 80 81 for k, v := range passwords { 82 cloud := clientconfig.Cloud{ 83 AuthInfo: &clientconfig.AuthInfo{ 84 Password: k, 85 }, 86 } 87 88 expectedConfig := `[Global] 89 password = "` + v + `" 90 ` 91 actualConfig, err := CloudProviderConfigSecret(&cloud) 92 assert.NoError(t, err, "failed to create cloud provider config") 93 assert.Equal(t, expectedConfig, string(actualConfig), "unexpected cloud provider config") 94 } 95 } 96 97 func TestCloudProviderConfig(t *testing.T) { 98 cases := []struct { 99 name string 100 installConfig *types.InstallConfig 101 expectedConfig string 102 }{ 103 { 104 name: "default install config", 105 installConfig: &types.InstallConfig{ 106 Networking: &types.Networking{}, 107 Platform: types.Platform{ 108 OpenStack: &openstack.Platform{}, 109 }, 110 }, 111 expectedConfig: `[Global] 112 secret-name = openstack-credentials 113 secret-namespace = kube-system 114 region = my_region 115 `, 116 }, 117 } 118 119 cloud := clientconfig.Cloud{ 120 AuthInfo: &clientconfig.AuthInfo{ 121 Username: "my_user", 122 Password: "my_secret_password", 123 AuthURL: "https://my_auth_url.com/v3/", 124 ProjectID: "f12f928576ae4d21bdb984da5dd1d3bf", 125 DomainID: "default", 126 DomainName: "Default", 127 }, 128 RegionName: "my_region", 129 } 130 131 for _, tc := range cases { 132 t.Run(tc.name, func(t *testing.T) { 133 actualConfig, _, err := generateCloudProviderConfig(context.Background(), nil, &cloud, *tc.installConfig) 134 assert.NoError(t, err, "unexpected error when generating cloud provider config") 135 assert.Equal(t, tc.expectedConfig, actualConfig, "unexpected cloud provider config") 136 }) 137 } 138 }