diff --git a/internal/cmd/certificate/create_test.go b/internal/cmd/certificate/create_test.go new file mode 100644 index 00000000..ea69b51f --- /dev/null +++ b/internal/cmd/certificate/create_test.go @@ -0,0 +1,49 @@ +package certificate + +import ( + "context" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestCreate(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := CreateCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + fx.Client.CertificateClient.EXPECT(). + CreateCertificate(gomock.Any(), hcloud.CertificateCreateOpts{ + Name: "test", + Type: hcloud.CertificateTypeManaged, + DomainNames: []string{"example.com"}, + }). + Return(hcloud.CertificateCreateResult{ + Certificate: &hcloud.Certificate{ + ID: 123, + Name: "test", + Type: hcloud.CertificateTypeManaged, + DomainNames: []string{"example.com"}, + }, + Action: &hcloud.Action{ID: 321}, + }, nil, nil) + fx.ActionWaiter.EXPECT(). + ActionProgress(gomock.Any(), &hcloud.Action{ID: 321}) + + out, err := fx.Run(cmd, []string{"--name", "test", "--type", "managed", "--domain", "example.com"}) + + expOut := "Certificate 123 created\n" + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/certificate/delete_test.go b/internal/cmd/certificate/delete_test.go new file mode 100644 index 00000000..babac312 --- /dev/null +++ b/internal/cmd/certificate/delete_test.go @@ -0,0 +1,43 @@ +package certificate + +import ( + "context" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDelete(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := DeleteCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + cert := &hcloud.Certificate{ + ID: 123, + Name: "test", + } + + fx.Client.CertificateClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(cert, nil, nil) + fx.Client.CertificateClient.EXPECT(). + Delete(gomock.Any(), cert). + Return(nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := "certificate test deleted\n" + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/certificate/describe_test.go b/internal/cmd/certificate/describe_test.go new file mode 100644 index 00000000..6fc005e5 --- /dev/null +++ b/internal/cmd/certificate/describe_test.go @@ -0,0 +1,68 @@ +package certificate + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDescribe(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := DescribeCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer) + fx.ExpectEnsureToken() + + fx.Client.CertificateClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(&hcloud.Certificate{ + ID: 123, + Name: "test", + Type: hcloud.CertificateTypeManaged, + Created: time.Date(2020, 8, 24, 12, 0, 0, 0, time.UTC), + NotValidBefore: time.Date(2020, 8, 24, 12, 0, 0, 0, time.UTC), + NotValidAfter: time.Date(2036, 8, 20, 12, 0, 0, 0, time.UTC), + DomainNames: []string{"example.com"}, + Labels: map[string]string{"key": "value", "key2": "value2"}, + UsedBy: []hcloud.CertificateUsedByRef{{ + ID: 123, + Type: hcloud.CertificateUsedByRefTypeLoadBalancer, + }}, + }, nil, nil) + fx.Client.LoadBalancerClient.EXPECT(). + LoadBalancerName(int64(123)). + Return("test") + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := `ID: 123 +Name: test +Type: managed +Fingerprint: +Created: Mon Aug 24 12:00:00 UTC 2020 (3 years ago) +Not valid before: Mon Aug 24 12:00:00 UTC 2020 (3 years ago) +Not valid after: Wed Aug 20 12:00:00 UTC 2036 (12 years from now) +Domain names: + - example.com +Labels: + key: value + key2: value2 +Used By: + - Type: load_balancer + - Name: test +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/certificate/list_test.go b/internal/cmd/certificate/list_test.go new file mode 100644 index 00000000..f87aa041 --- /dev/null +++ b/internal/cmd/certificate/list_test.go @@ -0,0 +1,51 @@ +package certificate + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestList(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + + fx.ExpectEnsureToken() + fx.Client.CertificateClient.EXPECT(). + AllWithOpts( + gomock.Any(), + hcloud.CertificateListOpts{ + ListOpts: hcloud.ListOpts{PerPage: 50}, + Sort: []string{"id:asc"}, + }, + ). + Return([]*hcloud.Certificate{ + { + ID: 123, + Name: "test", + Type: hcloud.CertificateTypeManaged, + DomainNames: []string{"example.com"}, + NotValidAfter: time.Date(2036, 8, 20, 12, 0, 0, 0, time.UTC), + Created: time.Now().Add(-20 * time.Minute), + }, + }, nil) + + out, err := fx.Run(cmd, []string{}) + + expOut := `ID NAME TYPE DOMAIN NAMES NOT VALID AFTER AGE +123 test managed example.com Wed Aug 20 12:00:00 UTC 2036 20m +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/datacenter/describe_test.go b/internal/cmd/datacenter/describe_test.go new file mode 100644 index 00000000..49330fda --- /dev/null +++ b/internal/cmd/datacenter/describe_test.go @@ -0,0 +1,57 @@ +package datacenter + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDescribe(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := DescribeCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer) + fx.ExpectEnsureToken() + + fx.Client.DatacenterClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(&hcloud.Datacenter{ + ID: 4, + Name: "fsn1-dc14", + Location: &hcloud.Location{Name: "fsn1"}, + Description: "Falkenstein 1 virtual DC 14", + }, nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := `ID: 4 +Name: fsn1-dc14 +Description: Falkenstein 1 virtual DC 14 +Location: + Name: fsn1 + Description: + Country: + City: + Latitude: 0.000000 + Longitude: 0.000000 +Server Types: + Available: + No available server types + Supported: + No supported server types +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/datacenter/list_test.go b/internal/cmd/datacenter/list_test.go new file mode 100644 index 00000000..009d8aa2 --- /dev/null +++ b/internal/cmd/datacenter/list_test.go @@ -0,0 +1,49 @@ +package datacenter + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestList(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + + fx.ExpectEnsureToken() + fx.Client.DatacenterClient.EXPECT(). + AllWithOpts( + gomock.Any(), + hcloud.DatacenterListOpts{ + ListOpts: hcloud.ListOpts{PerPage: 50}, + Sort: []string{"id:asc"}, + }, + ). + Return([]*hcloud.Datacenter{ + { + ID: 4, + Name: "fsn1-dc14", + Location: &hcloud.Location{Name: "fsn1"}, + Description: "Falkenstein 1 virtual DC 14", + }, + }, nil) + + out, err := fx.Run(cmd, []string{}) + + expOut := `ID NAME DESCRIPTION LOCATION +4 fsn1-dc14 Falkenstein 1 virtual DC 14 fsn1 +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/firewall/create_test.go b/internal/cmd/firewall/create_test.go new file mode 100644 index 00000000..913c4625 --- /dev/null +++ b/internal/cmd/firewall/create_test.go @@ -0,0 +1,46 @@ +package firewall + +import ( + "context" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestCreate(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := CreateCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + fx.Client.FirewallClient.EXPECT(). + Create(gomock.Any(), hcloud.FirewallCreateOpts{ + Name: "test", + Labels: make(map[string]string), + }). + Return(hcloud.FirewallCreateResult{ + Firewall: &hcloud.Firewall{ + ID: 123, + Name: "test", + }, + Actions: []*hcloud.Action{{ID: 321}}, + }, nil, nil) + fx.ActionWaiter.EXPECT(). + WaitForActions(gomock.Any(), []*hcloud.Action{{ID: 321}}) + + out, err := fx.Run(cmd, []string{"--name", "test"}) + + expOut := "Firewall 123 created\n" + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/firewall/delete_test.go b/internal/cmd/firewall/delete_test.go new file mode 100644 index 00000000..f43157c7 --- /dev/null +++ b/internal/cmd/firewall/delete_test.go @@ -0,0 +1,43 @@ +package firewall + +import ( + "context" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDelete(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := DeleteCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + firewall := &hcloud.Firewall{ + ID: 123, + Name: "test", + } + + fx.Client.FirewallClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(firewall, nil, nil) + fx.Client.FirewallClient.EXPECT(). + Delete(gomock.Any(), firewall). + Return(nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := "firewall test deleted\n" + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/firewall/describe_test.go b/internal/cmd/firewall/describe_test.go new file mode 100644 index 00000000..cdc9cdc7 --- /dev/null +++ b/internal/cmd/firewall/describe_test.go @@ -0,0 +1,77 @@ +package firewall + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDescribe(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := DescribeCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer) + fx.ExpectEnsureToken() + + fx.Client.FirewallClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(&hcloud.Firewall{ + ID: 123, + Name: "test", + Rules: []hcloud.FirewallRule{ + { + Direction: hcloud.FirewallRuleDirectionIn, + Description: hcloud.Ptr("ssh"), + Port: hcloud.Ptr("22"), + Protocol: hcloud.FirewallRuleProtocolTCP, + }, + }, + AppliedTo: []hcloud.FirewallResource{ + { + Type: hcloud.FirewallResourceTypeServer, + Server: &hcloud.FirewallResourceServer{ + ID: 321, + }, + }, + }, + Labels: map[string]string{ + "key": "value", + }, + }, nil, nil) + fx.Client.ServerClient.EXPECT(). + ServerName(int64(321)). + Return("myServer") + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := `ID: 123 +Name: test +Created: Mon Jan 1 00:00:00 UTC 0001 (a long while ago) +Labels: + key: value +Rules: + - Direction: in + Description: ssh + Protocol: tcp + Port: 22 + Source IPs: +Applied To: + - Type: server + Server ID: 321 + Server Name: myServer +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/firewall/list_test.go b/internal/cmd/firewall/list_test.go new file mode 100644 index 00000000..bd815008 --- /dev/null +++ b/internal/cmd/firewall/list_test.go @@ -0,0 +1,50 @@ +package firewall + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestList(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + + fx.ExpectEnsureToken() + fx.Client.FirewallClient.EXPECT(). + AllWithOpts( + gomock.Any(), + hcloud.FirewallListOpts{ + ListOpts: hcloud.ListOpts{PerPage: 50}, + Sort: []string{"id:asc"}, + }, + ). + Return([]*hcloud.Firewall{ + { + ID: 123, + Name: "test", + Rules: make([]hcloud.FirewallRule, 5), + AppliedTo: make([]hcloud.FirewallResource, 2), + Labels: make(map[string]string), + }, + }, nil) + + out, err := fx.Run(cmd, []string{}) + + expOut := `ID NAME RULES COUNT APPLIED TO COUNT +123 test 5 Rules 2 Servers | 0 Label Selectors +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/floatingip/create_test.go b/internal/cmd/floatingip/create_test.go new file mode 100644 index 00000000..5852829b --- /dev/null +++ b/internal/cmd/floatingip/create_test.go @@ -0,0 +1,104 @@ +package floatingip + +import ( + "context" + "net" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestCreate(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := CreateCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + fx.Client.FloatingIPClient.EXPECT(). + Create(gomock.Any(), hcloud.FloatingIPCreateOpts{ + Name: hcloud.Ptr("myFloatingIP"), + Type: hcloud.FloatingIPTypeIPv4, + HomeLocation: &hcloud.Location{Name: "fsn1"}, + Labels: make(map[string]string), + Description: hcloud.Ptr(""), + }). + Return(hcloud.FloatingIPCreateResult{ + FloatingIP: &hcloud.FloatingIP{ + ID: 123, + Name: "myFloatingIP", + IP: net.ParseIP("192.168.2.1"), + Type: hcloud.FloatingIPTypeIPv4, + }, + Action: nil, + }, nil, nil) + + out, err := fx.Run(cmd, []string{"--name", "myFloatingIP", "--type", "ipv4", "--home-location", "fsn1"}) + + expOut := `Floating IP 123 created +IPv4: 192.168.2.1 +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} + +func TestCreateProtection(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := CreateCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + floatingIp := &hcloud.FloatingIP{ + ID: 123, + Name: "myFloatingIP", + IP: net.ParseIP("192.168.2.1"), + Type: hcloud.FloatingIPTypeIPv4, + } + + fx.Client.FloatingIPClient.EXPECT(). + Create(gomock.Any(), hcloud.FloatingIPCreateOpts{ + Name: hcloud.Ptr("myFloatingIP"), + Type: hcloud.FloatingIPTypeIPv4, + HomeLocation: &hcloud.Location{Name: "fsn1"}, + Labels: make(map[string]string), + Description: hcloud.Ptr(""), + }). + Return(hcloud.FloatingIPCreateResult{ + FloatingIP: floatingIp, + Action: &hcloud.Action{ + ID: 321, + }, + }, nil, nil) + fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), &hcloud.Action{ID: 321}).Return(nil) + + fx.Client.FloatingIPClient.EXPECT(). + ChangeProtection(gomock.Any(), floatingIp, hcloud.FloatingIPChangeProtectionOpts{ + Delete: hcloud.Ptr(true), + }). + Return(&hcloud.Action{ID: 333}, nil, nil) + fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), &hcloud.Action{ID: 333}).Return(nil) + + out, err := fx.Run(cmd, []string{"--name", "myFloatingIP", "--type", "ipv4", "--home-location", "fsn1", "--enable-protection", "delete"}) + + expOut := `Floating IP 123 created +Resource protection enabled for floating IP 123 +IPv4: 192.168.2.1 +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/floatingip/delete_test.go b/internal/cmd/floatingip/delete_test.go new file mode 100644 index 00000000..e859a72c --- /dev/null +++ b/internal/cmd/floatingip/delete_test.go @@ -0,0 +1,43 @@ +package floatingip + +import ( + "context" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDelete(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := DeleteCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + floatingIP := &hcloud.FloatingIP{ + ID: 123, + Name: "test", + } + + fx.Client.FloatingIPClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(floatingIP, nil, nil) + fx.Client.FloatingIPClient.EXPECT(). + Delete(gomock.Any(), floatingIP). + Return(nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := "Floating IP test deleted\n" + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/floatingip/describe_test.go b/internal/cmd/floatingip/describe_test.go new file mode 100644 index 00000000..4b8c4762 --- /dev/null +++ b/internal/cmd/floatingip/describe_test.go @@ -0,0 +1,69 @@ +package floatingip + +import ( + "context" + "net" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDescribe(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := DescribeCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer) + fx.ExpectEnsureToken() + + fx.Client.FloatingIPClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(&hcloud.FloatingIP{ + ID: 123, + Type: hcloud.FloatingIPTypeIPv4, + Name: "test", + Server: &hcloud.Server{ID: 321}, + HomeLocation: &hcloud.Location{Name: "fsn1"}, + IP: net.ParseIP("192.168.2.1"), + Labels: map[string]string{ + "key": "value", + }, + Created: time.Date(2036, 8, 20, 12, 0, 0, 0, time.UTC), + }, nil, nil) + fx.Client.ServerClient.EXPECT(). + ServerName(int64(321)). + Return("myServer") + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := `ID: 123 +Type: ipv4 +Name: test +Description: - +Created: Wed Aug 20 12:00:00 UTC 2036 (12 years from now) +IP: 192.168.2.1 +Blocked: no +Home Location: fsn1 +Server: + ID: 321 + Name: myServer +DNS: + No reverse DNS entries +Protection: + Delete: no +Labels: + key: value +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/floatingip/list_test.go b/internal/cmd/floatingip/list_test.go new file mode 100644 index 00000000..19652415 --- /dev/null +++ b/internal/cmd/floatingip/list_test.go @@ -0,0 +1,52 @@ +package floatingip + +import ( + "context" + "net" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestList(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + + fx.ExpectEnsureToken() + fx.Client.FloatingIPClient.EXPECT(). + AllWithOpts( + gomock.Any(), + hcloud.FloatingIPListOpts{ + ListOpts: hcloud.ListOpts{PerPage: 50}, + Sort: []string{"id:asc"}, + }, + ). + Return([]*hcloud.FloatingIP{ + { + ID: 123, + Type: hcloud.FloatingIPTypeIPv4, + Name: "test", + HomeLocation: &hcloud.Location{Name: "fsn1"}, + IP: net.ParseIP("192.168.2.1"), + Created: time.Now().Add(-10 * time.Minute), + }, + }, nil) + + out, err := fx.Run(cmd, []string{}) + + expOut := `ID TYPE NAME DESCRIPTION IP HOME SERVER DNS AGE +123 ipv4 test - 192.168.2.1 fsn1 - - 10m +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/image/delete_test.go b/internal/cmd/image/delete_test.go new file mode 100644 index 00000000..00adbaf5 --- /dev/null +++ b/internal/cmd/image/delete_test.go @@ -0,0 +1,43 @@ +package image + +import ( + "context" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDelete(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := DeleteCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + image := &hcloud.Image{ + ID: 123, + Name: "test", + } + + fx.Client.ImageClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(image, nil, nil) + fx.Client.ImageClient.EXPECT(). + Delete(gomock.Any(), image). + Return(nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := "image test deleted\n" + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/image/describe_test.go b/internal/cmd/image/describe_test.go new file mode 100644 index 00000000..629b23b0 --- /dev/null +++ b/internal/cmd/image/describe_test.go @@ -0,0 +1,66 @@ +package image + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDescribe(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := DescribeCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer) + fx.ExpectEnsureToken() + + fx.Client.ImageClient.EXPECT(). + GetForArchitecture(gomock.Any(), "test", hcloud.ArchitectureX86). + Return(&hcloud.Image{ + ID: 123, + Type: hcloud.ImageTypeSystem, + Status: hcloud.ImageStatusAvailable, + Name: "test", + Created: time.Date(1905, 10, 6, 12, 0, 0, 0, time.UTC), + Description: "Test image", + ImageSize: 20.0, + DiskSize: 20.0, + Architecture: hcloud.ArchitectureX86, + Labels: map[string]string{ + "key": "value", + }, + }, nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := `ID: 123 +Type: system +Status: available +Name: test +Created: Fri Oct 6 12:00:00 UTC 1905 (a long while ago) +Description: Test image +Image size: 20.00 GB +Disk size: 20 GB +OS flavor: +OS version: - +Architecture: x86 +Rapid deploy: no +Protection: + Delete: no +Labels: + key: value +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/image/list_test.go b/internal/cmd/image/list_test.go new file mode 100644 index 00000000..d48582b3 --- /dev/null +++ b/internal/cmd/image/list_test.go @@ -0,0 +1,53 @@ +package image + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestList(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + + fx.ExpectEnsureToken() + fx.Client.ImageClient.EXPECT(). + AllWithOpts( + gomock.Any(), + hcloud.ImageListOpts{ + ListOpts: hcloud.ListOpts{PerPage: 50}, + Sort: []string{"id:asc"}, + IncludeDeprecated: true, + }, + ). + Return([]*hcloud.Image{ + { + ID: 123, + Type: hcloud.ImageTypeSystem, + Name: "test", + Architecture: hcloud.ArchitectureX86, + ImageSize: 20.0, + DiskSize: 15, + Created: time.Date(2036, 8, 20, 12, 0, 0, 0, time.UTC), + }, + }, nil) + + out, err := fx.Run(cmd, []string{}) + + expOut := `ID TYPE NAME DESCRIPTION ARCHITECTURE IMAGE SIZE DISK SIZE CREATED DEPRECATED +123 system test - x86 20.00 GB 15 GB Wed Aug 20 12:00:00 UTC 2036 - +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/iso/describe_test.go b/internal/cmd/iso/describe_test.go new file mode 100644 index 00000000..1e6ac170 --- /dev/null +++ b/internal/cmd/iso/describe_test.go @@ -0,0 +1,48 @@ +package iso + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDescribe(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := DescribeCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer) + fx.ExpectEnsureToken() + + fx.Client.ISOClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(&hcloud.ISO{ + ID: 123, + Name: "test", + Description: "Test ISO", + Type: hcloud.ISOTypePublic, + Architecture: hcloud.Ptr(hcloud.ArchitectureX86), + }, nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := `ID: 123 +Name: test +Description: Test ISO +Type: public +Architecture: x86 +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/iso/list_test.go b/internal/cmd/iso/list_test.go new file mode 100644 index 00000000..57c68f4f --- /dev/null +++ b/internal/cmd/iso/list_test.go @@ -0,0 +1,50 @@ +package iso + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestList(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + + fx.ExpectEnsureToken() + fx.Client.ISOClient.EXPECT(). + AllWithOpts( + gomock.Any(), + hcloud.ISOListOpts{ + ListOpts: hcloud.ListOpts{PerPage: 50}, + Sort: []string{"id:asc"}, + }, + ). + Return([]*hcloud.ISO{ + { + ID: 123, + Name: "test", + Description: "Test ISO", + Type: hcloud.ISOTypePublic, + Architecture: hcloud.Ptr(hcloud.ArchitectureX86), + }, + }, nil) + + out, err := fx.Run(cmd, []string{}) + + expOut := `ID NAME DESCRIPTION TYPE ARCHITECTURE +123 test Test ISO public x86 +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/loadbalancer/create_test.go b/internal/cmd/loadbalancer/create_test.go new file mode 100644 index 00000000..60753b5a --- /dev/null +++ b/internal/cmd/loadbalancer/create_test.go @@ -0,0 +1,118 @@ +package loadbalancer + +import ( + "context" + "net" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestCreate(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := CreateCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + fx.Client.LoadBalancerClient.EXPECT(). + Create(gomock.Any(), hcloud.LoadBalancerCreateOpts{ + Name: "myLoadBalancer", + LoadBalancerType: &hcloud.LoadBalancerType{Name: "lb11"}, + Location: &hcloud.Location{Name: "fsn1"}, + Labels: make(map[string]string), + }). + Return(hcloud.LoadBalancerCreateResult{ + LoadBalancer: &hcloud.LoadBalancer{ID: 123}, + Action: &hcloud.Action{ID: 321}, + }, nil, nil) + fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), &hcloud.Action{ID: 321}).Return(nil) + fx.Client.LoadBalancerClient.EXPECT(). + GetByID(gomock.Any(), int64(123)). + Return(&hcloud.LoadBalancer{ + ID: 123, + PublicNet: hcloud.LoadBalancerPublicNet{ + IPv4: hcloud.LoadBalancerPublicNetIPv4{ + IP: net.ParseIP("192.168.2.1"), + }, + IPv6: hcloud.LoadBalancerPublicNetIPv6{ + IP: net.IPv6zero, + }, + }, + }, nil, nil) + + out, err := fx.Run(cmd, []string{"--name", "myLoadBalancer", "--type", "lb11", "--location", "fsn1"}) + + expOut := `Load Balancer 123 created +IPv4: 192.168.2.1 +IPv6: :: +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} + +func TestCreateProtection(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := CreateCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + loadBalancer := &hcloud.LoadBalancer{ + ID: 123, + PublicNet: hcloud.LoadBalancerPublicNet{ + IPv4: hcloud.LoadBalancerPublicNetIPv4{ + IP: net.ParseIP("192.168.2.1"), + }, + IPv6: hcloud.LoadBalancerPublicNetIPv6{ + IP: net.IPv6zero, + }, + }, + } + + fx.Client.LoadBalancerClient.EXPECT(). + Create(gomock.Any(), hcloud.LoadBalancerCreateOpts{ + Name: "myLoadBalancer", + LoadBalancerType: &hcloud.LoadBalancerType{Name: "lb11"}, + Location: &hcloud.Location{Name: "fsn1"}, + Labels: make(map[string]string), + }). + Return(hcloud.LoadBalancerCreateResult{ + LoadBalancer: &hcloud.LoadBalancer{ID: 123}, + Action: &hcloud.Action{ID: 321}, + }, nil, nil) + fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), &hcloud.Action{ID: 321}).Return(nil) + fx.Client.LoadBalancerClient.EXPECT(). + GetByID(gomock.Any(), int64(123)). + Return(loadBalancer, nil, nil) + fx.Client.LoadBalancerClient.EXPECT(). + ChangeProtection(gomock.Any(), loadBalancer, hcloud.LoadBalancerChangeProtectionOpts{ + Delete: hcloud.Ptr(true), + }). + Return(&hcloud.Action{ID: 333}, nil, nil) + fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), &hcloud.Action{ID: 333}).Return(nil) + + out, err := fx.Run(cmd, []string{"--name", "myLoadBalancer", "--type", "lb11", "--location", "fsn1", "--enable-protection", "delete"}) + + expOut := `Load Balancer 123 created +Resource protection enabled for Load Balancer 123 +IPv4: 192.168.2.1 +IPv6: :: +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/loadbalancer/delete_test.go b/internal/cmd/loadbalancer/delete_test.go new file mode 100644 index 00000000..a81c4a7f --- /dev/null +++ b/internal/cmd/loadbalancer/delete_test.go @@ -0,0 +1,43 @@ +package loadbalancer + +import ( + "context" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDelete(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := DeleteCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + loadBalancer := &hcloud.LoadBalancer{ + ID: 123, + Name: "test", + } + + fx.Client.LoadBalancerClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(loadBalancer, nil, nil) + fx.Client.LoadBalancerClient.EXPECT(). + Delete(gomock.Any(), loadBalancer). + Return(nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := "Load Balancer test deleted\n" + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/loadbalancer/describe_test.go b/internal/cmd/loadbalancer/describe_test.go new file mode 100644 index 00000000..495a65ea --- /dev/null +++ b/internal/cmd/loadbalancer/describe_test.go @@ -0,0 +1,99 @@ +package loadbalancer + +import ( + "context" + "net" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/cmd/util" + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDescribe(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := DescribeCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer) + fx.ExpectEnsureToken() + + fx.Client.LoadBalancerClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(&hcloud.LoadBalancer{ + ID: 123, + Name: "test", + LoadBalancerType: &hcloud.LoadBalancerType{ + ID: 123, + Name: "lb11", + Description: "LB11", + MaxServices: 5, + MaxConnections: 10000, + MaxTargets: 25, + MaxAssignedCertificates: 10, + }, + Created: time.Date(1905, 10, 6, 12, 0, 0, 0, time.UTC), + PublicNet: hcloud.LoadBalancerPublicNet{ + Enabled: true, + IPv4: hcloud.LoadBalancerPublicNetIPv4{ + IP: net.ParseIP("192.168.2.1"), + }, + IPv6: hcloud.LoadBalancerPublicNetIPv6{ + IP: net.IPv6loopback, + }, + }, + Algorithm: hcloud.LoadBalancerAlgorithm{ + Type: hcloud.LoadBalancerAlgorithmTypeLeastConnections, + }, + IncludedTraffic: 20 * util.Tebibyte, + IngoingTraffic: 10 * util.Tebibyte, + OutgoingTraffic: 10 * util.Tebibyte, + }, nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := `ID: 123 +Name: test +Created: Fri Oct 6 12:00:00 UTC 1905 (a long while ago) +Public Net: + Enabled: yes + IPv4: 192.168.2.1 + IPv4 DNS PTR: + IPv6: ::1 + IPv6 DNS PTR: +Private Net: + No Private Network +Algorithm: least_connections +Load Balancer Type: lb11 (ID: 123) + ID: 123 + Name: lb11 + Description: LB11 + Max Services: 5 + Max Connections: 10000 + Max Targets: 25 + Max assigned Certificates: 10 +Services: + No services +Targets: + No targets +Traffic: + Outgoing: 10 TiB + Ingoing: 10 TiB + Included: 20 TiB +Protection: + Delete: no +Labels: + No labels +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/loadbalancertype/describe_test.go b/internal/cmd/loadbalancertype/describe_test.go new file mode 100644 index 00000000..c4bbdaa4 --- /dev/null +++ b/internal/cmd/loadbalancertype/describe_test.go @@ -0,0 +1,53 @@ +package loadbalancertype + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDescribe(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := DescribeCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer) + fx.ExpectEnsureToken() + + fx.Client.LoadBalancerTypeClient.EXPECT(). + Get(gomock.Any(), "lb11"). + Return(&hcloud.LoadBalancerType{ + ID: 123, + Name: "lb11", + Description: "LB11", + MaxServices: 5, + MaxConnections: 10000, + MaxTargets: 25, + MaxAssignedCertificates: 10, + }, nil, nil) + + out, err := fx.Run(cmd, []string{"lb11"}) + + expOut := `ID: 123 +Name: lb11 +Description: LB11 +Max Services: 5 +Max Connections: 10000 +Max Targets: 25 +Max assigned Certificates: 10 +Pricings per Location: +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/loadbalancertype/list_test.go b/internal/cmd/loadbalancertype/list_test.go new file mode 100644 index 00000000..824a305c --- /dev/null +++ b/internal/cmd/loadbalancertype/list_test.go @@ -0,0 +1,50 @@ +package loadbalancertype + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestList(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + + fx.ExpectEnsureToken() + fx.Client.LoadBalancerTypeClient.EXPECT(). + AllWithOpts( + gomock.Any(), + hcloud.LoadBalancerTypeListOpts{ + ListOpts: hcloud.ListOpts{PerPage: 50}, + Sort: []string{"id:asc"}, + }, + ). + Return([]*hcloud.LoadBalancerType{ + { + ID: 123, + Name: "test", + MaxServices: 12, + MaxConnections: 100, + MaxTargets: 5, + }, + }, nil) + + out, err := fx.Run(cmd, []string{}) + + expOut := `ID NAME DESCRIPTION MAX SERVICES MAX CONNECTIONS MAX TARGETS +123 test - 12 100 5 +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/location/list_test.go b/internal/cmd/location/list_test.go new file mode 100644 index 00000000..73e98915 --- /dev/null +++ b/internal/cmd/location/list_test.go @@ -0,0 +1,50 @@ +package location + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestList(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + + fx.ExpectEnsureToken() + fx.Client.LocationClient.EXPECT(). + AllWithOpts( + gomock.Any(), + hcloud.LocationListOpts{ + ListOpts: hcloud.ListOpts{PerPage: 50}, + Sort: []string{"id:asc"}, + }, + ). + Return([]*hcloud.Location{ + { + ID: 1, + Name: "fsn1", + NetworkZone: hcloud.NetworkZoneEUCentral, + Country: "DE", + City: "Falkenstein", + }, + }, nil) + + out, err := fx.Run(cmd, []string{}) + + expOut := `ID NAME DESCRIPTION NETWORK ZONE COUNTRY CITY +1 fsn1 - eu-central DE Falkenstein +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/network/create_test.go b/internal/cmd/network/create_test.go new file mode 100644 index 00000000..ebe9ee78 --- /dev/null +++ b/internal/cmd/network/create_test.go @@ -0,0 +1,87 @@ +package network + +import ( + "context" + "net" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestCreate(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := CreateCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + _, ipRange, _ := net.ParseCIDR("10.0.0.0/24") + fx.Client.NetworkClient.EXPECT(). + Create(gomock.Any(), hcloud.NetworkCreateOpts{ + Name: "myNetwork", + IPRange: ipRange, + Labels: make(map[string]string), + }). + Return(&hcloud.Network{ + ID: 123, + Name: "myNetwork", + IPRange: ipRange, + }, nil, nil) + + out, err := fx.Run(cmd, []string{"--name", "myNetwork", "--ip-range", "10.0.0.0/24"}) + + expOut := "Network 123 created\n" + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} + +func TestCreateProtection(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := CreateCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + _, ipRange, _ := net.ParseCIDR("10.0.0.0/24") + network := &hcloud.Network{ + ID: 123, + Name: "myNetwork", + IPRange: ipRange, + } + + fx.Client.NetworkClient.EXPECT(). + Create(gomock.Any(), hcloud.NetworkCreateOpts{ + Name: "myNetwork", + IPRange: ipRange, + Labels: make(map[string]string), + }). + Return(network, nil, nil) + fx.Client.NetworkClient.EXPECT(). + ChangeProtection(gomock.Any(), network, hcloud.NetworkChangeProtectionOpts{ + Delete: hcloud.Ptr(true), + }). + Return(&hcloud.Action{ID: 123}, nil, nil) + fx.ActionWaiter.EXPECT().ActionProgress(gomock.Any(), &hcloud.Action{ID: 123}).Return(nil) + + out, err := fx.Run(cmd, []string{"--name", "myNetwork", "--ip-range", "10.0.0.0/24", "--enable-protection", "delete"}) + + expOut := `Network 123 created +Resource protection enabled for network 123 +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/network/delete_test.go b/internal/cmd/network/delete_test.go new file mode 100644 index 00000000..54a6630f --- /dev/null +++ b/internal/cmd/network/delete_test.go @@ -0,0 +1,43 @@ +package network + +import ( + "context" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDelete(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := DeleteCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + network := &hcloud.Network{ + ID: 123, + Name: "test", + } + + fx.Client.NetworkClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(network, nil, nil) + fx.Client.NetworkClient.EXPECT(). + Delete(gomock.Any(), network). + Return(nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := "Network test deleted\n" + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/network/describe_test.go b/internal/cmd/network/describe_test.go new file mode 100644 index 00000000..4dcd198e --- /dev/null +++ b/internal/cmd/network/describe_test.go @@ -0,0 +1,58 @@ +package network + +import ( + "context" + "net" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDescribe(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := DescribeCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer) + fx.ExpectEnsureToken() + + fx.Client.NetworkClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(&hcloud.Network{ + ID: 123, + Name: "test", + Created: time.Date(1905, 10, 6, 12, 0, 0, 0, time.UTC), + IPRange: &net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(24, 32)}, + Protection: hcloud.NetworkProtection{Delete: true}, + Labels: map[string]string{"key": "value"}, + }, nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := `ID: 123 +Name: test +Created: Fri Oct 6 12:00:00 UTC 1905 (a long while ago) +IP Range: 10.0.0.0/24 +Expose Routes to vSwitch: no +Subnets: + No subnets +Routes: + No routes +Protection: + Delete: yes +Labels: + key: value +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/server/delete_test.go b/internal/cmd/server/delete_test.go new file mode 100644 index 00000000..aecf953f --- /dev/null +++ b/internal/cmd/server/delete_test.go @@ -0,0 +1,47 @@ +package server + +import ( + "context" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDelete(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := DeleteCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + srv := &hcloud.Server{ + ID: 123, + Name: "test", + } + + fx.Client.ServerClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(srv, nil, nil) + fx.Client.ServerClient.EXPECT(). + DeleteWithResult(gomock.Any(), srv). + Return(&hcloud.ServerDeleteResult{ + Action: &hcloud.Action{ID: 321}, + }, nil, nil) + fx.ActionWaiter.EXPECT(). + ActionProgress(gomock.Any(), &hcloud.Action{ID: 321}) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := "Server test deleted\n" + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/server/describe_test.go b/internal/cmd/server/describe_test.go new file mode 100644 index 00000000..b5308985 --- /dev/null +++ b/internal/cmd/server/describe_test.go @@ -0,0 +1,144 @@ +package server + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/cmd/util" + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDescribe(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := DescribeCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer) + fx.ExpectEnsureToken() + + fx.Client.ServerClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(&hcloud.Server{ + ID: 123, + Name: "test", + ServerType: &hcloud.ServerType{ + ID: 45, + Name: "cax11", + Description: "CAX11", + Cores: 2, + CPUType: hcloud.CPUTypeShared, + Memory: 4.0, + Disk: 40, + StorageType: hcloud.StorageTypeLocal, + }, + Image: &hcloud.Image{ + ID: 123, + Type: hcloud.ImageTypeSystem, + Status: hcloud.ImageStatusAvailable, + Name: "test", + Created: time.Date(1905, 10, 6, 12, 0, 0, 0, time.UTC), + Description: "Test image", + ImageSize: 20.0, + DiskSize: 20.0, + Architecture: hcloud.ArchitectureX86, + Labels: map[string]string{ + "key": "value", + }, + }, + Datacenter: &hcloud.Datacenter{ + ID: 4, + Name: "hel1-dc2", + Location: &hcloud.Location{ + ID: 3, + Name: "hel1", + Description: "Helsinki DC Park 1", + NetworkZone: "eu-central", + Country: "FI", + City: "Helsinki", + Latitude: 60.169855, + Longitude: 24.938379, + }, + Description: "Helsinki 1 virtual DC 2", + }, + IncludedTraffic: 20 * util.Tebibyte, + Protection: hcloud.ServerProtection{Delete: true, Rebuild: true}, + }, nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := `ID: 123 +Name: test +Status: +Created: Mon Jan 1 00:00:00 UTC 0001 (a long while ago) +Server Type: cax11 (ID: 45) + ID: 45 + Name: cax11 + Description: CAX11 + Cores: 2 + CPU Type: shared + Memory: 4 GB + Disk: 0 GB + Storage Type: local + Public Net: + IPv4: + No Primary IPv4 + IPv6: + No Primary IPv6 + Floating IPs: + No Floating IPs +Private Net: + No Private Networks +Volumes: + No Volumes +Image: + ID: 123 + Type: system + Status: available + Name: test + Description: Test image + Image size: 20.00 GB + Disk size: 20 GB + Created: Fri Oct 6 12:00:00 UTC 1905 (a long while ago) + OS flavor: + OS version: - + Rapid deploy: no +Datacenter: + ID: 4 + Name: hel1-dc2 + Description: Helsinki 1 virtual DC 2 + Location: + Name: hel1 + Description: Helsinki DC Park 1 + Country: FI + City: Helsinki + Latitude: 60.169855 + Longitude: 24.938379 +Traffic: + Outgoing: 0 B + Ingoing: 0 B + Included: 20 TiB +Backup Window: Backups disabled +Rescue System: disabled +ISO: + No ISO attached +Protection: + Delete: yes + Rebuild: yes +Labels: + No labels +Placement Group: + No Placement Group set +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/server/list_test.go b/internal/cmd/server/list_test.go new file mode 100644 index 00000000..700ce90b --- /dev/null +++ b/internal/cmd/server/list_test.go @@ -0,0 +1,56 @@ +package server + +import ( + "context" + "net" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestList(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + + fx.ExpectEnsureToken() + fx.Client.ServerClient.EXPECT(). + AllWithOpts( + gomock.Any(), + hcloud.ServerListOpts{ + ListOpts: hcloud.ListOpts{PerPage: 50}, + Sort: []string{"id:asc"}, + }, + ). + Return([]*hcloud.Server{ + { + ID: 123, + Name: "test", + Status: hcloud.ServerStatusRunning, + Datacenter: &hcloud.Datacenter{Name: "fsn1-dc14"}, + PublicNet: hcloud.ServerPublicNet{ + IPv4: hcloud.ServerPublicNetIPv4{ + IP: net.ParseIP("192.168.2.1"), + }, + }, + Created: time.Now().Add(-20 * time.Second), + }, + }, nil) + + out, err := fx.Run(cmd, []string{}) + + expOut := `ID NAME STATUS IPV4 IPV6 PRIVATE NET DATACENTER AGE +123 test running 192.168.2.1 - - fsn1-dc14 20s +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/servertype/describe_test.go b/internal/cmd/servertype/describe_test.go new file mode 100644 index 00000000..c10be7d5 --- /dev/null +++ b/internal/cmd/servertype/describe_test.go @@ -0,0 +1,57 @@ +package servertype + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDescribe(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := DescribeCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer) + fx.ExpectEnsureToken() + + fx.Client.ServerTypeClient.EXPECT(). + Get(gomock.Any(), "cax11"). + Return(&hcloud.ServerType{ + ID: 45, + Name: "cax11", + Description: "CAX11", + Cores: 2, + CPUType: hcloud.CPUTypeShared, + Memory: 4.0, + Disk: 40, + StorageType: hcloud.StorageTypeLocal, + }, nil, nil) + + out, err := fx.Run(cmd, []string{"cax11"}) + + expOut := `ID: 45 +Name: cax11 +Description: CAX11 +Cores: 2 +CPU Type: shared +Architecture: +Memory: 4.0 GB +Disk: 40 GB +Storage Type: local +Included Traffic: 0 TB +Pricings per Location: +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/servertype/list_test.go b/internal/cmd/servertype/list_test.go new file mode 100644 index 00000000..7651a795 --- /dev/null +++ b/internal/cmd/servertype/list_test.go @@ -0,0 +1,55 @@ +package servertype + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/cmd/util" + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestList(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + + fx.ExpectEnsureToken() + fx.Client.ServerTypeClient.EXPECT(). + AllWithOpts( + gomock.Any(), + hcloud.ServerTypeListOpts{ + ListOpts: hcloud.ListOpts{PerPage: 50}, + Sort: []string{"id:asc"}, + }, + ). + Return([]*hcloud.ServerType{ + { + ID: 123, + Name: "test", + Cores: 2, + CPUType: hcloud.CPUTypeShared, + Architecture: hcloud.ArchitectureARM, + Memory: 8.0, + Disk: 80, + StorageType: hcloud.StorageTypeLocal, + IncludedTraffic: 20 * util.Tebibyte, + }, + }, nil) + + out, err := fx.Run(cmd, []string{}) + + expOut := `ID NAME CORES CPU TYPE ARCHITECTURE MEMORY DISK STORAGE TYPE TRAFFIC +123 test 2 shared arm 8.0 GB 80 GB local 20 TB +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/sshkey/create_test.go b/internal/cmd/sshkey/create_test.go new file mode 100644 index 00000000..a296300f --- /dev/null +++ b/internal/cmd/sshkey/create_test.go @@ -0,0 +1,43 @@ +package sshkey + +import ( + "context" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestCreate(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := CreateCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + fx.Client.SSHKeyClient.EXPECT(). + Create(gomock.Any(), hcloud.SSHKeyCreateOpts{ + Name: "test", + PublicKey: "test", + Labels: make(map[string]string), + }). + Return(&hcloud.SSHKey{ + ID: 123, + Name: "test", + PublicKey: "test", + }, nil, nil) + + out, err := fx.Run(cmd, []string{"--name", "test", "--public-key", "test"}) + + expOut := "SSH key 123 created\n" + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/sshkey/delete_test.go b/internal/cmd/sshkey/delete_test.go new file mode 100644 index 00000000..5badfa23 --- /dev/null +++ b/internal/cmd/sshkey/delete_test.go @@ -0,0 +1,43 @@ +package sshkey + +import ( + "context" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDelete(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := DeleteCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + sshKey := &hcloud.SSHKey{ + ID: 123, + Name: "test", + } + + fx.Client.SSHKeyClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(sshKey, nil, nil) + fx.Client.SSHKeyClient.EXPECT(). + Delete(gomock.Any(), sshKey). + Return(nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := "SSH Key test deleted\n" + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/sshkey/describe_test.go b/internal/cmd/sshkey/describe_test.go new file mode 100644 index 00000000..fce16642 --- /dev/null +++ b/internal/cmd/sshkey/describe_test.go @@ -0,0 +1,51 @@ +package sshkey + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDescribe(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := DescribeCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer) + fx.ExpectEnsureToken() + + fx.Client.SSHKeyClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(&hcloud.SSHKey{ + ID: 123, + Name: "test", + Created: time.Date(1905, 10, 6, 12, 0, 0, 0, time.UTC), + Fingerprint: "fingerprint", + PublicKey: "public key", + }, nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := `ID: 123 +Name: test +Created: Fri Oct 6 12:00:00 UTC 1905 (a long while ago) +Fingerprint: fingerprint +Public Key: +public key +Labels: + No labels +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/sshkey/list_test.go b/internal/cmd/sshkey/list_test.go new file mode 100644 index 00000000..5d846b78 --- /dev/null +++ b/internal/cmd/sshkey/list_test.go @@ -0,0 +1,48 @@ +package sshkey + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestList(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + + fx.ExpectEnsureToken() + fx.Client.SSHKeyClient.EXPECT(). + AllWithOpts( + gomock.Any(), + hcloud.SSHKeyListOpts{ + ListOpts: hcloud.ListOpts{PerPage: 50}, + Sort: []string{"id:asc"}, + }, + ). + Return([]*hcloud.SSHKey{ + { + ID: 123, + Name: "test", + Created: time.Now().Add(-1 * time.Hour), + }, + }, nil) + + out, err := fx.Run(cmd, []string{}) + + expOut := `ID NAME FINGERPRINT AGE +123 test - 1h +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/volume/create_test.go b/internal/cmd/volume/create_test.go new file mode 100644 index 00000000..36feb46a --- /dev/null +++ b/internal/cmd/volume/create_test.go @@ -0,0 +1,105 @@ +package volume + +import ( + "context" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestCreate(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := CreateCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + fx.Client.VolumeClient.EXPECT(). + Create(gomock.Any(), hcloud.VolumeCreateOpts{ + Name: "test", + Size: 20, + Location: &hcloud.Location{Name: "fsn1"}, + Labels: make(map[string]string), + }). + Return(hcloud.VolumeCreateResult{ + Volume: &hcloud.Volume{ + ID: 123, + Name: "test", + Size: 20, + Location: &hcloud.Location{Name: "fsn1"}, + }, + Action: &hcloud.Action{ID: 321}, + NextActions: []*hcloud.Action{{ID: 1}, {ID: 2}, {ID: 3}}, + }, nil, nil) + fx.ActionWaiter.EXPECT(). + ActionProgress(gomock.Any(), &hcloud.Action{ID: 321}) + fx.ActionWaiter.EXPECT(). + WaitForActions(gomock.Any(), []*hcloud.Action{{ID: 1}, {ID: 2}, {ID: 3}}) + + out, err := fx.Run(cmd, []string{"--name", "test", "--size", "20", "--location", "fsn1"}) + + expOut := "Volume 123 created\n" + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} + +func TestCreateProtection(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := CreateCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + volume := &hcloud.Volume{ + ID: 123, + Name: "test", + Size: 20, + Location: &hcloud.Location{Name: "fsn1"}, + } + + fx.Client.VolumeClient.EXPECT(). + Create(gomock.Any(), hcloud.VolumeCreateOpts{ + Name: "test", + Size: 20, + Location: &hcloud.Location{Name: "fsn1"}, + Labels: make(map[string]string), + }). + Return(hcloud.VolumeCreateResult{ + Volume: volume, + Action: &hcloud.Action{ID: 321}, + NextActions: []*hcloud.Action{{ID: 1}, {ID: 2}, {ID: 3}}, + }, nil, nil) + fx.ActionWaiter.EXPECT(). + ActionProgress(gomock.Any(), &hcloud.Action{ID: 321}) + fx.ActionWaiter.EXPECT(). + WaitForActions(gomock.Any(), []*hcloud.Action{{ID: 1}, {ID: 2}, {ID: 3}}) + fx.Client.VolumeClient.EXPECT(). + ChangeProtection(gomock.Any(), volume, hcloud.VolumeChangeProtectionOpts{ + Delete: hcloud.Ptr(true), + }). + Return(&hcloud.Action{ID: 123}, nil, nil) + fx.ActionWaiter.EXPECT(). + ActionProgress(gomock.Any(), &hcloud.Action{ID: 123}) + + out, err := fx.Run(cmd, []string{"--name", "test", "--size", "20", "--location", "fsn1", "--enable-protection", "delete"}) + + expOut := `Volume 123 created +Resource protection enabled for volume 123 +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/volume/delete_test.go b/internal/cmd/volume/delete_test.go new file mode 100644 index 00000000..df1028d8 --- /dev/null +++ b/internal/cmd/volume/delete_test.go @@ -0,0 +1,43 @@ +package volume + +import ( + "context" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDelete(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + cmd := DeleteCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer, + fx.ActionWaiter) + fx.ExpectEnsureToken() + + volume := &hcloud.Volume{ + ID: 123, + Name: "test", + } + + fx.Client.VolumeClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(volume, nil, nil) + fx.Client.VolumeClient.EXPECT(). + Delete(gomock.Any(), volume). + Return(nil, nil) + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := "Volume test deleted\n" + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/volume/describe_test.go b/internal/cmd/volume/describe_test.go new file mode 100644 index 00000000..755f4d49 --- /dev/null +++ b/internal/cmd/volume/describe_test.go @@ -0,0 +1,75 @@ +package volume + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestDescribe(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := DescribeCmd.CobraCommand( + context.Background(), + fx.Client, + fx.TokenEnsurer) + fx.ExpectEnsureToken() + + fx.Client.VolumeClient.EXPECT(). + Get(gomock.Any(), "test"). + Return(&hcloud.Volume{ + ID: 123, + Name: "test", + Size: 50, + Server: &hcloud.Server{ID: 321}, + Location: &hcloud.Location{ + ID: 3, + Name: "hel1", + Description: "Helsinki DC Park 1", + NetworkZone: "eu-central", + Country: "FI", + City: "Helsinki", + Latitude: 60.169855, + Longitude: 24.938379, + }, + Created: time.Date(1905, 10, 6, 12, 0, 0, 0, time.UTC), + }, nil, nil) + fx.Client.ServerClient.EXPECT(). + ServerName(int64(321)). + Return("myServer") + + out, err := fx.Run(cmd, []string{"test"}) + + expOut := `ID: 123 +Name: test +Created: Fri Oct 6 12:00:00 UTC 1905 (a long while ago) +Size: 50 GB +Linux Device: +Location: + Name: hel1 + Description: Helsinki DC Park 1 + Country: FI + City: Helsinki + Latitude: 60.169855 + Longitude: 24.938379 +Server: + ID: 321 + Name: myServer +Protection: + Delete: no +Labels: + No labels +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +} diff --git a/internal/cmd/volume/list_test.go b/internal/cmd/volume/list_test.go new file mode 100644 index 00000000..4e00ae9b --- /dev/null +++ b/internal/cmd/volume/list_test.go @@ -0,0 +1,54 @@ +package volume + +import ( + "context" + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/hetznercloud/cli/internal/testutil" + "github.com/hetznercloud/hcloud-go/v2/hcloud" +) + +func TestList(t *testing.T) { + fx := testutil.NewFixture(t) + defer fx.Finish() + + time.Local = time.UTC + + cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + + fx.ExpectEnsureToken() + fx.Client.VolumeClient.EXPECT(). + AllWithOpts( + gomock.Any(), + hcloud.VolumeListOpts{ + ListOpts: hcloud.ListOpts{PerPage: 50}, + Sort: []string{"id:asc"}, + }, + ). + Return([]*hcloud.Volume{ + { + ID: 123, + Name: "test", + Size: 50, + Server: &hcloud.Server{ID: 321}, + Location: &hcloud.Location{Name: "fsn1"}, + Created: time.Now().Add(-1 * time.Hour), + }, + }, nil) + fx.Client.ServerClient.EXPECT(). + ServerName(int64(321)). + Return("myServer") + + out, err := fx.Run(cmd, []string{}) + + expOut := `ID NAME SIZE SERVER LOCATION AGE +123 test 50 GB myServer fsn1 1h +` + + assert.NoError(t, err) + assert.Equal(t, expOut, out) +}