Merge branch '18947-githttpd'
[arvados.git] / lib / config / load_test.go
index 3e0368cc03835c7763c26534d237da882d5e8f3e..2d87b906c9b399f49f243d170984a31d712f6ed9 100644 (file)
@@ -29,6 +29,8 @@ func Test(t *testing.T) {
 
 var _ = check.Suite(&LoadSuite{})
 
+var emptyConfigYAML = `Clusters: {"z1111": {}}`
+
 // Return a new Loader that reads cluster config from configdata
 // (instead of the usual default /etc/arvados/config.yml), and logs to
 // logdst or (if that's nil) c.Log.
@@ -59,7 +61,7 @@ func (s *LoadSuite) TestEmpty(c *check.C) {
 }
 
 func (s *LoadSuite) TestNoConfigs(c *check.C) {
-       cfg, err := testLoader(c, `Clusters: {"z1111": {}}`, nil).Load()
+       cfg, err := testLoader(c, emptyConfigYAML, nil).Load()
        c.Assert(err, check.IsNil)
        c.Assert(cfg.Clusters, check.HasLen, 1)
        cc, err := cfg.GetCluster("z1111")
@@ -69,6 +71,18 @@ func (s *LoadSuite) TestNoConfigs(c *check.C) {
        c.Check(cc.API.MaxItemsPerResponse, check.Equals, 1000)
 }
 
+func (s *LoadSuite) TestNullKeyDoesNotOverrideDefault(c *check.C) {
+       ldr := testLoader(c, `{"Clusters":{"z1111":{"API":}}}`, nil)
+       ldr.SkipDeprecated = true
+       cfg, err := ldr.Load()
+       c.Assert(err, check.IsNil)
+       c1, err := cfg.GetCluster("z1111")
+       c.Assert(err, check.IsNil)
+       c.Check(c1.ClusterID, check.Equals, "z1111")
+       c.Check(c1.API.MaxRequestAmplification, check.Equals, 4)
+       c.Check(c1.API.MaxItemsPerResponse, check.Equals, 1000)
+}
+
 func (s *LoadSuite) TestMungeLegacyConfigArgs(c *check.C) {
        f, err := ioutil.TempFile("", "")
        c.Check(err, check.IsNil)
@@ -79,7 +93,7 @@ func (s *LoadSuite) TestMungeLegacyConfigArgs(c *check.C) {
        f, err = ioutil.TempFile("", "")
        c.Check(err, check.IsNil)
        defer os.Remove(f.Name())
-       io.WriteString(f, "Clusters: {aaaaa: {}}\n")
+       io.WriteString(f, emptyConfigYAML)
        newfile := f.Name()
 
        for _, trial := range []struct {
@@ -209,18 +223,16 @@ Clusters:
       KeepStore:
         InternalURLs:
           "http://host.example:12345": {}
-      # we use Keepproxy instead of Keepstore for the RendezVous test,
-      # to avoid the "keepstore has no volumes" warning
-      Keepproxy:
+      Keepstore:
         InternalURLs:
           "http://host.example:12345":
-            # ideally we would reject Rendezvous here too, but
-            # currently we don't
             RendezVous: x
     ServiceS:
       Keepstore:
         InternalURLs:
           "http://host.example:12345": {}
+    Volumes:
+      zzzzz-nyw5e-aaaaaaaaaaaaaaa: {}
 `, &logbuf).Load()
        c.Assert(err, check.IsNil)
        c.Log(logbuf.String())
@@ -293,8 +305,6 @@ func (s *LoadSuite) TestNoUnrecognizedKeysInDefaultConfig(c *check.C) {
 
 func (s *LoadSuite) TestNoWarningsForDumpedConfig(c *check.C) {
        var logbuf bytes.Buffer
-       logger := logrus.New()
-       logger.Out = &logbuf
        cfg, err := testLoader(c, `
 Clusters:
  zzzzz:
@@ -328,11 +338,7 @@ func (s *LoadSuite) TestUnacceptableTokens(c *check.C) {
        } {
                c.Logf("trying bogus config: %s", trial.example)
                _, err := testLoader(c, "Clusters:\n zzzzz:\n  "+trial.example, nil).Load()
-               if trial.short {
-                       c.Check(err, check.ErrorMatches, `Clusters.zzzzz.`+trial.configPath+`: unacceptable characters in token.*`)
-               } else {
-                       c.Check(err, check.ErrorMatches, `Clusters.zzzzz.`+trial.configPath+`: unacceptable characters in token.*`)
-               }
+               c.Check(err, check.ErrorMatches, `Clusters.zzzzz.`+trial.configPath+`: unacceptable characters in token.*`)
        }
 }
 
@@ -564,11 +570,191 @@ func (s *LoadSuite) TestListKeys(c *check.C) {
                c.Errorf("Should have produced an error")
        }
 
-       var logbuf bytes.Buffer
-       loader := testLoader(c, string(DefaultYAML), &logbuf)
+       loader := testLoader(c, string(DefaultYAML), nil)
        cfg, err := loader.Load()
        c.Assert(err, check.IsNil)
        if err := checkListKeys("", cfg); err != nil {
                c.Error(err)
        }
 }
+
+func (s *LoadSuite) TestImplicitStorageClasses(c *check.C) {
+       // If StorageClasses and Volumes.*.StorageClasses are all
+       // empty, there is a default storage class named "default".
+       ldr := testLoader(c, `{"Clusters":{"z1111":{}}}`, nil)
+       cfg, err := ldr.Load()
+       c.Assert(err, check.IsNil)
+       cc, err := cfg.GetCluster("z1111")
+       c.Assert(err, check.IsNil)
+       c.Check(cc.StorageClasses, check.HasLen, 1)
+       c.Check(cc.StorageClasses["default"].Default, check.Equals, true)
+       c.Check(cc.StorageClasses["default"].Priority, check.Equals, 0)
+
+       // The implicit "default" storage class is used by all
+       // volumes.
+       ldr = testLoader(c, `
+Clusters:
+ z1111:
+  Volumes:
+   z: {}`, nil)
+       cfg, err = ldr.Load()
+       c.Assert(err, check.IsNil)
+       cc, err = cfg.GetCluster("z1111")
+       c.Assert(err, check.IsNil)
+       c.Check(cc.StorageClasses, check.HasLen, 1)
+       c.Check(cc.StorageClasses["default"].Default, check.Equals, true)
+       c.Check(cc.StorageClasses["default"].Priority, check.Equals, 0)
+       c.Check(cc.Volumes["z"].StorageClasses["default"], check.Equals, true)
+
+       // The "default" storage class isn't implicit if any classes
+       // are configured explicitly.
+       ldr = testLoader(c, `
+Clusters:
+ z1111:
+  StorageClasses:
+   local:
+    Default: true
+    Priority: 111
+  Volumes:
+   z:
+    StorageClasses:
+     local: true`, nil)
+       cfg, err = ldr.Load()
+       c.Assert(err, check.IsNil)
+       cc, err = cfg.GetCluster("z1111")
+       c.Assert(err, check.IsNil)
+       c.Check(cc.StorageClasses, check.HasLen, 1)
+       c.Check(cc.StorageClasses["local"].Default, check.Equals, true)
+       c.Check(cc.StorageClasses["local"].Priority, check.Equals, 111)
+
+       // It is an error for a volume to refer to a storage class
+       // that isn't listed in StorageClasses.
+       ldr = testLoader(c, `
+Clusters:
+ z1111:
+  StorageClasses:
+   local:
+    Default: true
+    Priority: 111
+  Volumes:
+   z:
+    StorageClasses:
+     nx: true`, nil)
+       _, err = ldr.Load()
+       c.Assert(err, check.ErrorMatches, `z: volume refers to storage class "nx" that is not defined.*`)
+
+       // It is an error for a volume to refer to a storage class
+       // that isn't listed in StorageClasses ... even if it's
+       // "default", which would exist implicitly if it weren't
+       // referenced explicitly by a volume.
+       ldr = testLoader(c, `
+Clusters:
+ z1111:
+  Volumes:
+   z:
+    StorageClasses:
+     default: true`, nil)
+       _, err = ldr.Load()
+       c.Assert(err, check.ErrorMatches, `z: volume refers to storage class "default" that is not defined.*`)
+
+       // If the "default" storage class is configured explicitly, it
+       // is not used implicitly by any volumes, even if it's the
+       // only storage class.
+       var logbuf bytes.Buffer
+       ldr = testLoader(c, `
+Clusters:
+ z1111:
+  StorageClasses:
+   default:
+    Default: true
+    Priority: 111
+  Volumes:
+   z: {}`, &logbuf)
+       _, err = ldr.Load()
+       c.Assert(err, check.ErrorMatches, `z: volume has no StorageClasses listed`)
+
+       // If StorageClasses are configured explicitly, there must be
+       // at least one with Default: true. (Calling one "default" is
+       // not sufficient.)
+       ldr = testLoader(c, `
+Clusters:
+ z1111:
+  StorageClasses:
+   default:
+    Priority: 111
+  Volumes:
+   z:
+    StorageClasses:
+     default: true`, nil)
+       _, err = ldr.Load()
+       c.Assert(err, check.ErrorMatches, `there is no default storage class.*`)
+}
+
+func (s *LoadSuite) TestPreemptiblePriceFactor(c *check.C) {
+       yaml := `
+Clusters:
+ z1111:
+  InstanceTypes:
+   Type1:
+    RAM: 12345M
+    VCPUs: 8
+    Price: 1.23
+ z2222:
+  Containers:
+   PreemptiblePriceFactor: 0.5
+  InstanceTypes:
+   Type1:
+    RAM: 12345M
+    VCPUs: 8
+    Price: 1.23
+ z3333:
+  Containers:
+   PreemptiblePriceFactor: 0.5
+  InstanceTypes:
+   Type1:
+    RAM: 12345M
+    VCPUs: 8
+    Price: 1.23
+   Type1.preemptible: # higher price than the auto-added variant would use -- should generate warning
+    ProviderType: Type1
+    RAM: 12345M
+    VCPUs: 8
+    Price: 1.23
+    Preemptible: true
+   Type2:
+    RAM: 23456M
+    VCPUs: 16
+    Price: 2.46
+   Type2.preemptible: # identical to the auto-added variant -- so no warning
+    ProviderType: Type2
+    RAM: 23456M
+    VCPUs: 16
+    Price: 1.23
+    Preemptible: true
+`
+       var logbuf bytes.Buffer
+       cfg, err := testLoader(c, yaml, &logbuf).Load()
+       c.Assert(err, check.IsNil)
+       cc, err := cfg.GetCluster("z1111")
+       c.Assert(err, check.IsNil)
+       c.Check(cc.InstanceTypes["Type1"].Price, check.Equals, 1.23)
+       c.Check(cc.InstanceTypes, check.HasLen, 1)
+
+       cc, err = cfg.GetCluster("z2222")
+       c.Assert(err, check.IsNil)
+       c.Check(cc.InstanceTypes["Type1"].Preemptible, check.Equals, false)
+       c.Check(cc.InstanceTypes["Type1"].Price, check.Equals, 1.23)
+       c.Check(cc.InstanceTypes["Type1.preemptible"].Preemptible, check.Equals, true)
+       c.Check(cc.InstanceTypes["Type1.preemptible"].Price, check.Equals, 1.23/2)
+       c.Check(cc.InstanceTypes["Type1.preemptible"].ProviderType, check.Equals, "Type1")
+       c.Check(cc.InstanceTypes, check.HasLen, 2)
+
+       cc, err = cfg.GetCluster("z3333")
+       c.Assert(err, check.IsNil)
+       // Don't overwrite the explicitly configured preemptible variant
+       c.Check(cc.InstanceTypes["Type1.preemptible"].Price, check.Equals, 1.23)
+       c.Check(cc.InstanceTypes, check.HasLen, 4)
+       c.Check(logbuf.String(), check.Matches, `(?ms).*Clusters\.z3333\.InstanceTypes\[Type1\.preemptible\]: already exists, so not automatically adding a preemptible variant of Type1.*`)
+       c.Check(logbuf.String(), check.Not(check.Matches), `(?ms).*Type2\.preemptible.*`)
+       c.Check(logbuf.String(), check.Not(check.Matches), `(?ms).*(z1111|z2222)[^\n]*InstanceTypes.*`)
+}