~wwitzel3/juju-core/009-ha-rsyslog-api

« back to all changes in this revision

Viewing changes to environs/config/config_test.go

  • Committer: Wayne Witzel III
  • Date: 2014-05-22 13:23:37 UTC
  • mfrom: (2743.1.10 ha-rsyslog-api)
  • Revision ID: wayne.witzel@canonical.com-20140522132337-m9py79a81mjdm9su
merge mfoord

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
        "launchpad.net/juju-core/juju/osenv"
19
19
        "launchpad.net/juju-core/schema"
20
20
        "launchpad.net/juju-core/testing"
21
 
        "launchpad.net/juju-core/testing/testbase"
22
21
        "launchpad.net/juju-core/version"
23
22
)
24
23
 
27
26
}
28
27
 
29
28
type ConfigSuite struct {
30
 
        testbase.LoggingSuite
 
29
        testing.FakeJujuHomeSuite
31
30
        home string
32
31
}
33
32
 
34
33
var _ = gc.Suite(&ConfigSuite{})
35
34
 
36
35
func (s *ConfigSuite) SetUpTest(c *gc.C) {
37
 
        s.LoggingSuite.SetUpTest(c)
 
36
        s.FakeJujuHomeSuite.SetUpTest(c)
38
37
        // Make sure that the defaults are used, which
39
38
        // is <root>=WARNING
40
39
        loggo.ResetLoggers()
61
60
type configTest struct {
62
61
        about       string
63
62
        useDefaults config.Defaulting
64
 
        attrs       map[string]interface{}
 
63
        attrs       testing.Attrs
 
64
        expected    testing.Attrs
65
65
        err         string
66
66
}
67
67
 
140
140
                        "authorized-keys-path": "~/.ssh/authorized_keys2",
141
141
                },
142
142
        }, {
 
143
                about:       "LXC clone values",
 
144
                useDefaults: config.UseDefaults,
 
145
                attrs: testing.Attrs{
 
146
                        "type":           "my-type",
 
147
                        "name":           "my-name",
 
148
                        "default-series": "precise",
 
149
                        "lxc-clone":      true,
 
150
                        "lxc-clone-aufs": true,
 
151
                },
 
152
        }, {
 
153
                about:       "Deprecated lxc-use-clone used",
 
154
                useDefaults: config.UseDefaults,
 
155
                attrs: testing.Attrs{
 
156
                        "type":          "my-type",
 
157
                        "name":          "my-name",
 
158
                        "lxc-use-clone": true,
 
159
                },
 
160
        }, {
 
161
                about:       "Deprecated lxc-use-clone ignored",
 
162
                useDefaults: config.UseDefaults,
 
163
                attrs: testing.Attrs{
 
164
                        "type":          "my-type",
 
165
                        "name":          "my-name",
 
166
                        "lxc-use-clone": false,
 
167
                        "lxc-clone":     true,
 
168
                },
 
169
        }, {
143
170
                about:       "CA cert & key from path",
144
171
                useDefaults: config.UseDefaults,
145
172
                attrs: testing.Attrs{
696
723
        name, data string
697
724
}
698
725
 
699
 
func (*ConfigSuite) TestConfig(c *gc.C) {
 
726
func (s *ConfigSuite) TestConfig(c *gc.C) {
700
727
        files := []testing.TestFile{
701
728
                {".ssh/id_dsa.pub", "dsa"},
702
729
                {".ssh/id_rsa.pub", "rsa\n"},
711
738
                {"othercert.pem", caCert3},
712
739
                {"otherkey.pem", caKey3},
713
740
        }
714
 
        h := testing.MakeFakeHomeWithFiles(c, files)
715
 
        defer h.Restore()
 
741
        s.FakeHomeSuite.Home.AddFiles(c, files...)
716
742
        for i, test := range configTests {
717
743
                c.Logf("test %d. %s", i, test.about)
718
 
                test.check(c, h)
 
744
                test.check(c, s.FakeHomeSuite.Home)
719
745
        }
720
746
}
721
747
 
741
767
        },
742
768
}
743
769
 
744
 
