Skip to content

Commit

Permalink
test: coverage for objectstore uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRichardson committed Dec 11, 2024
1 parent e7700ac commit cb2114b
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 6 deletions.
29 changes: 28 additions & 1 deletion core/objectstore/uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ObjectStoreUUIDSuite struct {

var _ = gc.Suite(&ObjectStoreUUIDSuite{})

func (*ObjectStoreUUIDSuite) TestIDValidate(c *gc.C) {
func (*ObjectStoreUUIDSuite) TestUUIDValidate(c *gc.C) {
tests := []struct {
uuid string
err error
Expand Down Expand Up @@ -48,3 +48,30 @@ func (*ObjectStoreUUIDSuite) TestIDValidate(c *gc.C) {
c.Check(err, jc.ErrorIs, test.err)
}
}

func (*ObjectStoreUUIDSuite) TestUUIDIsEmpty(c *gc.C) {
tests := []struct {
uuid string
value bool
}{
{
uuid: "",
value: true,
},
{
uuid: "invalid",
value: false,
},
{
uuid: uuid.MustNewUUID().String(),
value: false,
},
}

for i, test := range tests {
c.Logf("test %d: %q", i, test.uuid)
empty := UUID(test.uuid).IsEmpty()

c.Check(empty, gc.Equals, test.value)
}
}
13 changes: 8 additions & 5 deletions domain/application/service/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (s *applicationServiceSuite) TestCreateApplication(c *gc.C) {
defer s.setupMocks(c).Finish()

id := applicationtesting.GenApplicationUUID(c)
objectStoreUUID := objectstoretesting.GenObjectStoreUUID(c)

u := application.AddUnitArg{
UnitName: "ubuntu/666",
Expand All @@ -80,11 +81,12 @@ func (s *applicationServiceSuite) TestCreateApplication(c *gc.C) {
Name: "ubuntu",
RunAs: "default",
},
Manifest: s.minimalManifest(c),
ReferenceName: "ubuntu",
Source: domaincharm.CharmHubSource,
Revision: 42,
Architecture: architecture.ARM64,
Manifest: s.minimalManifest(c),
ReferenceName: "ubuntu",
Source: domaincharm.CharmHubSource,
Revision: 42,
Architecture: architecture.ARM64,
ObjectStoreUUID: objectStoreUUID,
}
platform := application.Platform{
Channel: "24.04",
Expand Down Expand Up @@ -139,6 +141,7 @@ func (s *applicationServiceSuite) TestCreateApplication(c *gc.C) {
DownloadURL: "https://example.com/foo",
DownloadSize: 42,
},
CharmObjectStoreUUID: objectStoreUUID,
}, a)
c.Assert(err, jc.ErrorIsNil)
}
Expand Down
92 changes: 92 additions & 0 deletions domain/application/state/charm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"database/sql"
"fmt"

"github.com/canonical/sqlair"
"github.com/juju/clock"
"github.com/juju/errors"
jc "github.com/juju/testing/checkers"
Expand All @@ -17,6 +18,8 @@ import (
corecharm "github.com/juju/juju/core/charm"
charmtesting "github.com/juju/juju/core/charm/testing"
coredatabase "github.com/juju/juju/core/database"
"github.com/juju/juju/core/objectstore"
objectstoretesting "github.com/juju/juju/core/objectstore/testing"
"github.com/juju/juju/domain/application/architecture"
"github.com/juju/juju/domain/application/charm"
applicationerrors "github.com/juju/juju/domain/application/errors"
Expand Down Expand Up @@ -89,6 +92,95 @@ VALUES (?, 'foo')`, id.String())
c.Check(charmID, gc.Equals, id)
}

func (s *charmStateSuite) TestSetCharmObjectStoreUUID(c *gc.C) {
st := NewState(s.TxnRunnerFactory(), clock.WallClock,
loggertesting.WrapCheckLog(c))

objectStoreUUID := objectstoretesting.GenObjectStoreUUID(c)

expected := charm.Metadata{
Name: "foo",
Summary: "summary",
Description: "description",
Subordinate: true,
RunAs: charm.RunAsRoot,
MinJujuVersion: version.MustParse("4.0.0"),
Assumes: []byte("null"),
}

err := s.TxnRunner().StdTxn(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, `
INSERT INTO object_store_metadata (uuid, sha_256, sha_384, size) VALUES (?, 'foo', 'bar', 42)
`, objectStoreUUID.String())
return err
})
c.Assert(err, jc.ErrorIsNil)

id, err := st.SetCharm(context.Background(), charm.Charm{
Metadata: expected,
Manifest: s.minimalManifest(c),
Source: charm.LocalSource,
Revision: 42,
ReferenceName: "foo",
Hash: "hash",
Version: "deadbeef",
ObjectStoreUUID: objectStoreUUID,
}, nil)
c.Assert(err, jc.ErrorIsNil)

var resultObjectStoreUUID objectstore.UUID
err = s.TxnRunner().Txn(context.Background(), func(ctx context.Context, tx *sqlair.TX) error {
ch, err := st.getCharmState(ctx, tx, charmID{UUID: id.String()})
if err != nil {
return errors.Trace(err)
}
resultObjectStoreUUID = ch.ObjectStoreUUID
return nil
})
c.Assert(err, jc.ErrorIsNil)
c.Check(resultObjectStoreUUID, gc.Equals, objectStoreUUID)
}

func (s *charmStateSuite) TestSetCharmWithoutObjectStoreUUID(c *gc.C) {
st := NewState(s.TxnRunnerFactory(), clock.WallClock,
loggertesting.WrapCheckLog(c))

expected := charm.Metadata{
Name: "foo",
Summary: "summary",
Description: "description",
Subordinate: true,
RunAs: charm.RunAsRoot,
MinJujuVersion: version.MustParse("4.0.0"),
Assumes: []byte("null"),
}

// The archive path is empty, so it's not immediately available.

id, err := st.SetCharm(context.Background(), charm.Charm{
Metadata: expected,
Manifest: s.minimalManifest(c),
Source: charm.LocalSource,
Revision: 42,
ReferenceName: "foo",
Hash: "hash",
Version: "deadbeef",
}, nil)
c.Assert(err, jc.ErrorIsNil)

var resultObjectStoreUUID objectstore.UUID
err = s.TxnRunner().Txn(context.Background(), func(ctx context.Context, tx *sqlair.TX) error {
ch, err := st.getCharmState(ctx, tx, charmID{UUID: id.String()})
if err != nil {
return errors.Trace(err)
}
resultObjectStoreUUID = ch.ObjectStoreUUID
return nil
})
c.Assert(err, jc.ErrorIsNil)
c.Check(resultObjectStoreUUID, gc.Equals, objectstore.UUID(""))
}

func (s *charmStateSuite) TestSetCharmNotAvailable(c *gc.C) {
st := NewState(s.TxnRunnerFactory(), clock.WallClock,
loggertesting.WrapCheckLog(c))
Expand Down

0 comments on commit cb2114b

Please sign in to comment.