|
| 1 | +package tester |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "goa.design/clue/debug" |
| 7 | + "goa.design/clue/log" |
| 8 | + goa "goa.design/goa/v3/pkg" |
| 9 | + "google.golang.org/grpc" |
| 10 | + |
| 11 | + genfront "goa.design/clue/example/weather/services/front/gen/front" |
| 12 | + genclient "goa.design/clue/example/weather/services/tester/gen/grpc/tester/client" |
| 13 | + gentester "goa.design/clue/example/weather/services/tester/gen/tester" |
| 14 | +) |
| 15 | + |
| 16 | +type ( |
| 17 | + Client interface { |
| 18 | + // Runs ALL API Integration Tests from the Tester service, allowing for filtering on included or excluded tests |
| 19 | + TestAll(ctx context.Context, included, excluded []string) (*genfront.TestResults, error) |
| 20 | + // Runs API Integration Tests' Smoke Tests ONLY from the Tester service |
| 21 | + TestSmoke(ctx context.Context) (*genfront.TestResults, error) |
| 22 | + } |
| 23 | + |
| 24 | + TestAllPayload struct { |
| 25 | + Include []string |
| 26 | + Exclude []string |
| 27 | + } |
| 28 | + |
| 29 | + client struct { |
| 30 | + testSmoke goa.Endpoint |
| 31 | + testAll goa.Endpoint |
| 32 | + } |
| 33 | +) |
| 34 | + |
| 35 | +// Creates a new client for the Tester service. |
| 36 | +func New(cc *grpc.ClientConn) Client { |
| 37 | + c := genclient.NewClient(cc, grpc.WaitForReady(true)) |
| 38 | + return &client{ |
| 39 | + debug.LogPayloads(debug.WithClient())(c.TestSmoke()), |
| 40 | + debug.LogPayloads(debug.WithClient())(c.TestAll()), |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// TestSmoke runs the Smoke collection as defined in func_map.go of the tester service |
| 45 | +func (c *client) TestSmoke(ctx context.Context) (*genfront.TestResults, error) { |
| 46 | + res, err := c.testSmoke(ctx, nil) |
| 47 | + if err != nil { |
| 48 | + log.Errorf(ctx, err, "failed to run smoke tests: %s", err) |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + return testerTestResultsToFrontTestResults(res.(*gentester.TestResults)), nil |
| 52 | +} |
| 53 | + |
| 54 | +// TestAll runs all tests in all collections. Obeys include and exclude filters. |
| 55 | +// include and exclude are mutually exclusive and cannot be used together (400 error, bad request) |
| 56 | +func (c *client) TestAll(ctx context.Context, included, excluded []string) (*genfront.TestResults, error) { |
| 57 | + gtPayload := &gentester.TesterPayload{ |
| 58 | + Include: included, |
| 59 | + Exclude: excluded, |
| 60 | + } |
| 61 | + res, err := c.testAll(ctx, gtPayload) |
| 62 | + if err != nil { |
| 63 | + log.Errorf(ctx, err, "failed to run all tests: %s", err) |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + return testerTestResultsToFrontTestResults(res.(*gentester.TestResults)), nil |
| 67 | +} |
| 68 | + |
| 69 | +func testerTestResultsToFrontTestResults(testResults *gentester.TestResults) *genfront.TestResults { |
| 70 | + var res = &genfront.TestResults{} |
| 71 | + if testResults != nil { |
| 72 | + res.Collections = testerTestCollectionsArrToFrontTestCollectionsArr(testResults.Collections) |
| 73 | + res.Duration = testResults.Duration |
| 74 | + res.PassCount = testResults.PassCount |
| 75 | + res.FailCount = testResults.FailCount |
| 76 | + } |
| 77 | + return res |
| 78 | +} |
| 79 | + |
| 80 | +func testerTestCollectionsArrToFrontTestCollectionsArr(testCollection []*gentester.TestCollection) []*genfront.TestCollection { |
| 81 | + var res []*genfront.TestCollection |
| 82 | + for _, v := range testCollection { |
| 83 | + res = append(res, testerTestCollectionToFrontTestCollection(v)) |
| 84 | + } |
| 85 | + return res |
| 86 | +} |
| 87 | + |
| 88 | +func testerTestCollectionToFrontTestCollection(testCollection *gentester.TestCollection) *genfront.TestCollection { |
| 89 | + var res = &genfront.TestCollection{} |
| 90 | + if testCollection != nil { |
| 91 | + res.Name = testCollection.Name |
| 92 | + res.Results = testerTestResultsArrToFrontTestResultsArr(testCollection.Results) |
| 93 | + res.Duration = testCollection.Duration |
| 94 | + res.PassCount = testCollection.PassCount |
| 95 | + res.FailCount = testCollection.FailCount |
| 96 | + } |
| 97 | + return res |
| 98 | +} |
| 99 | + |
| 100 | +func testerTestResultsArrToFrontTestResultsArr(testResults []*gentester.TestResult) []*genfront.TestResult { |
| 101 | + var res []*genfront.TestResult |
| 102 | + for _, v := range testResults { |
| 103 | + res = append(res, (*genfront.TestResult)(v)) |
| 104 | + } |
| 105 | + return res |
| 106 | +} |
0 commit comments