|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/google/go-cmp/cmp" |
| 12 | + "github.com/sourcegraph/sourcegraph/lib/errors" |
| 13 | + "github.com/sourcegraph/sourcegraph/lib/output" |
| 14 | + |
| 15 | + "github.com/sourcegraph/src-cli/internal/api" |
| 16 | + "github.com/sourcegraph/src-cli/internal/instancehealth" |
| 17 | +) |
| 18 | + |
| 19 | +// note that this file is called '_testcmd.go' because '_test.go' cannot be used |
| 20 | + |
| 21 | +func init() { |
| 22 | + usage := `'src snapshot test' uses exported summary data to validate a restored and upgraded instance. |
| 23 | +
|
| 24 | +USAGE |
| 25 | + src login # site-admin authentication required |
| 26 | + src [-v] snapshot test [-summary-path="./src-snapshot-summary.json"] |
| 27 | +` |
| 28 | + flagSet := flag.NewFlagSet("test", flag.ExitOnError) |
| 29 | + snapshotSummaryPath := flagSet.String("summary-path", "./src-snapshot-summary.json", "path to read snapshot summary from") |
| 30 | + since := flagSet.Duration("since", 1*time.Hour, "duration ago to look for healthcheck data") |
| 31 | + apiFlags := api.NewFlags(flagSet) |
| 32 | + |
| 33 | + snapshotCommands = append(snapshotCommands, &command{ |
| 34 | + flagSet: flagSet, |
| 35 | + handler: func(args []string) error { |
| 36 | + if err := flagSet.Parse(args); err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + out := output.NewOutput(flagSet.Output(), output.OutputOpts{Verbose: *verbose}) |
| 40 | + client := cfg.apiClient(apiFlags, flagSet.Output()) |
| 41 | + |
| 42 | + // Fetch health data |
| 43 | + instanceHealth, err := instancehealth.GetIndicators(context.Background(), client) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + // Optionally validate snapshots |
| 49 | + if *snapshotSummaryPath != "" { |
| 50 | + f, err := os.OpenFile(*snapshotSummaryPath, os.O_RDONLY, os.ModePerm) |
| 51 | + if err != nil { |
| 52 | + return errors.Wrap(err, "open snapshot file") |
| 53 | + } |
| 54 | + var recordedSummary snapshotSummary |
| 55 | + if err := json.NewDecoder(f).Decode(&recordedSummary); err != nil { |
| 56 | + return errors.Wrap(err, "read snapshot file") |
| 57 | + } |
| 58 | + // Fetch new snapshot |
| 59 | + newSummary, err := fetchSnapshotSummary(context.Background(), client) |
| 60 | + if err != nil { |
| 61 | + return errors.Wrap(err, "get snapshot") |
| 62 | + } |
| 63 | + if err := compareSnapshotSummaries(out, recordedSummary, *newSummary); err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // generate checks set |
| 69 | + checks := instancehealth.NewChecks(*since, *instanceHealth) |
| 70 | + |
| 71 | + // Run checks |
| 72 | + var validationErrors error |
| 73 | + for _, check := range checks { |
| 74 | + validationErrors = errors.Append(validationErrors, check(out)) |
| 75 | + } |
| 76 | + if validationErrors != nil { |
| 77 | + out.WriteLine(output.Linef(output.EmojiFailure, output.StyleFailure, |
| 78 | + "Critical issues found: %s", err.Error())) |
| 79 | + return errors.New("validation failed") |
| 80 | + } |
| 81 | + out.WriteLine(output.Line(output.EmojiSuccess, output.StyleSuccess, |
| 82 | + "No critical issues found!")) |
| 83 | + return nil |
| 84 | + }, |
| 85 | + usageFunc: func() { fmt.Fprint(flag.CommandLine.Output(), usage) }, |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +func compareSnapshotSummaries(out *output.Output, recordedSummary, newSummary snapshotSummary) error { |
| 90 | + b := out.Block(output.Styled(output.StyleBold, "Snapshot contents")) |
| 91 | + defer b.Close() |
| 92 | + |
| 93 | + // Compare |
| 94 | + diff := cmp.Diff(recordedSummary, newSummary) |
| 95 | + if diff != "" { |
| 96 | + b.WriteLine(output.Line(output.EmojiFailure, output.StyleFailure, "Snapshot diff detected:")) |
| 97 | + b.WriteCode("diff", diff) |
| 98 | + return errors.New("snapshot mismatch") |
| 99 | + } |
| 100 | + b.WriteLine(output.Emoji(output.EmojiSuccess, "Snapshots match!")) |
| 101 | + return nil |
| 102 | +} |
0 commit comments