yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/loader/proxyfunc_test.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package loader
    16  
    17  import (
    18  	"net/http"
    19  	"net/url"
    20  	"testing"
    21  
    22  	"yunion.io/x/s3cli"
    23  
    24  	"yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/onecloud/pkg/util/httputils"
    27  )
    28  
    29  func TestProxyFunc(t *testing.T) {
    30  	runT := func(t *testing.T, cpcfg *cloudprovider.ProviderConfig) {
    31  		vendor := cpcfg.Vendor
    32  		proxied := false
    33  		proxyFunc := func(req *http.Request) (*url.URL, error) {
    34  			proxied = true
    35  			return url.Parse("http://TestProxyFunc" + "." + vendor + "/")
    36  		}
    37  		cpcfg.ProxyFunc = proxyFunc
    38  		_, err := cloudprovider.GetProvider(*cpcfg)
    39  		if !proxied {
    40  			if err != nil {
    41  				t.Logf("vendor %s: err: %v", vendor, err)
    42  			}
    43  			t.Errorf("vendor %s: proxyFunc not working", vendor)
    44  		}
    45  		t.Run("default client no proxy", func(t *testing.T) {
    46  			proxied = false
    47  			client := httputils.GetDefaultClient()
    48  			client.Get("http://default-client-no-proxy.TestProxyFunc." + vendor + "/")
    49  			if proxied {
    50  				t.Errorf("%s: default client proxy changed", vendor)
    51  			}
    52  		})
    53  	}
    54  
    55  	t.Parallel()
    56  	t.Run("cloud", func(t *testing.T) {
    57  		cpcfgs := map[string]*cloudprovider.ProviderConfig{
    58  			compute.CLOUD_PROVIDER_VMWARE:    &cloudprovider.ProviderConfig{},
    59  			compute.CLOUD_PROVIDER_QCLOUD:    &cloudprovider.ProviderConfig{},
    60  			compute.CLOUD_PROVIDER_AZURE:     &cloudprovider.ProviderConfig{},
    61  			compute.CLOUD_PROVIDER_HUAWEI:    &cloudprovider.ProviderConfig{},
    62  			compute.CLOUD_PROVIDER_OPENSTACK: &cloudprovider.ProviderConfig{},
    63  			compute.CLOUD_PROVIDER_ZSTACK:    &cloudprovider.ProviderConfig{},
    64  			compute.CLOUD_PROVIDER_UCLOUD:    &cloudprovider.ProviderConfig{},
    65  			compute.CLOUD_PROVIDER_ALIYUN:    &cloudprovider.ProviderConfig{},
    66  			compute.CLOUD_PROVIDER_AWS:       &cloudprovider.ProviderConfig{},
    67  			compute.CLOUD_PROVIDER_CTYUN:     &cloudprovider.ProviderConfig{},
    68  			compute.CLOUD_PROVIDER_GOOGLE:    &cloudprovider.ProviderConfig{},
    69  		}
    70  		for vendor := range cpcfgs {
    71  			cpcfgs[vendor].Vendor = vendor
    72  			cpcfgs[vendor].Id = vendor + "-Id"
    73  			cpcfgs[vendor].Name = vendor + "-Name"
    74  			cpcfgs[vendor].URL = "http://" + vendor + ".AccessUrl/TestProxyFunc"
    75  			cpcfgs[vendor].Account = vendor + "-Account"
    76  			cpcfgs[vendor].Secret = vendor + "-Secret"
    77  		}
    78  
    79  		if true {
    80  			cpcfgs[compute.CLOUD_PROVIDER_OPENSTACK].Account = "projectId/username/domainName"
    81  		}
    82  		if true {
    83  			cpcfgs[compute.CLOUD_PROVIDER_AZURE].URL = "AzureChinaCloud"
    84  			cpcfgs[compute.CLOUD_PROVIDER_AZURE].Account = "tenantId/subscriptionId"
    85  			cpcfgs[compute.CLOUD_PROVIDER_AZURE].Secret = "appId/appKey"
    86  		}
    87  		if true {
    88  			cpcfgs[compute.CLOUD_PROVIDER_UCLOUD].Account = "accessKey::projectId"
    89  		}
    90  		if true {
    91  			const pkey = `-----BEGIN RSA PRIVATE KEY-----` + "\n" +
    92  				`MIICXgIBAAKBgQDIUZ819CKNzPf3dL1aTC9tx6Y+Z/wQ74hWgyxM4DM5kxMZDoWd` + "\n" +
    93  				`2zzj7B8vUU64tYaeCaPFahxcMNs705wNACkFnmqop08zUSWTfbPg/uNdhGvuF0vT` + "\n" +
    94  				`3o5oNbUfVLrusvVJwP6mAnfJsECZJsPMqVIyM5z6uMBpAkjsaqEP7cOISwIDAQAB` + "\n" +
    95  				`AoGBAIzBXZ/ptcXxumM0skCau9DlspizNFkqFqDDdwNlnljcUVUP8S9kd7qnlZoI` + "\n" +
    96  				`BcqgMsElgOAcmWTmJ4Y4QLSZ4jgDthDUqt+dv384G4tUHv5LDU4SMnKiPFzqzsOn` + "\n" +
    97  				`PR72ZcoZZZW9PvNMmJDWSIwuEXgWovXhK5pO3wuuHUDjPHlxAkEA8DdcBRAV9yuq` + "\n" +
    98  				`AbVFSWxBIpNkXkIoOCQiBOP8LBTvua5Dkuxe5qZkDXuTupYmLRmIoDJuZVKo0jWw` + "\n" +
    99  				`6xBg1Io4BQJBANV7J3u5HV7IxlG35g8iCsU/AnLYQYWIWqF9+DKM8fqkDHGx6KGl` + "\n" +
   100  				`THG0gKGjhkWe/qzTsQbe4gHWZp6z1apVQA8CQQC9aDkzeMKJGTG8lQzj3urg82zx` + "\n" +
   101  				`caK62arFRmrA62U2eHSptZ0CqvI7O1R5jAjgCMEU547qb6nTzigIwOpIzA9tAkB6` + "\n" +
   102  				`EyaM1Vo6LU1axXJFDQ5jpJhd29P76/EHj+Ux4u8siEqlaTrB1KhWDQXRaJULktPH` + "\n" +
   103  				`OHZum11Z96RO9D8mXAF5AkEAq1xtCrJp8rWTGH5PGShyCOk2ZNbV0AL+may4FAgc` + "\n" +
   104  				`hbZ+WzxylVxMKJmWqEBAYF3/7oouyteF8Vq3TVOv442NSg==` + "\n" +
   105  				`-----END RSA PRIVATE KEY-----`
   106  			cpcfgs[compute.CLOUD_PROVIDER_GOOGLE].Account = "projectId/email"
   107  			cpcfgs[compute.CLOUD_PROVIDER_GOOGLE].Secret = "keyid/" + pkey
   108  		}
   109  		for _, cpcfg := range cpcfgs {
   110  			cpcfg := cpcfg
   111  			t.Run(cpcfg.Vendor, func(t *testing.T) {
   112  				//t.Parallel()
   113  				runT(t, cpcfg)
   114  			})
   115  		}
   116  	})
   117  	t.Run("objectstore", func(t *testing.T) {
   118  		s3cli.MaxRetry = 1
   119  		cpcfgs := map[string]*cloudprovider.ProviderConfig{
   120  			compute.CLOUD_PROVIDER_CEPH:      &cloudprovider.ProviderConfig{},
   121  			compute.CLOUD_PROVIDER_XSKY:      &cloudprovider.ProviderConfig{},
   122  			compute.CLOUD_PROVIDER_GENERICS3: &cloudprovider.ProviderConfig{},
   123  		}
   124  		for vendor := range cpcfgs {
   125  			cpcfgs[vendor].Vendor = vendor
   126  			cpcfgs[vendor].Id = vendor + "-Id"
   127  			cpcfgs[vendor].Name = vendor + "-Name"
   128  			cpcfgs[vendor].URL = "http://" + vendor + ".AccessUrl/TestProxyFunc"
   129  			cpcfgs[vendor].Account = vendor + "-Account"
   130  			cpcfgs[vendor].Secret = vendor + "-Secret"
   131  		}
   132  		for _, cpcfg := range cpcfgs {
   133  			cpcfg := cpcfg
   134  			t.Run(cpcfg.Vendor, func(t *testing.T) {
   135  				//t.Parallel()
   136  				runT(t, cpcfg)
   137  			})
   138  		}
   139  	})
   140  }