Skip to content

Commit

Permalink
Fix secret unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
anvial committed Apr 1, 2024
1 parent d88eee9 commit 95ab50e
Showing 1 changed file with 31 additions and 89 deletions.
120 changes: 31 additions & 89 deletions internal/juju/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ package juju
import (
"testing"

"github.com/juju/juju/core/resources"
"github.com/stretchr/testify/suite"
"go.uber.org/mock/gomock"
)

// Basic imports

type UserSecretSuite struct {
type SecretSuite struct {
suite.Suite

testModelName string
Expand All @@ -26,109 +25,52 @@ type UserSecretSuite struct {
mockSharedClient *MockSharedClient
}

func (s *UserSecretSuite) SetupTest() {}
func (s *SecretSuite) SetupTest() {}

func (s *SecretSuite) setupMocks(t *testing.T) *gomock.Controller {
s.testModelName = "test-secret-model"

func (s *UserSecretSuite) setupMocks(t *testing.T) *gomock.Controller {
ctlr := gomock.NewController(t)
s.mockSecretClient = NewMockSecretAPIClient(ctlr)
s.mockClient = NewMockClientAPIClient(ctlr)

s.mockConnection = NewMockConnection(ctlr)
s.mockConnection.EXPECT().Close().Return(nil).AnyTimes()

// TODO: Need to add all resources from the following plan, ad then is this Mocked TF state in tests:
// ```
//terraform {
// required_providers {
// juju = {
// source = "juju/juju"
// version = "0.11.0"
// }
// }
//}
//
//provider "juju" {}
//
//resource "juju_model" "my_model" {
// name = "tf-secret-test"
//}
//
//resource "juju_application" "githubrunner" {
// name = "github-runner"
// model = juju_model.my_model.name
//
// charm {
// name = "github-runner"
// revision = 96
// channel = "latest/edge"
// }
//
// config = {
// runner-storage = "runner-storage-value"
// }
//
// units = 1
//}
//
//resource "juju_secret" "my_secret" {
// model = juju_model.my_model.name
// name = "my_secret_name"
// value = {
// key1 = "value1"
// key2 = "value2"
// }
// info = "This is my secret"
//}
//
//resource "juju_access_secret" "my_secret_access" {
// model = juju_model.my_model.name
// applications = [
// juju_application.githubrunner
// ]
// secret_id = juju_secret.my_secret.secret_id
//}
// ```

s.mockResourceAPIClient = NewMockResourceAPIClient(ctlr)
s.mockResourceAPIClient.EXPECT().ListResources(gomock.Any()).DoAndReturn(
func(applications []string) ([]resources.ApplicationResources, error) {
results := make([]resources.ApplicationResources, len(applications))
return results, nil
}).AnyTimes()

log := func(msg string, additionalFields ...map[string]interface{}) {
s.T().Logf("logging from shared client %q, %+v", msg, additionalFields)
}
s.mockSharedClient = NewMockSharedClient(ctlr)
s.mockSharedClient.EXPECT().Debugf(gomock.Any(), gomock.Any()).Do(log).AnyTimes()
s.mockSharedClient.EXPECT().Errorf(gomock.Any(), gomock.Any()).Do(log).AnyTimes()
s.mockSharedClient.EXPECT().Tracef(gomock.Any(), gomock.Any()).Do(log).AnyTimes()
s.mockSharedClient.EXPECT().JujuLogger().Return(&jujuLoggerShim{}).AnyTimes()
s.mockSharedClient.EXPECT().GetConnection(&s.testModelName).Return(s.mockConnection, nil).AnyTimes()

return ctlr
}

func (s *UserSecretSuite) TestCreateSecret() {
func (s *SecretSuite) getSecretsClient() secretsClient {
return secretsClient{
SharedClient: s.mockSharedClient,
}
}

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

secretName := "test-secret-name"
secretValue := map[string]string{"key": "value"}
secretInfo := "test-info"
secretsInput := &CreateSecretInput{
ModelName: s.testModelName,
Name: "test-secret-name",
Value: map[string]string{"key": "value"},
Info: "test-info",
}

s.mockSecretClient.EXPECT().CreateSecret(gomock.Any()).Return(CreateSecretOutput{
s.mockSecretClient.EXPECT().CreateSecret(secretsInput).Return(&CreateSecretOutput{
SecretId: "secret-id",
}, nil).AnyTimes()

client := newSecretsClient(s.mockSharedClient)
secret, err := client.CreateSecret(&CreateSecretInput{
secret, err := s.mockSecretClient.CreateSecret(&CreateSecretInput{
ModelName: s.testModelName,
Name: secretName,
Value: secretValue,
Info: secretInfo,
Name: secretsInput.Name,
Value: secretsInput.Value,
Info: secretsInput.Info,
})

s.Require().NoError(err)

s.Require().NotNil(secret)
s.Equal("secret-id", secret.SecretId)
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestUserSecretSuite(t *testing.T) {
suite.Run(t, new(SecretSuite))
}

0 comments on commit 95ab50e

Please sign in to comment.