1
1
// Copyright The OpenTelemetry Authors
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
- package archive // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/archive "
4
+ package archive_test // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/archive_test "
5
5
6
6
import (
7
7
"context"
8
8
"fmt"
9
9
"math/rand/v2"
10
10
"testing"
11
11
12
- "github.com/google/uuid"
13
12
"github.com/stretchr/testify/require"
14
13
"go.uber.org/zap"
15
14
16
- "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/checkpoint "
15
+ "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/archive "
17
16
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/fileset"
18
17
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/fingerprint"
19
18
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/reader"
20
- "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
21
19
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil"
22
20
)
23
21
22
+ func archiveKey (i int ) string {
23
+ return fmt .Sprintf ("knownFiles%d" , i )
24
+ }
25
+
24
26
func TestArchiveCRUD (t * testing.T ) {
25
27
// pollsToArchiveMatrix contains different polls_to_archive settings to test
26
28
pollsToArchiveMatrix := []int {10 , 20 , 50 , 100 , 200 }
@@ -36,10 +38,7 @@ func TestArchiveCRUD(t *testing.T) {
36
38
37
39
func testArchive (t * testing.T , pollsToArchive int ) {
38
40
persister := testutil .NewUnscopedMockPersister ()
39
- a := New (context .Background (), zap .L (), pollsToArchive , persister )
40
- archive , isArchive := a .(* archive )
41
- require .True (t , isArchive , "expected archive" )
42
- require .Equal (t , 0 , archive .archiveIndex , "expected archiveIndex to be 0 at the beginning" )
41
+ a := archive .New (context .Background (), zap .L (), pollsToArchive , persister )
43
42
44
43
m := make (map [int ]* fingerprint.Fingerprint )
45
44
@@ -60,36 +59,31 @@ func testArchive(t *testing.T, pollsToArchive int) {
60
59
set := fileset.New [* reader.Metadata ](0 )
61
60
set .Add (& reader.Metadata {Fingerprint : fp })
62
61
63
- archive .WriteFiles (context .Background (), set )
62
+ a .WriteFiles (context .Background (), set )
64
63
}
65
64
66
65
// sub-test 1: rolled over fingerprints should not be a part of archive
67
66
for _ , fp := range rolledOverFps {
68
- matchedData := archive .FindFiles (context .Background (), []* fingerprint.Fingerprint {fp })
67
+ matchedData := a .FindFiles (context .Background (), []* fingerprint.Fingerprint {fp })
69
68
require .Nil (t , matchedData [0 ])
70
69
}
71
70
72
71
// sub-test 2: newer fingerprints should be part of archive
73
- for index , fp := range m {
74
- oldMetadata , err := checkpoint .LoadKey (context .Background (), persister , archiveKey (index ))
75
- require .NoErrorf (t , err , "expected no error while loading metadata, got %v" , err )
76
- require .Lenf (t , oldMetadata , 1 , "index %d should have exactly one item" , index )
77
-
72
+ for _ , fp := range m {
78
73
// FindFiles removes the data from persister.
79
- matchedData := archive .FindFiles (context .Background (), []* fingerprint.Fingerprint {fp })
74
+ matchedData := a .FindFiles (context .Background (), []* fingerprint.Fingerprint {fp })
80
75
require .NotNil (t , matchedData [0 ])
81
76
require .True (t , fp .Equal (matchedData [0 ].GetFingerprint ()), "expected fingerprints to match" )
82
77
83
78
// archive should no longer contain data (as FindFiles removed the data)
84
- newMetadata , err := checkpoint .LoadKey (context .Background (), persister , archiveKey (index ))
85
- require .NoErrorf (t , err , "expected no error while loading metadata, got %v" , err )
86
- require .Emptyf (t , newMetadata , "index %d should have no items, found %d items" , index , len (newMetadata ))
79
+ matchedData = a .FindFiles (context .Background (), []* fingerprint.Fingerprint {fp })
80
+ require .Nil (t , matchedData [0 ])
87
81
}
88
82
}
89
83
90
84
func testArchiveNop (t * testing.T ) {
91
85
persister := testutil .NewUnscopedMockPersister ()
92
- a := New (context .Background (), zap .L (), - 1 , persister )
86
+ a := archive . New (context .Background (), zap .L (), - 1 , persister )
93
87
94
88
m := make (map [int ]* fingerprint.Fingerprint )
95
89
@@ -105,89 +99,16 @@ func testArchiveNop(t *testing.T) {
105
99
}
106
100
107
101
// fingerprints should not be part of archive, as it's nop
108
- for index , fp := range m {
109
- oldMetadata , err := checkpoint .LoadKey (context .Background (), persister , archiveKey (index ))
110
- require .NoErrorf (t , err , "expected no error while loading metadata, got %v" , err )
111
- require .Emptyf (t , oldMetadata , "index %d should have no items, found %d items" , index , len (oldMetadata ))
102
+ for _ , fp := range m {
103
+ matchedData := a .FindFiles (context .Background (), []* fingerprint.Fingerprint {fp })
104
+ require .Nil (t , matchedData [0 ])
112
105
113
106
// FindFiles removes the data from persister.
114
- matchedData : = a .FindFiles (context .Background (), []* fingerprint.Fingerprint {fp })
107
+ matchedData = a .FindFiles (context .Background (), []* fingerprint.Fingerprint {fp })
115
108
require .Nil (t , matchedData [0 ], "fingerprints shouldn't match for nopArchive" )
116
109
}
117
110
}
118
111
119
- func TestFindFilesOrder (t * testing.T ) {
120
- fps := make ([]* fingerprint.Fingerprint , 0 )
121
- for i := 0 ; i < 100 ; i ++ {
122
- fps = append (fps , fingerprint .New ([]byte (uuid .NewString ())))
123
- }
124
- persister := testutil .NewUnscopedMockPersister ()
125
- fpInStorage := populatedPersisterData (persister , fps )
126
-
127
- archive := New (context .Background (), zap .L (), 100 , persister )
128
- matchables := archive .FindFiles (context .Background (), fps )
129
-
130
- require .Len (t , matchables , len (fps ), "return slice should be of same length as input slice" )
131
-
132
- for i := 0 ; i < len (matchables ); i ++ {
133
- if fpInStorage [i ] {
134
- // if current fingerprint is present in storage, the corresponding return type should not be nil
135
- require .NotNilf (t , matchables [i ], "resulting index %d should be not be nil type" , i )
136
- require .Truef (t , fps [i ].Equal (matchables [i ].GetFingerprint ()), "fingerprint at index %d is not equal to corresponding return value" , i )
137
- } else {
138
- // if current fingerprint is absent from storage, the corresponding index should be empty i.e. "nil"
139
- require .Nil (t , matchables [i ], "resulting index %d should be of nil type" , i )
140
- }
141
- }
142
- }
143
-
144
- func TestIndexInBounds (t * testing.T ) {
145
- persister := testutil .NewUnscopedMockPersister ()
146
- pollsToArchive := 100
147
- testArchive := New (context .Background (), zap .L (), 100 , persister ).(* archive )
148
-
149
- // no index exists. archiveIndex should be 0
150
- require .Equal (t , 0 , testArchive .archiveIndex )
151
-
152
- // run archiving. Each time, index should be in bound.
153
- for i := 0 ; i < 1098 ; i ++ {
154
- require .Equalf (t , i % pollsToArchive , testArchive .archiveIndex , "Index should %d, but was %d" , i % pollsToArchive , testArchive .archiveIndex )
155
- testArchive .WriteFiles (context .Background (), & fileset.Fileset [* reader.Metadata ]{})
156
- require .Truef (t , testArchive .archiveIndex >= 0 && testArchive .archiveIndex < pollsToArchive , "Index should be between 0 and %d, but was %d" , pollsToArchive , testArchive .archiveIndex )
157
- }
158
- oldIndex := testArchive .archiveIndex
159
-
160
- // re-create archive
161
- testArchive = New (context .Background (), zap .L (), 100 , persister ).(* archive )
162
-
163
- // index should exist and new archiveIndex should be equal to oldIndex
164
- require .Equalf (t , oldIndex , testArchive .archiveIndex , "New index should %d, but was %d" , oldIndex , testArchive .archiveIndex )
165
-
166
- // re-create archive, with reduced pollsToArchive
167
- pollsToArchive = 70
168
-
169
- testArchive = New (context .Background (), zap .L (), pollsToArchive , persister ).(* archive )
170
- // index should exist but it is out of bounds. So it should reset to 0
171
- require .Equalf (t , 0 , testArchive .archiveIndex , "Index should be reset to 0 but was %d" , testArchive .archiveIndex )
172
- }
173
-
174
- func populatedPersisterData (persister operator.Persister , fps []* fingerprint.Fingerprint ) []bool {
175
- md := make ([]* reader.Metadata , 0 )
176
-
177
- fpInStorage := make ([]bool , len (fps ))
178
- for i , fp := range fps {
179
- // 50-50 chance that a fingerprint exists in the storage
180
- if rand .Float32 () < 0.5 {
181
- md = append (md , & reader.Metadata {Fingerprint : fp })
182
- fpInStorage [i ] = true // mark the fingerprint at index i in storage
183
- }
184
- }
185
- // save half keys in knownFiles0 and other half in knownFiles1
186
- _ = checkpoint .SaveKey (context .Background (), persister , md [:len (md )/ 2 ], "knownFiles0" )
187
- _ = checkpoint .SaveKey (context .Background (), persister , md [len (md )/ 2 :], "knownFiles1" )
188
- return fpInStorage
189
- }
190
-
191
112
func createRandomString (length int ) string {
192
113
// some characters for the random generation
193
114
const letterBytes = " ,.;:*-+/[]{}<>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
0 commit comments