Skip to content

Commit

Permalink
test: fix date testing
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Nov 7, 2023
1 parent bc87709 commit ba6541e
Show file tree
Hide file tree
Showing 15 changed files with 274 additions and 238 deletions.
47 changes: 27 additions & 20 deletions internal/cmd/certificate/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package certificate

import (
"context"
"fmt"
"testing"
"time"

"github.com/dustin/go-humanize"
"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"
)
Expand All @@ -24,44 +27,48 @@ func TestDescribe(t *testing.T) {
fx.TokenEnsurer)
fx.ExpectEnsureToken()

cert := &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, 12, 12, 0, 0, 0, time.UTC),
DomainNames: []string{"example.com"},
Labels: map[string]string{"key": "value"},
UsedBy: []hcloud.CertificateUsedByRef{{
ID: 123,
Type: hcloud.CertificateUsedByRefTypeLoadBalancer,
}},
}

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)
Return(cert, nil, nil)
fx.Client.LoadBalancerClient.EXPECT().
LoadBalancerName(int64(123)).
Return("test")

out, err := fx.Run(cmd, []string{"test"})

expOut := `ID: 123
expOut := fmt.Sprintf(`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)
Created: %s (%s)
Not valid before: %s (%s)
Not valid after: %s (%s)
Domain names:
- example.com
Labels:
key: value
key2: value2
Used By:
- Type: load_balancer
- Name: test
`
`,
util.Datetime(cert.Created), humanize.Time(cert.Created),
util.Datetime(cert.NotValidBefore), humanize.Time(cert.NotValidBefore),
util.Datetime(cert.NotValidAfter), humanize.Time(cert.NotValidAfter))

assert.NoError(t, err)
assert.Equal(t, expOut, out)
Expand Down
3 changes: 0 additions & 3 deletions internal/cmd/datacenter/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package datacenter
import (
"context"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
Expand All @@ -16,8 +15,6 @@ func TestDescribe(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

time.Local = time.UTC

cmd := DescribeCmd.CobraCommand(
context.Background(),
fx.Client,
Expand Down
56 changes: 31 additions & 25 deletions internal/cmd/firewall/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package firewall

import (
"context"
"fmt"
"testing"
"time"

"github.com/dustin/go-humanize"
"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"
)
Expand All @@ -24,40 +27,43 @@ func TestDescribe(t *testing.T) {
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,
},
firewall := &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,
},
},
AppliedTo: []hcloud.FirewallResource{
{
Type: hcloud.FirewallResourceTypeServer,
Server: &hcloud.FirewallResourceServer{
ID: 321,
},
},
Labels: map[string]string{
"key": "value",
},
}, nil, nil)
},
Labels: map[string]string{
"key": "value",
},
Created: time.Date(2036, 8, 12, 12, 0, 0, 0, time.UTC),
}

fx.Client.FirewallClient.EXPECT().
Get(gomock.Any(), "test").
Return(firewall, nil, nil)
fx.Client.ServerClient.EXPECT().
ServerName(int64(321)).
Return("myServer")

out, err := fx.Run(cmd, []string{"test"})

expOut := `ID: 123
expOut := fmt.Sprintf(`ID: 123
Name: test
Created: Mon Jan 1 00:00:00 UTC 0001 (a long while ago)
Created: %s (%s)
Labels:
key: value
Rules:
Expand All @@ -70,7 +76,7 @@ Applied To:
- Type: server
Server ID: 321
Server Name: myServer
`
`, util.Datetime(firewall.Created), humanize.Time(firewall.Created))

assert.NoError(t, err)
assert.Equal(t, expOut, out)
Expand Down
35 changes: 20 additions & 15 deletions internal/cmd/floatingip/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package floatingip

import (
"context"
"fmt"
"net"
"testing"
"time"

"github.com/dustin/go-humanize"
"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"
)
Expand All @@ -25,31 +28,33 @@ func TestDescribe(t *testing.T) {
fx.TokenEnsurer)
fx.ExpectEnsureToken()

floatingIP := &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, 12, 12, 0, 0, 0, time.UTC),
}

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)
Return(floatingIP, nil, nil)
fx.Client.ServerClient.EXPECT().
ServerName(int64(321)).
Return("myServer")

out, err := fx.Run(cmd, []string{"test"})

expOut := `ID: 123
expOut := fmt.Sprintf(`ID: 123
Type: ipv4
Name: test
Description: -
Created: Wed Aug 20 12:00:00 UTC 2036 (12 years from now)
Created: %s (%s)
IP: 192.168.2.1
Blocked: no
Home Location: fsn1
Expand All @@ -62,7 +67,7 @@ Protection:
Delete: no
Labels:
key: value
`
`, util.Datetime(floatingIP.Created), humanize.Time(floatingIP.Created))

assert.NoError(t, err)
assert.Equal(t, expOut, out)
Expand Down
39 changes: 22 additions & 17 deletions internal/cmd/image/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package image

import (
"context"
"fmt"
"testing"
"time"

"github.com/dustin/go-humanize"
"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"
)
Expand All @@ -24,30 +27,32 @@ func TestDescribe(t *testing.T) {
fx.TokenEnsurer)
fx.ExpectEnsureToken()

img := &hcloud.Image{
ID: 123,
Type: hcloud.ImageTypeSystem,
Status: hcloud.ImageStatusAvailable,
Name: "test",
Created: time.Date(2036, 8, 12, 12, 0, 0, 0, time.UTC),
Description: "Test image",
ImageSize: 20.0,
DiskSize: 20.0,
Architecture: hcloud.ArchitectureX86,
Labels: map[string]string{
"key": "value",
},
}

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)
Return(img, nil, nil)

out, err := fx.Run(cmd, []string{"test"})

expOut := `ID: 123
expOut := fmt.Sprintf(`ID: 123
Type: system
Status: available
Name: test
Created: Fri Oct 6 12:00:00 UTC 1905 (a long while ago)
Created: %s (%s)
Description: Test image
Image size: 20.00 GB
Disk size: 20 GB
Expand All @@ -59,7 +64,7 @@ Protection:
Delete: no
Labels:
key: value
`
`, util.Datetime(img.Created), humanize.Time(img.Created))

assert.NoError(t, err)
assert.Equal(t, expOut, out)
Expand Down
3 changes: 0 additions & 3 deletions internal/cmd/iso/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package iso
import (
"context"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
Expand All @@ -16,8 +15,6 @@ func TestDescribe(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

time.Local = time.UTC

cmd := DescribeCmd.CobraCommand(
context.Background(),
fx.Client,
Expand Down
Loading

0 comments on commit ba6541e

Please sign in to comment.