github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/agent/format-1.16_whitebox_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Copyright 2014 Cloudbase Solutions SRL 3 // Licensed under the AGPLv3, see LICENCE file for details. 4 5 // The format tests are white box tests, meaning that the tests are in the 6 // same package as the code, as all the format details are internal to the 7 // package. 8 9 package agent 10 11 import ( 12 "io/ioutil" 13 "path/filepath" 14 15 jc "github.com/juju/testing/checkers" 16 "github.com/juju/utils" 17 "github.com/juju/utils/series" 18 gc "gopkg.in/check.v1" 19 20 "github.com/juju/juju/juju/paths" 21 "github.com/juju/juju/testing" 22 "github.com/juju/juju/version" 23 ) 24 25 type format_1_16Suite struct { 26 testing.BaseSuite 27 } 28 29 var _ = gc.Suite(&format_1_16Suite{}) 30 31 func (s *format_1_16Suite) TestMissingAttributes(c *gc.C) { 32 logDir, err := paths.LogDir(series.HostSeries()) 33 c.Assert(err, jc.ErrorIsNil) 34 realDataDir, err := paths.DataDir(series.HostSeries()) 35 c.Assert(err, jc.ErrorIsNil) 36 37 realDataDir = filepath.FromSlash(realDataDir) 38 logPath := filepath.Join(logDir, "juju") 39 logPath = filepath.FromSlash(logPath) 40 41 dataDir := c.MkDir() 42 formatPath := filepath.Join(dataDir, legacyFormatFilename) 43 err = utils.AtomicWriteFile(formatPath, []byte(legacyFormatFileContents), 0600) 44 c.Assert(err, jc.ErrorIsNil) 45 configPath := filepath.Join(dataDir, agentConfigFilename) 46 47 err = utils.AtomicWriteFile(configPath, []byte(configDataWithoutNewAttributes), 0600) 48 c.Assert(err, jc.ErrorIsNil) 49 readConfig, err := ReadConfig(configPath) 50 c.Assert(err, jc.ErrorIsNil) 51 c.Assert(readConfig.UpgradedToVersion(), gc.Equals, version.MustParse("1.16.0")) 52 configLogDir := filepath.FromSlash(readConfig.LogDir()) 53 configDataDir := filepath.FromSlash(readConfig.DataDir()) 54 55 c.Assert(configLogDir, gc.Equals, logPath) 56 c.Assert(configDataDir, gc.Equals, realDataDir) 57 // Test data doesn't include a StateServerKey so StateServingInfo 58 // should *not* be available 59 _, available := readConfig.StateServingInfo() 60 c.Assert(available, jc.IsFalse) 61 } 62 63 func (*format_1_16Suite) TestStatePortParsed(c *gc.C) { 64 dataDir := c.MkDir() 65 formatPath := filepath.Join(dataDir, legacyFormatFilename) 66 err := utils.AtomicWriteFile(formatPath, []byte(legacyFormatFileContents), 0600) 67 c.Assert(err, jc.ErrorIsNil) 68 configPath := filepath.Join(dataDir, agentConfigFilename) 69 err = utils.AtomicWriteFile(configPath, []byte(stateMachineConfigData), 0600) 70 c.Assert(err, jc.ErrorIsNil) 71 readConfig, err := ReadConfig(configPath) 72 c.Assert(err, jc.ErrorIsNil) 73 info, available := readConfig.StateServingInfo() 74 c.Assert(available, jc.IsTrue) 75 c.Assert(info.StatePort, gc.Equals, 37017) 76 } 77 78 func (*format_1_16Suite) TestReadConfReadsLegacyFormatAndWritesNew(c *gc.C) { 79 dataDir := c.MkDir() 80 formatPath := filepath.Join(dataDir, legacyFormatFilename) 81 err := utils.AtomicWriteFile(formatPath, []byte(legacyFormatFileContents), 0600) 82 c.Assert(err, jc.ErrorIsNil) 83 configPath := filepath.Join(dataDir, agentConfigFilename) 84 err = utils.AtomicWriteFile(configPath, []byte(agentConfig1_16Contents), 0600) 85 c.Assert(err, jc.ErrorIsNil) 86 87 config, err := ReadConfig(configPath) 88 c.Assert(err, jc.ErrorIsNil) 89 c.Assert(config, gc.NotNil) 90 // Test we wrote a currently valid config. 91 config, err = ReadConfig(configPath) 92 c.Assert(err, jc.ErrorIsNil) 93 c.Assert(config, gc.NotNil) 94 c.Assert(config.UpgradedToVersion(), jc.DeepEquals, version.MustParse("1.16.0")) 95 c.Assert(config.Jobs(), gc.HasLen, 0) 96 97 // Old format was deleted. 98 assertFileNotExist(c, formatPath) 99 // And new contents were written. 100 data, err := ioutil.ReadFile(configPath) 101 c.Assert(err, jc.ErrorIsNil) 102 c.Assert(string(data), gc.Not(gc.Equals), agentConfig1_16Contents) 103 } 104 105 const legacyFormatFileContents = "format 1.16" 106 107 var agentConfig1_16Contents = ` 108 tag: machine-0 109 nonce: user-admin:bootstrap 110 cacert: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUNXekNDQWNhZ0F3SUJBZ0lCQURBTEJna3Foa2lHOXcwQkFRVXdRekVOTUFzR0ExVUVDaE1FYW5WcWRURXkKTURBR0ExVUVBd3dwYW5WcWRTMW5aVzVsY21GMFpXUWdRMEVnWm05eUlHVnVkbWx5YjI1dFpXNTBJQ0pzYjJOaApiQ0l3SGhjTk1UUXdNekExTVRJeE9ERTJXaGNOTWpRd016QTFNVEl5TXpFMldqQkRNUTB3Q3dZRFZRUUtFd1JxCmRXcDFNVEl3TUFZRFZRUUREQ2xxZFdwMUxXZGxibVZ5WVhSbFpDQkRRU0JtYjNJZ1pXNTJhWEp2Ym0xbGJuUWcKSW14dlkyRnNJakNCbnpBTkJna3Foa2lHOXcwQkFRRUZBQU9CalFBd2dZa0NnWUVBd3NaVUg3NUZGSW1QUWVGSgpaVnVYcmlUWmNYdlNQMnk0VDJaSU5WNlVrY2E5VFdXb01XaWlPYm4yNk03MjNGQllPczh3WHRYNEUxZ2l1amxYCmZGeHNFckloczEyVXQ1S3JOVkkyMlEydCtVOGViakZMUHJiUE5Fb3pzdnU3UzFjZklFbjBXTVg4MWRBaENOMnQKVkxGaC9hS3NqSHdDLzJ5Y3Z0VSttTngyVG5FQ0F3RUFBYU5qTUdFd0RnWURWUjBQQVFIL0JBUURBZ0NrTUE4RwpBMVVkRXdFQi93UUZNQU1CQWY4d0hRWURWUjBPQkJZRUZKVUxKZVlIbERsdlJ3T0owcWdyemcwclZGZUVNQjhHCkExVWRJd1FZTUJhQUZKVUxKZVlIbERsdlJ3T0owcWdyemcwclZGZUVNQXNHQ1NxR1NJYjNEUUVCQlFPQmdRQ2UKRlRZbThsWkVYZUp1cEdPc3pwc2pjaHNSMEFxeXROZ1dxQmE1cWUyMS9xS2R3TUFSQkNFMTU3eUxGVnl6MVoycQp2YVhVNy9VKzdKbGNiWmtHRHJ5djE2S2UwK2RIY3NEdG5jR2FOVkZKMTAxYnNJNG1sVEkzQWpQNDErNG5mQ0VlCmhwalRvYm1YdlBhOFN1NGhQYTBFc1E4bXFaZGFabmdwRU0vb1JiZ0RMdz09Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K 111 stateaddresses: 112 - localhost:37017 113 statepassword: OlUMkte5J3Ss0CH9yxedilIC 114 apiaddresses: 115 - localhost:17070 116 apipassword: OlUMkte5J3Ss0CH9yxedilIC 117 oldpassword: oBlMbFUGvCb2PMFgYVzjS6GD 118 values: 119 PROVIDER_TYPE: local 120 SHARED_STORAGE_ADDR: 10.0.3.1:8041 121 SHARED_STORAGE_DIR: /home/user/.juju/local/shared-storage 122 STORAGE_ADDR: 10.0.3.1:8040 123 STORAGE_DIR: /home/user/.juju/local/storage 124 stateservercert: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUNJakNDQVkyZ0F3SUJBZ0lCQURBTEJna3Foa2lHOXcwQkFRVXdRekVOTUFzR0ExVUVDaE1FYW5WcWRURXkKTURBR0ExVUVBd3dwYW5WcWRTMW5aVzVsY21GMFpXUWdRMEVnWm05eUlHVnVkbWx5YjI1dFpXNTBJQ0pzYjJOaApiQ0l3SGhjTk1UUXdNekExTVRJeE9ESXlXaGNOTWpRd016QTFNVEl5TXpJeVdqQWJNUTB3Q3dZRFZRUUtFd1JxCmRXcDFNUW93Q0FZRFZRUURFd0VxTUlHZk1BMEdDU3FHU0liM0RRRUJBUVVBQTRHTkFEQ0JpUUtCZ1FDdVA0dTAKQjZtbGs0V0g3SHFvOXhkSFp4TWtCUVRqV2VLTkhERzFMb21SWmc2RHA4Z0VQK0ZNVm5IaUprZW1pQnJNSEk3OAo5bG4zSVRBT0NJT0xna0NkN3ZsaDJub2FheTlSeXpUaG9PZ0RMSzVpR0VidmZDeEFWZThhWDQvbThhOGNLWE9TC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Tnk2NGtDUVFEbXRFWHNTL2kxTm5pSwozZVJkeHM4UVFGN0pKVG5SR042ZUh6ZHlXb242Zjl2ZkxrSDROWUdxcFUydjVBNUl1Nno3K3NJdXVHU2ZSeEI1CktrZVFXdlVQQWtFQXdWcVdlczdmc3NLbUFCZGxER3ozYzNxMjI2eVVaUE00R3lTb1cxYXZsYzJ1VDVYRm9vVUsKNjRpUjJuU2I1OHZ2bGY1RjRRMnJuRjh2cFRLcFJwK0lWUUpBTlcwZ0dFWEx0ZU9FYk54UUMydUQva1o1N09rRApCNnBUdTVpTkZaMWtBSy9sY2p6YktDanorMW5Hc09vR2FNK1ZrdEVTY1JGZ3RBWVlDWWRDQldzYS93SkFROWJXCnlVdmdMTVlpbkJHWlFKelN6VStXN01oR1lJejllSGlLSVZIdTFTNlBKQmsyZUdrWmhiNHEvbXkvYnJxYzJ4R1YKenZxTzVaUjRFUXdQWEZvSTZRSkFkeVdDMllOTTF2a1BuWnJqZzNQQXFHRHJQMHJwNEZ0bFV4alh0ay8vcW9hNgpRcXVYcE9vNjd4THRieW1PTlJTdDFiZGE5ZE5tbGljMFVNZ0JQRHgrYnc9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo= 126 apiport: 17070 127 `[1:] 128 129 const configDataWithoutNewAttributes = ` 130 tag: user-omg 131 nonce: a nonce 132 cacert: Y2EgY2VydA== 133 stateaddresses: 134 - localhost:1234 135 apiaddresses: 136 - localhost:1235 137 oldpassword: sekrit 138 values: {} 139 ` 140 141 const stateMachineConfigData = ` 142 tag: machine-0 143 nonce: user-admin:bootstrap 144 cacert: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUNXekNDQWNhZ0F3SUJBZ0lCQURBTEJna3Foa2lHOXcwQkFRVXdRekVOTUFzR0ExVUVDaE1FYW5WcWRURXkKTURBR0ExVUVBd3dwYW5WcWRTMW5aVzVsY21GMFpXUWdRMEVnWm05eUlHVnVkbWx5YjI1dFpXNTBJQ0p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apipassword: +PCNyLFNAg2f5SN3ig6uHHum 151 oldpassword: Jc1GMZX/d35BgbQ6F9nxrTY4 152 values: 153 PROVIDER_TYPE: local 154 SHARED_STORAGE_ADDR: 10.0.3.1:8041 155 SHARED_STORAGE_DIR: /home/rog/.juju/local/shared-storage 156 STORAGE_ADDR: 10.0.3.1:8040 157 STORAGE_DIR: /home/rog/.juju/local/storage 158 stateservercert: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUNJakNDQVkyZ0F3SUJBZ0lCQURBTEJna3Foa2lHOXcwQkFRVXdRekVOTUFzR0ExVUVDaE1FYW5WcWRURXkKTURBR0ExVUVBd3dwYW5WcWRTMW5aVzVsY21GMFpXUWdRMEVnWm05eUlHVnVkbWx5YjI1dFpXNTBJQ0pzYjJOaApiQ0l3SGhjTk1UUXdOREF4TVRNd016SXlXaGNOTWpRd05EQXhNVE13T0RJeVdqQWJNUTB3Q3dZRFZRUUtFd1JxCmRXcDFNUW93Q0FZRFZRUURFd0VxTUlHZk1BMEdDU3FHU0liM0RRRUJBUVVBQTRHTkFEQ0JpUUtCZ1FDa1E1RzEKbUFuQU0wb3REVzVwREo3R3pQbTg5OUtySlVlR0NIZytGV2l0d1RETnJiK0NhYk1TYWRsc3JYb0crYjdETDFIcApXNTdnQXZoNjBTeUFLWHJCVW9tMG1pdVI1QkhYeitpWkZsZDZHS0UySTFIMUlON0pldUdmTURyVUN4WlVYNkdkCjVlcStUU3JvQ3ZPVGxDYWFtNDRkaHd0S1JHMlFQQ2RYbTNSbWxRSURBUUFCbzFJd1VEQU9CZ05WSFE4QkFmOEUKQkFNQ0FCQXdIUVlEVlIwT0JCWUVGTElWeDdmUVJFUkRGZ3hCcWh4b3puMHZueUlXTUI4R0ExVWRJd1FZTUJhQQpGQ0JYZ2lxaUpIQVlWeUZQN0dYUktzZHFZRFc4TUFzR0NTcUdTSWIzRFFFQkJRT0JnUUFKeW9yaEtLa20ySEFBCmNtS2RyRFNyRlZraElxUFl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