|
| 1 | +package ctf |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + |
| 9 | + "ocm.software/open-component-model/bindings/go/blob" |
| 10 | + "ocm.software/open-component-model/bindings/go/ctf" |
| 11 | + "ocm.software/open-component-model/bindings/go/ctf/index/v1" |
| 12 | + repo "ocm.software/open-component-model/bindings/go/repository" |
| 13 | +) |
| 14 | + |
| 15 | +// MockCTF is a mock implementation of the CTF interface |
| 16 | +type MockCTF struct { |
| 17 | + // idx contains test data. |
| 18 | + idx v1.Index |
| 19 | +} |
| 20 | + |
| 21 | +var _ ctf.CTF = (*MockCTF)(nil) |
| 22 | + |
| 23 | +func TestListComponents(t *testing.T) { |
| 24 | + ctx := t.Context() |
| 25 | + testData := []string{ |
| 26 | + "component-descriptors/componentC", |
| 27 | + "component-descriptors/componentB", |
| 28 | + "not-a-component", |
| 29 | + "component-descriptors/duplicate", |
| 30 | + "component-descriptors/componentD", |
| 31 | + "component-descriptors/componentA", |
| 32 | + "component-descriptors/duplicate", |
| 33 | + "not-a-component-again", |
| 34 | + } |
| 35 | + |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + last string |
| 39 | + input []string |
| 40 | + expected []string |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "default behavior - store order preserved", |
| 44 | + input: testData, |
| 45 | + expected: []string{"componentA", "componentB", "componentC", "componentD", "duplicate"}, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "last parameter should be ignored", |
| 49 | + last: "2", |
| 50 | + input: testData, |
| 51 | + expected: []string{"componentA", "componentB", "componentC", "componentD", "duplicate"}, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "single component in the store - one result", |
| 55 | + input: []string{"component-descriptors/componentA"}, |
| 56 | + expected: []string{"componentA"}, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "empty store - empty result", |
| 60 | + input: []string{}, |
| 61 | + expected: []string{}, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "store does not contain components - empty result", |
| 65 | + input: []string{"not-a-component", "not-a-component-again"}, |
| 66 | + expected: []string{}, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "overlapping component names", |
| 70 | + input: []string{"component-descriptors/foo/bar", "component-descriptors/foo/bar/baz"}, |
| 71 | + expected: []string{"foo/bar", "foo/bar/baz"}, |
| 72 | + }, |
| 73 | + } |
| 74 | + for _, tt := range tests { |
| 75 | + t.Run(tt.name, func(t *testing.T) { |
| 76 | + // Create a mock CTF store with the test data. |
| 77 | + archive := NewMockCTF(tt.input) |
| 78 | + |
| 79 | + // Create an instance of the CTFComponentLister. |
| 80 | + var lister repo.ComponentLister |
| 81 | + lister = NewComponentLister(archive) |
| 82 | + |
| 83 | + // Collect the returned component names. |
| 84 | + result := []string{} |
| 85 | + err := lister.ListComponents(ctx, tt.last, func(names []string) error { |
| 86 | + result = append(result, names...) |
| 87 | + return nil |
| 88 | + }) |
| 89 | + |
| 90 | + assert.NoError(t, err) |
| 91 | + assert.Equal(t, tt.expected, result) |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestListComponentsFnNil(t *testing.T) { |
| 97 | + archive := NewMockCTF([]string{}) |
| 98 | + var lister repo.ComponentLister |
| 99 | + lister = NewComponentLister(archive) |
| 100 | + err := lister.ListComponents(t.Context(), "", nil) |
| 101 | + assert.EqualError(t, err, ErrFnNil.Error()) |
| 102 | +} |
| 103 | + |
| 104 | +// NewMockCTF creates a new empty mock CTF. |
| 105 | +func NewMockCTF(compNames []string) *MockCTF { |
| 106 | + m := &MockCTF{} |
| 107 | + m.idx = v1.NewIndex() |
| 108 | + |
| 109 | + for _, r := range compNames { |
| 110 | + a := v1.ArtifactMetadata{ |
| 111 | + Repository: r, |
| 112 | + Tag: "v1", |
| 113 | + Digest: "sha256:abc", |
| 114 | + MediaType: "type1", |
| 115 | + } |
| 116 | + m.idx.AddArtifact(a) |
| 117 | + } |
| 118 | + |
| 119 | + return m |
| 120 | +} |
| 121 | + |
| 122 | +// Methods of the CTF interface. |
| 123 | + |
| 124 | +// GetIndex is the only used method of the mock implementation. |
| 125 | +func (m *MockCTF) GetIndex(ctx context.Context) (v1.Index, error) { |
| 126 | + return m.idx, nil |
| 127 | +} |
| 128 | + |
| 129 | +func (m *MockCTF) Format() ctf.FileFormat { |
| 130 | + panic("not implemented") |
| 131 | +} |
| 132 | + |
| 133 | +func (m *MockCTF) SetIndex(ctx context.Context, index v1.Index) error { |
| 134 | + panic("not implemented") |
| 135 | +} |
| 136 | + |
| 137 | +func (m *MockCTF) ListBlobs(ctx context.Context) ([]string, error) { |
| 138 | + panic("not implemented") |
| 139 | +} |
| 140 | + |
| 141 | +func (m *MockCTF) GetBlob(ctx context.Context, digest string) (blob.ReadOnlyBlob, error) { |
| 142 | + panic("not implemented") |
| 143 | +} |
| 144 | + |
| 145 | +func (m *MockCTF) SaveBlob(ctx context.Context, blob blob.ReadOnlyBlob) error { |
| 146 | + panic("not implemented") |
| 147 | +} |
| 148 | + |
| 149 | +func (m *MockCTF) DeleteBlob(ctx context.Context, digest string) error { |
| 150 | + panic("not implemented") |
| 151 | +} |
0 commit comments