Пример #1
0
func TestExecuteTemplate(t *testing.T) {
	tests := []struct {
		inputTemplteRoot *template.Template
		templateName     string
		webServerAddr    string
		err              bool
		expected         string
	}{
	// TODO
	}

	mac1, _ := net.ParseMAC("FF:FF:FF:FF:00:0F")

	ds, err := datasource.ForTest(nil)
	if err != nil {
		t.Error("error in getting a DataSource instance for our test:", err)
		return
	}

	for i, tt := range tests {
		got, err := executeTemplate(
			tt.inputTemplteRoot, tt.templateName,
			ds, ds.MachineInterface(mac1),
			tt.webServerAddr)

		if tt.err && err == nil {
			t.Errorf("#%d: expected error, got nil", i)
			continue
		} else if !tt.err && err != nil {
			t.Errorf("#%d: expected no error, err=%q", i, err)
			continue
		}

		if tt.expected != got {
			t.Errorf("#%d: expected %q, got %q", i, tt.expected, got)
		}
	}
}
Пример #2
0
func TestMachineVariablesAPI(t *testing.T) {
	mac1, _ := net.ParseMAC("00:11:22:33:44:55")

	ds, err := datasource.ForTest(nil)
	if err != nil {
		t.Error("error in getting a DataSource instance for our test:", err)
		return
	}

	if err := ds.WhileMaster(); err != nil {
		t.Error("failed to register as the master instance:", err)
		return
	}
	defer func() {
		if err := ds.Shutdown(); err != nil {
			t.Error("failed to shutdown:", err)
		}
	}()

	r := &webServer{ds: ds}
	h := r.Handler()

	mi := ds.MachineInterface(mac1)
	_, err = mi.Machine(true, nil)
	if err != nil {
		t.Error("error while creating machine:", err)
		return
	}

	_ = mi.DeleteVariable("test")

	////////////////////////////////
	// MachineVariables
	req, err := http.NewRequest("GET", fmt.Sprintf(
		"http://test.com/api/machines/%s/variables/test", mac1), nil)
	if err != nil {
		t.Error("error while NewRequest:", err)
		return
	}

	w := httptest.NewRecorder()
	h.ServeHTTP(w, req)

	if w.Code != 200 {
		t.Error("unexpected status code while getting variables [1]:", w.Code)
		return
	}

	variables := make(map[string]string)
	err = json.Unmarshal(w.Body.Bytes(), &variables)
	if err != nil {
		t.Error("error while Unmarshal:", err, ", Body:", w.Body.String())
		return
	}

	if testVal, isIn := variables["test"]; isIn {
		t.Error("test shouldn't be among the variables, but has value=", testVal)
		return
	}

	////////////////////////////////
	// SetMachineVariable
	newVal := "192.168.1.1/24"

	req, err = http.NewRequest("PUT", fmt.Sprintf(
		"http://test.com/api/machines/%s/variables/test?value=%s", mac1, newVal), nil)
	if err != nil {
		t.Error("error while NewRequest:", err)
		return
	}

	w = httptest.NewRecorder()
	h.ServeHTTP(w, req)

	if w.Code != 200 {
		t.Error("unexpected status code while putting variable test:", w.Code)
		return
	}

	////////////////////////////////
	// MachineVariables
	req, err = http.NewRequest("GET", fmt.Sprintf(
		"http://test.com/api/machines/%s/variables/test", mac1), nil)
	if err != nil {
		t.Error("error while NewRequest:", err)
		return
	}

	w = httptest.NewRecorder()
	h.ServeHTTP(w, req)

	if w.Code != 200 {
		t.Error("unexpected status code while getting variables [2]:", w.Code)
		return
	}

	variables = make(map[string]string)
	err = json.Unmarshal(w.Body.Bytes(), &variables)
	if err != nil {
		t.Error("error while Unmarshal:", err, ", Body:", w.Body.String())
		return
	}

	testVal, isIn := variables["test"]
	if testVal != newVal {
		t.Errorf("expecting %q for test but got %q. isIn=%v", newVal, testVal, isIn)
		return
	}
}