Exemple #1
0
func (s *testSuite) TestRuntimeWatch(c *C) {
	c.Assert(s.client.Set(testPolicies["basic"]), IsNil)
	policy := db.NewPolicy("basic")
	c.Assert(s.client.Get(policy), IsNil)

	vol, err := db.CreateVolume(&db.VolumeRequest{Policy: policy, Name: "bar"})
	c.Assert(err, IsNil)

	objChan, errChan := s.client.WatchPrefix(&db.RuntimeOptions{})
	defer s.client.WatchPrefixStop(&db.RuntimeOptions{})
	opts := db.NewRuntimeOptions(vol.PolicyName, vol.VolumeName)
	opts.RateLimit.ReadBPS = 1000
	c.Assert(s.client.Set(opts), IsNil)

	select {
	case err := <-errChan:
		c.Assert(err, IsNil)
	case obj := <-objChan:
		c.Assert(obj.(*db.RuntimeOptions).RateLimit.ReadBPS, Equals, uint64(1000))
		c.Assert(obj.(*db.RuntimeOptions).Policy(), Not(Equals), "")
		c.Assert(obj.(*db.RuntimeOptions).Policy(), Equals, vol.PolicyName)
		c.Assert(obj.(*db.RuntimeOptions).Volume(), Not(Equals), "")
		c.Assert(obj.(*db.RuntimeOptions).Volume(), Equals, vol.VolumeName)
	}
}
Exemple #2
0
func (s *testSuite) TestVolumeCRUD(c *C) {
	policyNames := []string{"foo", "bar"}
	volumeNames := []string{"baz", "quux"}

	c.Assert(s.client.Set(&db.Volume{}), NotNil)

	_, err := db.CreateVolume(&db.VolumeRequest{Policy: nil})
	c.Assert(err, NotNil)

	// populate the policies so the next few tests don't give false positives
	for _, policy := range policyNames {
		copy := testPolicies["basic"].Copy()
		copy.(*db.Policy).Name = policy
		err := s.client.Set(copy)
		c.Assert(err, IsNil, Commentf("%v", err))
	}

	_, err = db.CreateVolume(&db.VolumeRequest{Policy: db.NewPolicy("foo"), Name: "bar", Options: map[string]string{"quux": "derp"}})
	c.Assert(err, NotNil)

	_, err = db.CreateVolume(&db.VolumeRequest{Policy: db.NewPolicy("foo"), Name: ""})
	c.Assert(err, NotNil)

	vol := db.NewVolume("foo", "bar")
	c.Assert(s.client.Get(vol).(*errored.Error).Contains(errors.NotExists), Equals, true)

	// _, err = s.client.ListVolumes("quux")
	// c.Assert(err, NotNil)

	for _, policyName := range policyNames {
		for _, volumeName := range volumeNames {
			policy := db.NewPolicy(policyName)
			c.Assert(s.client.Get(policy), IsNil)
			vcfg, err := db.CreateVolume(&db.VolumeRequest{Policy: policy, Name: volumeName, Options: map[string]string{"filesystem": ""}})
			c.Assert(err, IsNil)
			err = s.client.Set(vcfg)
			c.Assert(err, IsNil, Commentf("%v", err))
			err = s.client.Set(vcfg)
			c.Assert(err.(*errored.Error).Contains(errors.Exists), Equals, true)

			c.Assert(vcfg.CreateOptions.FileSystem, Equals, "ext4")

			defer func() { c.Assert(s.client.Delete(vcfg), IsNil) }()

			c.Assert(vcfg.VolumeName, Equals, volumeName)

			vcfg2 := db.NewVolume(policyName, volumeName)
			c.Assert(s.client.Get(vcfg2), IsNil)
			c.Assert(vcfg, DeepEquals, vcfg2)

			runtime := db.NewRuntimeOptions(policyName, volumeName)
			c.Assert(s.client.Get(runtime), IsNil)
			c.Assert(runtime, DeepEquals, vcfg.RuntimeOptions)

			vcfg.CreateOptions.Size = "0"
			c.Assert(s.client.Set(vcfg), NotNil)
		}

		volumes, err := s.client.ListPrefix(policyName, &db.Volume{})
		c.Assert(err, IsNil)

		volumeKeys := []string{}
		for _, volume := range volumes {
			volumeKeys = append(volumeKeys, volume.(*db.Volume).VolumeName)
		}

		sort.Strings(volumeKeys)

		c.Assert(volumeNames, DeepEquals, volumeKeys)
		for _, entity := range volumes {
			vol := entity.(*db.Volume)
			testPolicies["basic"].RuntimeOptions.SetKey(vol.String())
			c.Assert(vol.CreateOptions, DeepEquals, testPolicies["basic"].CreateOptions)
			c.Assert(vol.RuntimeOptions, DeepEquals, testPolicies["basic"].RuntimeOptions)
		}
	}

	allVols, err := s.client.List(&db.Volume{})
	c.Assert(err, IsNil)

	for _, policy := range policyNames {
		for _, volume := range volumeNames {
			found := false
			for _, ent := range allVols {
				vol := ent.(*db.Volume)
				if vol.PolicyName == policy && vol.VolumeName == volume {
					found = true
				}

				c.Assert(vol.CreateOptions, DeepEquals, testPolicies["basic"].CreateOptions)
				// Cannot use deepequals because of the private members in runtimeoptions now.
				c.Assert(vol.RuntimeOptions.UseSnapshots, Equals, testPolicies["basic"].RuntimeOptions.UseSnapshots)
				c.Assert(vol.RuntimeOptions.Snapshot, DeepEquals, testPolicies["basic"].RuntimeOptions.Snapshot)
				c.Assert(vol.RuntimeOptions.RateLimit, DeepEquals, testPolicies["basic"].RuntimeOptions.RateLimit)
			}

			c.Assert(found, Equals, true, Commentf("%s/%s", policy, volume))
		}
	}
}