func (*ConfigSuite) TestConfigNoCertFiles(c *gc.C) {
745
 
        h := testing.MakeEmptyFakeHome(c)
746
 
        defer h.Restore()
 
770
func (s *ConfigSuite) TestConfigNoCertFiles(c *gc.C) {
747
771
        for i, test := range noCertFilesTests {
748
772
                c.Logf("test %d. %s", i, test.about)
749
 
                test.check(c, h)
 
773
                test.check(c, s.FakeHomeSuite.Home)
750
774
        }
751
775
}
752
776
 
803
827
        }, */
804
828
}
805
829
 
806
 
func (*ConfigSuite) TestConfigEmptyCertFiles(c *gc.C) {
 
830
func (s *ConfigSuite) TestConfigEmptyCertFiles(c *gc.C) {
807
831
        files := []testing.TestFile{
808
832
                {".juju/my-name-cert.pem", ""},
809
833
                {".juju/my-name-private-key.pem", ""},
810
834
        }
811
 
        h := testing.MakeFakeHomeWithFiles(c, files)
812
 
        defer h.Restore()
 
835
        s.FakeHomeSuite.Home.AddFiles(c, files...)
813
836
 
814
837
        for i, test := range emptyCertFilesTests {
815
838
                c.Logf("test %d. %s", i, test.about)
816
 
                test.check(c, h)
 
839
                test.check(c, s.FakeHomeSuite.Home)
817
840
        }
818
841
}
819
842
 
981
1004
                c.Assert(oldURLAttrPresent, jc.IsFalse)
982
1005
                c.Assert(oldToolsURL, gc.Equals, "")
983
1006
        }
 
1007
 
 
1008
        useLxcClone, useLxcClonePresent := cfg.LXCUseClone()
 
1009
        oldUseClone, oldUseClonePresent := cfg.AllAttrs()["lxc-use-clone"]
 
1010
        if v, ok := test.attrs["lxc-clone"]; ok {
 
1011
                c.Assert(useLxcClone, gc.Equals, v)
 
1012
                c.Assert(useLxcClonePresent, jc.IsTrue)
 
1013
        } else {
 
1014
                if oldUseClonePresent {
 
1015
                        c.Assert(useLxcClonePresent, jc.IsTrue)
 
1016
                        c.Assert(useLxcClone, gc.Equals, oldUseClone)
 
1017
                } else {
 
1018
                        c.Assert(useLxcClonePresent, jc.IsFalse)
 
1019
                        c.Assert(useLxcClone, gc.Equals, false)
 
1020
                }
 
1021
        }
 
1022
        useLxcCloneAufs, ok := cfg.LXCUseCloneAUFS()
 
1023
        if v, ok := test.attrs["lxc-clone-aufs"]; ok {
 
1024
                c.Assert(useLxcCloneAufs, gc.Equals, v)
 
1025
        } else {
 
1026
                c.Assert(useLxcCloneAufs, gc.Equals, false)
 
1027
        }
984
1028
}
985
1029
 
986
1030
func (test configTest) assertDuration(c *gc.C, name string, actual time.Duration, defaultInSeconds int) {
1028
1072
        attrs["tools-url"] = ""
1029
1073
        attrs["image-stream"] = ""
1030
1074
        attrs["proxy-ssh"] = false
1031
 
        attrs["lxc-use-clone"] = false
 
1075
        attrs["lxc-clone-aufs"] = false
1032
1076
 
1033
1077
        // Default firewall mode is instance
1034
1078
        attrs["firewall-mode"] = string(config.FwInstance)
1112
1156
        new:   testing.Attrs{"bootstrap-timeout": 5},
1113
1157
        err:   `cannot change bootstrap-timeout from 600 to 5`,
1114
1158
}, {
1115
 
        about: "Cannot change lxc-use-clone",
1116
 
        old:   testing.Attrs{"lxc-use-clone": false},
1117
 
        new:   testing.Attrs{"lxc-use-clone": true},
1118
 
        err:   `cannot change lxc-use-clone from false to true`,
 
1159
        about: "Cannot change lxc-clone",
 
1160
        old:   testing.Attrs{"lxc-clone": false},
 
1161
        new:   testing.Attrs{"lxc-clone": true},
 
1162
        err:   `cannot change lxc-clone from false to true`,
 
1163
}, {
 
1164
        about: "Cannot change lxc-clone-aufs",
 
1165
        old:   testing.Attrs{"lxc-clone-aufs": false},
 
1166
        new:   testing.Attrs{"lxc-clone-aufs": true},
 
1167
        err:   `cannot change lxc-clone-aufs from false to true`,
1119
1168
}}
1120
1169
 
