Skip to content

Commit

Permalink
added unit test for ssh keys
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkrim committed Aug 22, 2023
1 parent 5a999e3 commit f29cd9c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ require github.com/stretchr/testify v1.8.1
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ=
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
23 changes: 20 additions & 3 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
)

func setupServiceTestCase(t *testing.T) *Client {
Expand Down Expand Up @@ -469,14 +470,22 @@ func TestServiceHandler_AddSSHPublicKey(t *testing.T) {

projectID := "596"
serviceID := "c4686e74-c75c-4ca8-9aaa-26f83eaaae97"
keyName := "test"
keyData := "ssh-rsa fakeKey test@macbook"

err := c.Service.AddSSHPublicKey(serviceID, "test", "ssh-rsa fakeKey adam@macbook")
err := c.Service.AddSSHPublicKey(serviceID, keyName, keyData)
require.NoError(t, err, "expected no error when adding ssh key")

updatedService, err := c.Service.Get(projectID, serviceID)
require.NoError(t, err, "expected no error when getting service")
require.NotEqual(
t,
-1,
slices.IndexFunc(updatedService.SSHPublicKeys, func(s ServiceSSHPublicKey) bool { return s.Name == keyName && s.Key == keyData }),
"expected ssh key to be added",
)

fmt.Fprintf(os.Stdout, "Service: %v", updatedService.SSHPublicKeys)
fmt.Fprintf(os.Stdout, "Service ssh public keys after added: %v", updatedService.SSHPublicKeys)
}

func TestServiceHandler_RemoveSSHPublicKey(t *testing.T) {
Expand All @@ -485,14 +494,22 @@ func TestServiceHandler_RemoveSSHPublicKey(t *testing.T) {

projectID := "596"
serviceID := "c4686e74-c75c-4ca8-9aaa-26f83eaaae97"
keyName := "test"
keyData := "ssh-rsa fakeKey test@macbook"

err := c.Service.RemoveSSHPublicKey(serviceID, "test")
require.NoError(t, err, "expected no error when removing ssh key")

updatedService, err := c.Service.Get(projectID, serviceID)
require.NoError(t, err, "expected no error when getting service")
require.Equal(
t,
-1,
slices.IndexFunc(updatedService.SSHPublicKeys, func(s ServiceSSHPublicKey) bool { return s.Name == keyName && s.Key == keyData }),
"expected ssh key to be removed",
)

fmt.Fprintf(os.Stdout, "Service: %v", updatedService.SSHPublicKeys)
fmt.Fprintf(os.Stdout, "Service ssh public key after remove: %v", updatedService.SSHPublicKeys)
}

func TestServiceHandler_Reboot(t *testing.T) {
Expand Down

0 comments on commit f29cd9c

Please sign in to comment.