github.com/argoproj/argo-cd/v3@v3.2.1/util/manifeststream/stream_test.go (about) 1 package manifeststream_test 2 3 import ( 4 "errors" 5 "io" 6 "math" 7 "os" 8 "path/filepath" 9 "testing" 10 "time" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 15 applicationpkg "github.com/argoproj/argo-cd/v3/pkg/apiclient/application" 16 "github.com/argoproj/argo-cd/v3/reposerver/apiclient" 17 "github.com/argoproj/argo-cd/v3/test" 18 "github.com/argoproj/argo-cd/v3/util/io/files" 19 "github.com/argoproj/argo-cd/v3/util/manifeststream" 20 ) 21 22 type applicationStreamMock struct { 23 messages chan *applicationpkg.ApplicationManifestQueryWithFilesWrapper 24 done chan bool 25 } 26 27 func (m *applicationStreamMock) Recv() (*applicationpkg.ApplicationManifestQueryWithFilesWrapper, error) { 28 select { 29 case message := <-m.messages: 30 return message, nil 31 case <-m.done: 32 return nil, io.EOF 33 case <-time.After(500 * time.Millisecond): 34 return nil, errors.New("timeout receiving message mock") 35 } 36 } 37 38 func (m *applicationStreamMock) Send(message *applicationpkg.ApplicationManifestQueryWithFilesWrapper) error { 39 m.messages <- message 40 return nil 41 } 42 43 func newApplicationStreamMock() *applicationStreamMock { 44 messagesCh := make(chan *applicationpkg.ApplicationManifestQueryWithFilesWrapper) 45 doneCh := make(chan bool) 46 return &applicationStreamMock{ 47 messages: messagesCh, 48 done: doneCh, 49 } 50 } 51 52 type repoStreamMock struct { 53 messages chan *apiclient.ManifestRequestWithFiles 54 done chan bool 55 } 56 57 func (m *repoStreamMock) Recv() (*apiclient.ManifestRequestWithFiles, error) { 58 select { 59 case message := <-m.messages: 60 return message, nil 61 case <-m.done: 62 return nil, io.EOF 63 case <-time.After(500 * time.Millisecond): 64 return nil, errors.New("timeout receiving message mock") 65 } 66 } 67 68 func (m *repoStreamMock) Send(message *apiclient.ManifestRequestWithFiles) error { 69 m.messages <- message 70 return nil 71 } 72 73 func newRepoStreamMock() *repoStreamMock { 74 messagesCh := make(chan *apiclient.ManifestRequestWithFiles) 75 doneCh := make(chan bool) 76 return &repoStreamMock{ 77 messages: messagesCh, 78 done: doneCh, 79 } 80 } 81 82 func TestManifestStream(t *testing.T) { 83 appStreamMock := newApplicationStreamMock() 84 repoStreamMock := newRepoStreamMock() 85 workdir, err := files.CreateTempDir("") 86 require.NoError(t, err) 87 88 appDir := filepath.Join(getTestDataDir(t), "app") 89 90 go func() { 91 err := manifeststream.SendApplicationManifestQueryWithFiles(t.Context(), appStreamMock, "test", "test", appDir, nil) 92 assert.NoError(t, err) 93 appStreamMock.done <- true 94 }() 95 96 query, err := manifeststream.ReceiveApplicationManifestQueryWithFiles(appStreamMock) 97 require.NoError(t, err) 98 require.NotNil(t, query) 99 100 req := &apiclient.ManifestRequest{} 101 102 go func() { 103 err = manifeststream.SendRepoStream(repoStreamMock, appStreamMock, req, *query.Checksum) 104 assert.NoError(t, err) 105 repoStreamMock.done <- true 106 }() 107 108 req2, meta, err := manifeststream.ReceiveManifestFileStream(t.Context(), repoStreamMock, workdir, math.MaxInt64, math.MaxInt64) 109 require.NoError(t, err) 110 require.NotNil(t, req2) 111 require.NotNil(t, meta) 112 113 files, err := os.ReadDir(workdir) 114 require.NoError(t, err) 115 require.Len(t, files, 1) 116 names := []string{} 117 for _, f := range files { 118 names = append(names, f.Name()) 119 } 120 assert.Contains(t, names, "DUMMY.md") 121 } 122 123 func getTestDataDir(t *testing.T) string { 124 t.Helper() 125 return filepath.Join(test.GetTestDir(t), "testdata") 126 }