1121
 
func (*ConfigSuite) TestValidateChange(c *gc.C) {
 
1170
func (s *ConfigSuite) TestValidateChange(c *gc.C) {
1122
1171
        files := []testing.TestFile{
1123
1172
                {".ssh/identity.pub", "identity"},
1124
1173
        }
1125
 
        h := testing.MakeFakeHomeWithFiles(c, files)
1126
 
        defer h.Restore()
 
1174
        s.FakeHomeSuite.Home.AddFiles(c, files...)
1127
1175
 
1128
1176
        for i, test := range validationTests {
1129
1177
                c.Logf("test %d: %s", i, test.about)
1131
1179
                oldConfig := newTestConfig(c, test.old)
1132
1180
                err := config.Validate(newConfig, oldConfig)
1133
1181
                if test.err == "" {
1134
 
                        c.Assert(err, gc.IsNil)
 
1182
                        c.Check(err, gc.IsNil)
1135
1183
                } else {
1136
 
                        c.Assert(err, gc.ErrorMatches, test.err)
 
1184
                        c.Check(err, gc.ErrorMatches, test.err)
1137
1185
                }
1138
1186
        }
1139
1187
}
1140
1188
 
1141
 
func makeFakeHome(c *gc.C) *testing.FakeHome {
1142
 
        return testing.MakeFakeHomeWithFiles(c, []testing.TestFile{
 
1189
func (s *ConfigSuite) addJujuFiles(c *gc.C) {
 
1190
        s.FakeHomeSuite.Home.AddFiles(c, []testing.TestFile{
1143
1191
                {".ssh/id_rsa.pub", "rsa\n"},
1144
1192
                {".juju/myenv-cert.pem", caCert},
1145
1193
                {".juju/myenv-private-key.pem", caKey},
1146
 
        })
 
1194
        }...)
1147
1195
}
1148
1196
 
1149
 
func (*ConfigSuite) TestValidateUnknownAttrs(c *gc.C) {
1150
 
        defer makeFakeHome(c).Restore()
 
1197
func (s *ConfigSuite) TestValidateUnknownAttrs(c *gc.C) {
 
1198
        s.addJujuFiles(c)
1151
1199
        cfg, err := config.New(config.UseDefaults, map[string]interface{}{
1152
1200
                "name":    "myenv",
1153
1201
                "type":    "other",
1199
1247
        return result
1200
1248
}
1201
1249
 
1202
 
func (*ConfigSuite) TestLoggingConfig(c *gc.C) {
1203
 
        defer makeFakeHome(c).Restore()
1204
 
 
 
1250
func (s *ConfigSuite) TestLoggingConfig(c *gc.C) {
 
1251
        s.addJujuFiles(c)
1205
1252
        config := newTestConfig(c, testing.Attrs{
1206
1253
                "logging-config": "<root>=WARNING;juju=DEBUG"})
1207
1254
        c.Assert(config.LoggingConfig(), gc.Equals, "<root>=WARNING;juju=DEBUG;unit=DEBUG")
1208
1255
}
1209
1256
 
1210
 
func (*ConfigSuite) TestLoggingConfigWithUnit(c *gc.C) {
1211
 
        defer makeFakeHome(c).Restore()
1212
 
 
 
1257
func (s *ConfigSuite) TestLoggingConfigWithUnit(c *gc.C) {
 
1258
        s.addJujuFiles(c)
1213
1259
        config := newTestConfig(c, testing.Attrs{
1214
1260
                "logging-config": "<root>=WARNING;unit=INFO"})
1215
1261
        c.Assert(config.LoggingConfig(), gc.Equals, "<root>=WARNING;unit=INFO")
1216
1262
}
1217
1263
 
1218
1264
func (s *ConfigSuite) TestLoggingConfigFromEnvironment(c *gc.C) {
1219
 
        defer makeFakeHome(c).Restore()
 
1265
        s.addJujuFiles(c)
1220
1266
        s.PatchEnvironment(osenv.JujuLoggingConfigEnvKey, "<root>=INFO")
1221
1267
 
1222
1268
        config := newTestConfig(c, nil)
1223
1269
        c.Assert(config.LoggingConfig(), gc.Equals, "<root>=INFO;unit=DEBUG")
1224
1270
}
1225
1271
 
1226
 
func (*ConfigSuite) TestProxyValuesWithFallback(c *gc.C) {
1227
 
        defer makeFakeHome(c).Restore()
 
1272
func (s *ConfigSuite) TestProxyValuesWithFallback(c *gc.C) {
 
1273
        s.addJujuFiles(c)
1228
1274
 
1229
1275
        config := newTestConfig(c, testing.Attrs{
1230
1276
                "http-proxy":  "http://user@10.0.0.1",
1241
1287
        c.Assert(config.NoProxy(), gc.Equals, "localhost,10.0.3.1")
1242
1288
}
1243
1289
 
1244
 
func (*ConfigSuite) TestProxyValues(c *gc.C) {
1245
 
        defer makeFakeHome(c).Restore()
1246
 
 
 
1290
func (s *ConfigSuite) TestProxyValues(c *gc.C) {
 
1291
        s.addJujuFiles(c)
1247
1292
        config := newTestConfig(c, testing.Attrs{
1248
1293
                "http-proxy":      "http://user@10.0.0.1",
1249
1294
                "https-proxy":     "https://user@10.0.0.1",
1260
1305
        c.Assert(config.AptFtpProxy(), gc.Equals, "ftp://user@10.0.0.2")
1261
1306
}
1262
1307
 
1263
 
func (*ConfigSuite) TestProxyValuesNotSet(c *gc.C) {
1264
 
        defer makeFakeHome(c).Restore()
1265
 
 
 
1308
func (s *ConfigSuite) TestProxyValuesNotSet(c *gc.C) {
 
1309
        s.addJujuFiles(c)
1266
1310
        config := newTestConfig(c, testing.Attrs{})
1267
1311
        c.Assert(config.HttpProxy(), gc.Equals, "")
1268
1312
        c.Assert(config.AptHttpProxy(), gc.Equals, "")
1273
1317
        c.Assert(config.NoProxy(), gc.Equals, "")
1274
1318
}
1275
1319
 
1276
 
func (*ConfigSuite) TestProxyConfigMap(c *gc.C) {
1277
 
        defer makeFakeHome(c).Restore()
1278
 
 
 
1320
func (s *ConfigSuite) TestProxyConfigMap(c *gc.C) {
 
1321
        s.addJujuFiles(c)
1279
1322
        cfg := newTestConfig(c, testing.Attrs{})
1280
1323
        proxy := osenv.ProxySettings{
1281
1324
                Http:    "http proxy",
1291
1334
        c.Assert(cfg.AptProxySettings(), gc.DeepEquals, proxy)
1292
1335
}
1293
1336
 
1294
 
func (*ConfigSuite) TestAptProxyConfigMap(c *gc.C) {
1295
 
        defer makeFakeHome(c).Restore()
1296
 
 
 
1337
func (s *ConfigSuite) TestAptProxyConfigMap(c *gc.C) {
 
1338
        s.addJujuFiles(c)
1297
1339
        cfg := newTestConfig(c, testing.Attrs{})
1298
1340
        proxy := osenv.ProxySettings{
1299
1341
                Http:  "http proxy",
1307
1349
        c.Assert(cfg.AptProxySettings(), gc.DeepEquals, proxy)
1308
1350
}
1309
1351
 
1310
 
func (*ConfigSuite) TestGenerateStateServerCertAndKey(c *gc.C) {
1311
 
        // In order to test missing certs, it checks the JUJU_HOME dir, so we need
1312
 
        // a fake home.
1313
 
        defer testing.MakeFakeHomeWithFiles(c, []testing.TestFile{
1314
 
                {".ssh/id_rsa.pub", "rsa\n"},
1315
 
        }).Restore()
 
1352
func (s *ConfigSuite) TestGenerateStateServerCertAndKey(c *gc.C) {
 
1353
        // Add a cert.
 
1354
        s.FakeHomeSuite.Home.AddFiles(c, testing.TestFile{".ssh/id_rsa.pub", "rsa\n"})
1316
1355
 
1317
1356
        for _, test := range []struct {
1318
1357
                configValues map[string]interface{}