Beispiel #1
0
func CreateMachine(c *echo.Context) error {
	rt := &machine{}

	err := utils.ParseJSONBody(c, rt)
	if err != nil {
		log.Error(err.Error())
		return err
	}

	machineType, err := vms.Type(rt.Type)
	if err != nil {
		return errors.MachineTypeNotFound
	}

	attr := vm.MachineAttributes{
		Type:     machineType,
		Name:     rt.Name,
		Username: rt.Username,
		Password: rt.AdminPassword,
		Ip:       rt.Ip,
	}

	m, err := vms.Create(attr)
	if err != nil {
		log.Error(err)
		return errors.UnableToCreateTheMachine
	}

	rt, err = getSerializableMachine(m.Id())
	if err != nil {
		return err
	}

	return utils.JSON(c, http.StatusOK, rt)
}
Beispiel #2
0
func TestSetNil(t *testing.T) {
	machine_attributes := vms.MachineAttributes{
		Type:     nil,
		Name:     "machine-test",
		Username: "******",
		Password: "******",
		Ip:       "127.0.0.1",
	}

	SetNil()
	new_machine, _ := connector.Create(machine_attributes)
	if new_machine != nil {
		t.Errorf("Create(): Returned value should be nil")
	}

	SetNil()
	machine_type, _ := connector.Type("default-test-machine-type")
	if machine_type != nil {
		t.Errorf("Type(): Returned value should be nil")
	}

	SetNil()
	all_machines, _ := connector.Types()
	if all_machines != nil {
		t.Errorf("Types(): Returned value should be nil")
	}
}
Beispiel #3
0
func CreateVM(c *echo.Context) error {
	//TODO READ BODY TO GET PASSWORD AND TYPE
	vm, err := vms.Create(c.Param("id"), "", nil)
	if err != nil {
		return retJsonError(c, err)
	}
	return c.JSON(
		http.StatusOK, hash{
			"vm": machinetoStruct(vm),
		})
}
Beispiel #4
0
func TestSetDelay(t *testing.T) {
	machine := getMachine()
	machine_attributes := vms.MachineAttributes{
		Type:     nil,
		Name:     "machine-test",
		Username: "******",
		Password: "******",
		Ip:       "127.0.0.1",
	}

	SetDelay(1000)
	called := time.Now()
	connector.Create(machine_attributes)
	if time.Since(called) < time.Duration(1000) {
		t.Errorf("Create(): Delay should be observed")
	}

	SetDelay(1000)
	called = time.Now()
	connector.Type("default-test-machine-type")
	if time.Since(called) < time.Duration(1000) {
		t.Errorf("Type(): Delay should be observed")
	}

	SetDelay(1000)
	called = time.Now()
	connector.Types()
	if time.Since(called) < time.Duration(1000) {
		t.Errorf("Types(): Delay should be observed")
	}

	SetDelay(1000)
	called = time.Now()
	machine.Start()
	if time.Since(called) < time.Duration(1000) {
		t.Errorf("Start(): Delay should be observed")
	}

	SetDelay(1000)
	called = time.Now()
	machine.Stop()
	if time.Since(called) < time.Duration(1000) {
		t.Errorf("Stop(): Delay should be observed")
	}

	SetDelay(1000)
	called = time.Now()
	machine.Terminate()
	if time.Since(called) < time.Duration(1000) {
		t.Errorf("Terminate(): Delay should be observed")
	}
}
Beispiel #5
0
func TestSetFail(t *testing.T) {
	machine := getMachine()
	machine_attributes := vms.MachineAttributes{
		Type:     nil,
		Name:     "machine-test",
		Username: "******",
		Password: "******",
		Ip:       "127.0.0.1",
	}

	SetFail()
	_, err := connector.Create(machine_attributes)
	if err == nil {
		t.Errorf("Create(): Error should not be nil")
	}

	SetFail()
	_, err = connector.Type("default-test-machine-type")
	if err == nil {
		t.Errorf("Type(): Error should not be nil")
	}

	SetFail()
	_, err = connector.Types()
	if err == nil {
		t.Errorf("Types(): Error should not be nil")
	}

	SetFail()
	err = machine.Start()
	if err == nil {
		t.Errorf("Start(): Error should not be nil")
	}

	SetFail()
	err = machine.Stop()
	if err == nil {
		t.Errorf("Stop(): Error should not be nil")
	}

	SetFail()
	err = machine.Terminate()
	if err == nil {
		t.Errorf("Terminate(): Error should not be nil")
	}
}
Beispiel #6
0
func TestCreate(t *testing.T) {
	machine_attributes := vms.MachineAttributes{
		Type:     nil,
		Name:     "machine-test",
		Username: "******",
		Password: "******",
		Ip:       "127.0.0.1",
	}

	new_machine, err := connector.Create(machine_attributes)
	if err != nil {
		log.Panicln(err)
	}
	if new_machine == nil {
		t.Fatalf("Machine was not created")
	}

	machine_id = new_machine.Id()
}