github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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) TestHandleCloud(c *C) { 41 tests := []struct { 42 instData string 43 name string 44 region string 45 availabilityZone string 46 }{ 47 {"", "", "", ""}, 48 {`{ 49 }`, "", "", ""}, 50 {"{", "", "", ""}, 51 {`{ 52 "v1": { 53 "availability-zone": "us-east-2b", 54 "cloud-name": "aws", 55 "instance-id": "i-03bdbe0d89f4c8ec9", 56 "local-hostname": "ip-10-41-41-143", 57 "region": "us-east-2" 58 } 59 }`, "aws", "us-east-2", "us-east-2b"}, 60 {`{ 61 "v1": { 62 "availability-zone": null, 63 "cloud-name": "azure", 64 "instance-id": "1C63DFBB-46A0-7243-A11F-5A1F6EAEBCCB", 65 "public-hostname": "my-b1", 66 "public-ipv4-address": null, 67 "public-ipv6-address": null, 68 "region": null 69 } 70 }`, "azure", "", ""}, 71 {`{ 72 "v1": { 73 "availability-zone": "nova", 74 "cloud-name": "openstack", 75 "instance-id": "3e39d278-0644-4728-9479-678f9212d8f0", 76 "local-hostname": "xenial-test", 77 "region": null 78 } 79 }`, "openstack", "", "nova"}, 80 {`{ 81 "v1": { 82 "availability-zone": null, 83 "cloud-name": "nocloud", 84 "instance-id": "b5", 85 "local-hostname": "b5", 86 "region": null 87 } 88 }`, "", "", ""}, 89 {}, 90 {`{ 91 "v1": null, 92 }`, "", "", ""}, 93 {`{ 94 "v1": { 95 "cloud-name": "none" 96 } 97 }`, "", "", ""}, 98 // both _ and - are supported 99 {`{ 100 "v1": { 101 "availability_zone": "nova", 102 "cloud_name": "openstack", 103 "instance-id": "b5", 104 "local-hostname": "b5", 105 "region": null 106 } 107 }`, "openstack", "", "nova"}, 108 {`{ 109 "v1": { 110 "availability_zone": "nova", 111 "availability-zone": "nova", 112 "cloud_name": "openstack", 113 "cloud-name": "openstack", 114 "instance-id": "b5", 115 "local-hostname": "b5", 116 "region": null 117 } 118 }`, "openstack", "", "nova"}, 119 // cloud_name takes precedence, if set, and other fields follow 120 {`{ 121 "v1": { 122 "availability_zone": "us-east-2b", 123 "availability-zone": "none", 124 "cloud_name": "aws", 125 "cloud_name": "aws", 126 "instance-id": "b5", 127 "local-hostname": "b5", 128 "region": null 129 } 130 }`, "aws", "", "us-east-2b"}, 131 {`{ 132 "v1": { 133 "availability_zone": "nova", 134 "availability-zone": "gibberish", 135 "cloud_name": "openstack", 136 "cloud-name": "aws", 137 "instance-id": "b5", 138 "local-hostname": "b5", 139 "region": null 140 } 141 }`, "openstack", "", "nova"}, 142 {`{ 143 "v1": { 144 "availability_zone": "gibberish", 145 "availability-zone": "nova", 146 "cloud_name": null, 147 "cloud-name": "openstack", 148 "instance-id": "b5", 149 "local-hostname": "b5", 150 "region": null 151 } 152 }`, "openstack", "", "nova"}, 153 } 154 155 err := os.MkdirAll(filepath.Dir(dirs.CloudInstanceDataFile), 0755) 156 c.Assert(err, IsNil) 157 158 for i, t := range tests { 159 c.Logf("tc: %v", i) 160 os.Remove(dirs.CloudInstanceDataFile) 161 if t.instData != "" { 162 err = ioutil.WriteFile(dirs.CloudInstanceDataFile, []byte(t.instData), 0600) 163 c.Assert(err, IsNil) 164 } 165 166 tr := &mockConf{ 167 state: s.state, 168 } 169 err := configcore.Run(tr) 170 c.Assert(err, IsNil) 171 172 var cloudInfo auth.CloudInfo 173 tr.Get("core", "cloud", &cloudInfo) 174 175 c.Check(cloudInfo.Name, Equals, t.name) 176 c.Check(cloudInfo.Region, Equals, t.region) 177 c.Check(cloudInfo.AvailabilityZone, Equals, t.availabilityZone) 178 } 179 } 180 181 func (s *cloudSuite) TestHandleCloudAlreadySeeded(c *C) { 182 instData := `{ 183 "v1": { 184 "availability-zone": "us-east-2b", 185 "cloud-name": "aws", 186 "instance-id": "i-03bdbe0d89f4c8ec9", 187 "local-hostname": "ip-10-41-41-143", 188 "region": "us-east-2" 189 } 190 }` 191 192 err := os.MkdirAll(filepath.Dir(dirs.CloudInstanceDataFile), 0755) 193 c.Assert(err, IsNil) 194 err = ioutil.WriteFile(dirs.CloudInstanceDataFile, []byte(instData), 0600) 195 c.Assert(err, IsNil) 196 197 s.state.Lock() 198 s.state.Set("seeded", true) 199 s.state.Unlock() 200 tr := &mockConf{ 201 state: s.state, 202 } 203 err = configcore.Run(tr) 204 c.Assert(err, IsNil) 205 206 var cloudInfo auth.CloudInfo 207 tr.Get("core", "cloud", &cloudInfo) 208 209 c.Check(cloudInfo.Name, Equals, "") 210 }