func (s *S) Test_ConfigFileCRUD(t *C) { configFile, err := New("tenant_id", "/testpath", servicedefinition.ConfigFile{Content: "Test content", Filename: "testname"}) t.Assert(err, IsNil) configFile2 := SvcConfigFile{} if err := s.ps.Get(s.ctx, Key(configFile.ID), &configFile2); !datastore.IsErrNoSuchEntity(err) { t.Errorf("Expected ErrNoSuchEntity, got: %v", err) } err = s.ps.Put(s.ctx, Key(configFile.ID), configFile) if err != nil { t.Errorf("Unexpected failure creating configFile %-v", configFile) } //Test update configFile.ConfFile.Owner = "newowner" err = s.ps.Put(s.ctx, Key(configFile.ID), configFile) err = s.ps.Get(s.ctx, Key(configFile.ID), &configFile2) if err != nil { t.Errorf("Unexpected error: %v", err) } if configFile.ConfFile.Owner != configFile2.ConfFile.Owner { t.Errorf("configFiles did not match after update") } //test delete err = s.ps.Delete(s.ctx, Key(configFile.ID)) err = s.ps.Get(s.ctx, Key(configFile.ID), &configFile2) if err != nil && !datastore.IsErrNoSuchEntity(err) { t.Errorf("Unexpected error: %v", err) } }
func (s *S) Test_ServiceTemplateCRUD(t *C) { id := "st_test_id" st := ServiceTemplate{ID: id} _, err := s.store.Get(s.ctx, st.ID) t.Assert(datastore.IsErrNoSuchEntity(err), Equals, true) err = s.store.Put(s.ctx, st) t.Assert(err, NotNil) st.Name = testutils.ValidSvcDef.Name st.Services = []servicedefinition.ServiceDefinition{*testutils.ValidSvcDef} err = s.store.Put(s.ctx, st) t.Assert(err, IsNil) st2, err := s.store.Get(s.ctx, st.ID) t.Assert(err, IsNil) t.Assert(st2, NotNil) t.Assert(st2.Equals(&st), Equals, true) //Test update st.Name = "newName" st.Services[0].Command = "blam" err = s.store.Put(s.ctx, st) st2, err = s.store.Get(s.ctx, st.ID) t.Assert(err, IsNil) t.Assert(st2, NotNil) t.Assert(st2.Equals(&st), Equals, true) }
// AddService adds a service; return error if service already exists func (f *Facade) AddService(ctx datastore.Context, svc service.Service) error { glog.V(2).Infof("Facade.AddService: %+v", svc) store := f.serviceStore _, err := store.Get(ctx, svc.ID) if err != nil && !datastore.IsErrNoSuchEntity(err) { return err } else if err == nil { return fmt.Errorf("error adding service; %v already exists", svc.ID) } // verify the service with parent ID does not exist with the given name if s, err := store.FindChildService(ctx, svc.DeploymentID, svc.ParentServiceID, svc.Name); err != nil { glog.Errorf("Could not verify service path for %s: %s", svc.Name, err) return err } else if s != nil { err := fmt.Errorf("service %s found at %s", svc.Name, svc.ParentServiceID) glog.Errorf("Cannot create service %s: %s", svc.Name, err) return err } // Strip the database version; we already know this is a create svc.DatabaseVersion = 0 // Save a copy for checking configs later svcCopy := svc err = store.Put(ctx, &svc) if err != nil { glog.V(2).Infof("Facade.AddService: %+v", err) return err } glog.V(2).Infof("Facade.AddService: id %+v", svc.ID) // Compare the incoming config files to see if there are modifications from // the original. If there are, we need to perform an update to add those // modifications to the service. if svcCopy.OriginalConfigs != nil && !reflect.DeepEqual(svcCopy.OriginalConfigs, svcCopy.ConfigFiles) { // Get the current service in order to get the database version. We // don't save this because it won't have any of the updated config // files, among other things. cursvc, err := store.Get(ctx, svc.ID) if err != nil { glog.V(2).Infof("Facade.AddService: %+v", err) return err } svcCopy.DatabaseVersion = cursvc.DatabaseVersion for key, _ := range svcCopy.OriginalConfigs { glog.V(2).Infof("Facade.AddService: calling updateService for %s due to OriginalConfigs of %+v", svc.Name, key) } return f.updateService(ctx, &svcCopy) } glog.V(2).Infof("Facade.AddService: calling zk.updateService for %s %d ConfigFiles", svc.Name, len(svc.ConfigFiles)) return zkAPI(f).UpdateService(&svc) }
//func TestPutGetDelete(t *testing.T) { func (s *S) TestPutGetDelete(t *C) { esdriver := s.Driver() // driver, err := getConnection() // if err != nil { // t.Fatalf("Error initializing driver: %v", err) // } conn, err := esdriver.GetConnection() if err != nil { t.Fatalf("Error getting connection: %v", err) } k := datastore.NewKey("tweet", "1") tweet := map[string]string{ "user": "******", "post_date": "2009-11-15T14:12:12", "message": "trying out Elasticsearch", } tweetJSON, err := json.Marshal(tweet) err = conn.Put(k, datastore.NewJSONMessage(tweetJSON, 0)) if err != nil { t.Errorf("%v", err) } //Get tweet raw, err := conn.Get(k) if err != nil { t.Fatalf("Unexpected: %v", err) } glog.Infof("raw is %v", string(raw.Bytes())) var tweetMap map[string]string json.Unmarshal(raw.Bytes(), &tweetMap) glog.Infof("tweet is %v", tweetMap) if tweetMap["user"] != "kimchy" { t.Errorf("Expected kimchy, found %s", tweetMap["user"]) } //Delete tweet err = conn.Delete(k) if err != nil { t.Errorf("Unexpected delete error: %v", err) } //test not found raw, err = conn.Get(k) if raw != nil { t.Errorf("Expected nil return;") } if err == nil { t.Error("Expected error, not nil") } else if !datastore.IsErrNoSuchEntity(err) { glog.Infof("type is %s", reflect.ValueOf(err)) t.Fatalf("Unexpected: %v", err) } }
func (s *S) Test_AddressAssignmentCRUD(t *C) { defer s.ps.Delete(s.ctx, Key("testid")) assignment := &AddressAssignment{ ID: "testID", AssignmentType: "static", ServiceID: "svcid", EndpointName: "epname", IPAddr: "10.0.1.5", HostID: "hostid", Port: 500, } assignment2 := AddressAssignment{} if err := s.ps.Get(s.ctx, Key(assignment.ID), &assignment2); !datastore.IsErrNoSuchEntity(err) { t.Errorf("Expected ErrNoSuchEntity, got: %v", err) } err := s.ps.Put(s.ctx, Key(assignment.ID), assignment) t.Assert(err, IsNil) //Test update assignment.EndpointName = "BLAM" err = s.ps.Put(s.ctx, Key(assignment.ID), assignment) t.Assert(err, IsNil) err = s.ps.Get(s.ctx, Key(assignment.ID), &assignment2) t.Assert(err, IsNil) if assignment.EndpointName != assignment2.EndpointName { t.Errorf("assignments did not match after update") } //test delete err = s.ps.Delete(s.ctx, Key(assignment.ID)) t.Assert(err, IsNil) err = s.ps.Get(s.ctx, Key(assignment.ID), &assignment2) if err != nil && !datastore.IsErrNoSuchEntity(err) { t.Errorf("Unexpected error: %v", err) } }
//addUser places a new user record into elastic searchp func (this *ControlPlaneDao) AddUser(newUser userdomain.User, userName *string) error { glog.V(2).Infof("ControlPlane.NewUser: %+v", newUser) name := strings.TrimSpace(*userName) newUser.Password = hashPassword(newUser.Password) // save the user var existing userdomain.User if err := this.GetUser(name, &existing); err != nil && !datastore.IsErrNoSuchEntity(err) { return err } store := userdomain.NewStore() return store.Put(datastore.Get(), userdomain.Key(name), &newUser) }
// GetHost gets a host by id. Returns nil if host not found func (f *Facade) GetHost(ctx datastore.Context, hostID string) (*host.Host, error) { glog.V(2).Infof("Facade.GetHost: id=%s", hostID) var value host.Host err := f.hostStore.Get(ctx, host.HostKey(hostID), &value) glog.V(4).Infof("Facade.GetHost: get error %v", err) if datastore.IsErrNoSuchEntity(err) { return nil, nil } if err != nil { return nil, err } return &value, nil }
func (s *S) Test_PoolCRUD(t *C) { defer s.ps.Delete(s.ctx, Key("testid")) pool := New("testID") pool.Realm = "default" pool2 := ResourcePool{} if err := s.ps.Get(s.ctx, Key(pool.ID), &pool2); !datastore.IsErrNoSuchEntity(err) { t.Errorf("Expected ErrNoSuchEntity, got: %v", err) } err := s.ps.Put(s.ctx, Key(pool.ID), pool) if err != nil { t.Errorf("Unexpected failure creating pool %-v", pool) } //Test update pool.CoreLimit = 1024 err = s.ps.Put(s.ctx, Key(pool.ID), pool) err = s.ps.Get(s.ctx, Key(pool.ID), &pool2) if err != nil { t.Errorf("Unexpected error: %v", err) } if pool.CoreLimit != pool2.CoreLimit { t.Errorf("pools did not match after update") } //test delete err = s.ps.Delete(s.ctx, Key(pool.ID)) err = s.ps.Get(s.ctx, Key(pool.ID), &pool2) if err != nil && !datastore.IsErrNoSuchEntity(err) { t.Errorf("Unexpected error: %v", err) } }
func (s *S) TestPutGetDelete(t *C) { ctx := s.ctx ds := datastore.New() key := datastore.NewKey("tweet", "1") tweet := tweettest{"kimchy", "", "2009-11-15T14:12:12", "trying out Elasticsearch", version} err := ds.Put(ctx, key, &tweet) if err != nil { t.Errorf("%v", err) } //Get tweet var storedtweet tweettest err = ds.Get(ctx, key, &storedtweet) if err != nil { t.Fatalf("Unexpected: %v", err) } glog.Infof("tweet is %v", &storedtweet) if storedtweet.User != "kimchy" { t.Errorf("Expected kimchy, found %s", storedtweet.User) } //Delete tweet err = ds.Delete(ctx, key) if err != nil { t.Errorf("Unexpected delete error: %v", err) } //test not found err = ds.Get(ctx, key, &storedtweet) if err == nil { t.Error("Expected error, not nil") } else if !datastore.IsErrNoSuchEntity(err) { glog.Infof("type is %s", reflect.ValueOf(err)) t.Fatalf("Unexpected: %v", err) } }
func (s *FacadeTest) Test_HostCRUD(t *C) { testid := "deadb10f" poolid := "pool-id" defer s.Facade.RemoveHost(s.CTX, testid) //fill host with required values h, err := host.Build("", "65535", poolid, []string{}...) h.ID = testid if err != nil { t.Fatalf("Unexpected error building host: %v", err) } glog.Infof("Facade test add host %v", h) err = s.Facade.AddHost(s.CTX, h) //should fail since pool doesn't exist if err == nil { t.Errorf("Expected error: %v", err) } //create pool for test rp := pool.New(poolid) if err := s.Facade.AddResourcePool(s.CTX, rp); err != nil { t.Fatalf("Could not add pool for test: %v", err) } defer s.Facade.RemoveResourcePool(s.CTX, poolid) err = s.Facade.AddHost(s.CTX, h) if err != nil { t.Errorf("Unexpected error adding host: %v", err) } //Test re-add fails err = s.Facade.AddHost(s.CTX, h) if err == nil { t.Errorf("Expected already exists error: %v", err) } h2, err := s.Facade.GetHost(s.CTX, testid) if err != nil { t.Errorf("Unexpected error: %v", err) } if h2 == nil { t.Error("Unexpected nil host") } else if !host.HostEquals(t, h, h2) { t.Error("Hosts did not match") } //Test update h.Memory = 1024 err = s.Facade.UpdateHost(s.CTX, h) h2, err = s.Facade.GetHost(s.CTX, testid) if err != nil { t.Errorf("Unexpected error: %v", err) } if !host.HostEquals(t, h, h2) { t.Error("Hosts did not match") } //test delete err = s.Facade.RemoveHost(s.CTX, testid) h2, err = s.Facade.GetHost(s.CTX, testid) if err != nil && !datastore.IsErrNoSuchEntity(err) { t.Errorf("Unexpected error: %v", err) } }
func (s *S) Test_HostCRUD(t *C) { hostID := "deadb40f" defer s.hs.Delete(s.ctx, HostKey(hostID)) var host2 Host if err := s.hs.Get(s.ctx, HostKey(hostID), &host2); !datastore.IsErrNoSuchEntity(err) { t.Errorf("Expected ErrNoSuchEntity, got: %v", err) } host := New() err := s.hs.Put(s.ctx, HostKey(hostID), host) if err == nil { t.Errorf("Expected failure to create host %-v", host) } host.ID = hostID err = s.hs.Put(s.ctx, HostKey(host.ID), host) if err == nil { t.Errorf("Expected failure to create host %-v", host) } //fill host with required values host, err = Build("", "65535", "pool-id", []string{}...) host.ID = hostID if err != nil { t.Fatalf("Unexpected error building host: %v", err) } err = s.hs.Put(s.ctx, HostKey(hostID), host) if err != nil { t.Errorf("Unexpected error: %v", err) } err = s.hs.Get(s.ctx, HostKey(hostID), &host2) if err != nil { t.Errorf("Unexpected error: %v", err) } if !HostEquals(t, host, &host2) { t.Error("Hosts did not match") } //Test update host.Memory = 1024 err = s.hs.Put(s.ctx, HostKey(host.ID), host) err = s.hs.Get(s.ctx, HostKey(hostID), &host2) if err != nil { t.Errorf("Unexpected error: %v", err) } if !HostEquals(t, host, &host2) { t.Error("Hosts did not match") } //test delete err = s.hs.Delete(s.ctx, HostKey(hostID)) err = s.hs.Get(s.ctx, HostKey(hostID), &host2) if err != nil && !datastore.IsErrNoSuchEntity(err) { t.Errorf("Unexpected error: %v", err) } }