Skip to content

Commit

Permalink
Move manifest unit tests to package manifest_test
Browse files Browse the repository at this point in the history
This prevents an import cycle in tests: with the new Helm deployer
mocks, having manifest unit tests in package `manifest` would lead to:
```
package github.com/rancher/fleet/internal/manifest
        imports github.com/rancher/fleet/internal/mocks
        imports github.com/rancher/fleet/internal/helmdeployer
        imports github.com/rancher/fleet/internal/manifest: import cycle not allowed in test
```
  • Loading branch information
weyfonk committed Aug 8, 2024
1 parent a5ccfc0 commit f59e9b8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions internal/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ func (m *Manifest) ID() (string, error) {
if err != nil {
return "", err
}
return toSHA256ID(shasum), nil
return ToSHA256ID(shasum), nil
}

// toSHA256ID generates a valid Kubernetes name (max length of 64) from a provided SHA256 sum
func toSHA256ID(shasum string) string {
// ToSHA256ID generates a valid Kubernetes name (max length of 64) from a provided SHA256 sum
func ToSHA256ID(shasum string) string {
return ("s-" + shasum)[:63]
}
8 changes: 4 additions & 4 deletions internal/manifest/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (

func NewStore(client client.Client) *ContentStore {
return &ContentStore{
client: client,
Client: client,
}
}

type ContentStore struct {
client client.Client
Client client.Client
}

// Store stores the manifest as a content resource.
Expand All @@ -30,7 +30,7 @@ func (c *ContentStore) Store(ctx context.Context, manifest *Manifest) error {
return err
}

if err := c.client.Get(ctx, types.NamespacedName{Name: id}, &fleet.Content{}); err != nil && !apierrors.IsNotFound(err) {
if err := c.Client.Get(ctx, types.NamespacedName{Name: id}, &fleet.Content{}); err != nil && !apierrors.IsNotFound(err) {
return err
} else if err == nil {
return nil
Expand All @@ -55,7 +55,7 @@ func (c *ContentStore) createContents(ctx context.Context, id string, manifest *
return err
}

err = c.client.Create(ctx, &fleet.Content{
err = c.Client.Create(ctx, &fleet.Content{
ObjectMeta: metav1.ObjectMeta{
Name: id,
},
Expand Down
15 changes: 8 additions & 7 deletions internal/manifest/store_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package manifest
package manifest_test

import (
"bytes"
Expand All @@ -11,14 +11,15 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

"github.com/rancher/fleet/internal/manifest"
"github.com/rancher/fleet/internal/mocks"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
)

func testManifest(t *testing.T, data string) *Manifest {
func testManifest(t *testing.T, data string) *manifest.Manifest {
t.Helper()

m, err := FromJSON([]byte(data), "")
m, err := manifest.FromJSON([]byte(data), "")
if err != nil {
t.Fatal(err)
}
Expand All @@ -35,7 +36,7 @@ func Test_contentStore_Store(t *testing.T) {
resources := `{"resources": [{"name": "foo", "content": "bar"}]}`
checksum := "752ebbb975f52eea5e87950ef2ca5de4055de3c68a17f54d94527d7fd79c21fd"
type args struct {
manifest *Manifest
manifest *manifest.Manifest
cached bool
}
tests := []struct {
Expand All @@ -48,23 +49,23 @@ func Test_contentStore_Store(t *testing.T) {
args: args{
manifest: testManifest(t, resources),
},
want: toSHA256ID(checksum),
want: manifest.ToSHA256ID(checksum),
},
{
name: "existing manifest",
args: args{
manifest: testManifest(t, resources),
cached: true,
},
want: toSHA256ID(checksum),
want: manifest.ToSHA256ID(checksum),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
client := mocks.NewMockClient(ctrl)

store := &ContentStore{client}
store := &manifest.ContentStore{client}
ctx := context.TODO()
nsn := types.NamespacedName{Name: tt.want}

Expand Down

0 comments on commit f59e9b8

Please sign in to comment.