func (s *testSuite) TestEtcdDown(c *C) { if !IsEtcd() { c.Skip("not etcd") } volClient, err := etcd.NewClient(etcdHosts, "volplugin") c.Assert(err, IsNil) c.Assert(exec.Command("/bin/sh", "-c", "sudo systemctl stop etcd").Run(), IsNil) for { if err := exec.Command("etcdctl", "cluster-health").Run(); err != nil { break } time.Sleep(time.Second / 4) } defer func() { c.Assert(exec.Command("/bin/sh", "-c", "sudo systemctl start etcd").Run(), IsNil) for { if err := exec.Command("etcdctl", "cluster-health").Run(); err == nil { break } time.Sleep(time.Second / 4) } }() policy := db.NewPolicy("policy") c.Assert(volClient.Set(policy), NotNil) c.Assert(policy, DeepEquals, db.NewPolicy("policy")) }
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) } }
func (s *testSuite) TestPolicyCRUD(c *C) { for _, name := range passingPolicies { c.Assert(s.client.Set(testPolicies[name]), IsNil, Commentf("%v", name)) policy := db.NewPolicy(testPolicies[name].Name) c.Assert(s.client.Get(policy), IsNil, Commentf("%v", name)) c.Assert(policy, DeepEquals, testPolicies[name]) } for _, name := range failingPolicies { c.Assert(s.client.Set(testPolicies[name]), NotNil, Commentf("%v", name)) } }
func (s *testSuite) TestConsulDown(c *C) { if !IsConsul() { c.Skip("not consul") } volClient, err := consul.NewClient("volplugin", &api.Config{}) c.Assert(err, IsNil) c.Assert(exec.Command("/bin/sh", "-c", "sudo systemctl stop consul").Run(), IsNil) for { if err := exec.Command("/bin/sh", "-c", "consul info | grep -q 'members = 2'").Run(); err != nil { break } time.Sleep(time.Second / 4) } defer startConsul(c) policy := db.NewPolicy("policy") c.Assert(volClient.Set(policy), NotNil) c.Assert(policy, DeepEquals, db.NewPolicy("policy")) }
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)) } } }