github.com/rigado/snapd@v2.42.5-go-mod+incompatible/overlord/configstate/configcore/cloud_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package configcore_test 21 22 import ( 23 "io/ioutil" 24 "os" 25 "path/filepath" 26 27 . "gopkg.in/check.v1" 28 29 "github.com/snapcore/snapd/dirs" 30 "github.com/snapcore/snapd/overlord/auth" 31 "github.com/snapcore/snapd/overlord/configstate/configcore" 32 ) 33 34 type cloudSuite struct { 35 configcoreSuite 36 } 37 38 var _ = Suite(&cloudSuite{}) 39 40 func (s *cloudSuite) SetUpTest(c *C) { 41 s.configcoreSuite.SetUpTest(c) 42 dirs.SetRootDir(c.MkDir()) 43 } 44 45 func (s *cloudSuite) TearDownTest(c *C) { 46 dirs.SetRootDir("/") 47 } 48 49 func (s *cloudSuite) TestHandleCloud(c *C) { 50 tests := []struct { 51 instData string 52 name string 53 region string 54 availabilityZone string 55 }{ 56 {"", "", "", ""}, 57 {`{ 58 }`, "", "", ""}, 59 {"{", "", "", ""}, 60 {`{ 61 "v1": { 62 "availability-zone": "us-east-2b", 63 "cloud-name": "aws", 64 "instance-id": "i-03bdbe0d89f4c8ec9", 65 "local-hostname": "ip-10-41-41-143", 66 "region": "us-east-2" 67 } 68 }`, "aws", "us-east-2", "us-east-2b"}, 69 {`{ 70 "v1": { 71 "availability-zone": null, 72 "cloud-name": "azure", 73 "instance-id": "1C63DFBB-46A0-7243-A11F-5A1F6EAEBCCB", 74 "public-hostname": "my-b1", 75 "public-ipv4-address": null, 76 "public-ipv6-address": null, 77 "region": null 78 } 79 }`, "azure", "", ""}, 80 {`{ 81 "v1": { 82 "availability-zone": "nova", 83 "cloud-name": "openstack", 84 "instance-id": "3e39d278-0644-4728-9479-678f9212d8f0", 85 "local-hostname": "xenial-test", 86 "region": null 87 } 88 }`, "openstack", "", "nova"}, 89 {`{ 90 "v1": { 91 "availability-zone": null, 92 "cloud-name": "nocloud", 93 "instance-id": "b5", 94 "local-hostname": "b5", 95 "region": null 96 } 97 }`, "", "", ""}, 98 {}, 99 {`{ 100 "v1": null, 101 }`, "", "", ""}, 102 {`{ 103 "v1": { 104 "cloud-name": "none" 105 } 106 }`, "", "", ""}, 107 // both _ and - are supported 108 {`{ 109 "v1": { 110 "availability_zone": "nova", 111 "cloud_name": "openstack", 112 "instance-id": "b5", 113 "local-hostname": "b5", 114 "region": null 115 } 116 }`, "openstack", "", "nova"}, 117 {`{ 118 "v1": { 119 "availability_zone": "nova", 120 "availability-zone": "nova", 121 "cloud_name": "openstack", 122 "cloud-name": "openstack", 123 "instance-id": "b5", 124 "local-hostname": "b5", 125 "region": null 126 } 127 }`, "openstack", "", "nova"}, 128 // cloud_name takes precedence, if set, and other fields follow 129 {`{ 130 "v1": { 131 "availability_zone": "us-east-2b", 132 "availability-zone": "none", 133 "cloud_name": "aws", 134 "cloud_name": "aws", 135 "instance-id": "b5", 136 "local-hostname": "b5", 137 "region": null 138 } 139 }`, "aws", "", "us-east-2b"}, 140 {`{ 141 "v1": { 142 "availability_zone": "nova", 143 "availability-zone": "gibberish", 144 "cloud_name": "openstack", 145 "cloud-name": "aws", 146 "instance-id": "b5", 147 "local-hostname": "b5", 148 "region": null 149 } 150 }`, "openstack", "", "nova"}, 151 {`{ 152 "v1": { 153 "availability_zone": "gibberish", 154 "availability-zone": "nova", 155 "cloud_name": null, 156 "cloud-name": "openstack", 157 "instance-id": "b5", 158 "local-hostname": "b5", 159 "region": null 160 } 161 }`, "openstack", "", "nova"}, 162 } 163 164 err := os.MkdirAll(filepath.Dir(dirs.CloudInstanceDataFile), 0755) 165 c.Assert(err, IsNil) 166 167 for i, t := range tests { 168 c.Logf("tc: %v", i) 169 os.Remove(dirs.CloudInstanceDataFile) 170 if t.instData != "" { 171 err = ioutil.WriteFile(dirs.CloudInstanceDataFile, []byte(t.instData), 0600) 172 c.Assert(err, IsNil) 173 } 174 175 tr := &mockConf{ 176 state: s.state, 177 } 178 err := configcore.Run(tr) 179 c.Assert(err, IsNil) 180 181 var cloudInfo auth.CloudInfo 182 tr.Get("core", "cloud", &cloudInfo) 183 184 c.Check(cloudInfo.Name, Equals, t.name) 185 c.Check(cloudInfo.Region, Equals, t.region) 186 c.Check(cloudInfo.AvailabilityZone, Equals, t.availabilityZone) 187 } 188 } 189 190 func (s *cloudSuite) TestHandleCloudAlreadySeeded(c *C) { 191 instData := `{ 192 "v1": { 193 "availability-zone": "us-east-2b", 194 "cloud-name": "aws", 195 "instance-id": "i-03bdbe0d89f4c8ec9", 196 "local-hostname": "ip-10-41-41-143", 197 "region": "us-east-2" 198 } 199 }` 200 201 err := os.MkdirAll(filepath.Dir(dirs.CloudInstanceDataFile), 0755) 202 c.Assert(err, IsNil) 203 err = ioutil.WriteFile(dirs.CloudInstanceDataFile, []byte(instData), 0600) 204 c.Assert(err, IsNil) 205 206 s.state.Lock() 207 s.state.Set("seeded", true) 208 s.state.Unlock() 209 tr := &mockConf{ 210 state: s.state, 211 } 212 err = configcore.Run(tr) 213 c.Assert(err, IsNil) 214 215 var cloudInfo auth.CloudInfo 216 tr.Get("core", "cloud", &cloudInfo) 217 218 c.Check(cloudInfo.Name, Equals, "") 219 }