Skip to content

Commit

Permalink
Add unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anvial committed Apr 1, 2024
1 parent eb0e7f4 commit fd55d5a
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 1 deletion.
2 changes: 2 additions & 0 deletions internal/juju/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Client struct {
Offers offersClient
SSHKeys sshKeysClient
Users usersClient
Secrets secretsClient
}

type jujuModel struct {
Expand Down Expand Up @@ -87,6 +88,7 @@ func NewClient(ctx context.Context, config ControllerConfiguration) (*Client, er
Offers: *newOffersClient(sc),
SSHKeys: *newSSHKeysClient(sc),
Users: *newUsersClient(sc),
Secrets: *newSecretsClient(sc),
}, nil
}

Expand Down
156 changes: 155 additions & 1 deletion internal/juju/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
package juju

import (
"encoding/base64"
"errors"
"github.com/juju/juju/api"
apisecrets "github.com/juju/juju/api/client/secrets"
coresecrets "github.com/juju/juju/core/secrets"
"testing"

"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -76,7 +80,157 @@ func (s *SecretSuite) TestCreateSecret() {
})
s.Require().NoError(err)
s.Require().NotNil(output)
s.Require().Equal("secret-id", output.SecretId)

s.Assert().Equal("secret-id", output.SecretId)
}

func (s *SecretSuite) TestCreateSecretError() {
ctlr := s.setupMocks(s.T())
defer ctlr.Finish()

errBoom := errors.New("boom")

s.mockSecretClient.EXPECT().CreateSecret(
"test-secret", "test info", map[string]string{"key": "value"},
).Return("", errBoom).AnyTimes()

client := s.getSecretsClient()
output, err := client.CreateSecret(&CreateSecretInput{
ModelName: s.testModelName,
Name: "test-secret",
Value: map[string]string{"key": "value"},
Info: "test info",
})
s.Require().Error(err)
s.Require().Nil(output)

s.Assert().Equal(errBoom, err)
}

func (s *SecretSuite) TestReadSecret() {
ctlr := s.setupMocks(s.T())
defer ctlr.Finish()

secretName := "test-secret"

value := base64.StdEncoding.EncodeToString([]byte("value"))
s.mockSecretClient.EXPECT().ListSecrets(
true, coresecrets.Filter{Label: &secretName},
).Return([]apisecrets.SecretDetails{
{
Metadata: coresecrets.SecretMetadata{
Version: 1,
},
Revisions: []coresecrets.SecretRevisionMetadata{
{
Revision: 1,
},
},
Value: coresecrets.NewSecretValue(map[string]string{"key": value}),
Error: "",
},
}, nil).AnyTimes()

client := s.getSecretsClient()
output, err := client.ReadSecret(&ReadSecretInput{
ModelName: s.testModelName,
Name: secretName,
})
s.Require().NoError(err)
s.Require().NotNil(output)

s.Assert().Equal(map[string]string{"key": "value"}, output.Value)
}

func (s *SecretSuite) TestReadSecretError() {
ctlr := s.setupMocks(s.T())
defer ctlr.Finish()

secretName := "test-secret"

errBoom := errors.New("boom")
s.mockSecretClient.EXPECT().ListSecrets(
true, coresecrets.Filter{Label: &secretName},
).Return([]apisecrets.SecretDetails{
{
Error: errBoom.Error(),
},
}, nil).AnyTimes()

client := s.getSecretsClient()
output, err := client.ReadSecret(&ReadSecretInput{
ModelName: s.testModelName,
Name: secretName,
})
s.Require().Error(err)
s.Require().Nil(output)

s.Assert().Equal(errBoom, err)
}

func (s *SecretSuite) TestUpdateSecret() {
ctlr := s.setupMocks(s.T())
defer ctlr.Finish()

secretName := "test-secret"

// update secret with value2
value2 := base64.StdEncoding.EncodeToString([]byte("value2"))
s.mockSecretClient.EXPECT().UpdateSecret(
nil, secretName, nil, secretName, "secret info", map[string]string{"key": value2},
).Return(nil).AnyTimes()

client := s.getSecretsClient()
err := client.UpdateSecret(&UpdateSecretInput{
ModelName: s.testModelName,
Name: secretName,
Value: map[string]string{"key": value2},
Info: "secret info",
})
s.Require().NoError(err)

s.mockSecretClient.EXPECT().ListSecrets(
true, coresecrets.Filter{Label: &secretName},
).Return([]apisecrets.SecretDetails{
{
Metadata: coresecrets.SecretMetadata{
Version: 1,
},
Revisions: []coresecrets.SecretRevisionMetadata{
{
Revision: 1,
},
},
Value: coresecrets.NewSecretValue(map[string]string{"key": value2}),
Error: "",
},
}, nil).Times(1)

// read secret and check if value is updated
output, err := client.ReadSecret(&ReadSecretInput{
ModelName: s.testModelName,
Name: secretName,
})
s.Require().NoError(err)
s.Require().NotNil(output)

s.Assert().Equal(map[string]string{"key": "value2"}, output.Value)
}

func (s *SecretSuite) TestDeleteSecret() {
ctlr := s.setupMocks(s.T())
defer ctlr.Finish()

secretName := "test-secret"

s.mockSecretClient.EXPECT().RemoveSecret(nil, secretName, nil).Return(nil).AnyTimes()

client := s.getSecretsClient()
err := client.DeleteSecret(&DeleteSecretInput{
ModelName: s.testModelName,
Name: secretName,
})
s.Require().NoError(err)

Check failure on line 234 in internal/juju/secrets_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unnecessary trailing newline (whitespace)
}

Expand Down

0 comments on commit fd55d5a

Please sign in to comment.