Skip to content

Commit

Permalink
test: add tests for label commands
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Nov 7, 2023
1 parent ba6541e commit c1c1f1b
Show file tree
Hide file tree
Showing 11 changed files with 740 additions and 43 deletions.
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)
}
71 changes: 71 additions & 0 deletions internal/cmd/firewall/labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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 TestLabelAdd(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

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

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

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

expOut := "Label key added to firewall 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.FirewallClient.EXPECT().
Get(gomock.Any(), "123").
Return(&hcloud.Firewall{
ID: 123,
Labels: map[string]string{
"key": "value",
},
}, nil, nil)
fx.Client.FirewallClient.EXPECT().
Update(gomock.Any(), &hcloud.Firewall{ID: 123}, hcloud.FirewallUpdateOpts{
Labels: make(map[string]string),
})

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

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

assert.NoError(t, err)
assert.Equal(t, expOut, out)
}
71 changes: 71 additions & 0 deletions internal/cmd/floatingip/labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package floatingip

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.FloatingIPClient.EXPECT().
Get(gomock.Any(), "123").
Return(&hcloud.FloatingIP{ID: 123}, nil, nil)
fx.Client.FloatingIPClient.EXPECT().
Update(gomock.Any(), &hcloud.FloatingIP{ID: 123}, hcloud.FloatingIPUpdateOpts{
Labels: map[string]string{
"key": "value",
},
})

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

expOut := "Label key added to Floating IP 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.FloatingIPClient.EXPECT().
Get(gomock.Any(), "123").
Return(&hcloud.FloatingIP{
ID: 123,
Labels: map[string]string{
"key": "value",
},
}, nil, nil)
fx.Client.FloatingIPClient.EXPECT().
Update(gomock.Any(), &hcloud.FloatingIP{ID: 123}, hcloud.FloatingIPUpdateOpts{
Labels: make(map[string]string),
})

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

expOut := "Label key removed from Floating IP 123\n"

assert.NoError(t, err)
assert.Equal(t, expOut, out)
}
71 changes: 71 additions & 0 deletions internal/cmd/image/labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package image

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.ImageClient.EXPECT().
Get(gomock.Any(), "123").
Return(&hcloud.Image{ID: 123}, nil, nil)
fx.Client.ImageClient.EXPECT().
Update(gomock.Any(), &hcloud.Image{ID: 123}, hcloud.ImageUpdateOpts{
Labels: map[string]string{
"key": "value",
},
})

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

expOut := "Label key added to image 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.ImageClient.EXPECT().
Get(gomock.Any(), "123").
Return(&hcloud.Image{
ID: 123,
Labels: map[string]string{
"key": "value",
},
}, nil, nil)
fx.Client.ImageClient.EXPECT().
Update(gomock.Any(), &hcloud.Image{ID: 123}, hcloud.ImageUpdateOpts{
Labels: make(map[string]string),
})

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

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

assert.NoError(t, err)
assert.Equal(t, expOut, out)
}
71 changes: 71 additions & 0 deletions internal/cmd/loadbalancer/labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package loadbalancer

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.LoadBalancerClient.EXPECT().
Get(gomock.Any(), "123").
Return(&hcloud.LoadBalancer{ID: 123}, nil, nil)
fx.Client.LoadBalancerClient.EXPECT().
Update(gomock.Any(), &hcloud.LoadBalancer{ID: 123}, hcloud.LoadBalancerUpdateOpts{
Labels: map[string]string{
"key": "value",
},
})

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

expOut := "Label key added to Load Balancer 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.LoadBalancerClient.EXPECT().
Get(gomock.Any(), "123").
Return(&hcloud.LoadBalancer{
ID: 123,
Labels: map[string]string{
"key": "value",
},
}, nil, nil)
fx.Client.LoadBalancerClient.EXPECT().
Update(gomock.Any(), &hcloud.LoadBalancer{ID: 123}, hcloud.LoadBalancerUpdateOpts{
Labels: make(map[string]string),
})

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

expOut := "Label key removed from Load Balancer 123\n"

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

0 comments on commit c1c1f1b

Please sign in to comment.