Skip to content

Commit

Permalink
test: add missing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Nov 2, 2023
1 parent ee8e6ed commit bc87709
Show file tree
Hide file tree
Showing 41 changed files with 2,483 additions and 0 deletions.
49 changes: 49 additions & 0 deletions internal/cmd/certificate/create_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
43 changes: 43 additions & 0 deletions internal/cmd/certificate/delete_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
68 changes: 68 additions & 0 deletions internal/cmd/certificate/describe_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
51 changes: 51 additions & 0 deletions internal/cmd/certificate/list_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
57 changes: 57 additions & 0 deletions internal/cmd/datacenter/describe_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
49 changes: 49 additions & 0 deletions internal/cmd/datacenter/list_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
46 changes: 46 additions & 0 deletions internal/cmd/firewall/create_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading

0 comments on commit bc87709

Please sign in to comment.