Beispiel #1
0
func TestServicedCLI_CmdTemplateCompile(t *testing.T) {
	dir := "/path/to/template"

	expected, err := DefaultTemplateAPITest.CompileServiceTemplate(api.CompileTemplateConfig{Dir: dir})
	if err != nil {
		t.Fatal(err)
	}

	var actual template.ServiceTemplate
	output := pipe(InitTemplateAPITest, "serviced", "template", "compile", dir)
	if err := json.Unmarshal(output, &actual); err != nil {
		t.Fatalf("error unmarshaling resource: %s", err)
	}

	// Did you remember to update ServiceTemplate.Equals?
	if !actual.Equals(expected) {
		t.Fatalf("got:\n%+v\nwant:\n%+v", actual, expected)
	}
}
Beispiel #2
0
func TestServicedCLI_CmdTemplateList_one(t *testing.T) {
	templateID := "test-template-1"

	expected, err := DefaultTemplateAPITest.GetServiceTemplate(templateID)
	if err != nil {
		t.Fatal(err)
	}

	var actual template.ServiceTemplate
	output := pipe(InitTemplateAPITest, "serviced", "template", "list", templateID)

	if err := json.Unmarshal(output, &actual); err != nil {
		t.Fatalf("error unmarshaling resource: %s", err)
	}

	// Did you remember to update ServiceTemplate.Equals?
	if !actual.Equals(expected) {
		t.Fatalf("got:\n%+v\nwant:\n%+v", actual, expected)
	}
}
//AddServiceTemplate  adds a service template to the system. Returns the id of the template added
func (f *Facade) AddServiceTemplate(ctx datastore.Context, serviceTemplate servicetemplate.ServiceTemplate) (string, error) {
	hash, err := serviceTemplate.Hash()
	if err != nil {
		return "", err
	}
	serviceTemplate.ID = hash

	if st, _ := f.templateStore.Get(ctx, hash); st != nil {
		// This id already exists in the system
		glog.Infof("Not replacing existing template %s", hash)
		return hash, nil
	}

	if err = f.templateStore.Put(ctx, serviceTemplate); err != nil {
		return "", err
	}

	// this takes a while so don't block the main thread
	go LogstashContainerReloader(ctx, f)
	return hash, err
}
func (dt *DaoTest) TestDao_ServiceTemplate(t *C) {
	glog.V(0).Infof("TestDao_AddServiceTemplate started")
	defer glog.V(0).Infof("TestDao_AddServiceTemplate finished")

	var (
		unused     int
		templateId string
		templates  map[string]servicetemplate.ServiceTemplate
	)

	// Clean up old templates...
	if e := dt.Dao.GetServiceTemplates(0, &templates); e != nil {
		t.Fatalf("Failure getting service templates with error: %s", e)
	}
	for id, _ := range templates {
		if e := dt.Dao.RemoveServiceTemplate(id, &unused); e != nil {
			t.Fatalf("Failure removing service template %s with error: %s", id, e)
		}
	}

	template := servicetemplate.ServiceTemplate{
		ID:          "",
		Name:        "test_template",
		Description: "test template",
	}

	if e := dt.Dao.AddServiceTemplate(template, &templateId); e != nil {
		t.Fatalf("Failure adding service template %+v with error: %s", template, e)
	}

	if e := dt.Dao.GetServiceTemplates(0, &templates); e != nil {
		t.Fatalf("Failure getting service templates with error: %s", e)
	}
	if len(templates) != 1 {
		t.Fatalf("Expected 1 template. Found %d", len(templates))
	}
	if _, ok := templates[templateId]; !ok {
		t.Fatalf("Expected to find template that was added (%s), but did not.", templateId)
	}
	if templates[templateId].Name != "test_template" {
		t.Fatalf("Expected to find test_template. Found %s", templates[templateId].Name)
	}
	template.ID = templateId
	template.Description = "test_template_modified"
	if e := dt.Dao.UpdateServiceTemplate(template, &unused); e != nil {
		t.Fatalf("Failure updating service template %+v with error: %s", template, e)
	}
	if e := dt.Dao.GetServiceTemplates(0, &templates); e != nil {
		t.Fatalf("Failure getting service templates with error: %s", e)
	}
	if len(templates) != 1 {
		t.Fatalf("Expected 1 template. Found %d", len(templates))
	}
	if _, ok := templates[templateId]; !ok {
		t.Fatalf("Expected to find template that was updated (%s), but did not.", templateId)
	}
	if templates[templateId].Name != "test_template" {
		t.Fatalf("Expected to find test_template. Found %s", templates[templateId].Name)
	}
	if templates[templateId].Description != "test_template_modified" {
		t.Fatalf("Expected template to be modified. It hasn't changed!")
	}
	if e := dt.Dao.RemoveServiceTemplate(templateId, &unused); e != nil {
		t.Fatalf("Failure removing service template with error: %s", e)
	}
	time.Sleep(1 * time.Second) // race condition. :(
	if e := dt.Dao.GetServiceTemplates(0, &templates); e != nil {
		t.Fatalf("Failure getting service templates with error: %s", e)
	}
	if len(templates) != 0 {
		t.Fatalf("Expected zero templates. Found %d", len(templates))
	}
	if e := dt.Dao.UpdateServiceTemplate(template, &unused); e != nil {
		t.Fatalf("Failure updating service template %+v with error: %s", template, e)
	}
	if e := dt.Dao.GetServiceTemplates(0, &templates); e != nil {
		t.Fatalf("Failure getting service templates with error: %s", e)
	}
	if len(templates) != 1 {
		t.Fatalf("Expected 1 template. Found %d", len(templates))
	}
	if _, ok := templates[templateId]; !ok {
		t.Fatalf("Expected to find template that was updated (%s), but did not.", templateId)
	}
	if templates[templateId].Name != "test_template" {
		t.Fatalf("Expected to find test_template. Found %s", templates[templateId].Name)
	}
}