Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add more unit tests #598

Merged
merged 5 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions internal/cmd/certificate/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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 TestCreateManaged(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"})
phm07 marked this conversation as resolved.
Show resolved Hide resolved

expOut := "Certificate 123 created\n"

assert.NoError(t, err)
assert.Equal(t, expOut, out)
}

func TestCreateUploaded(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().
Create(gomock.Any(), hcloud.CertificateCreateOpts{
Name: "test",
Type: hcloud.CertificateTypeUploaded,
Certificate: "certificate file content",
PrivateKey: "key file content",
}).
Return(&hcloud.Certificate{
ID: 123,
Name: "test",
Type: hcloud.CertificateTypeUploaded,
}, nil, nil)

out, err := fx.Run(cmd, []string{"--name", "test", "--key-file", "testdata/key.pem", "--cert-file", "testdata/cert.pem"})

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)
}
87 changes: 87 additions & 0 deletions internal/cmd/certificate/describe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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"
)

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()

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,
}},
Status: &hcloud.CertificateStatus{
Error: &hcloud.Error{
Code: hcloud.ErrorCode("cert_error"),
Message: "Certificate error",
},
Issuance: hcloud.CertificateStatusTypeFailed,
Renewal: hcloud.CertificateStatusTypeScheduled,
},
}

fx.Client.CertificateClient.EXPECT().
Get(gomock.Any(), "test").
Return(cert, nil, nil)
fx.Client.LoadBalancerClient.EXPECT().
LoadBalancerName(int64(123)).
Return("test")

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

expOut := fmt.Sprintf(`ID: 123
Name: test
Type: managed
Fingerprint:
Created: %s (%s)
Not valid before: %s (%s)
Not valid after: %s (%s)
Status:
Issuance: failed
Renewal: scheduled
Failure reason: Certificate error
Domain names:
- example.com
Labels:
key: value
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)
}
71 changes: 71 additions & 0 deletions internal/cmd/certificate/labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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 TestLabelAdd(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := LabelCmds.AddCobraCommand(
context.Background(),
fx.Client,
fx.TokenEnsurer)
fx.ExpectEnsureToken()

fx.Client.CertificateClient.EXPECT().
Get(gomock.Any(), "123").
Return(&hcloud.Certificate{ID: 123}, nil, nil)
fx.Client.CertificateClient.EXPECT().
Update(gomock.Any(), &hcloud.Certificate{ID: 123}, hcloud.CertificateUpdateOpts{
Labels: map[string]string{
"key": "value",
},
})

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

expOut := "Label key added to certificate 123\n"

assert.NoError(t, err)
assert.Equal(t, expOut, out)
}

func TestLabelRemove(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := LabelCmds.RemoveCobraCommand(
context.Background(),
fx.Client,
fx.TokenEnsurer)
fx.ExpectEnsureToken()

fx.Client.CertificateClient.EXPECT().
Get(gomock.Any(), "123").
Return(&hcloud.Certificate{
ID: 123,
Labels: map[string]string{
"key": "value",
},
}, nil, nil)
fx.Client.CertificateClient.EXPECT().
Update(gomock.Any(), &hcloud.Certificate{ID: 123}, hcloud.CertificateUpdateOpts{
Labels: make(map[string]string),
})

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

expOut := "Label key removed from certificate 123\n"

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)
}
1 change: 1 addition & 0 deletions internal/cmd/certificate/testdata/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
certificate file content
1 change: 1 addition & 0 deletions internal/cmd/certificate/testdata/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
key file content
38 changes: 38 additions & 0 deletions internal/cmd/certificate/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 TestUpdateName(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := UpdateCmd.CobraCommand(
context.Background(),
fx.Client,
fx.TokenEnsurer)
fx.ExpectEnsureToken()

fx.Client.CertificateClient.EXPECT().
Get(gomock.Any(), "123").
Return(&hcloud.Certificate{ID: 123}, nil, nil)
fx.Client.CertificateClient.EXPECT().
Update(gomock.Any(), &hcloud.Certificate{ID: 123}, hcloud.CertificateUpdateOpts{
Name: "new-name",
})

out, err := fx.Run(cmd, []string{"123", "--name", "new-name"})

expOut := "certificate 123 updated\n"

assert.NoError(t, err)
assert.Equal(t, expOut, out)
}
Loading
Loading