github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/cloud/validations_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cloud_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/cloud"
    11  )
    12  
    13  func (s *cloudSuite) TestValidateValidCloud(c *gc.C) {
    14  	validCloud := `
    15            clouds:
    16              vmwarestack-trusty:
    17                type: maas
    18                description: A mass cloud
    19                auth-types: [oauth1]
    20                host-cloud-region: host/region
    21                endpoint: http://10.245.200.27/MAAS
    22                config:
    23                  default-series: trusty
    24                  bootstrap-timeout: 900
    25                  http-proxy: http://10.245.200.27:8000/
    26                regions:
    27                  dev1:
    28                    endpoint: https://openstack.example.com:35574/v3.0/
    29                ca-certificates:
    30                - |-
    31                  -----BEGIN CERTIFICATE-----
    32                  -----END CERTIFICATE-----`
    33  
    34  	yaml := []byte(validCloud)
    35  	err := cloud.ValidateCloudSet(yaml)
    36  	c.Assert(err, gc.IsNil)
    37  }
    38  
    39  func (s *cloudSuite) TestValidateInvalidCloud(c *gc.C) {
    40  	validCloud := `
    41            clouds:
    42              vmwarestack-trusty:
    43                tpe: maas
    44                descript: A mass cloud
    45                auth-types: [oauth1]
    46                endpoint: http://10.245.200.27/MAAS
    47                config:
    48                  default-series: trusty
    49                  bootstrap-timeout: 900
    50                  http-proxy: http://10.245.200.27:8000/
    51                regions:
    52                  dev1:
    53                    endpont: https://openstack.example.com:35574/v3.0/`
    54  
    55  	yaml := []byte(validCloud)
    56  	err := cloud.ValidateCloudSet(yaml)
    57  	c.Assert(err.Error(), jc.Contains, `"endpont" is invalid. Perhaps you mean "endpoint"`)
    58  	c.Assert(err.Error(), jc.Contains, `"descript" is invalid. Perhaps you mean "description"`)
    59  	c.Assert(err.Error(), jc.Contains, `"tpe" is invalid. Perhaps you mean "type"`)
    60  }
    61  
    62  func (s *cloudSuite) TestValidateMultipleValidClouds(c *gc.C) {
    63  	validCloud := `
    64            clouds:
    65              vmwarestack-trusty:
    66                type: maas
    67                auth-types: [oauth1]
    68                endpoint: http://10.245.200.27/MAAS
    69                config:
    70                  default-series: trusty
    71                  bootstrap-timeout: 900
    72                  http-proxy: http://10.245.200.27:8000/
    73                regions:
    74                  dev1:
    75                    endpoint: https://openstack.example.com:35574/v3.0/
    76              vmwarestack-xenial:
    77                type: maas
    78                auth-types: [oauth1]
    79                endpoint: http://10.245.200.28/MAAS
    80                config:
    81                  default-series: xenial
    82                  bootstrap-timeout: 900
    83                  http-proxy: http://10.245.200.28:8000/
    84                regions:
    85                  dev1:
    86                    endpoint: https://openstack.example.com:35575/v3.0/`
    87  
    88  	yaml := []byte(validCloud)
    89  	err := cloud.ValidateCloudSet(yaml)
    90  	c.Assert(err, gc.IsNil)
    91  }
    92  
    93  func (s *cloudSuite) TestValidateMultipleInvalidClouds(c *gc.C) {
    94  	validCloud := `
    95            clouds:
    96              vmwarestack-trusty:
    97                type: maas
    98                auth-types: [oauth1]
    99                endpoint: http://10.245.200.27/MAAS
   100                config:
   101                  default-series: trusty
   102                  bootstrap-timeout: 900
   103                  http-proxy: http://10.245.200.27:8000/
   104                regions:
   105                  dev1:
   106                    endpoint: https://openstack.example.com:35574/v3.0/
   107              vmwarestack-xenial:
   108                type: maas
   109                auth-tpes: [oauth1]
   110                endpoit: http://10.245.200.28/MAAS
   111                config:
   112                  default-series: xenial
   113                  bootstrap-timeout: 900
   114                  http-proxy: http://10.245.200.28:8000/
   115                regions:
   116                  dev1:
   117                    endpoint: https://openstack.example.com:35575/v3.0/`
   118  
   119  	yaml := []byte(validCloud)
   120  	err := cloud.ValidateCloudSet(yaml)
   121  	c.Assert(err.Error(), jc.Contains, `"endpoit" is invalid. Perhaps you mean "endpoint"`)
   122  	c.Assert(err.Error(), jc.Contains, `"auth-tpes" is invalid. Perhaps you mean "auth-types"`)
   123  }
   124  
   125  func (s *cloudSuite) TestValidateInvalidPropertyWithNoSuggestion(c *gc.C) {
   126  	validCloud := `
   127            clouds:
   128              vmwarestack-trusty:
   129                type: maas
   130                auth-types: [oauth1]
   131                endpoint: http://10.245.200.27/MAAS
   132                config:
   133                  default-series: trusty
   134                  bootstrap-timeout: 900
   135                  http-proxy: http://10.245.200.27:8000/
   136                regions:
   137                  dev1:
   138                    endpoint: https://openstack.example.com:35574/v3.0/
   139              vmwarestack-xenial:
   140                type: maas
   141                auth-types: [oauth1]
   142                invalidProperty: "something strange"
   143                endpoit: http://10.245.200.28/MAAS
   144                config:
   145                  default-series: xenial
   146                  bootstrap-timeout: 900
   147                  http-proxy: http://10.245.200.28:8000/
   148                regions:
   149                  dev1:
   150                    endpoint: https://openstack.example.com:35575/v3.0/`
   151  
   152  	yaml := []byte(validCloud)
   153  	err := cloud.ValidateCloudSet(yaml)
   154  	c.Assert(err.Error(), jc.Contains, `"endpoit" is invalid. Perhaps you mean "endpoint"`)
   155  	c.Assert(err.Error(), jc.Contains, `"invalidProperty" is invalid.`)
   156  }
   157  
   158  func (s *cloudSuite) TestValidateOneValidCloud(c *gc.C) {
   159  	validCloud := `
   160            name: vmwarestack-trusty
   161            type: maas
   162            description: A mass cloud
   163            auth-types: [oauth1]
   164            endpoint: http://10.245.200.27/MAAS
   165            config:
   166              default-series: trusty
   167              bootstrap-timeout: 900
   168              http-proxy: http://10.245.200.27:8000/
   169              regions:
   170                dev1:
   171                  endpoint: https://openstack.example.com:35574/v3.0/`
   172  
   173  	yaml := []byte(validCloud)
   174  	err := cloud.ValidateOneCloud(yaml)
   175  	c.Assert(err, gc.IsNil)
   176  }
   177  
   178  func (s *cloudSuite) TestValidateOneInvalidCloud(c *gc.C) {
   179  	validCloud := `
   180            nae: vmwarestack-trusty
   181            type: maas
   182            escription: A mass cloud
   183            auth-types: [oauth1]
   184            endpoint: http://10.245.200.27/MAAS
   185            config:
   186              default-series: trusty
   187              bootstrap-timeout: 900
   188              http-proxy: http://10.245.200.27:8000/
   189              regions:
   190                dev1:
   191                  endpoint: https://openstack.example.com:35574/v3.0/`
   192  
   193  	yaml := []byte(validCloud)
   194  	err := cloud.ValidateOneCloud(yaml)
   195  	c.Assert(err.Error(), jc.Contains, `"nae" is invalid. Perhaps you mean "name"`)
   196  	c.Assert(err.Error(), jc.Contains, `"escription" is invalid. Perhaps you mean "description"`)
   197  }