|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path" |
| 6 | + "runtime" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestInstall(t *testing.T) { |
| 16 | + configs := []struct { |
| 17 | + name string |
| 18 | + config string |
| 19 | + wantSourceFiles int |
| 20 | + wantDestFiles int |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "sync_success_sourcev1_destv0", |
| 24 | + config: "sync-success-sourcev1-destv0.yml", |
| 25 | + wantSourceFiles: 2, |
| 26 | + wantDestFiles: 2, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "multiple_sources", |
| 30 | + config: "multiple-sources.yml", |
| 31 | + wantSourceFiles: 2, |
| 32 | + wantDestFiles: 2, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "multiple_destinations", |
| 36 | + config: "multiple-destinations.yml", |
| 37 | + wantSourceFiles: 2, |
| 38 | + wantDestFiles: 4, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "multiple_sources_destinations", |
| 42 | + config: "multiple-sources-destinations.yml", |
| 43 | + wantSourceFiles: 2, |
| 44 | + wantDestFiles: 2, |
| 45 | + }, |
| 46 | + } |
| 47 | + _, filename, _, _ := runtime.Caller(0) |
| 48 | + currentDir := path.Dir(filename) |
| 49 | + |
| 50 | + for _, tc := range configs { |
| 51 | + tc := tc |
| 52 | + t.Run(tc.name, func(t *testing.T) { |
| 53 | + cqDir := t.TempDir() |
| 54 | + t.Cleanup(func() { |
| 55 | + CloseLogFile() |
| 56 | + os.RemoveAll(cqDir) |
| 57 | + }) |
| 58 | + testConfig := path.Join(currentDir, "testdata", tc.config) |
| 59 | + logFileName := path.Join(cqDir, "cloudquery.log") |
| 60 | + cmd := NewCmdRoot() |
| 61 | + cmd.SetArgs([]string{"install", testConfig, "--cq-dir", cqDir, "--log-file-name", logFileName}) |
| 62 | + err := cmd.Execute() |
| 63 | + assert.NoError(t, err) |
| 64 | + |
| 65 | + // check if all files were created |
| 66 | + justFiles := readFiles(t, cqDir, "") |
| 67 | + |
| 68 | + sourceFiles, destFiles := 0, 0 |
| 69 | + for _, file := range justFiles { |
| 70 | + if strings.HasPrefix(file, "plugins/source") { |
| 71 | + sourceFiles++ |
| 72 | + } else if strings.HasPrefix(file, "plugins/destination") { |
| 73 | + destFiles++ |
| 74 | + } |
| 75 | + } |
| 76 | + assert.Equalf(t, tc.wantSourceFiles, sourceFiles, "expected %d source files, got %d", tc.wantSourceFiles, sourceFiles) |
| 77 | + assert.Equalf(t, tc.wantDestFiles, destFiles, "expected %d destination files, got %d", tc.wantDestFiles, destFiles) |
| 78 | + if t.Failed() { |
| 79 | + t.Logf("files found: %v", justFiles) |
| 80 | + t.FailNow() |
| 81 | + } |
| 82 | + |
| 83 | + // check that log was written and contains some lines |
| 84 | + b, logFileError := os.ReadFile(path.Join(cqDir, "cloudquery.log")) |
| 85 | + logContent := string(b) |
| 86 | + require.NoError(t, logFileError, "failed to read cloudquery.log") |
| 87 | + require.NotEmpty(t, logContent, "cloudquery.log empty; expected some logs") |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func readFiles(t *testing.T, basedir, prefix string) []string { |
| 93 | + files, err := os.ReadDir(basedir) |
| 94 | + assert.NoError(t, err) |
| 95 | + var justFiles []string |
| 96 | + for i := range files { |
| 97 | + name := files[i].Name() |
| 98 | + |
| 99 | + if !files[i].IsDir() { |
| 100 | + justFiles = append(justFiles, path.Join(prefix, name)) |
| 101 | + continue |
| 102 | + } |
| 103 | + |
| 104 | + justFiles = append(justFiles, readFiles(t, path.Join(basedir, files[i].Name()), path.Join(prefix, name))...) |
| 105 | + } |
| 106 | + sort.Strings(justFiles) |
| 107 | + return justFiles |
| 108 | +} |
0 commit comments