func TestCreateServer(t *testing.T) { assert := assert.New(t) client := NewMockClient() client.On("Post", "http://localhost/v2/servers/test", mock.Anything, mock.Anything).Return(nil) service := server.New(client) server := server.Server{ Name: "server", CPU: 1, MemoryGB: 1, GroupID: "group", SourceServerID: "UBUNTU", Type: "standard", } s, err := service.Create(server) ok, id := s.GetStatusID() assert.Nil(err) assert.True(s.IsQueued) assert.Equal(server.Name, s.Server) assert.True(ok) assert.NotEmpty(id) client.AssertExpectations(t) }
func TestUpdateServer_UpdateCredentials(t *testing.T) { assert := assert.New(t) client := NewMockClient() update := []api.Update{ api.Update{ Op: "set", Member: "password", Value: struct { Current string `json:"current"` Password string `json:"password"` }{ "current", "new", }, }, } client.On("Patch", "http://localhost/v2/servers/test/va1testserver01", update, mock.Anything).Return(nil) service := server.New(client) name := "va1testserver01" resp, err := service.Update(name, server.UpdateCredentials("current", "new")) assert.Nil(err) assert.Equal("status", resp.Rel) client.AssertExpectations(t) }
func TestCreateServer_InvalidServer(t *testing.T) { assert := assert.New(t) client := NewMockClient() service := server.New(client) s := server.Server{} _, err := service.Create(s) assert.NotNil(err) assert.Equal(err, server.ErrInvalidServer) }
func TestGetServerByUUID(t *testing.T) { assert := assert.New(t) client := NewMockClient() client.On("Get", "http://localhost/v2/servers/test/5404cf5ece2042dc9f2ac16ab67416bb?uuid=true", mock.Anything).Return(nil) service := server.New(client) resp, err := service.Get("5404cf5ece2042dc9f2ac16ab67416bb") assert.Nil(err) assert.Equal("va1testserver01", resp.Name) client.AssertExpectations(t) }
func TestPowerState_StopMaintenance(t *testing.T) { assert := assert.New(t) client := NewMockClient() client.On("Post", "http://localhost/v2/operations/test/servers/stopMaintenance", []string{"va1testserver01", "va1testserver02"}, mock.Anything).Return(nil) service := server.New(client) resp, err := service.PowerState(server.StopMaintenance, "va1testserver01", "va1testserver02") assert.Nil(err) assert.Equal(2, len(resp)) client.AssertExpectations(t) }
func TestDeleteServer(t *testing.T) { assert := assert.New(t) client := NewMockClient() client.On("Delete", "http://localhost/v2/servers/test/va1testserver01", mock.Anything).Return(nil) service := server.New(client) name := "va1testserver01" server, err := service.Delete(name) assert.Nil(err) assert.Equal(name, server.Server) client.AssertExpectations(t) }
func TestGetServer(t *testing.T) { assert := assert.New(t) client := NewMockClient() client.On("Get", "http://localhost/v2/servers/test/va1testserver01", mock.Anything).Return(nil) service := server.New(client) name := "va1testserver01" resp, err := service.Get(name) assert.Nil(err) assert.Equal(name, resp.Name) client.AssertExpectations(t) }
func TestRevertSnapshot(t *testing.T) { assert := assert.New(t) client := NewMockClient() client.On("Post", "http://localhost/v2/servers/test/va1testserver01/snapshots/10/restore", nil, mock.Anything).Return(nil) service := server.New(client) server := "va1testserver01" resp, err := service.RevertSnapshot(server, "10") assert.Nil(err) assert.NotEmpty(resp.ID) client.AssertExpectations(t) }
func TestGetServerCredentials(t *testing.T) { assert := assert.New(t) client := NewMockClient() client.On("Get", "http://localhost/v2/servers/test/va1testserver01/credentials", mock.Anything).Return(nil) service := server.New(client) resp, err := service.GetCredentials("va1testserver01") assert.Nil(err) assert.Equal("user", resp.Username) assert.Equal("pass", resp.Password) client.AssertExpectations(t) }
func New(config api.Config) *Client { c := &Client{ client: api.New(config), } c.Server = server.New(c.client) c.Status = status.New(c.client) c.AA = aa.New(c.client) c.Alert = alert.New(c.client) c.LB = lb.New(c.client) c.Group = group.New(c.client) c.DC = dc.New(c.client) return c }
func TestArchiveServer(t *testing.T) { assert := assert.New(t) client := NewMockClient() client.On("Post", "http://localhost/v2/operations/test/servers/archive", mock.Anything, mock.Anything).Return(nil) service := server.New(client) serverA := "va1testserver01" serverB := "va1testserver02" resp, err := service.Archive(serverA, serverB) assert.Nil(err) assert.Equal(2, len(resp)) client.AssertExpectations(t) }
func TestUpdateServer_UpdateCPU(t *testing.T) { assert := assert.New(t) client := NewMockClient() update := []api.Update{api.Update{Op: "set", Member: "cpu", Value: 1}} client.On("Patch", "http://localhost/v2/servers/test/va1testserver01", update, mock.Anything).Return(nil) service := server.New(client) name := "va1testserver01" resp, err := service.Update(name, server.UpdateCPU(1)) assert.Nil(err) assert.Equal("status", resp.Rel) client.AssertExpectations(t) }
func TestCreateSnapshot(t *testing.T) { assert := assert.New(t) client := NewMockClient() snapshot := server.Snapshot{Expiration: 3, Servers: []string{"va1testserver01", "va1testserver02"}} client.On("Post", "http://localhost/v2/operations/test/servers/createSnapshot", snapshot, mock.Anything).Return(nil) service := server.New(client) serverA := "va1testserver01" serverB := "va1testserver02" resp, err := service.CreateSnapshot(3, serverA, serverB) assert.Nil(err) assert.Equal(2, len(resp)) client.AssertExpectations(t) }
func TestDeletePublicIP(t *testing.T) { assert := assert.New(t) client := NewMockClient() client.On("Delete", mock.Anything, mock.Anything).Return(nil) service := server.New(client) addr := "10.0.0.1" name := "va1testserver01" resp, err := service.DeletePublicIP(name, addr) assert.Nil(err) assert.NotEmpty(resp.ID) client.AssertExpectations(t) }
func TestUpdateServer_UpdateGroupAndDescription(t *testing.T) { assert := assert.New(t) client := NewMockClient() update := []api.Update{ api.Update{Op: "set", Member: "groupId", Value: "12345"}, api.Update{Op: "set", Member: "description", Value: "new"}, } client.On("Patch", "http://localhost/v2/servers/test/va1testserver01", update, mock.Anything).Return(nil) service := server.New(client) name := "va1testserver01" err := service.Edit(name, server.UpdateGroup("12345"), server.UpdateDescription("new")) assert.Nil(err) client.AssertExpectations(t) }
func TestGetPublicIP(t *testing.T) { assert := assert.New(t) client := NewMockClient() client.On("Get", "http://localhost/v2/servers/test/va1testserver01/publicIPAddresses/10.0.0.1", mock.Anything).Return(nil) service := server.New(client) addr := "10.0.0.1" name := "va1testserver01" resp, err := service.GetPublicIP(name, addr) assert.Nil(err) assert.Equal(addr, resp.InternalIP) assert.Equal(1, len(resp.Ports)) client.AssertExpectations(t) }
func TestAddPublicIP(t *testing.T) { assert := assert.New(t) client := NewMockClient() client.On("Post", "http://localhost/v2/servers/test/va1testserver01/publicIPAddresses", mock.Anything, mock.Anything).Return(nil) service := server.New(client) name := "va1testserver01" ip := server.PublicIP{} ip.Ports = []server.Port{server.Port{Protocol: "TCP", Port: 8080}} resp, err := service.AddPublicIP(name, ip) assert.Nil(err) assert.Equal("status", resp.Rel) client.AssertExpectations(t) }
func TestAddSecondaryNetwork(t *testing.T) { assert := assert.New(t) client := NewMockClient() client.On("Post", "http://localhost/v2/servers/test/va1testserver01/networks", mock.Anything, mock.Anything).Return(nil) service := server.New(client) addr := "123.456.1.1" name := "va1testserver01" net := "61a7e67908ce4bedabfdaf694a1360fe" resp, err := service.AddSecondaryNetwork(name, net, addr) assert.Nil(err) assert.IsType(resp, &status.Status{}) assert.Equal(resp.ID, "2b70710dba4142dcaf3ab2de68e4f40c") client.AssertExpectations(t) }
func TestUpdatePublicIP(t *testing.T) { assert := assert.New(t) client := NewMockClient() client.On("Put", "http://localhost/v2/servers/test/va1testserver01/publicIPAddresses/10.0.0.1", mock.Anything, mock.Anything).Return(nil) service := server.New(client) addr := "10.0.0.1" name := "va1testserver01" ip := server.PublicIP{} ip.InternalIP = addr ip.Ports = []server.Port{server.Port{Protocol: "TCP", Port: 443}} resp, err := service.UpdatePublicIP(name, addr, ip) assert.Nil(err) assert.Equal("status", resp.Rel) client.AssertExpectations(t) }
func TestExecutePackage(t *testing.T) { assert := assert.New(t) client := NewMockClient() client.On("Post", "http://localhost/v2/operations/test/servers/executePackage", mock.Anything, mock.Anything).Return(nil) service := server.New(client) serverA := "va1testserver01" serverB := "va1testserver02" pkg := server.Package{ ID: "12345", Params: map[string]string{"key1": "value1", "key2": "value2"}, } resp, err := service.ExecutePackage(pkg, serverA, serverB) assert.Nil(err) assert.Equal(2, len(resp)) client.AssertExpectations(t) }
func TestUpdateServer_UpdateCustomfields(t *testing.T) { assert := assert.New(t) client := NewMockClient() fields := []api.Customfields{ api.Customfields{ ID: "deadbeef", Value: "abracadabra", }, } update := []api.Update{ api.Update{Op: "set", Member: "customFields", Value: fields}, } client.On("Patch", "http://localhost/v2/servers/test/va1testserver01", update, mock.Anything).Return(nil) service := server.New(client) name := "va1testserver01" err := service.Edit(name, server.UpdateCustomfields(fields)) assert.Nil(err) client.AssertExpectations(t) }
func TestUpdateServer_UpdateAdditionaldisks(t *testing.T) { assert := assert.New(t) client := NewMockClient() disks := []server.Disk{ server.Disk{ Path: "/data1", SizeGB: 10, Type: "partitioned", }, } update := []api.Update{ api.Update{Op: "set", Member: "disks", Value: disks}, } client.On("Patch", "http://localhost/v2/servers/test/va1testserver01", update, mock.Anything).Return(nil) service := server.New(client) name := "va1testserver01" err := service.Edit(name, server.UpdateAdditionaldisks(disks)) assert.Nil(err) client.AssertExpectations(t) }