-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
67 changed files
with
3,780 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
certificate file content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
key file content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.