-
Notifications
You must be signed in to change notification settings - Fork 80
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
7 changed files
with
552 additions
and
0 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,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) | ||
} |
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,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) | ||
} |
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,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) | ||
} |
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,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) | ||
} |
Oops, something went wrong.